Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

readNetFromTensorflow() errors from Mask_RCNN model

I made my Mask_RCNN model from this github project Enviroment : win7 x64 visual studio 2015 opencv 4.0.1 tensorflow

and I want to turn it to .pb and .pbtxt so that I can read it by readNetFromTensorflow() I wrote something like

def h5_to_pb(h5_model,output_dir,model_name,out_prefix = "output_",log_tensorboard = True):
    if osp.exists(output_dir) == False:
        os.mkdir(output_dir)
    out_nodes = []
    for i in range(len(h5_model.outputs)):
        out_nodes.append(out_prefix + str(i + 1))
        tf.identity(h5_model.output[i],out_prefix + str(i + 1))
    sess = K.get_session()
    from tensorflow.python.framework import graph_util,graph_io
    init_graph = sess.graph.as_graph_def()
    main_graph = graph_util.convert_variables_to_constants(sess,init_graph,out_nodes)
    graph_io.write_graph(main_graph,output_dir,name = model_name,as_text = False)
    if log_tensorboard:
        from tensorflow.python.tools import import_pb_to_tensorboard
        import_pb_to_tensorboard.import_to_tensorboard(osp.join(output_dir,model_name),output_dir)

output_dir = osp.join(os.getcwd(),"trans_model")
output_dir = "D:/"

ROOT_DIR = os.path.abspath("C:/Mask_RCNN/")
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
model = modellib.MaskRCNN(mode="inference", config=config,
                      model_dir=MODEL_DIR)
# model.keras_model.summary()
# print(model.keras_model.inputs)
# print(model.keras_model.outputs)

model.load_weights(weight_file_path, by_name=True)
h5_model = model.keras_model
print(len(h5_model.outputs))
h5_to_pb(h5_model,output_dir = output_dir,model_name = output_graph_name)

then I turned pb to pbtxt

def convert_pb_to_pbtxt(filename):
  with gfile.FastGFile(filename,'rb') as f:
    graph_def = tf.GraphDef()

    graph_def.ParseFromString(f.read())

    tf.import_graph_def(graph_def, name='')

    tf.train.write_graph(graph_def, './', 'protobuf.pbtxt', as_text=True)
  return

then I got the error Error parsing text-format opencv_te nsorflow.GraphDef: 41208:5: Unknown enumeration value of "DT_RESOURCE" for field "type". OpenCV(4.0.1) Error: Unspecified error (FAILED: ReadProtoFromTextFile(param_file , param). Failed to parse GraphDef file: D:/model_1.pbtxt) in cv::dnn::ReadTFNet ParamsFromTextFileOrDie, file C:\build\master_winpack-build-win64-vc14\opencv\mo dules\dnn\src\tensorflow\tf_io.cpp, line 54

it seem that the format of the pbtxt is not right, so can anyone tell me how to turn the model I trained to let the opencv read it to do the object detection please?

readNetFromTensorflow() errors from Mask_RCNN model

I made my Mask_RCNN model from this github project it is a project written with tensorflow and keras. Enviroment : win7 x64 visual studio 2015 opencv 4.0.1 tensorflow

and 1.12 GPU gtx1060 CUDA 9.0 since it saves its weights to .h5 file, I want to turn it to .pb and .pbtxt so that I can read it by readNetFromTensorflow() readNetFromTensorflow(). I wrote something like

def h5_to_pb(h5_model,output_dir,model_name,out_prefix = "output_",log_tensorboard = True):
    if osp.exists(output_dir) == False:
        os.mkdir(output_dir)
    out_nodes = []
    for i in range(len(h5_model.outputs)):
        out_nodes.append(out_prefix + str(i + 1))
        tf.identity(h5_model.output[i],out_prefix + str(i + 1))
    sess = K.get_session()
    from tensorflow.python.framework import graph_util,graph_io
    init_graph = sess.graph.as_graph_def()
    main_graph = graph_util.convert_variables_to_constants(sess,init_graph,out_nodes)
    graph_io.write_graph(main_graph,output_dir,name = model_name,as_text = False)
    if log_tensorboard:
        from tensorflow.python.tools import import_pb_to_tensorboard
        import_pb_to_tensorboard.import_to_tensorboard(osp.join(output_dir,model_name),output_dir)

output_dir = osp.join(os.getcwd(),"trans_model")
output_dir = "D:/"

ROOT_DIR = os.path.abspath("C:/Mask_RCNN/")
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
model = modellib.MaskRCNN(mode="inference", config=config,
                      model_dir=MODEL_DIR)
# model.keras_model.summary()
# print(model.keras_model.inputs)
# print(model.keras_model.outputs)

model.load_weights(weight_file_path, by_name=True)
h5_model = model.keras_model
print(len(h5_model.outputs))
h5_to_pb(h5_model,output_dir = output_dir,model_name = output_graph_name)

then I turned pb to pbtxt

def convert_pb_to_pbtxt(filename):
  with gfile.FastGFile(filename,'rb') as f:
    graph_def = tf.GraphDef()

    graph_def.ParseFromString(f.read())

    tf.import_graph_def(graph_def, name='')

    tf.train.write_graph(graph_def, './', 'protobuf.pbtxt', as_text=True)
  return

then I got the error Error parsing text-format opencv_te nsorflow.GraphDef: 41208:5: Unknown enumeration value of "DT_RESOURCE" for field "type". OpenCV(4.0.1) Error: Unspecified error (FAILED: ReadProtoFromTextFile(param_file , param). Failed to parse GraphDef file: D:/model_1.pbtxt) in cv::dnn::ReadTFNet ParamsFromTextFileOrDie, file C:\build\master_winpack-build-win64-vc14\opencv\mo dules\dnn\src\tensorflow\tf_io.cpp, line 54

it seem that the format of the pbtxt is not right, so can anyone tell me how to turn the model I trained to let the opencv read it to do the object detection please?