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 to 'batch_normalization_88/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_91/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_94/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_100/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_102/FusedBatchNorm_1'
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_104/FusedBatchNorm_1'
And I have used the link text get the config file cv.dnn.readNetFromTensorflow(".pb", ".pbtxt"), but get:
cv2.error: OpenCV(4.1.0) /io/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:497: error: (-2:Unspecified error) Input layer not found: batch_normalization_1/FusedBatchNorm_1 in function 'connect'
I upload the converted tensorflow model file to MEGA