Ask Your Question
0

readNetFromTensorflow when loading customized model [closed]

asked 2018-03-28 02:31:59 -0600

Hank538 gravatar image

updated 2018-03-28 02:56:08 -0600

Hi

OpenCV 3.4.1 Python 3.6 Tensorflow 1.5.0

I just construct a simple model for mnist.

x = tf.placeholder(tf.float32, [None, 784], name='input')
W = tf.Variable(tf.zeros([784,10]), dtype=tf.float32, name='W')
b = tf.Variable(tf.zeros([10]), dtype=tf.float32, name='b')
y = tf.nn.softmax(tf.matmul(x,W) + b, name='softmax')

And, save graph.pbtxt mode and trained weight ckpt data.

Then, freeze graph and weight to frozen.pb file.

Next, optimize frozen.pb file to frozen_inference_graph.pb

Model txt and binary files in below link. https://drive.google.com/file/d/1v6fo... https://drive.google.com/file/d/1bA7-... https://drive.google.com/file/d/1HC4m...

readNetFromTensorflow always fail no matter how I try. cv::Exception at memory location

Any advise or guidance would be greatly appreciated!

Thanks Hank

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Hank538
close date 2018-04-01 20:47:44.458457

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-03-28 03:32:40 -0600

dkurt gravatar image

updated 2018-03-28 03:34:30 -0600

@Hank538, The following code works fine for me (a master branch).

import cv2 as cv
import numpy as np

np.random.seed(2701)

net = cv.dnn.readNet('frozen.pb')
inp = np.random.standard_normal([1, 784]).astype(np.float32)

net.setInput(inp)
out = net.forward()
print out

Output:

[[  1.33815183e-06   5.64191076e-08   3.70280895e-06   7.99171573e-08
    6.58905017e-04   2.29165620e-09   8.71605792e-08   8.88653449e-04
    1.90605744e-04   9.98256624e-01]]
edit flag offensive delete link more

Comments

Hi Dmitry

Thanks for your feedback !

I followed OpenCV spec doc about argument, then test result now below.

readNetFromTensorflow(frozen.pb, graph.pbtxt) >>> exception readNetFromTensorflow(frozen.pb) >>> work readNetFromTensorflow(frozen_inference_graph.pb) >>> work

Does it just use one argument ?

Moreover, I want to know when will use frozen.pb or frozen_inference_graph.pb. Which scenario or models will use optimize_for_inference or transform_graph tool ?

Looking forward to your reply

Thanks Hank

Hank538 gravatar imageHank538 ( 2018-03-28 04:27:54 -0600 )edit

@Hank538, the second argument is optional and usually we manually modify it but not use an origin one. We're continuously working on improving TensorFlow importer to emit extra graph modifications. So these tools are used only if a frozen .pb file does not work. In your case you need just freeze the graph.

dkurt gravatar imagedkurt ( 2018-03-28 05:22:12 -0600 )edit

Hi Dmitry

Thanks for your hard work!

The DNN model work fine in my OpenCV code.

Now I trained a new CNN LeNet model and load successfully, but predicted almost wrong in my same OpenCV code.

Here is CNN Tensorflow structure

CNN LeNet.txt

https://drive.google.com/file/d/1s2LQ...

graph.pbtxt

https://drive.google.com/file/d/1dn3-...

frozen.pb

https://drive.google.com/file/d/1a_Yd...

frozen_inference_graph.pb

https://drive.google.com/file/d/14Rnz...

Please help!

Hank538 gravatar imageHank538 ( 2018-03-29 03:13:49 -0600 )edit

@Hank538, Try to use x = tf.placeholder(tf.float32, [None, 28, 28, 1], name='input') instead x = tf.placeholder(tf.float32, [None, 784], name='input') and x_image = tf.reshape(x, [-1,28,28,1]).

dkurt gravatar imagedkurt ( 2018-03-29 03:59:32 -0600 )edit

Hi

Followed your instruction,

ValueError: Cannot feed value of shape (100, 784) for Tensor 'input_30:0', which has shape '(?, 28, 28, 1)'

Sorry, I am new to Tensorflow and no idea how to modify and fit the fix.

Upload code and you could just copy and paste for duplication.

https://drive.google.com/file/d/1qif8...

Did this issue that could't parse reshpae method by DNN ?

Or should I modify OpenCV code for consistence with input format ?

Hank538 gravatar imageHank538 ( 2018-03-29 04:37:32 -0600 )edit

@Hank538, Please check the links above. frozen.pb is still the same.

dkurt gravatar imagedkurt ( 2018-03-29 05:37:34 -0600 )edit

Hello

? My new link upload new CNN pb file and Python source code.

I figure out there is wrong input data format when training.

But from your suggestion, DNN seems not support reshape operation in graph, right ?

Then, I had no idea how to modify the whole code from 1x784 input format to 28x28 input format including training step.

Besides, there is fully-connected layer using reshape method, too.

Hank538 gravatar imageHank538 ( 2018-03-30 04:10:01 -0600 )edit

@Hank538, which one of the links? Let's keep things in order. Problem from the topic has been solved. Please open a new question including all the files to reproduce. Thank you!

dkurt gravatar imagedkurt ( 2018-03-31 06:20:49 -0600 )edit

Ok. Will create another thread.

Hank538 gravatar imageHank538 ( 2018-04-01 20:47:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-28 02:31:59 -0600

Seen: 1,446 times

Last updated: Mar 28 '18