Issue 1:
System information (version)

OpenCV => 3.4.1

Operating System / Platform => Ubuntu 16.04 64Bit

Compiler => gcc 5.4.0

TensorFlow => 1.4.0

Model => faster_rcnn_resnet50_coco http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet50_coco_2018_01_28.tar.gz

Detailed description

I have trained the original model on myself image set with the form of VOC2007.

But when I try to load the frozen model in Tensorflow into OpenCV, the following error persists.

Steps to reproduce

Loading the frozen graphs

cvNet = cv.dnn.readNetFromTensorflow('./output_inference_graph/trans_graph.pb', './output_inference_graph/graph.pbtxt')

I get the error

OpenCV(3.4.1) Error: Unspecified error (Input layer not found: FirstStageFeatureExtractor/resnet_v1_50/resnet_v1_50/conv1/BatchNorm/FusedBatchNorm) in connect, file /io/opencv/modules/dnn/src/tensorflow/tf_importer.cpp, line 553 Traceback (most recent call last): File "detect_opencv_tensorflow_2.py", line 3, in <module> cvNet = cv.dnn.readNetFromTensorflow('./output_inference_graph/trans_graph.pb', './output_inference_graph/graph.pbtxt') cv2.error: OpenCV(3.4.1) /io/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:553: error: (-2) Input layer not found: FirstStageFeatureExtractor/resnet_v1_50/resnet_v1_50/conv1/BatchNorm/FusedBatchNorm in function connect

Issue 2:
System information (version)

OpenCV => 3.4 installed with pip

Operating System / Platform => Ubuntu 16.04

Python => Python 3.5.2

Tensorflow => 1.5

Detailed description

I'm trying to load in a pretrained MobileNet model from tensorflow.python.keras.applications. However I'm running into problems importing the model into dnn using cv2.dnn.readNetFromTensorflow(frozen_graph, pb_txt)

I followed some of the steps described in #9894 in addition to some of the other ones decribed by @dkurt in various posts.

General Steps

load in MobileNet from tensorflow.python.keras.applications

Freeze the graph

Save the frozen graph as a .pb file

optimize the graph for inference using the optimize_for_inference tool provided by tensorflow

Remove the constant nodes using a function provided by @dkurt in #9894 to make a .pbtxt

Modify the .pbtxt I can modify the RELU layers to be RELU6 instead of a RELU layer stacked with minimum and maxmium layers (since these are not supported by opencv)

Try to load in the .pb and .pbtxt using readNetFromTensorflow

The issue stems that when I use the code to generate a .pbtxt file to remove const nodes that openv doesn't use. Specifically it deletes some of the input constants being fed into some of my Fused Batch Normalization layers i.e.

OpenCV Error: Unspecified error (Input layer not found: conv1_bn/FusedBatchNorm) in connect, file /root/opencv/modules/dnn/src/tensorflow/tf_importer.cpp, line 553

Traceback (most recent call last):

File "/opt/PyCharm/helpers/pydev/pydevd.py", line 1596, in

globals = debugger.run(setup['file'], None, None, is_module)

File "/opt/PyCharm/helpers/pydev/pydevd.py", line 974, in run

pydev_imports.execfile(file, globals, locals) # execute the script

File "/opt/PyCharm/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile

exec(compile(contents+"\n", file, 'exec'), glob, loc)

File "/home/dl_docker_user/opencv_dnn_wrapper/main.py", line 38, in

net = cv2.dnn.readNetFromTensorflow(frozen_graph, pb_txt)

cv2.error: /root/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:553: error: (-2) Input layer not found: conv1_bn/FusedBatchNorm in function connect

Steps to reproduce

def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
graph = session.graph
with graph.as_default():
freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
output_names = output_names or []
output_names += [v.op.name for v in tf.global_variables()]
input_graph_def = graph.as_graph_def()
if clear_devices:
for node in input_graph_def.node:
node.device = ""
frozen_graph = convert_variables_to_constants(session, input_graph_def,
output_names, freeze_var_names)
return frozen_graph

from tensorflow.python.keras.applications import MobileNet
from tensorflow.python.keras import backend as K
import tensorflow as tf
from tensorflow.python.framework import graph_util, graph_io
K.set_learning_phase(False)
model = MobileNet(input_shape=(224, 224, 3), weights='mobilenet_1_0_224_tf.h5', include_top=True)
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()

frozen_graph = freeze_session(sess, output_names=["output_node0"])
graph_io.write_graph(frozen_graph, ".", "model.pb", as_text=False)

Then I used the optimize for inference tool provided by tensorflow

python /usr/local/lib/python3.5/dist-packages/tensorflow/python/tools/optimize_for_inference.py --input model.pb --output opt_model.pb --input_names input_1 --output_names output_node0 --frozen_graph True

Then I create the .pbtxt file using

def create_pbtxt_compatible_with_opencv(pb_path, pbtxt_path):
import tensorflow as tf

# Read the graph.
with tf.gfile.FastGFile(pb_path, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())

# Remove Const nodes.
for i in reversed(range(len(graph_def.node))):
if graph_def.node[i].op == 'Const':
del graph_def.node[i]
for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim',
'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training',
'Tpaddings']:
if attr in graph_def.node[i].attr:
del graph_def.node[i].attr[attr]

# Save as text.
tf.train.write_graph(graph_def, "", pbtxt_path, as_text=True)

Lastly, I use this dnn call on the .pb and .pbtxt files

frozen_graph = "opt_model.pb"

I've also attached the pbtxt file for reference. Note I'm aware I need to replace the RELU functions followed by min and max with a relu6 layer instead. Thanks for the help!
[opt_model_pbtxt.txt](https://github.com/opencv/opencv/files/1725401/opt_model_pbtxt.txt)

pb_txt = "opt_model.pbtxt"

net = cv2.dnn.readNetFromTensorflow(frozen_graph, pb_txt)

Resulting in,

OpenCV Error: Unspecified error (Input layer not found: conv1_bn/FusedBatchNorm) in connect, file /root/opencv/modules/dnn/src/tensorflow/tf_importer.cpp, line 553

Traceback (most recent call last):

File "/opt/PyCharm/helpers/pydev/pydevd.py", line 1596, in

globals = debugger.run(setup['file'], None, None, is_module)

File "/opt/PyCharm/helpers/pydev/pydevd.py", line 974, in run

pydev_imports.execfile(file, globals, locals) # execute the script

File "/opt/PyCharm/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile

exec(compile(contents+"\n", file, 'exec'), glob, loc)

File "/home/dl_docker_user/opencv_dnn_wrapper/main.py", line 38, in

net = cv2.dnn.readNetFromTensorflow(frozen_graph, pb_txt)

cv2.error: /root/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:553: error: (-2) Input layer not found: conv1_bn/FusedBatchNorm in function connect