Comments (6)
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.
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.
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.
Thanks you!
Maybe i should use Keras model directly to avoid the errors like that.
from snn_toolbox.
@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.
Please see this example script.
from snn_toolbox.
Related Issues (20)
- TypeError: can't multiply sequence by non-int of type 'float' HOT 4
- IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
- SpinnmanIOException: IO Error: Failed to communicate with the machine HOT 7
- Query regarding INI simulator HOT 6
- Conv1D Conversion Normalization Issue HOT 2
- ONNX model could not be ported to Keras.Mismatched elements 100% HOT 1
- Code required for a research paper HOT 2
- ModuleNotFoundError: No module named 'keras_rewiring' HOT 2
- Which neuromorphic hardware does SNNtoolbox simulate ? HOT 3
- Error happened while building parsed model HOT 2
- Key Error HOT 1
- index -1 is out of bounds for axis 1 with size 0 HOT 2
- Membrane Potential Values after spike conv layer. HOT 1
- Loading a a converted SNN .h5 model using 'load_model' HOT 1
- Energy and runtime estimation for running the SNN on neuromorphic simulator HOT 3
- TTTFS dyn thresh and TTFS corrective not working HOT 2
- Poisson Rate Encoding HOT 4
- Quantization HOT 6
- TTFS HOT 1
- Cannot import name 'literal' from typing. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from snn_toolbox.