Skip to content

readNetFromTensorflow crash, in "else if (type == "FusedBatchNorm") { //break here }" #14236

Closed

Description

@ib198669

System information (version)

  • OpenCV => 4.0.1
  • Operating System / Platform => Windows10 64 Bit
  • Compiler => Visual Studio 2015
  • Train System => Keras + inceptionV3 + tensorflow1.13 +Ubuntu14

My C++ code :


...
String tf_pb_file = "mosaic.model.pb";
Net net = readNetFromTensorflow(tf_pb_file);  //crash here!!!
...

Code in tf_importer.cpp crashed in line 1506


        else if (type == "FusedBatchNorm")
        {
            ... ... 
            if (isTraining)
            {
                if (layerParams.blobs.size() == 2)
                    CV_Error(Error::StsNotImplemented, "Cannot determine number "
                             "of parameters for batch normalization layer.");
                //Crashed here!
                mean = Mat::zeros(1, layerParams.blobs[3].total(), CV_32F);

the node of FusedBatchNorm in my model is as follows, I think there might be someting wrong with the attribute 'is_training' :
360截图18141217433342

My ‘.pb’ package is in the My Google Network disk , I will appreciate it if you can help me ~!

Activity

  1. dkurt commented on Apr 3, 2019

    @dkurt
    Member

    Try to turn Keras's training phase to false because this model serialized in the training mode.

  2. ib198669 commented on Apr 4, 2019

    @ib198669
    Author

    Try to turn Keras's training phase to false because this model serialized in the training mode.

    Thanks very much , after turning Keras's training phase to false by the following code , it will not crash in is_training code anymore

    
        for i in range(len(init_graph.node)):
            node = init_graph.node[i]
            if node.op == 'FusedBatchNorm':
                if 'is_training' in node.attr: 
                    if node.attr['is_training'].b == True :
                        print ('before:',node.attr)
                        node.attr['is_training'].b = False
                        print ('after' , node.attr)
    

    but it crashed in following code :

    
    //tf_importer.cpp crashed in  line  1521
            else if (type == "FusedBatchNorm")
            {
                ... ... 
                if (isTraining)
                {
                  ... ...
                }else{
                     ...
                    //crashed in getConstBlob  in  "case tensorflow::DT_HALF:  parseTensor( ... )"
                    blobFromTensor(getConstBlob(layer, value_id, 4), std);
                }
    

    I really don't know why it crashed here ~

  3. ib198669 commented on Apr 8, 2019

    @ib198669
    Author

    @ib198669, Take a look at https://keras.io/backend/#set_learning_phase

    Thanks @dkurt , I have tried to set_learning_phase(0), but it seems it doesn't hep . It displays is_training phase is True in tensorboard as well .
    my code :

    
        from tensorflow.python.framework import graph_util,graph_io
        sess = K.get_session()
        init_graph = sess.graph.as_graph_def()
        K.set_learning_phase(0)
        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)
    
  4. self-assigned this
    on Apr 9, 2019
  5. dkurt commented on Apr 9, 2019

    @dkurt
    Member

    @ib198669, Thank you. We've also found a bug analyzing this model so you need to apply these changes to the code: #14284.

    To enable this model in OpenCV please do the following:

    import tensorflow as tf
    from tensorflow.tools.graph_transforms import TransformGraph
    from tensorflow.python.tools import optimize_for_inference_lib
    
    with tf.gfile.FastGFile('mosaic.model.pb', 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    
        graph_def = TransformGraph(graph_def, ["input_1"], ["dense_2/Softmax"], ["fold_constants", "sort_by_execution_order"])
        graph_def = optimize_for_inference_lib.optimize_for_inference(graph_def, ['input_1'], ['dense_2/Softmax'], tf.float32.as_datatype_enum)
        with tf.gfile.FastGFile("graph.pb", 'wb') as f:
            f.write(graph_def.SerializeToString())

    Then import graph.pb with OpenCV.

  6. ib198669 commented on Apr 10, 2019

    @ib198669
    Author

    @dkurt Thanks . After I tried the python script you gave me above and modified the code of opencv4.0.1 , it crashed as well . It crashed in bellow code when type shows as 'Switch' .
    in tf_importer.cpp in line 1905: it crashed here when parsing layer "batch_normalization_94/cond/FusedBatchNorm/Switch_3"

    
                for (int i = 0; i < layer.input_size(); ++i)
                {
                    if (value_id.find(layer.input(i)) != value_id.end())
                    {
                         //it crashed here  line 1905
                        Mat blob = getTensorContent(getConstBlob(layer, value_id, i));
                        layerParams.blobs.push_back(blob);
                    }
                    else
                        inputsNames.push_back(layer.input(i));
                }
    

    crashed in tf_graph_simplifier.cpp in line 773 when Tensor's data type shows as DT_BOOL

    
                //crashed here in tf_graph_simplifier.cpp in line 773 
                CV_Error(Error::StsError, "Tensor's data type is not supported");
    
  7. dkurt commented on Apr 10, 2019

    @dkurt
    Member

    @ib198669, make sure you use the latest state of OpenCV (especially the changes from #14251).

  8. ib198669 commented on Apr 11, 2019

    @ib198669
    Author

    @dkurt thanks a lot . After update the newest code of opencv(4.10) and modify the dnn code as #14284 , the bug is gone .

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions