shariqfarooq123 / AdaBins

Official implementation of Adabins: Depth Estimation using adaptive bins
GNU General Public License v3.0
725 stars 156 forks source link

how to convert the model to ONNX #56

Open azuryl opened 2 years ago

azuryl commented 2 years ago

@shariqfarooq123 import torch import torch.onnx from torch2trt import torch2trt import model_io from models import UnetAdaptiveBins import sys

Function to Convert to ONNX

def Convert_ONNX():

 # Export the model   
torch.onnx.export(model,         # model being run 
     dummy_input,       # model input (or a tuple for multiple inputs) 
     "efficientnet.onnx",       # where to save the model  
     export_params=False,  # store the trained parameter weights inside the model file 
     opset_version=10,    # the ONNX version to export the model to 
     do_constant_folding=True,  # whether to execute constant folding for optimization 
     input_names = ['modelInput'],   # the model's input names 
     output_names = ['modelOutput'], # the model's output names 
     verbose=True,
     dynamic_axes={'modelInput' : {0 : 'batch_size'},    # variable length axes 
                            'modelOutput' : {0 : 'batch_size'}})
print(" ") 
print('Model has been converted to ONNX') 

device='cuda:0'

pretrained_path = "./pretrained/AdaBins_nyu.pt"

model = UnetAdaptiveBins.build(n_bins=256, min_val=1e-3, max_val=10) dummy_input = torch.rand((1, 3, 480, 640), requires_grad=True)#.to(device) model.eval() Convert_ONNX() oading base model ()...Done. Removing last two layers (global_pool & classifier). Building Encoder-Decoder model..Done. /home/jliu/.cache/torch/hub/rwightman_gen-efficientnet-pytorch_master/geffnet/conv2d_layers.py:47: UserWarning: floordiv is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor'). return max((-(i // -s) - 1) s + (k - 1) d + 1 - i, 0) /home/jliu/.cache/torch/hub/rwightman_gen-efficientnet-pytorch_master/geffnet/conv2d_layers.py:47: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! return max((-(i // -s) - 1) s + (k - 1) d + 1 - i, 0) /home/jliu/.cache/torch/hub/rwightman_gen-efficientnet-pytorch_master/geffnet/conv2d_layers.py:55: UserWarning: floordiv is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor'). return [pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2] /home/jliu/data2/torch2trt/models/layers.py:34: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! assert c == ck, "Number of channels in x and Embedding dimension (at dim 2) of K matrix must match" Traceback (most recent call last): File "/home/jliu/anaconda3/envs/adabins/lib/python3.9/site-packages/torch/onnx/symbolic_opset9.py", line 1032, in _convert_padding_node padding = [sym_help._get_const(v, "i", "padding") for v in input_list] File "/home/jliu/anaconda3/envs/adabins/lib/python3.9/site-packages/torch/onnx/symbolic_opset9.py", line 1032, in padding = [sym_help._get_const(v, "i", "padding") for v in input_list] File "/home/jliu/anaconda3/envs/adabins/lib/python3.9/site-packages/torch/onnx/symbolic_helper.py", line 112, in _get_const raise RuntimeError("ONNX symbolic expected a constant value of the {} argument, got {}".format(arg_name, value)) RuntimeError: ONNX symbolic expected a constant value of the padding argument, got `1050 defined in (%1050 : Long(requires_grad=0, device=cpu) = onnx::Castto=7 # /home/jliu/anaconda3/envs/adabins/lib/python3.9/site-packages/torch/_tensor.py:591:0