Ask Your Question

dnn::readNetFromTensorflow() fail on loading pre-trained network on age-gender detection

asked 2018-03-04 21:51:43 -0600

Paul Kuo gravatar image

Dear opencv dnn developers,

[Environment] TensorFlow 1.5 python 3.5 Win7 opencv3.4

I am very new to the Tensor flow. Recently I found a pretrained dnn for age gender detection The model is defined in "" and the latest model checkpoints can be found!kaZkWDjb!xQvWi9B--FgyIPtIYfjzLDoJeh2PUBEZPotmzO9N6_M

I loaded model with the latest check point into Tensor Flow and running detection ok, and managed to freeze the graph using but when I load this frozen graph by cv::dnn::readNetFromTensorflow("XXX.pb"), it gets error:..."Unknown layer type shape in op map/Shape)".... or I tried cv::dnn::readNetFromTensorflow("XXX.pb", "XXX.pbtxt"), it also gets error:...unknown enumeration values of "DT_RESOURCE" for field "type..."

I have searched over the internet trying to find a solution... someone suggested using "optimize_for_inference" or "graph_transform" might help. As I have limited Tensor Flow knowledge, I do not understand how these two processes can solve the problem. and Also, cv::dnn::readNetFromTensorflow() method has the 2nd argument, which I also do not know in what situation I should/should not provide a .pbtxt for it?

Please help. If there is any further information needed to clarify the question, just let me know and I will supply.

Thanks in advance

edit retag flag offensive close merge delete


@Paul Kuo, please attach a reference to a frozen graph.

dkurt gravatar imagedkurt ( 2018-03-05 02:24:47 -0600 )edit

Hi, dkurt, Thank you for your reply. The detail of getting the frozen graph is described below... .....

after loading the model, we perform some age-gendger detection and then save it again and is going to convet to a frozen graph, "./pbs/model_XXX.ckpt") #save to a check point

tf.train.write_graph(sess.graph_def, ".\pbs", "graphDef.pb", as_text=False) # saved in binary form

tf.train.write_graph(sess.graph_def, ".\pbs", "graphDef.pbtxt", as_text=True) # saved in text form .....

And then run (provided from tensorflow/python/tools) with the script below...

python --input_graph=./pbs/graphDef.pb --input_checkpoint=./pbs/model_XXX.ckpt --output_graph =./pbs/frozenGraph.pb --output_node_names=genderOut,ageOut --input_binary=true

Paul Kuo gravatar imagePaul Kuo ( 2018-03-05 23:28:40 -0600 )edit

then a frozenGraph.pb is generated.

Here you can download my generated "graphDef.pb" "graphDef.pbtxt" and "frozenGraph.pb" from and see if anyone can figure out any error from it...


Paul Kuo gravatar imagePaul Kuo ( 2018-03-05 23:30:07 -0600 )edit

Hi @dkurt, is there any luck to fix this problem...?

Everyone, any suggestions will be appreciated...

Thank you

Paul Kuo gravatar imagePaul Kuo ( 2018-03-12 20:37:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-03-13 03:33:36 -0600

dkurt gravatar image

@Paul Kuo, the problem is that graph has been saved in training mode. I think there is some placeholder similar to isTraining that should be turned into False before graph saving (note that it's about graph but not about checkpoint).

image description

Moreover you may see some unusual transformations over input image.

image description

May I ask you to try to find that isTraining flag, set it to false and save the graph again by tf.train.write_graph. Then freeze the same checkpoint files with new version of graph. Our first goal is emit train/test switches from the graph. Thank you!

edit flag offensive delete link more


Hi @dkurt,

Thank you for your suggestion. I was switched to another project now, but will come back to this issue and try your suggestion probably next week. Will let you know what is going on here. Thank you~~


Paul Kuo gravatar imagePaul Kuo ( 2018-03-15 20:22:51 -0600 )edit

Hi @dkurt,

I have checked the cod and, to the best of my knowledge, there should not be any "isTraining==True" existed. Here are the code for generating graphDef.pb (before freezing it) and definition of inception_resent_v1.

Could you please look through it and help me to find out anything might result in this conversion error.

Thank you again~


Paul Kuo gravatar imagePaul Kuo ( 2018-03-18 22:50:09 -0600 )edit

@Paul Kuo, There are phase_train, is_training. They should be turned to False before the tf.train.write_graph.

dkurt gravatar imagedkurt ( 2018-03-19 03:13:24 -0600 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-03-04 21:51:43 -0600

Seen: 82 times

Last updated: Mar 13