Ask Your Question
0

Dnn.forward(), vector subscript out of range

asked 2018-03-27 11:17:26 -0600

updated 2018-03-27 16:26:13 -0600

I have loaded a model but when doing evaluation using Net.forward() i am getting the error: "Vector subscript out of range" and an "Initialize openCL runtime" warning

I can access the nets layers and print the names, i have also printed out the Mat img dimensions and they match the input size of the model i created.

Any help suggestions to what the error is or how i could solve it would be appreciated. Thank you.

graph.pb

graphtext.txt

Code used to load model and perform forward pass

int main(int argc, char** argv)
{
    String modelFile = "graph.pb";
    String imageFile = "7img.png";

    Net net = readNetFromTensorflow(modelFile);
    Mat img = imread(imageFile, IMREAD_GRAYSCALE);

    // check if model loaded and print layers
    if (net.empty())
    {
        cout << endl << "net empty" << endl;
        exit(0);
        ...
    }
    else {
        cout << endl << "success" << endl;
        vector<String> names = net.getLayerNames();
    }

    cout << endl << img.cols << " img cols" << endl;
    cout << img.rows << " img rows" << endl;
    cout << img.channels() << " img channel(s)" << endl;

    net.setInput(img);
    Mat results = net.forward(); // ERROR HERE

    return 0;
}

Code used to create and save model

import tensorflow as tf
import tensorflow.contrib.keras as K
import numpy as np

# Define the model in Keras
model = K.models.Sequential()

model.add(K.layers.Conv2D(32,kernel_size=(3,3),input_shape=(28,28,1)))
model.add(K.layers.Activation('relu'))

model.add(K.layers.Conv2D(32,kernel_size=(3,3)))
model.add(K.layers.Activation('relu'))

model.add(K.layers.MaxPooling2D(pool_size=(2,2)))

a,b,c,d = model.output_shape
a = b*c*d

model.add(K.layers.Permute([1, 2, 3]))  # Indicate NHWC data layout
model.add(K.layers.Reshape((a,)))

model.add(K.layers.Dense(128))
model.add(K.layers.Activation('relu'))

model.add(K.layers.Dense(10))
model.add(K.layers.Activation('softmax'))

# Get Keras prediction
inp = np.random.standard_normal([1, 28, 28, 1]).astype(np.float32)
tf_out = model.predict(inp)

# Serialize the graph
sess = K.backend.get_session()
constant_graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), ['activation_4/Softmax'])
tf.train.write_graph(constant_graph, "", "graphtext.txt", as_text=True)
edit retag flag offensive close merge delete

Comments

@leyendecker321, Mentioned graph.pb would be the most useful thing to solve the problem.

dkurt gravatar imagedkurt ( 2018-03-27 15:19:12 -0600 )edit
1

edited post to inlucde graph model as .pb and .txt

leyendecker321 gravatar imageleyendecker321 ( 2018-03-27 16:13:03 -0600 )edit

Platform ? compiler ? opencv version?

LBerger gravatar imageLBerger ( 2018-03-28 01:59:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-03-28 00:12:04 -0600

dkurt gravatar image

updated 2018-03-28 00:14:42 -0600

@leyendecker321, OpenCV's output matches TensorFlow's one.

graph = 'graph.pb'
cvNet = cv.dnn.readNetFromTensorflow(graph)

with tf.gfile.FastGFile(graph) as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

with tf.Session() as sess:
    # Restore session
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')

    np.random.seed(324)
    inp = np.random.standard_normal([1, 1, 28, 28]).astype(np.float32)

    out = sess.run(sess.graph.get_tensor_by_name('activation_4/Softmax:0'),
                   feed_dict={'conv2d_1_input:0': inp.transpose(0, 2, 3, 1)})
    cvNet.setInput(inp)
    cvOut = cvNet.forward()

    print np.max(np.abs(cvOut - out))

Output:

2.98023e-08

So you need to find a bug in your application. Try to start from input image. You have to create a 4D blob from an image using blobFromImage. Follow one of tutorials.

edit flag offensive delete link more

Comments

I managed to solve the problem from the information you provided, thanks for your help.

leyendecker321 gravatar imageleyendecker321 ( 2018-04-01 06:57:32 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-27 11:17:26 -0600

Seen: 513 times

Last updated: Mar 28 '18