Ask Your Question
0

opencv4.1.1 dnn readNetFromTensorflow error

asked 2019-08-15 23:56:51 -0600

shuaizhang gravatar image

updated 2019-08-27 22:39:40 -0600

supra56 gravatar image

I have defined a keras model(.h5), and coverted to tensorflow (.pb). And it works on tensorflow :

 pb_path = r'*.pb'
 with tf.gfile.FastGFile(pb_path, 'rb') as f:
      graph_def = tf.GraphDef()
      graph_def.ParseFromString(f.read())
      _ = tf.import_graph_def(graph_def, name='')

 with tf.Session() as session:
      input = tf.get_default_graph().get_tensor_by_name("input_1:0")
      output_1 = tf.get_default_graph().get_tensor_by_name("trans/out_13/yolo_head/out_/concat:0")
      output_2 = tf.get_default_graph().get_tensor_by_name("trans/out_26/yolo_head/out_/concat:0")
      output_3 = tf.get_default_graph().get_tensor_by_name("trans/out_52/yolo_head/out_/concat:0")  
img = Image.open('/home/hyg/disk2/quanda_data/crop/1.png')
boxed_image = letterbox_image(img, (416, 416))
img = np.array(boxed_image, dtype='float32')
img *= 1./255.
img = np.expand_dims(img, axis=0)
out = session.run([output_1,output_2,output_3], feed_dict={input: img})

But when I use cv.dnn.readNetFromTensorflow("*.pb")

cv2.error: OpenCV(4.1.0) /io/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:1383: error: (-215:Assertion failed) scaleMat.type() == CV_32FC1 in function 'populateNet'

So, I try to use the tensorflow tools (optimize_for_inference.py) to optimize the model

Use tf.compat.v1.graph_util.remove_training_nodes
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_2/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_4/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_7/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_10/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_13/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_16/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_19/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_22/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_25/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_28/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_31/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_34/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_37/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_40/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_43/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_46/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_49/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_52/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_55/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_58/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_61/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_64/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_67/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_70/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_73/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_76/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_79/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_82/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_85/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input ...
(more)
edit retag flag offensive close merge delete

Comments

can any one help me ?

shuaizhang gravatar imageshuaizhang ( 2019-08-19 03:49:58 -0600 )edit

well not really but my instinct says your problem is somewhere here:

cv2.error: OpenCV(4.1.0) /io/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:1383: error: (-215:Assertion failed) scaleMat.type() == CV_32FC1 in function 'populateNet'

Lets think a bit - what is CV_32FC1 which is the type expected by the opencv dnn module? Well i am not 100% sure but i would say its a mat of 32Bit floating point values with 1 channel. This implies it must be a gray scale image? Only a wild guess.

img = np.expand_dims(img, axis=0)

This code somehow stacks / unstacks? The channels . Please post your opencv code which is NOT working in addition to the tensorflow code.

holger gravatar imageholger ( 2019-08-19 16:06:54 -0600 )edit

The more it think about it lets just understand the python code - its all there i think.

//load the rgb? images
img = Image.open('/home/hyg/disk2/quanda_data/crop/1.png')
//resize it to 416 * 416 
boxed_image = letterbox_image(img, (416, 416))
//convert the image into a numpy of type float 32
img = np.array(boxed_image, dtype='float32')
//do feature scaling - transform all values to floating values between 0-1
img *= 1./255.
//i am note 100% about this line - i think it will just stack the channels over each other ?
//so you now have rows * columns = (416 * 3) * 416
img = np.expand_dims(img, axis=0)
//you run with 3 arguments - the 3 channels?
out = session.run([output_1,output_2,output_3], feed_dict={input: img}
holger gravatar imageholger ( 2019-08-19 16:49:34 -0600 )edit

I noticed the word yolo: If you want to run a native(trained on yolo) yolo model on opencv : https://docs.opencv.org/master/da/d9d...

holger gravatar imageholger ( 2019-08-19 16:52:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2019-08-27 04:27:36 -0600

shuaizhang gravatar image

I found the problem. tensorflow.python.keras.layers .UpSampling2D , just like this:

 node {
      name: "up_sampling2d_3/mul"
        op: "Mul"
        input: "up_sampling2d_3/strided_slice"
       input: "up_sampling2d_3/Const"
     }

the op "Mul" assert "scaleMat.type() == CV_32FC1", when i removed the Upsample2D layer, it works.But now, i do not know how to fix it.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-08-15 23:56:51 -0600

Seen: 1,205 times

Last updated: Aug 27 '19