Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Opencv dnn import error for keras pretrained Vgg16 model

Hi,

I'm trying to export the keras pretrained VGG16 model to a tensorflow model and then I want to import the tensorflow model into opencv. To do this, I got the following python code:

from keras import applications
from keras import backend as K
import cv2 as cv
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

model = applications.VGG16(input_shape=(224, 224, 3), weights='imagenet', include_top=True)
K.set_learning_phase(0)

pred_node_names = [None]
pred = [None]
for i in range(1):
    pred_node_names[i] = "output_node"+str(i)
    pred[i] = tf.identity(model.outputs[i], name=pred_node_names[i])

 sess = K.get_session()
 constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
 graph_io.write_graph(constant_graph, ".", "model.pb", as_text=False)

net = cv.dnn.readNetFromTensorflow('model.pb')

Executing this code, gives following error (similar in C++):

cv2.error: C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:1487: error: (-2) Unknown layer type Shape in op flatten/Shape in function cv::dnn::experimental_dnn_v3::`anonymous-namespace'::TFImporter::populateNet

When I'm not including the top, I don't receive this error message.

Do you have any suggestions?

Opencv dnn import error for keras pretrained Vgg16 model

Hi,

I'm trying to export the keras pretrained VGG16 model to a tensorflow model and then I want to import the tensorflow model into opencv. To do this, I got the following python code:

from keras import applications
from keras import backend as K
import cv2 as cv
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

model = applications.VGG16(input_shape=(224, 224, 3), weights='imagenet', include_top=True)
K.set_learning_phase(0)

pred_node_names = [None]
pred = [None]
for i in range(1):
    pred_node_names[i] = "output_node"+str(i)
    pred[i] = tf.identity(model.outputs[i], name=pred_node_names[i])

 sess = K.get_session()
 constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
 graph_io.write_graph(constant_graph, ".", "model.pb", as_text=False)

net = cv.dnn.readNetFromTensorflow('model.pb')

Executing this code, gives following error (similar in C++):

cv2.error: C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:1487: error: (-2) Unknown layer type Shape in op flatten/Shape in function cv::dnn::experimental_dnn_v3::`anonymous-namespace'::TFImporter::populateNet

When I'm not including the top, I don't receive this error message.

Do you have any suggestions?

Opencv dnn import error for keras pretrained Vgg16 model

Hi,

I'm trying to export the keras pretrained VGG16 model to a tensorflow model and then I want to import the tensorflow model into opencv. To do this, I got the following python code:

from keras import applications
from keras import backend as K
import cv2 as cv
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

model = applications.VGG16(input_shape=(224, 224, 3), weights='imagenet', include_top=True)
K.set_learning_phase(0)

pred_node_names = [None]
pred = [None]
for i in range(1):
    pred_node_names[i] = "output_node"+str(i)
    pred[i] = tf.identity(model.outputs[i], name=pred_node_names[i])

 sess = K.get_session()
 constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
 graph_io.write_graph(constant_graph, ".", "model.pb", as_text=False)

net = cv.dnn.readNetFromTensorflow('model.pb')

Executing this code, gives following error (similar in C++):

cv2.error: C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:1487: error: (-2) Unknown layer type Shape in op flatten/Shape in function cv::dnn::experimental_dnn_v3::`anonymous-namespace'::TFImporter::populateNet

When I'm not including the top, I don't receive this error message.

Do you have any suggestions?

Opencv dnn import error for keras pretrained Vgg16 model

Hi,

I'm trying to export the keras pretrained VGG16 model to a tensorflow model and then I want to import the tensorflow model into opencv. To do this, I got the following python code:

from keras import applications
from keras import backend as K
import cv2 as cv
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

model = applications.VGG16(input_shape=(224, 224, 3), weights='imagenet', include_top=True)
K.set_learning_phase(0)

pred_node_names = [None]
pred = [None]
for i in range(1):
    pred_node_names[i] = "output_node"+str(i)
    pred[i] = tf.identity(model.outputs[i], name=pred_node_names[i])

 sess = K.get_session()
 constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
 graph_io.write_graph(constant_graph, ".", "model.pb", as_text=False)

net = cv.dnn.readNetFromTensorflow('model.pb')

Executing this code, gives following error (similar in C++):

cv2.error: C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:1487: error: (-2) Unknown layer type Shape in op flatten/Shape in function cv::dnn::experimental_dnn_v3::`anonymous-namespace'::TFImporter::populateNet

When I'm not including the top, I don't receive this error message.

Do you have any suggestions?

click to hide/show revision 5
retagged

Opencv dnn import error for keras pretrained Vgg16 model

Hi,

I'm trying to export the keras pretrained VGG16 model to a tensorflow model and then I want to import the tensorflow model into opencv. To do this, I got the following python code:

from keras import applications
from keras import backend as K
import cv2 as cv
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

model = applications.VGG16(input_shape=(224, 224, 3), weights='imagenet', include_top=True)
K.set_learning_phase(0)

pred_node_names = [None]
pred = [None]
for i in range(1):
    pred_node_names[i] = "output_node"+str(i)
    pred[i] = tf.identity(model.outputs[i], name=pred_node_names[i])

 sess = K.get_session()
 constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)
 graph_io.write_graph(constant_graph, ".", "model.pb", as_text=False)

net = cv.dnn.readNetFromTensorflow('model.pb')

Executing this code, gives following error (similar in C++):

cv2.error: C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:1487: error: (-2) Unknown layer type Shape in op flatten/Shape in function cv::dnn::experimental_dnn_v3::`anonymous-namespace'::TFImporter::populateNet

When I'm not including the top, I don't receive this error message.

Do you have any suggestions?