Skip to content

DNN, readNetFromTensorflow, assertion fails on addConstNodes #14224

Closed

Description

@sangjoonhong
System information (version)
  • OpenCV => 4.0.1 / 3.4.5, I tested various version.
  • Operating System / Platform => Ubuntu 16.04
Detailed description

I also faced the same problem, and tried many things but failed to fix the problem.
(#14218)
I'll Attach the model I tried. This model is MobileFaceNet based on mobilenet_v2
(https://github.com/sirius-ai/MobileFaceNet_TF)

frozen_model.pb is the model I tried the first time, and
frozen_inference_graph_opt was generated through optimize_for_inference.py

I have been blocked during few weeks with this problem.
If you need more information, I will give you it as soon as possible.
Plz help me...

frozen_models.zip

Activity

  1. dkurt commented on Apr 2, 2019

    @dkurt
    Member

    The problem is in Switch nodes. Looks like TensorFlow tools cannot remove it despite their inputs are false boolean constants.

    image

    similar problem but with different subgraph: #14073

  2. dkurt commented on Apr 2, 2019

    @dkurt
    Member

    @sangjoonhong, There is a workaround:

    import tensorflow as tf
    from tensorflow.tools.graph_transforms import TransformGraph
    
    graph = 'frozen_inference_graph_opt.pb'
    with tf.gfile.FastGFile(graph) as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    
        for i in reversed(range(len(graph_def.node))):
            op = graph_def.node[i].op
            name = graph_def.node[i].name
    
            if op == 'Switch' or op == 'Merge':
                inp = graph_def.node[i].input[0]
                for node in graph_def.node:
                    for j in range(len(node.input)):
                        if name == node.input[j]:
                            node.input[j] = inp
                del graph_def.node[i]
    
        graph_def = TransformGraph(graph_def, ["img_inputs"], ["embeddings"], ["strip_unused_nodes"])
        with tf.gfile.FastGFile("graph.pb", 'wb') as f:
            f.write(graph_def.SerializeToString())

    However we'll try to propose a PR to fix it.

  3. sangjoonhong commented on Apr 3, 2019

    @sangjoonhong
    Author

    @dkurt Thank you!! It works well.

  4. dkurt commented on Apr 3, 2019

    @dkurt
    Member

    @sangjoonhong, BTW, can you run frozen_inference_graph_opt.pb using TensorFlow? I got the following error with TF 1.12.0:

    ValueError: NodeDef expected inputs '' do not match 1 inputs specified; Op<name=Const; signature= -> output:dtype; attr=value:tensor; attr=dtype:type>; NodeDef: {{node MobileFaceNet/MobileFaceNet/Conv2d_0/BatchNorm/cond_1/AssignMovingAvg_1/decay}} = Const[_class=["loc:@MobileFaceNet/Conv2d_0/BatchNorm/moving_variance"], dtype=DT_FLOAT, value=Tensor<type: float shape: [] values: 0.005>](MobileFaceNet/MobileFaceNet/Conv2d_0/BatchNorm/cond_1/Switch:1)
    

    Which TensorFlow version do you use?

  5. sangjoonhong commented on Apr 4, 2019

    @sangjoonhong
    Author

    @dkurt I'm using TF version 1.13.
    I only tested frozen_model.pb using TF.
    frozen_inference_graph_opt.pb was generated for opencv. so I didn't test using TF

  6. dkurt commented on Apr 4, 2019

    @dkurt
    Member

    @sangjoonhong, This PR now handles this workaround: #14251

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions