gmalivenko / pytorch2keras

PyTorch to Keras model convertor
https://pytorch2keras.readthedocs.io/en/latest/
MIT License
857 stars 143 forks source link

AttributeError: 'tuple' object has no attribute 'ndims' #137

Open LamyaMohaned opened 2 years ago

LamyaMohaned commented 2 years ago

Describe the bug I want to convert nfnet (eca_nfnet_l2, dm_nfnet_f3) models from pytorch to keras. These models are implemented by timm. So, I used pytorch2keras package. Also, I tried converting the model first to onnx using torch.onnx then converting it to keras using keras2onnx alone. But, in the two cases I'm facing this error with onnx2keras: AttributeError: 'tuple' object has no attribute 'ndims'

I've use these two packages in converting many pytorch models without any problems (resnet, densenet, resnext, efficientnet). To Reproduce Steps to reproduce the behavior:

model = dm_nfnet_f1(pretrained=True) 
for parameter in model.parameters():
        parameter.requires_grad = False
model.eval()
input_np = np.random.uniform(0, 1, (1, 3, 224, 224))
input_var = Variable(torch.FloatTensor(input_np))
k_model = pytorch_to_keras(modelB, input_var, [(3, 224, 224,)], verbose=True,change_ordering=False) 

Screenshots error message:

AttributeError                            Traceback (most recent call last)
<ipython-input-25-12ab5dd3bc2e> in <module>
----> 1 k_model = pytorch_to_keras(modelB, input_var, [(3, 224, 224,)], verbose=True,change_ordering=False)

~/anaconda3/envs/tf1/lib/python3.6/site-packages/pytorch2keras/converter.py in pytorch_to_keras(model, args, input_shapes, change_ordering, verbose, name_policy, use_optimizer, do_constant_folding)
     81     k_model = onnx_to_keras(onnx_model=onnx_model, input_names=input_names,
     82                             input_shapes=input_shapes, name_policy=name_policy,
---> 83                             verbose=verbose, change_ordering=change_ordering)
     84 
     85     return k_model

~/anaconda3/envs/tf1/lib/python3.6/site-packages/onnx2keras/converter.py in onnx_to_keras(onnx_model, input_names, input_shapes, name_policy, verbose, change_ordering)
    179             lambda_funcs,
    180             node_name,
--> 181             keras_names
    182         )
    183         if isinstance(keras_names, list):

~/anaconda3/envs/tf1/lib/python3.6/site-packages/onnx2keras/reshape_layers.py in convert_transpose(node, params, layers, lambda_func, node_name, keras_name)
     28     else:
     29         permute = keras.layers.Permute(params['perm'][1:], name=keras_name)
---> 30         layers[node_name] = permute(layers[input_name])
     31 
     32 

~/anaconda3/envs/tf1/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    536       if not self.built:
    537         # Build layer if applicable (if the `build` method has been overridden).
--> 538         self._maybe_build(inputs)
    539         # We must set self.built since user defined build functions are not
    540         # constrained to set self.built.

~/anaconda3/envs/tf1/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   1589     # Check input assumptions set before layer building, e.g. input rank.
   1590     input_spec.assert_input_compatibility(
-> 1591         self.input_spec, inputs, self.name)
   1592     input_list = nest.flatten(inputs)
   1593     if input_list and self._dtype is None:

~/anaconda3/envs/tf1/lib/python3.6/site-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    107         spec.min_ndim is not None or
    108         spec.max_ndim is not None):
--> 109       if x.shape.ndims is None:
    110         raise ValueError('Input ' + str(input_index) + ' of layer ' +
    111                          layer_name + ' is incompatible with the layer: '

AttributeError: 'tuple' object has no attribute 'ndims'

Desktop (please complete the following information):

Additional context I'm not sure if onnx2keras supports nfnet models, so do you recommend any other packages?