You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inputs.size() > 0 in function 'getMemoryShapes' #14290
Strange error when I am trying to convert model from keras to onnx and run through opencv dnn.
OpenCV == 4.1.0
Operating System / Platform == Ubuntu 16.04 / 64 Bit
Compiler == conda 4.6.11 / libgcc-ng 8.2.0
onnxmltools == 1.4.0 / onnx = 1.4.1
keras == 2.2.4 / tensorflow == 1.13.1
import os
os.environ['CUDA_VISIBLE_DEVICES'] = ''
import numpy as np
from keras import regularizers
from keras import initializers
from keras import layers
from keras.models import Model
def get_model():
inp = layers.Input(shape=(48,80,1))
x = layers.Conv2D(32, 7)(inp)
x = layers.BatchNormalization()(x)
out = layers.ReLU()(x)
return Model(inp, out)
if __name__ == '__main__':
model = get_model()
model.compile(optimizer='adam', loss='mse')
X = np.random.rand(32, 48, 80, 1)
y = np.random.rand(32, 42, 74, 32)
model.fit(X, y, epochs=1, validation_data=(X, y))
model.save('model.h5')
import onnxmltools
from keras.models import load_model
model = load_model('model.h5')
onnx_model = onnxmltools.convert_keras(model)
onnxmltools.utils.save_model(onnx_model, 'model.onnx')
import cv2
cv2.dnn.readNetFromONNX('model.onnx')
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-3-6da663bc3421> in <module>
----> 1 cv2.dnn.readNetFromONNX("models/eyegaze_reduced.onnx")
error: OpenCV(4.1.0) ../modules/dnn/src/layers/permute_layer.cpp:124: error: (-215:Assertion failed) inputs.size() > 0 in function 'getMemoryShapes'
So with tool tensorflow-onnx i converted this model.pb to model.onnx. (Command: python -m tf2onnx.convert --input model.pb --inputs input_1:0 --outputs re_lu_1/Relu:0 --output model.onnx --verbose)
And this model.onnx file work without any "nodes reordering".
But i wrote a small script to check whether reordering is needed:
import tensorflow as tf
from tensorflow.tools.graph_transforms import TransformGraph
import numpy as np
graph = 'model.pb'
with tf.gfile.GFile(graph, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
print('before sort')
for n in graph_def.node:
print(n.name)
graph_def = TransformGraph(graph_def, ['input_1'], ['re_lu_1'], ['sort_by_execution_order'])
print('after sort')
for n in graph_def.node:
print(n.name)
with tf.gfile.FastGFile('model_sorted.pb', 'wb') as f:
f.write(graph_def.SerializeToString())
@dkurt, models.zip model.h5 - original keras model model.onnx - onnx model from model.h5 ( converted by onnxmltools ) model.pb - .pb from model.h5 ( converted by keras_to_tensorflow ) model_pb.onnx - onnx from model.pb (converted by tensorflow-onnx )
I suppose, that model.onnx and model_pb.onnx should be similar, but first doesn't work and second does work.
Update: So can we close an issue? Yes, model_pb.onnx is similar to model.onnx but with ordered nodes. For now OpenCV can sort graphs only from Keras but not from ONNX so both models from Keras (orderer and unordered) will work but only ordered model from ONNX.
Activity
dkurt commentedon Apr 10, 2019
Is it possible to share the model? It may help us to solve it faster.
alexionby commentedon Apr 10, 2019
@dkurt , done.
models.zip
dkurt commentedon Apr 11, 2019
It looks like ONNX doen't sort graphs in the topological order before conversion. This is how nodes are stored in the model:
As you may see, node
batch_normalization_1_cond_Merge_0usesTFNodes_re_lu_1_Relu_0before it's definition.Please try one of the following ways:
.h5model to TensorFlow model.pband try to import it with OpenCValexionby commentedon Apr 11, 2019
@dkurt
I converted model to .pb with this repo: keras_to_tensorflow
And then I tried to infer it in opencv. Inference was succesfull.
So with tool tensorflow-onnx i converted this
model.pbtomodel.onnx. (Command:python -m tf2onnx.convert --input model.pb --inputs input_1:0 --outputs re_lu_1/Relu:0 --output model.onnx --verbose)And this
model.onnxfile work without any "nodes reordering".But i wrote a small script to check whether reordering is needed:
Output:
So, nodes order has changed, but in case of
.pbfile it works anyway. And when I try to import it directly fromkerastoonnxit fails.Can something else be wrong except the nodes order?
dkurt commentedon Apr 11, 2019
@alexionby, Please upload mentioned model which produces error.
alexionby commentedon Apr 11, 2019
@dkurt, models.zip
model.h5- original keras modelmodel.onnx- onnx model frommodel.h5( converted byonnxmltools)model.pb-.pbfrommodel.h5( converted bykeras_to_tensorflow)model_pb.onnx-onnxfrommodel.pb(converted bytensorflow-onnx)I suppose, that
model.onnxandmodel_pb.onnxshould be similar, but first doesn't work and second does work.dkurt commentedon Apr 11, 2019
@alexionby,
model_pb.onnxworks fine for me. Please try to update OpenCV locally:Update: So can we close an issue? Yes,
model_pb.onnxis similar tomodel.onnxbut with ordered nodes. For now OpenCV can sort graphs only from Keras but not from ONNX so both models from Keras (orderer and unordered) will work but only ordered model from ONNX.alexionby commentedon Apr 11, 2019
@dkurt , yes we can.
*** I tried to convert both ordered and unordered
.pbto.onnx. And they both worked. That's what i was trying to say.