opencv4.1.1 dnn readNetFromTensorflow error
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 ...
can any one help me ?
well not really but my instinct says your problem is somewhere here:
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.
This code somehow stacks / unstacks? The channels . Please post your opencv code which is NOT working in addition to the tensorflow code.
The more it think about it lets just understand the python code - its all there i think.
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...