# 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)  - 如果需要進行 training 則需要新增 config 檔案。以simipu為例,在資料夾內新增一個 simipu_r50_scratch_endo.py,可以由其他檔案複製並進行修改。  - 並加入 dataset config 位置 **../_base_/datasets/custom.py**  - 目前我有測試過一輪所有模型的 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**。   接著再將 base_onnx.py rename 成 base.py。  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 | | -------- | -------- | -------- | | | | | |||| |||| |||| |||| |||| ###### tags: `ML` `Depth Former`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up