change_ordering flag not working on a VGG19 pre-trained model
See original GitHub issueI 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:
- Created 4 years ago
- Comments:10 (5 by maintainers)

Top Related StackOverflow Question
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
Thank you so much, Grigory. It is now working perfectly.