Monocular-Depth-Estimation-Toolbox icon indicating copy to clipboard operation
Monocular-Depth-Estimation-Toolbox copied to clipboard

Convert to ONNX model

Open Lilyo opened this issue 3 years ago • 4 comments

Hi @zhyever, Thanks for your great work! I'm curious if BinsFormer can be converted to ONNX model? If yes, please also provide sample code so we can verify it.

Thanks in advance!

Lilyo avatar Jul 13 '22 11:07 Lilyo

I have implemented several codes to convert the model to ONNX but not release in this repo now. The most tricky issue is whether the ONNX has supported ops in models.

Relative codes can be like:

    sample_input = torch.randn(1, 3, 480, 640)
    input_nodes = ['input']
    output_nodes = ['output']
    
    print("\n\n\n Start export onnx")
    # torch.onnx.export(model, sample_input, "model.onnx", export_params=True, input_names=input_nodes, output_names=output_nodes)
    torch.onnx.export(model, sample_input, "nfs/model.onnx", export_params=True, input_names=input_nodes, output_names=output_nodes, opset_version=11)

    # Converting model to Tensorflow
    print("\n\n\n Start export_graph")
    onnx_model = onnx.load("nfs/model.onnx")

zhyever avatar Jul 14 '22 12:07 zhyever

Hi @zhyever ,

I'm just trying to convert BTS model to onnx. The issue is that the original pth model needs a dict as input (with following inference code: data = {'img_metas': xxxx, 'img': xxxx} pred = model(return_loss=False, **data), which means the 'sample_input' in the following code should also be a dict torch.onnx.export(model, sample_input, "nfs/model.onnx", export_params=True, input_names=input_nodes, output_names=output_nodes, opset_version=11) However the torch.onnx.export module doesn't support a dict as input. Do you know how to fix this? Thanks a lot.

ReBoRn8888 avatar Jul 22 '22 03:07 ReBoRn8888

Sorry for the late reply, I missed your questions. Yes, the input of the model during converting should be single. In other words, the forward function should be like: def forward(self, x). No more parameters are supported. Before you convert the model, you should hack a def forward(self, x) in the depther, so that the converter can successfully pass variables.

zhyever avatar Aug 09 '22 01:08 zhyever

Thanks for your reply. I'll check that.

ReBoRn8888 avatar Aug 09 '22 05:08 ReBoRn8888

Close for now.

zhyever avatar Sep 21 '22 23:09 zhyever

@ReBoRn8888 Hi, I am also trying to convert the model to ONNX. However, I encounter the error like below. Do you have any experiences to deal with that? image

a227799770055 avatar Nov 21 '22 05:11 a227799770055