# DepthFormer convert to TensorRT - 需要連進 insign02進行作業 - Project 位置為 **/home/insign2/work/Monocular-Depth-Estimation-Toolbox** ## Step 1. Training pytorch model 參考 Andy 之前的 [GitHub](https://github.com/a07458666/Monocular-Depth-Estimation-Toolbox) ```bash! bash ./tools/dist_train.sh configs/depthformer/depthformer_swint_w7_endoscopy.py 1 --work-dir ./checkpoints/depthformer_endoscopy_scale16 ``` - 如果需要更換模型,請修改 **config** 檔案 (configs/depthformer/depthformer_swint_w7_endoscopy.py) - **- -work-dir** 為訓練結果儲存的位置 - 目前者有提供下列[模型](https://github.com/zhyever/Monocular-Depth-Estimation-Toolbox/blob/main/docs/model_zoo.md) ![](https://i.imgur.com/d7CY0WF.png) - 如果需要進行 training 則需要新增 config 檔案。以simipu為例,在資料夾內新增一個 simipu_r50_scratch_endo.py,可以由其他檔案複製並進行修改。 ![](https://i.imgur.com/dJ6yE3M.png) - 並加入 dataset config 位置 **../_base_/datasets/custom.py** ![](https://i.imgur.com/KJu53oi.png) - 目前我有測試過一輪所有模型的 training,測試結果整理如下; | Model name | 測試結果 | todo | checkpoint path | | -------- | -------- | -------- |-------- | | depthformer| 可 training 但無法轉 int8| 等官方釋出新版 tensorrt|checkpoints/depthformer_endoscopy_scale16_fake/latest.pth| | simpui | 轉 int8 成功! | |checkpoints/simpui_0302/latest.pth| | adabins | 無法 training,no chamfer_distance|要裝 pytorch3D| | bts | 無法training,no cam_intrinsic參數|須另外加入cam_intrinsic參數| | binsformer |還沒測試 || | dps |RuntimeError: Caught UnicodeDecodeError in DataLoader worker process 0. | | ## Step 2. Convert to ONNX model 由於 onnx conversion 只能傳入單一個 list,原本的 depth former model 除了要輸入image 之外,還要輸入 img_metas,所以需要修改 inference 的 source code。 在 **./depth/models/depther** 下有一個 base.py,與 base_onnx.py。一個用於訓練時使用,一個用於轉換成 onnx 使用。 須將 base.py rename 成別的檔名(ex. base_train.py), rename 過程中 vscode 會詢問你須不需要針對 project 中所有跟 base.py 的檔案進行 refactoring change,此時選擇 **skip changes**。 ![rename 之前](https://i.imgur.com/V9VdMdN.png) ![](https://i.imgur.com/uMSY840.png) 接著再將 base_onnx.py rename 成 base.py。 ![](https://i.imgur.com/pUbqVh0.png) 1. 修改 ./tensorrt/pytorch2onnx.py 內的 model config ```python! depth_cfg=load_depth_cfg('./configs/depthformer/depthformer_swint_w7_endoscopy.py') depth_cfg2='./checkpoints/depthformer_endoscopy_scale16_fake/best_abs_rel_iter_14400.pth' ``` 2. 修改 ./tensorrt/pytorch2onnx.py 內的 onnx model 儲存位置 ```python! torch.onnx.export(depthformer_net, img, "./tensorrt/depthformer.onnx", # 修改這段 opset_version=16, do_constant_folding=True, input_names=["input"], output_names=["output"], ) ``` 3. 執行轉檔 ```bash! python3 tensorrt/pytorch2onnx.py ``` ## Step 3. Convert to TensorRT 1. 修改 tensorrt/onnx2trt.py 內, onnx model 位置,與 tensorrt model 儲存位置。 ```python! parser = argparse.ArgumentParser() parser.add_argument('--onnx', default='tensorrt/simipu_sim.onnx') # onnx model parser.add_argument('--fp16', action='store_true') parser.add_argument('--int8', action='store_true') parser.add_argument('--savepth', default='./model.trt') # tensorrt model args = parser.parse_args() ``` 2. convert to tensorrt ```bash! # FP16 mode python3 tensorrt/onnx2trt.py --fp16 ``` ```bash! # INT8 mode python3 tensorrt/onnx2trt.py --int8 ``` ## Model compare | original | depth former | simpui | | -------- | -------- | -------- | | ![](https://i.imgur.com/9QtK6ud.png)| ![](https://i.imgur.com/A6Kaq9a.png)| ![](https://i.imgur.com/C3qfZro.png)| |![](https://i.imgur.com/35MFPlS.png)|![](https://i.imgur.com/Q9rGqvS.png)|![](https://i.imgur.com/j2PVrSA.png)| |![](https://i.imgur.com/xiTt3uL.png)|![](https://i.imgur.com/HaSkFx6.png)|![](https://i.imgur.com/2Nurm6h.png)| |![](https://i.imgur.com/gsgC5sg.jpg)|![](https://i.imgur.com/zdTOtmp.jpg)|![](https://i.imgur.com/DcYKLVp.jpg)| |![](https://i.imgur.com/lPgWn12.jpg)|![](https://i.imgur.com/AaiZS1y.jpg)|![](https://i.imgur.com/W03NlvB.jpg)| |![](https://i.imgur.com/p6eRtRS.jpg)|![](https://i.imgur.com/xw4vCvM.jpg)|![](https://i.imgur.com/4NgbISC.jpg)| ###### tags: `ML` `Depth Former`