Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertionError #324

Open
jjaacckkyy63 opened this issue Jul 20, 2018 · 22 comments
Open

AssertionError #324

jjaacckkyy63 opened this issue Jul 20, 2018 · 22 comments

Comments

@jjaacckkyy63
Copy link

Platform (like ubuntu 16.04/win10):
ubuntu 18.04
Python version:
3.6.5
Source framework with version (like Tensorflow 1.4.1 with GPU):
pytorch 0.4.0 with GPU
Destination framework with version (like CNTK 2.3 with GPU):
keras 2.2.0 with GPU
Running scripts:
mmconvert -sf pytorch -iw imagenet_inception_v3.pth -df keras -om p2k.h5

and give this error:
Traceback (most recent call last):
File "/home/ej/.local/bin/mmconvert", line 11, in
sys.exit(_main())
File "/home/ej/.local/lib/python3.6/site-packages/mmdnn/conversion/_script/convert.py", line 95, in _main
ret = convertToIR._convert(ir_args)
File "/home/ej/.local/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 73, in _convert
assert args.inputShape != None
AssertionError

@jjaacckkyy63
Copy link
Author

even the sample model from mmdownload ca't work

@kitstar
Copy link
Contributor

kitstar commented Jul 21, 2018

@jjaacckkyy63 You need to specify the input shape like --inputShape 3,224,224

@jjaacckkyy63
Copy link
Author

Thank you for reply.
but when I fill in the --inputShape, another error occur
ValueError: invalid literal for int() with base 10: '3,224,224'
is that mean I should type shape in another form?

@kitstar
Copy link
Contributor

kitstar commented Jul 21, 2018

If you are using pip package, the format is "3 224 224". (Sorry for the inconvenient and it will be changed in the coming new pip package).

@jjaacckkyy63
Copy link
Author

It helps! thank you
BTW, why is there need a input shape and tutorial don not have this part
is the the problem of my model?

@kitstar
Copy link
Contributor

kitstar commented Jul 21, 2018

No, all pytorch models need the shape.

@jjaacckkyy63
Copy link
Author

thanks, another issue occurs
ModuleNotFoundError: No module named 'torch.jit'
I do install pytorch well
thank you

@namizzz
Copy link
Contributor

namizzz commented Jul 23, 2018

Hi @jjaacckkyy63 , please check if python can import torch.jit. Thanks!

@sherlockblaze
Copy link

Hi, I'm a new guy to deep-learning
There's a question:
How can I get the input shape of a pytorch model.
For now I use the model what download from model collection.
Thanks for help

@jjaacckkyy63
Copy link
Author

Hi again, I finally fixed all the environment issue,
but there were still some error as below:

$ mmconvert -sf pytorch --inputShape 1 224 224 -iw resnet18.pth -df keras --outputModel pytorch2keras.h5
Traceback (most recent call last):
File "/home/ej/.virtualenvs/mmdnn/bin/mmconvert", line 11, in
sys.exit(_main())
File "/home/ej/.virtualenvs/mmdnn/lib/python3.6/site-packages/mmdnn/conversion/_script/convert.py", line 102, in _main
ret = convertToIR._convert(ir_args)
File "/home/ej/.virtualenvs/mmdnn/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 92, in _convert
parser = PytorchParser(args.network, inputshape[0])
File "/home/ej/.virtualenvs/mmdnn/lib/python3.6/site-packages/mmdnn/conversion/pytorch/pytorch_parser.py", line 67, in init
if not os.path.exists(model_file_name):
File "/home/ej/.virtualenvs/mmdnn/lib/python3.6/genericpath.py", line 19, in exists
os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

Thank you

@namizzz
Copy link
Contributor

namizzz commented Jul 25, 2018

Hi @sherlockblaze ,if you download model from collection, you can get the input shape here
eg.

'alexnet'       : lambda path : TestKit.Standard(path, 227),
means
--inputShape 3,227,227

@sherlockblaze
Copy link

@namizzz Tks Very Much. I'll try it.

@namizzz
Copy link
Contributor

namizzz commented Jul 26, 2018

Hi @jjaacckkyy63 ,thanks for your issue. We have fixed it. You can try the newest version.
eg.

mmconvert -sf pytorch --inputShape 3,224,224 -iw ./imagenet_resnet152.pth -df keras --outputModel pytorch_test.h5

@sherlockblaze
Copy link

Hi, @namizzz
that's not work on my Mac. Here's the bug info. Maybe you can do me a favor. Tks very much.

mmconvert -sf pytorch --inputShape 3,224,224 -iw ./resnet152-b121ed2d.pth -df keras --outputModel pytorch_test.h5
Traceback (most recent call last):
File "/anaconda3/envs/MMdnn-newest/bin/mmconvert", line 11, in
sys.exit(_main())
File "/anaconda3/envs/MMdnn-newest/lib/python3.6/site-packages/mmdnn/conversion/_script/convert.py", line 102, in _main
ret = convertToIR._convert(ir_args)
File "/anaconda3/envs/MMdnn-newest/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 94, in _convert
parser = PytorchParser(model, inputshape[0])
File "/anaconda3/envs/MMdnn-newest/lib/python3.6/site-packages/mmdnn/conversion/pytorch/pytorch_parser.py", line 81, in init
self.pytorch_graph = PytorchGraph(model)
File "/anaconda3/envs/MMdnn-newest/lib/python3.6/site-packages/mmdnn/conversion/pytorch/pytorch_graph.py", line 59, in init
self.state_dict = _unique_state_dict(self.model)
File "/anaconda3/envs/MMdnn-newest/lib/python3.6/site-packages/torch/jit/init.py", line 91, in _unique_state_dict
state_dict = module.state_dict(keep_vars=keep_vars)
AttributeError: 'collections.OrderedDict' object has no attribute 'state_dict'

@mrgloom
Copy link

mrgloom commented Sep 22, 2018

Trying to convert resnet18 from Pytorch to Keras on MacOS:

mmdownload -f pytorch -n resnet18

mmconvert -sf pytorch --inputShape 3,224,224 -iw ./imagenet_resnet18.pth -df keras -om ./pytorch_to_keras_resnet18.dnn

Get an error:

Traceback (most recent call last):
  File "/Users/myuser/anaconda3/bin/mmconvert", line 11, in <module>
    sys.exit(_main())
  File "/Users/myuser/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/_script/convert.py", line 102, in _main
    ret = convertToIR._convert(ir_args)
  File "/Users/myuser/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 92, in _convert
    parser = PytorchParser(args.network, inputshape[0])
  File "/Users/myuser/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/pytorch/pytorch_parser.py", line 67, in __init__
    if not os.path.exists(model_file_name):
  File "/Users/myuser/anaconda3/lib/python3.6/genericpath.py", line 19, in exists
    os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

Also input shape dim ordering should be in Pytorch format?

@mrgloom
Copy link

mrgloom commented Sep 22, 2018

Using it like this
mmconvert -sf pytorch --inputShape 3,224,244 -in imagenet_resnet18.pth -df keras -om mynet.dnn

Give error:

Traceback (most recent call last):
  File "/home/myuser/anaconda3/bin/mmconvert", line 11, in <module>
    sys.exit(_main())
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/mmdnn/conversion/_script/convert.py", line 102, in _main
    ret = convertToIR._convert(ir_args)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/mmdnn/conversion/_script/convertToIR.py", line 92, in _convert
    parser = PytorchParser(args.network, inputshape[0])
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/mmdnn/conversion/pytorch/pytorch_parser.py", line 83, in __init__
    self.pytorch_graph.build(self.input_shape)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/mmdnn/conversion/pytorch/pytorch_graph.py", line 122, in build
    trace, output = torch.jit.get_trace_graph(self.model, (dummy_input, ))
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/torch/jit/__init__.py", line 77, in get_trace_graph
    return LegacyTracedModule(f)(*args, **kwargs)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/torch/jit/__init__.py", line 109, in forward
    out = self.inner(*trace_inputs)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/torch/nn/modules/module.py", line 475, in __call__
    result = self._slow_forward(*input, **kwargs)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/torch/nn/modules/module.py", line 465, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/torchvision/models/resnet.py", line 151, in forward
    x = self.fc(x)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/torch/nn/modules/module.py", line 475, in __call__
    result = self._slow_forward(*input, **kwargs)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/torch/nn/modules/module.py", line 465, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/torch/nn/modules/linear.py", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/myuser/anaconda3/lib/python3.5/site-packages/torch/nn/functional.py", line 1024, in linear
    return torch.addmm(bias, input, weight.t())
RuntimeError: size mismatch, m1: [1 x 1024], m2: [512 x 1000] at /opt/conda/conda-bld/pytorch_1533672544752/work/aten/src/TH/generic/THTensorMath.cpp:2070

What does it mean?

@namizzz
Copy link
Contributor

namizzz commented Sep 23, 2018

Hi @mrgloom , I tried on Ubuntu. It works.
image
From the Traceback, It seems a problem about the input shape. Maybe you can try to search your model file to find the right input shape?

@mrgloom
Copy link

mrgloom commented Sep 23, 2018

@namizzz what command do you run?

I use resnet18 from pytorch so 3,224,224 should be ok.

All pre-trained models expect input images normalized in the same way, i.e. mini-batches of 3-channel RGB images of shape (3 x H x W), where H and W are expected to be at least 224.
https://pytorch.org/docs/stable/torchvision/models.html

Also as we can see here it also 224.

@mrgloom
Copy link

mrgloom commented Sep 23, 2018

https://stackoverflow.com/a/52469645

Also I have printed structure of downloaded imagenet_resnet18.pth and I can't see where [1 x 1024], m2: [512 x 1000] shape mismatch can came from.

ResNet(
  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (relu): ReLU(inplace)
  (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
  (layer1): Sequential(
    (0): BasicBlock(
      (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
    (1): BasicBlock(
      (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
  )
  (layer2): Sequential(
    (0): BasicBlock(
      (conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (downsample): Sequential(
        (0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): BasicBlock(
      (conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
  )
  (layer3): Sequential(
    (0): BasicBlock(
      (conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (downsample): Sequential(
        (0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): BasicBlock(
      (conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
  )
  (layer4): Sequential(
    (0): BasicBlock(
      (conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (downsample): Sequential(
        (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): BasicBlock(
      (conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    )
  )
  (avgpool): AvgPool2d(kernel_size=7, stride=1, padding=0)
  (fc): Linear(in_features=512, out_features=1000, bias=True)
)
------------------------------------------------------------
conv1.weight : (64, 3, 7, 7)
bn1.weight : (64,)
bn1.bias : (64,)
layer1.0.conv1.weight : (64, 64, 3, 3)
layer1.0.bn1.weight : (64,)
layer1.0.bn1.bias : (64,)
layer1.0.conv2.weight : (64, 64, 3, 3)
layer1.0.bn2.weight : (64,)
layer1.0.bn2.bias : (64,)
layer1.1.conv1.weight : (64, 64, 3, 3)
layer1.1.bn1.weight : (64,)
layer1.1.bn1.bias : (64,)
layer1.1.conv2.weight : (64, 64, 3, 3)
layer1.1.bn2.weight : (64,)
layer1.1.bn2.bias : (64,)
layer2.0.conv1.weight : (128, 64, 3, 3)
layer2.0.bn1.weight : (128,)
layer2.0.bn1.bias : (128,)
layer2.0.conv2.weight : (128, 128, 3, 3)
layer2.0.bn2.weight : (128,)
layer2.0.bn2.bias : (128,)
layer2.0.downsample.0.weight : (128, 64, 1, 1)
layer2.0.downsample.1.weight : (128,)
layer2.0.downsample.1.bias : (128,)
layer2.1.conv1.weight : (128, 128, 3, 3)
layer2.1.bn1.weight : (128,)
layer2.1.bn1.bias : (128,)
layer2.1.conv2.weight : (128, 128, 3, 3)
layer2.1.bn2.weight : (128,)
layer2.1.bn2.bias : (128,)
layer3.0.conv1.weight : (256, 128, 3, 3)
layer3.0.bn1.weight : (256,)
layer3.0.bn1.bias : (256,)
layer3.0.conv2.weight : (256, 256, 3, 3)
layer3.0.bn2.weight : (256,)
layer3.0.bn2.bias : (256,)
layer3.0.downsample.0.weight : (256, 128, 1, 1)
layer3.0.downsample.1.weight : (256,)
layer3.0.downsample.1.bias : (256,)
layer3.1.conv1.weight : (256, 256, 3, 3)
layer3.1.bn1.weight : (256,)
layer3.1.bn1.bias : (256,)
layer3.1.conv2.weight : (256, 256, 3, 3)
layer3.1.bn2.weight : (256,)
layer3.1.bn2.bias : (256,)
layer4.0.conv1.weight : (512, 256, 3, 3)
layer4.0.bn1.weight : (512,)
layer4.0.bn1.bias : (512,)
layer4.0.conv2.weight : (512, 512, 3, 3)
layer4.0.bn2.weight : (512,)
layer4.0.bn2.bias : (512,)
layer4.0.downsample.0.weight : (512, 256, 1, 1)
layer4.0.downsample.1.weight : (512,)
layer4.0.downsample.1.bias : (512,)
layer4.1.conv1.weight : (512, 512, 3, 3)
layer4.1.bn1.weight : (512,)
layer4.1.bn1.bias : (512,)
layer4.1.conv2.weight : (512, 512, 3, 3)
layer4.1.bn2.weight : (512,)
layer4.1.bn2.bias : (512,)
fc.weight : (1000, 512)
fc.bias : (1000,)

@mrgloom
Copy link

mrgloom commented Sep 23, 2018

However I fixed this problem with onnx #426
This command mmconvert -sf pytorch --inputShape 3,224,244 -in imagenet_resnet18.pth -df keras -om mynet.dnn gives the same error.

So I go the long way from IR to keras(according to this tutor https://github.com/Microsoft/MMdnn/tree/master/mmdnn/conversion/keras ):

#Convert from pytorch to IR
mmdownload -f pytorch -n resnet18
mmtoir -f pytorch -n imagenet_resnet18.pth --inputShape 3,224,224 -o pytorch_resnet18_ir

#Convert from IR to Keras
mmtocode -f keras --IRModelPath pytorch_resnet18_ir.pb --dstModelPath keras_resnet18.py
python -m mmdnn.conversion.examples.keras.imagenet_test -n keras_resnet18.py -w pytorch_resnet18_ir.npy --dump keras_resnet18.h5

But looks like it produce only.h5 weight file, I tried to load it and gives me an error ValueError: No model found in config file.:

from keras.models import load_model
m = load_model('keras_resnet18.h5')
m.summary() 

So my question is how to use this .h5 file, should I build model by hand in .py code?

@namizzz
Copy link
Contributor

namizzz commented Sep 24, 2018

Hi @mrgloom , you are right!
It's a confusing for users. in the imagenet_test. MMdnn dumps model in the json file and a weight file(.h5 only weights). https://github.com/Microsoft/MMdnn/blob/master/mmdnn/conversion/examples/keras/imagenet_test.py#L135. We have changed it to model.save now. Thanks!

@HongChow
Copy link

HongChow commented Mar 4, 2019

@sherlockblaze I've met the same problem as you, how can you fix it? would you pls do me a favor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants