question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

change_ordering flag not working on a VGG19 pre-trained model

See original GitHub issue

I am trying to convert a pre-trained VGG19 model from ONNX to Keras and then run it on my CPU (run = just predict, not train).

I managed to convert it with onnx2keras but then ran into issues with NCHW channel_first (3, 224, 224) v. NHWC channel_last (224, 224, 3). Here is the error I get when running the converted model with Keras (TensorFlow backend):

tensorflow.python.framework.errors_impl.InvalidArgumentError: Default MaxPoolingOp only supports NHWC on device type CPU

I went back to onnx2keras and realized there is an experimental flag change_ordering that seems to do what I needed!

Unfortunately, change_ordering does not seem to work with my model:

ValueError: Operands could not be broadcast together with shapes (224, 224, 3) (3, 224, 224)

Here is the full stack trace:

ValueError                                Traceback (most recent call last)
<ipython-input-1-3a0e0ae82f31> in <module>()
      8 
      9 
---> 10 keras_m = onnx_to_keras(onnx_m, ['input'], verbose=True, change_ordering=True)

~/Documents/dev/venv/lib/python3.7/site-packages/onnx2keras/converter.py in onnx_to_keras(onnx_model, input_names, input_shapes, name_policy, verbose, change_ordering)
    212
    213 
--> 214         model_tf_ordering = keras.models.Model.from_config(conf)
    215 
    216         for dst_layer, src_layer in zip(model_tf_ordering.layers,

~/Documents/dev/venv/lib/python3.7/site-packages/keras/engine/network.py in from_config(cls, config, custom_objects)
   1030                 if layer in unprocessed_nodes:
   1031                     for node_data in unprocessed_nodes.pop(layer):
-> 1032                         process_node(layer, node_data)
   1033 
   1034         name = config.get('name')

~/Documents/dev/venv/lib/python3.7/site-packages/keras/engine/network.py in process_node(layer, node_data)
    989             # and building the layer if needed.
    990             if input_tensors:
--> 991                 layer(unpack_singleton(input_tensors), **kwargs)
    992 
    993         def process_layer(layer_data):

~/Documents/dev/venv/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
    429                                          'You can build it manually via: '
    430                                          '`layer.build(batch_input_shape)`')
--> 431                 self.build(unpack_singleton(input_shapes))
    432                 self.built = True
    433 

~/Documents/dev/venv/lib/python3.7/site-packages/keras/layers/merge.py in build(self, input_shape)
    254 
    255     def build(self, input_shape):
--> 256         super(Subtract, self).build(input_shape)
    257         if len(input_shape) != 2:
    258             raise ValueError('A `Subtract` layer should be called '

~/Documents/dev/venv/lib/python3.7/site-packages/keras/layers/merge.py in build(self, input_shape)
     89                 shape = input_shape[i][1:]
     90             output_shape = self._compute_elemwise_op_output_shape(output_shape,
---> 91                                                                   shape)
     92         # If the inputs have different ranks, we have to reshape them
     93         # to make them broadcastable.

~/Documents/dev/venv/lib/python3.7/site-packages/keras/layers/merge.py in _compute_elemwise_op_output_shape(self, shape1, shape2)
     59                     raise ValueError('Operands could not be broadcast '
     60                                      'together with shapes ' +
---> 61                                      str(shape1) + ' ' + str(shape2))
     62                 output_shape.append(i)
     63         return tuple(output_shape)

ValueError: Operands could not be broadcast together with shapes (224, 224, 3) (3, 224, 224)

I am using python3.7 and (all from pip): onnx==1.5.0, onnx2keras==0.0.4, Keras==2.2.4, and tensorflow==1.14.0.

Here is the code that gets me the stack trace above (on a Jupyter Notebook):

import onnx
from onnx2keras import onnx_to_keras
onnx_m = onnx.load('VGG19_PRETRAINED.onnx')
keras_m = onnx_to_keras(onnx_m, ['input'], verbose=True, change_ordering=True)

@nerox8664, thanks a lot for this (very useful!) library. I do appreciate any hints you might have about my issue.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gmalivenkocommented, Oct 1, 2019

The model contains constants (e.g. for input normalization) which were not permuted well. Now the converter (0.0.9) is able to permute constant shapes, and finally I’m able to convert your ONNX model.

Snippet

0reactions
lucasrlacommented, Oct 1, 2019

Thank you so much, Grigory. It is now working perfectly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

change_ordering flag not working on a VGG19 pre-trained ...
I am trying to convert a pre-trained VGG19 model from ONNX to Keras and then run it on my CPU (run = just...
Read more >
Transfer learning with VGG16 and VGG19, the simpler way!
In this blog, we are using the pre-trained weights of VGG16 and VGG19, change the output layer and solve a classification problem on...
Read more >
Pretrained VGG19 UNET in TensorFlow using Keras - YouTube
In this video, we are going to implement UNET in TensorFlow using Keras API. Here we are going to replace the encoder part...
Read more >
How to Use The Pre-Trained VGG Model to Classify Objects in ...
In this tutorial, we will focus on the use case of classifying new images using the VGG model. Keras provides both the 16-layer...
Read more >
Gaierror while importing pretrained vgg model on kaggle
It looks like you might not have enabled internet access in your kernel. You can do that in the panel on the right...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found