OpenCV dnn load tensorflow model

asked 2018-08-02 07:13:13 -0600

murphbg gravatar image

updated 2018-08-02 07:30:53 -0600

Hi,

I'm trying to export a simple Tensorflow model and import it using opencv.dnn

inp = tf.placeholder(tf.float64, (20,3), name='input')
out = tf.matmul(inp, np.random.random((3,1)),  name='output')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    tf.saved_model.simple_save(sess, './pb',  {"input": inp},  {"output": out})
    tf.train.write_graph(sess.graph_def, './pbtxt', 'network.pbtxt', True)

and then I try to load it :

net = cv.dnn.readNetFromTensorflow('./pb/saved_model.pb', './pbtxt/network.pbtxt')

I get the following error:

[libprotobuf ERROR opencv/3rdparty/protobuf/src/google/protobuf/wire_format_lite.cc:629] String field 'tensorflow.GradientDef.function_name' contains invalid UTF-8 data when parsing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes. 
Traceback (most recent call last):
     net = cv.dnn.readNetFromTensorflow('./pb/saved_model.pb', './pbtxt/network.pbtxt')
cv2.error: OpenCV(4.0.0-pre) opencv/modules/dnn/src/tensorflow/tf_io.cpp:44: error: (-2:Unspecified error) FAILED: ReadProtoFromBinaryFile(param_file, param). Failed to parse GraphDef file: ./pb/saved_model.pb in function 'ReadTFNetParamsFromBinaryFileOrDie'

Do you have any suggestions what might be the problem and what is the right approach to use the opencv.dnn module?

Attached is the pbtxt file

Thanks!

The pbtxt file is:

  node {
    name: "input"
    op: "Placeholder"
    attr {
      key: "dtype"
      value {
        type: DT_DOUBLE
      }
    }
    attr {
      key: "shape"
      value {
        shape {
          dim {
            size: 20
          }
          dim {
            size: 3
          }
        }
      }
    }
  }
  node {
    name: "output/b"
    op: "Const"
    attr {
      key: "dtype"
      value {
        type: DT_DOUBLE
      }
    }
    attr {
      key: "value"
      value {
        tensor {
          dtype: DT_DOUBLE
          tensor_shape {
            dim {
              size: 3
            }
            dim {
              size: 1
            }
          }
          tensor_content: "@]S\254\275\252\207?\306\234\032\266\244\253\357?\373hw\304\263V\341?"
        }
      }
    }
  }
  node {
    name: "output"
    op: "MatMul"
    input: "input"
    input: "output/b"
    attr {
      key: "T"
      value {
        type: DT_DOUBLE
      }
    }
    attr {
      key: "transpose_a"
      value {
        b: false
      }
    }
    attr {
      key: "transpose_b"
      value {
        b: false
      }
    }
  }
  node {
    name: "init"
    op: "NoOp"
  }
  versions {
    producer: 26
  }
edit retag flag offensive close merge delete

Comments

@murphbg, could you please attach both mentioned files?

dkurt gravatar imagedkurt ( 2018-08-02 09:33:15 -0600 )edit