microsoft / MMdnn

MMdnn is a set of tools to help users inter-operate among different deep learning frameworks. E.g. model conversion and visualization. Convert models between Caffe, Keras, MXNet, Tensorflow, CNTK, PyTorch Onnx and CoreML.
MIT License
5.8k stars 965 forks source link

pytorch model conversion - UnboundLocalError: local variable 'output_shape' referenced before assignment #897

Closed manikandtan-ck closed 4 years ago

manikandtan-ck commented 4 years ago

Platform (like ubuntu 16.04/win10): Ubuntu 18.04

Python version: 3.6.9

Source framework with version (like Tensorflow 1.4.1 with GPU): pytorch 1.5.1+cpu

Destination framework with version (like CNTK 2.3 with GPU): IR

Pre-trained model path (webpath or webdisk path): https://github.com/duanzhiihao/RAPiD/releases/download/v0.1/pL1_MWHB1024_Mar11_4000.ckpt

Running scripts: mmtoir -f pytorch -d resnet50 --inputShape 3,1024,1024 -n models/RAPiD_full.pt

created the .pt file by running the following in api.py in the RAPiD repo:

model.eval()
torch.save(model, "/home/dev/Projects/RAPiD/RAPiD/models/RAPiD_full.pt")

Error:

Traceback (most recent call last): File "/home/dev/.local/bin/mmtoir", line 8, in sys.exit(_main()) File "/home/dev/.local/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 197, in _main ret = _convert(args) File "/home/dev/.local/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 97, in _convert parser = PytorchParser151(model, inputshape[0]) File "/home/dev/.local/lib/python3.6/site-packages/mmdnn/conversion/pytorch/pytorch_parser.py", line 523, in init self.build_graph(input_shape) File "/home/dev/.local/lib/python3.6/site-packages/mmdnn/conversion/pytorch/pytorch_parser.py", line 92, in build_graph self.pytorch_graph.build(self.input_shape) File "/home/dev/.local/lib/python3.6/site-packages/mmdnn/conversion/pytorch/pytorch_graph.py", line 142, in build self.shape_dict[node_name] = output_shape UnboundLocalError: local variable 'output_shape' referenced before assignment

the Error seems to be coming from mmdnn/conversion/pytorch/pytorch_graph.py", line 138:

            if '%' in output_shape_str:
                out_put_shape = None
            else:
                output_shape = [int(x.replace('!', '')) for x in output_shape_str.split(',')]

is the out_put_shape a typo?

JiahaoYao commented 4 years ago

by the way, i don't think i can find the same thing here: https://github.com/microsoft/MMdnn/blob/master/mmdnn/conversion/pytorch/pytorch_graph.py

manikandtan-ck commented 4 years ago

Yeah. Sorry that was from the pip install version. the source version from GitHub, while having the same version number, seems to have different code. The issue is not present there.