Skip to content

fix tf_text_graph_common tensor_content type bug - #13904

Merged
opencv-pushbot merged 1 commit into
opencv:3.4from
cftang0827:dnn_tf_text_graph_common_bug
Feb 27, 2019
Merged

opencv-pushbot merged 1 commit into
opencv:3.4from
cftang0827:dnn_tf_text_graph_common_bug

Conversation

@cftang0827

Copy link
Copy Markdown

This pullrequest changes

Tensorflow version: 1.12.0
OpenCV version: 3.4.3

Fix the bug in creating tensorflow model's pbtxt file.
The type of tensor.tensor_content should be bytes not str

node.attr['value'].tensor.tensor_content = ""
--> 
node.attr['value'].tensor.tensor_content = bytes('', 'utf-8')

@dkurt

dkurt commented Feb 25, 2019

Copy link
Copy Markdown
Member

@cftang0827, thank you! Could you please share a way how this bug affects import?

@cftang0827

Copy link
Copy Markdown
Author

Hi, actually it's in opencv/samples/dnn/ , therefore, it would not affect import and cause any error.
However, if you want to use opencv to inference a tf-based model, you need to create a config file.

https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API#generate-a-config-file

That's the script that generate model related config file.
I also tested in opencv-4.0.0, there's no such problem because there's an implementation of cv.dnn.writeTextGraph(modelPath, outputPath). However, if you use older version of opencv, the bug of type error would be showed up.

def writeTextGraph(modelPath, outputPath, outNodes):
    try:
        import cv2 as cv

        cv.dnn.writeTextGraph(modelPath, outputPath)
    except:
        import tensorflow as tf
        from tensorflow.tools.graph_transforms import TransformGraph

        with tf.gfile.FastGFile(modelPath, 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())

            graph_def = TransformGraph(graph_def, ['image_tensor'], outNodes, ['sort_by_execution_order'])

            for node in graph_def.node:
                if node.op == 'Const':
                    if 'value' in node.attr and node.attr['value'].tensor.tensor_content:
                        node.attr['value'].tensor.tensor_content = bytes('', 'utf-8')

        tf.train.write_graph(graph_def, "", outputPath, as_text=True)

Thanks.

@dkurt dkurt self-assigned this Feb 25, 2019
@dkurt

dkurt commented Feb 25, 2019

Copy link
Copy Markdown
Member

@cftang0827, This code part depends on TensorFlow version only. In example, for TensorFlow 1.12.0-rc1 I have <type 'str'> for type(node.attr['value'].tensor.tensor_content).

Comment thread samples/dnn/tf_text_graph_common.py Outdated
if node.op == 'Const':
if 'value' in node.attr and node.attr['value'].tensor.tensor_content:
node.attr['value'].tensor.tensor_content = ''
node.attr['value'].tensor.tensor_content = bytes('', 'utf-8')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to propose a bit different solution:

node.attr['value'].tensor.tensor_content = b''

I can now reproduce the problem (tested with different versions of TF and python).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggestion, and I think it's the better and simpler solution. Thank you.

@dkurt

dkurt commented Feb 25, 2019

Copy link
Copy Markdown
Member

@cftang0827, please change the base branch from 3.4 to master (Please do not close pull request but just create a new branch with the same name dnn_tf_text_graph_common_bug from 3.4 and then force push it to your fork).

@cftang0827
cftang0827 force-pushed the dnn_tf_text_graph_common_bug branch from d9e9ec4 to 98d8436 Compare February 25, 2019 17:21
@dkurt
dkurt changed the base branch from master to 3.4 February 25, 2019 17:38

@dkurt dkurt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good! Thank you!

@opencv-pushbot
opencv-pushbot merged commit 98d8436 into opencv:3.4 Feb 27, 2019
@alalek alalek mentioned this pull request Mar 5, 2019
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants