Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

asked 2017-10-23 04:13:44 -0600

berak gravatar image

another tensorflow import problem

a fairly simple network (full code here) crashes the tf_importer OpenCV Error: Assertion failed (!beginsData.empty()) in populateNet, file C:\p\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, line 941 def conv_net(x, n_classes, dropout, reuse, is_training): # Define a scope for reusing the variables with tf.variable_scope('ConvNet', reuse=reuse):

        # Convolution Layer with 32 filters and a kernel size of 5
        conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv1 = tf.layers.max_pooling2d(conv1, 2, 2)

        # Convolution Layer with 32 filters and a kernel size of 5
        conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv2 = tf.layers.max_pooling2d(conv2, 2, 2)

        # Flatten the data to a 1-D vector for the fully connected layer
        fc1 = tf.contrib.layers.flatten(conv2)

        # Fully connected layer (in contrib folder for now)
        fc1 = tf.layers.dense(fc1, 1024)
        # Apply Dropout (if is_training is False, dropout is not applied)
        fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)

        # Output layer, class prediction
        out = tf.layers.dense(fc1, n_classes)
        # Because 'softmax_cross_entropy_with_logits' already apply softmax,
        # we only apply softmax to testing network
        out = tf.nn.softmax(out) if not is_training else out

    return out

then we have:

 python freeze.py --output_node ConvNet/Softmax --model_dir .
 python optimize_for_inference.py --input frozen_model.pb --output face_opt.pb --frozen_graph True --input_names ConvNet_1/conv2d/convolution  --output_names ConvNet_1/Softmax

and some minimal c++ code, to load it into the dnn:

#include <opencv2/dnn.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace cv::dnn;
using namespace std;


int main(int argc, char **argv)
{
    String modelBin = "mnist/face_opt.pb";
    String imageFile = (argc > 1) ? argv[1] : "test.jpg";
    Net net = dnn::readNetFromTensorflow(modelBin);
    Mat img = imread(imageFile, 0);
    resize(img, img, Size(64,64));  
    Mat inputBlob = blobFromImage(img);
    net.setInput(inputBlob, "ConvNet_1/conv2d/convolution"); 

    Mat probMat =  net.forward();
    Point p; minMaxLoc(probMat, 0,0,0, &p);
    cout << p.x << " " << probMat << endl;
    return 0;
}

resulting in a crash:

OpenCV Error: Assertion failed (!beginsData.empty()) in populateNet, file C:\p\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, line 941

debugging it a bit, i see, that tf inserted some layers here:

    at C:\p\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:623
623             LayerParams layerParams;
(gdb) p name.c_str()
$1 = 0x5b21644 "ConvNet/conv2d/bias"
...
$2 = 0x5b21684 "ConvNet/conv2d_1/kernel"
$3 = 0x5b216c4 "ConvNet/conv2d_1/bias"
$4 = 0x5b215c4 "ConvNet/dense/kernel"
$5 = 0x5b21584 "ConvNet/dense/bias"
$6 = 0x5b215c4 "ConvNet/dense_1/kernel"
$7 = 0x5b21644 "ConvNet/dense_1/bias"
$8 = 0x5b21244 "ConvNet_1/conv2d/convolution"
$9 = 0x5b21684 "ConvNet_1/conv2d/BiasAdd"
$10 = 0x5b214c4 "ConvNet_1/conv2d/Relu"
$11 = 0x5b6c7c4 "ConvNet_1/max_pooling2d/MaxPool"
$12 = 0x5b6c304 "ConvNet_1/conv2d_2/convolution"
$13 = 0x5b6cb04 "ConvNet_1/conv2d_2/BiasAdd"
$14 = 0x5b6d2c4 "ConvNet_1/conv2d_2/Relu"
$15 = 0x5b21104 "ConvNet_1/max_pooling2d_2/MaxPool"
$16 = 0x5b6d6c4 "ConvNet_1/Flatten/Shape"
$17 = 0x5b6ec44 "ConvNet_1/Flatten/Slice/begin"
$18 = 0x5b6ec44 "ConvNet_1/Flatten/Slice/size"
$19 = 0x5b6ed84 "ConvNet_1/Flatten/Slice"

looking here: i see, that beginsData is indeed an empty string, while sizesData seems to contain something.

another tensorflow import problem

a fairly simple network (full code here) crashes the tf_importer tf_importer:

OpenCV Error: Assertion failed (!beginsData.empty()) in populateNet, file C:\p\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, line 941
 

(full code here)

def conv_net(x, n_classes, dropout, reuse, is_training):
     # Define a scope for reusing the variables
     with tf.variable_scope('ConvNet', reuse=reuse):

reuse=reuse):

        # Convolution Layer with 32 filters and a kernel size of 5
        conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv1 = tf.layers.max_pooling2d(conv1, 2, 2)

        # Convolution Layer with 32 filters and a kernel size of 5
        conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv2 = tf.layers.max_pooling2d(conv2, 2, 2)

        # Flatten the data to a 1-D vector for the fully connected layer
        fc1 = tf.contrib.layers.flatten(conv2)

        # Fully connected layer (in contrib folder for now)
        fc1 = tf.layers.dense(fc1, 1024)
        # Apply Dropout (if is_training is False, dropout is not applied)
        fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)

        # Output layer, class prediction
        out = tf.layers.dense(fc1, n_classes)
        # Because 'softmax_cross_entropy_with_logits' already apply softmax,
        # we only apply softmax to testing network
        out = tf.nn.softmax(out) if not is_training else out

    return out

then we have:

 python freeze.py --output_node ConvNet/Softmax --model_dir .
 python optimize_for_inference.py --input frozen_model.pb --output face_opt.pb --frozen_graph True --input_names ConvNet_1/conv2d/convolution  --output_names ConvNet_1/Softmax

and some minimal c++ code, to load it into the dnn:

#include <opencv2/dnn.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace cv::dnn;
using namespace std;


int main(int argc, char **argv)
{
    String modelBin = "mnist/face_opt.pb";
    String imageFile = (argc > 1) ? argv[1] : "test.jpg";
    Net net = dnn::readNetFromTensorflow(modelBin);
    Mat img = imread(imageFile, 0);
    resize(img, img, Size(64,64));  
    Mat inputBlob = blobFromImage(img);
    net.setInput(inputBlob, "ConvNet_1/conv2d/convolution"); 

    Mat probMat =  net.forward();
    Point p; minMaxLoc(probMat, 0,0,0, &p);
    cout << p.x << " " << probMat << endl;
    return 0;
}

resulting in a crash:

OpenCV Error: Assertion failed (!beginsData.empty()) in populateNet, file C:\p\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, line 941

debugging it a bit, i see, that tf inserted some layers here:

    at C:\p\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:623
623             LayerParams layerParams;
(gdb) p name.c_str()
$1 = 0x5b21644 "ConvNet/conv2d/bias"
...
$2 = 0x5b21684 "ConvNet/conv2d_1/kernel"
$3 = 0x5b216c4 "ConvNet/conv2d_1/bias"
$4 = 0x5b215c4 "ConvNet/dense/kernel"
$5 = 0x5b21584 "ConvNet/dense/bias"
$6 = 0x5b215c4 "ConvNet/dense_1/kernel"
$7 = 0x5b21644 "ConvNet/dense_1/bias"
$8 = 0x5b21244 "ConvNet_1/conv2d/convolution"
$9 = 0x5b21684 "ConvNet_1/conv2d/BiasAdd"
$10 = 0x5b214c4 "ConvNet_1/conv2d/Relu"
$11 = 0x5b6c7c4 "ConvNet_1/max_pooling2d/MaxPool"
$12 = 0x5b6c304 "ConvNet_1/conv2d_2/convolution"
$13 = 0x5b6cb04 "ConvNet_1/conv2d_2/BiasAdd"
$14 = 0x5b6d2c4 "ConvNet_1/conv2d_2/Relu"
$15 = 0x5b21104 "ConvNet_1/max_pooling2d_2/MaxPool"
$16 = 0x5b6d6c4 "ConvNet_1/Flatten/Shape"
$17 = 0x5b6ec44 "ConvNet_1/Flatten/Slice/begin"
$18 = 0x5b6ec44 "ConvNet_1/Flatten/Slice/size"
$19 = 0x5b6ed84 "ConvNet_1/Flatten/Slice"

looking here: i see, that beginsData is indeed an empty string, while sizesData seems to contain something.

another tensorflow import problem

a fairly simple network crashes the tf_importer:

OpenCV Error: Assertion failed (!beginsData.empty()) in populateNet, file C:\p\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, line 941

(full code here)

def conv_net(x, n_classes, dropout, reuse, is_training):
    # Define a scope for reusing the variables
    with tf.variable_scope('ConvNet', reuse=reuse):

        # Convolution Layer with 32 filters and a kernel size of 5
        conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv1 = tf.layers.max_pooling2d(conv1, 2, 2)

        # Convolution Layer with 32 filters and a kernel size of 5
        conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv2 = tf.layers.max_pooling2d(conv2, 2, 2)

        # Flatten the data to a 1-D vector for the fully connected layer
        fc1 = tf.contrib.layers.flatten(conv2)

        # Fully connected layer (in contrib folder for now)
        fc1 = tf.layers.dense(fc1, 1024)
        # Apply Dropout (if is_training is False, dropout is not applied)
        fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)

        # Output layer, class prediction
        out = tf.layers.dense(fc1, n_classes)
        # Because 'softmax_cross_entropy_with_logits' already apply softmax,
        # we only apply softmax to testing network
        out = tf.nn.softmax(out) if not is_training else out

    return out

then we have:

 python freeze.py --output_node ConvNet/Softmax --model_dir .
 python optimize_for_inference.py --input frozen_model.pb --output face_opt.pb --frozen_graph True --input_names ConvNet_1/conv2d/convolution  --output_names ConvNet_1/Softmax

and some minimal c++ code, to load it into the dnn:

#include <opencv2/dnn.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace cv::dnn;
using namespace std;


int main(int argc, char **argv)
{
    String modelBin = "mnist/face_opt.pb";
    String imageFile = (argc > 1) ? argv[1] : "test.jpg";
    Net net = dnn::readNetFromTensorflow(modelBin);
    Mat img = imread(imageFile, 0);
    resize(img, img, Size(64,64));  
    Mat inputBlob = blobFromImage(img);
    net.setInput(inputBlob, "ConvNet_1/conv2d/convolution"); 

    Mat probMat =  net.forward();
    Point p; minMaxLoc(probMat, 0,0,0, &p);
    cout << p.x << " " << probMat << endl;
    return 0;
}

resulting in a crash:

OpenCV Error: Assertion failed (!beginsData.empty()) in populateNet, file C:\p\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, line 941

debugging it a bit, i see, that tf inserted some layers here:

    at C:\p\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:623
623             LayerParams layerParams;
(gdb) p name.c_str()
$1 = 0x5b21644 "ConvNet/conv2d/bias"
...
$2 = 0x5b21684 "ConvNet/conv2d_1/kernel"
$3 = 0x5b216c4 "ConvNet/conv2d_1/bias"
$4 = 0x5b215c4 "ConvNet/dense/kernel"
$5 = 0x5b21584 "ConvNet/dense/bias"
$6 = 0x5b215c4 "ConvNet/dense_1/kernel"
$7 = 0x5b21644 "ConvNet/dense_1/bias"
$8 = 0x5b21244 "ConvNet_1/conv2d/convolution"
$9 = 0x5b21684 "ConvNet_1/conv2d/BiasAdd"
$10 = 0x5b214c4 "ConvNet_1/conv2d/Relu"
$11 = 0x5b6c7c4 "ConvNet_1/max_pooling2d/MaxPool"
$12 = 0x5b6c304 "ConvNet_1/conv2d_2/convolution"
$13 = 0x5b6cb04 "ConvNet_1/conv2d_2/BiasAdd"
$14 = 0x5b6d2c4 "ConvNet_1/conv2d_2/Relu"
$15 = 0x5b21104 "ConvNet_1/max_pooling2d_2/MaxPool"
$16 = 0x5b6d6c4 "ConvNet_1/Flatten/Shape"
$17 = 0x5b6ec44 "ConvNet_1/Flatten/Slice/begin"
$18 = 0x5b6ec44 "ConvNet_1/Flatten/Slice/size"
$19 = 0x5b6ed84 "ConvNet_1/Flatten/Slice"

looking here: i see, that beginsData is indeed an empty string, while sizesData seems to contain something.