1

I am using the following simple convolutional network:

def forward_pass(train_batch):

    with tf.name_scope('input'):
        inputData = tf.identity(train_batch)

    #network definition
    #image input 32 x 32 x 3
    #weights for the first convolution layer

    with tf.name_scope('conv1'):
        W_conv1 = weight_variable([5, 5, 3, 6])
        b_conv1 = bias_variable([6])
        #structure of the first convolution layer: convolution, activation, pooling
        h_conv1 = tf.nn.tanh(tf.nn.bias_add(conv2d(inputData, W_conv1), b_conv1))

    with tf.name_scope('pool1'):
        h_pool1 = max_pool_2x2(h_conv1)

    #image input 14 x 14 x 6
    #weights for the second convolution layer 

    with tf.name_scope('conv2'):
        W_conv2 = weight_variable([5, 5, 6, 16])
        b_conv2 = bias_variable([16])
        #structure of the second convolution layer: convolution, activation, pooling
        h_conv2 = tf.nn.tanh(tf.nn.bias_add(conv2d(h_pool1, W_conv2), b_conv2))

    with tf.name_scope('pool2'):
        h_pool2 = max_pool_2x2(h_conv2)

    #image input 10 x 10 x 16

    with tf.name_scope('reshape'):
        h_pool2_flat = tf.reshape(h_pool2, [-1, 5 * 5 * 16])


    #fully connected layer
    with tf.name_scope('fc'):
        W_fc1 = weight_variable([5 * 5 * 16, 46])
        b_fc1 = bias_variable([46])
        y_re = tf.matmul(h_pool2_flat, W_fc1)
        fc_output = tf.nn.bias_add(y_re, b_fc1)

    with tf.name_scope('output'):
        output = tf.multiply(fc_output, 1.)


    return output

To train the network I use the Adam optimizer as follows:

output = forward_pass(train_batch)

with tf.name_scope('one-hot'):
    label_batch_vector = tf.one_hot(label_batch, 46)

with tf.name_scope('loss'):
    cross_entropy = tf.reduce_mean(
     tf.nn.softmax_cross_entropy_with_logits(labels=label_batch_vector, logits=output))

with tf.name_scope('adam-optimizer'):
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

To save the network I use at the end:

tf.train.write_graph(sess.graph.as_graph_def(), "", 'graph.pb')

Training with TensorFlow yields good results but now I would like to use the network from OpenCV.

I transform the network as follows:

python3 $TF_TOOLS/freeze_graph.py \
  --input_graph=graph.pb \
  --input_checkpoint=Model.ckpt \
  --output_graph=frozen_graph.pb \
  --output_node_names=output/Mul

python3 $TF_TOOLS/optimize_for_inference.py \
  --input frozen_graph.pb \
  --output opt_graph.pb \
   --frozen_graph True \
   --input_names input/Identity \
  --output_names output/Mul

I am using then opt_graph.pb to load the model with cv::dnn::readNetFromTensorflow. Then I am testing on my training data and I get a 2% recognition rate. Can anyone help ? Is it possible that I did not save the weights properly in opt_graph.pb ? To me it seems like the training data has been lost.

1

1 Answer 1

0

The solution is to add a tf.transpose(h_pool2, [0, 3, 1, 2]) operation before the tf.reshape operation. See additional info in https://github.com/opencv/opencv/issues/10065.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.