GithubHelp home page GithubHelp logo

Comments (6)

rbodo avatar rbodo commented on July 20, 2024

Apologies, this is an undocumented hack. Pytorch support is still in its infancy.

In your pytorch model script, please give the Model class an input_shape attribute like so:

class Model(nn.Module):
     def __init__(self):
         super(Model, self).__init__()
         self.input_shape = [3, 128, 128]  # This is new
         self.features = nn.Sequential(
         ...

Also, you can see from the warning that the pytorch state_dict couldn't be loaded completely because your variables are called features.module.0.weight etc but the toolbox expects features.0.weight. A simple fix is to rename the keys in your state_dict:

new_state_dict = {}
for k, v in state_dict.items():
    k = k.replace('module.', '')
    new_state_dict[k] = v

If you'd like to submit a more sustainable solution, a pull-request would be very welcome.

from snn_toolbox.

minhson avatar minhson commented on July 20, 2024

Thanks you!
I used your advice and the conversion from Pytorch to ONNX was successful. However, the conversion from ONNX to Keras failed due to this error as below:

Traceback (most recent call last):
  File "cifar10_pytorch_INI.py", line 182, in <module>
    main(config_filepath)
  File "/home/sonlm/.local/lib/python3.6/site-packages/snntoolbox/bin/run.py", line 31, in main
    run_pipeline(config)
  File "/home/sonlm/.local/lib/python3.6/site-packages/snntoolbox/bin/utils.py", line 73, in run_pipeline
    config.get('paths', 'filename_ann'))
  File "/home/sonlm/.local/lib/python3.6/site-packages/snntoolbox/parsing/model_libs/pytorch_input_lib.py", line 108, in load
    model_keras = pytorch_to_keras(model_pytorch, input_torch, [(3, 32, 32,)], verbose=True, change_ordering=change_ordering)
  File "/home/sonlm/.local/lib/python3.6/site-packages/pytorch2keras/converter.py", line 83, in pytorch_to_keras
    verbose=verbose, change_ordering=change_ordering)
  File "/home/sonlm/.local/lib/python3.6/site-packages/onnx2keras/converter.py", line 251, in onnx_to_keras
    model_tf_ordering = keras.models.Model.from_config(conf)
  File "/home/sonlm/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 987, in from_config
    config, custom_objects)
  File "/home/sonlm/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 2029, in reconstruct_from_config
    process_node(layer, node_data)
  File "/home/sonlm/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1977, in process_node
    output_tensors = layer(input_tensors, **kwargs)
  File "/home/sonlm/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 897, in __call__
    self._maybe_build(inputs)
  File "/home/sonlm/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 2416, in _maybe_build
    self.build(input_shapes)  # pylint:disable=not-callable
  File "/home/sonlm/.local/lib/python3.6/site-packages/tensorflow/python/keras/layers/normalization.py", line 327, in build
    raise ValueError('Invalid axis: %d' % x)
ValueError: Invalid axis: 3

I think this error happened due to the image data format of Pytorch and Keras.
Could you help me with this?

from snn_toolbox.

rbodo avatar rbodo commented on July 20, 2024

No, I think this error is because of the one-dimensional BatchNorm layer in the classifier. Tensorflow complains that the axis is set to 3 (corresponding to the channel dimension), which is more axes than the Dense layer has. It seems ONNX does not convert the axis parameter of 1D BatchNorm layers correctly.

I cannot debug this further as it seems to be an ONNX issue.

from snn_toolbox.

minhson avatar minhson commented on July 20, 2024

Thanks you!
Maybe i should use Keras model directly to avoid the errors like that.

from snn_toolbox.

Naitik1502 avatar Naitik1502 commented on July 20, 2024

@minhson I am also trying to convert a pytorch model. Could you please share your config file as I am having trouble filling it.
Thanks in advance!!

from snn_toolbox.

rbodo avatar rbodo commented on July 20, 2024

Please see this example script.

from snn_toolbox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.