Skip to content

opencv dnn crash when loading tensorflow model with split layer #15056

@moberweger

Description

@moberweger
System information (version)
  • OpenCV == 4.1.0
  • python == 2.7.12
  • Operating System / Platform == Ubuntu 16.04 x86_64
  • compiler == gcc 7.4.0
  • tensorflow == 1.14.0
Detailed description

Loading a specific configuration of a model from tensorflow to be run in OpenCV triggers an error when running cv2.dnn.readNetFromTensorflow. The cause of the error is related to splitting a tensor. As an example, I have attached a file that can trigger the error for a simple model. The model splits the last channel of the a tensor and outputs both splits.

The code raises the following error:
terminate called after throwing an instance of 'std::bad_alloc'

Steps to reproduce

The following python script can be used to trigger the error. Therefore, simply change the flag for the if.

import numpy
import cv2
import tensorflow as tf
from tensorflow.tools.graph_transforms import TransformGraph

if __name__ == '__main__':
    batch_size = 4
    input_shape = [batch_size, 32, 32, 1]
    features = tf.placeholder(tf.float32, input_shape, name='input')

    features0 = tf.layers.conv2d(inputs=features, filters=8, kernel_size=[3, 3], padding='same')
    # CHANGE THIS TO True to TRIGGER CRASH
    if True:
        features1 = tf.split(features0, num_or_size_splits=2, axis=3)[0]
        features2 = tf.split(features0, num_or_size_splits=2, axis=3)[1]
    else:
        features1, features2 = tf.split(features0, num_or_size_splits=2, axis=3)

    in1 = tf.layers.conv2d(inputs=features1, filters=8, kernel_size=[3, 3], strides=(2, 2), padding='same')
    in2 = tf.layers.conv2d(inputs=features2, filters=8, kernel_size=[3, 3], strides=(2, 2), padding='same')

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        constant_graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(),
                                                                      ['conv2d_1/BiasAdd', 'conv2d_2/BiasAdd'])
        tf.train.write_graph(constant_graph, "", "graph_final.pb", as_text=False)

    # export
    with tf.gfile.FastGFile("graph_final.pb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        graph_def = TransformGraph(graph_def, ['input'], ['conv2d_1/BiasAdd', 'conv2d_2/BiasAdd'],
                                   ['strip_unused_nodes'])
        with tf.gfile.FastGFile('saved_model.pb', 'wb') as f:
            f.write(graph_def.SerializeToString())

    # read model
    cvNet = cv2.dnn.readNetFromTensorflow('./saved_model.pb')
    img = numpy.zeros((32, 32, 1), dtype='uint8')
    cvNet.setInput(cv2.dnn.blobFromImage(img, size=(32, 32), swapRB=False, crop=False).repeat(batch_size, axis=0))
    cvOut = cvNet.forward()
    print len(cvOut)

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