models icon indicating copy to clipboard operation
models copied to clipboard

MobileNet TF-Slim freezing graph / input and output nodes

Open 2ori opened this issue 8 years ago • 11 comments

System Information:

Tensorflow Version: 'v1.4.0-rc1-11-g130a514', '1.4.0' Also tested this with all versions from 1.0.0 to 1.4.0 OS: Ubuntu 16.04 CUDA: CUDA 8.0

Problem

I retrained the in TF-Slim implemented MobileNet on a custom dataset. I'm using this implementation of the Mobile Net in TF-Slim : https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet_v1.py

The retraining worked fine, but now I want to freeze and optimize the graph, so that I can deploy the model to an app. The problem now is, that I really can't figure out which are the input and output node names. I already inspected the graph with the summarize graph script, but I just found out that the node MobilenetV1/Predictions/Reshape_1 could be a possible output node.

In this, in the TF-Slim Model Repository, provided script for exporting the MobileNet, there is input and MobilenetV1/Predictions/Reshape_1 provided as input and output node. But if I'm using input as input node in for example label_image.py there I'm getting this error: "The name 'import/input' refers to an Operation not in the graph."

What worked for running python label_image.py --graph="./train_dir/frozen_model.pb" --input_layer="fifo_queue_Dequeue" --output_layer="MobilenetV1/Predictions/Reshape_1" --input_width=224 --input_height=224 --labels="../dataset/labels.txt" --image="two.jpg" is using fifo_queue_Dequeue as input node, but then I'm getting this error:

[[17 1 20 0 6 2 4 12 24 9 3 10 25 22 11 15 14 19 16 8 7 5 23 13 18 21] [ 1 17 20 0 6 2 4 12 24 9 3 25 10 22 15 11 19 14 16 8 7 5 23 13 18 21] [17 1 20 0 6 2 4 12 24 9 3 25 10 22 15 11 19 14 16 8 7 5 23 13 18 21] [17 1 20 0 6 2 4 12 9 24 3 25 10 22 15 11 14 19 16 8 7 5 23 13 18 21] [ 1 17 20 0 6 2 4 12 24 9 3 25 10 22 15 11 16 14 19 8 7 5 23 13 18 21]] ['0:Aloe Vera', '1:Dracanea marginata', '2:Haworthia limifolia', '3:Persea gratissima', '4:Sansevieria trifasciata', '5:Tradescantia fluminensis Tricolor', '6:Tradescantia pallida', '7:calathea lancifolia', '8:calathea orbifolia', '9:chlorophytum comosum vittatum', '10:crassula ovata', '11:dracaea reflexa', '12:echeveria elegans', '13:euphorbia triangularis', '14:euphorbia trigona rubra', '15:fatsia japonica', '16:ficus elastica', '17:ficus lyrata', '18:maranta leuconeura erythroneura', '19:monstera deliciosa', '20:oxalis triangularis', '21:phlebodium pseudoaureum', '22:pilea peperomioides', '23:senecio kleiniiformis', '24:senecio rowleyanus', '25:tradescantia zebrina'] Traceback (most recent call last): File "label_image.py", line 134, in print(labels[i], results[i]) TypeError: only integer scalar arrays can be converted to a scalar index

Here I'm printing my labels and the top 5 results, which have to be scalars instead of vectors. I think the problem is here the given output node. For labeling I'm using this python script.

2ori avatar Feb 22 '18 12:02 2ori

Could you elaborate on how you created the unfrozen graph to begin with? Were you using the export script, or did you use something else to create the graph that was later frozen? If not using the script, the name of the input node would depend on the details of how you created the graph.

asimshankar avatar Feb 23 '18 09:02 asimshankar

I used this script to freeze the graph, which uses the checkpoint state files. In the export script there is an unfrozen graph as pb file needed, which I did not have trough the training. For training I used the train_image_classifier.py which is provided in TF-Slim and it does not write me a pb file after training.

2ori avatar Feb 23 '18 11:02 2ori

I also tried the export script from the scripts directory independent of my graph to test if it works. I got this error

ERROR: Skipping 'slim:export_inference_graph': no such package 'scripts/slim': BUILD file not found on package path WARNING: Target pattern parsing failed. ERROR: no such package 'scripts/slim': BUILD file not found on package path INFO: Elapsed time: 1,464s FAILED: Build did NOT complete successfully (0 packages loaded) ERROR: Build failed. Not running target

Edit: I made it work

2ori avatar Feb 23 '18 12:02 2ori

@zorancupic How did you make it work? Thanks!

rbothra3 avatar Feb 27 '18 03:02 rbothra3

@zorancupic how you make it work? I might run into the same problem. I write the graph to plain text and open it, finding that it's quite difficult to find my output node names. for example, my out put node: conv4_1 = slim.conv2d(net,num_outputs=2,kernel_size=[1,1],stride=1,scope='conv4_1',activation_fn=tf.nn.softmax)

in my graph.pb file: I find that conv4_1 appears several times in it like: node { name: "conv4_1/weights/Initializer/random_uniform/shape" ... node { name: "conv4_1/kernel/Regularizer/l2_regularizer" ... node { name: "conv4_1/Conv2D" op: "Conv2D" input: "conv3/add" input: "conv4_1/weights/read" ... node { name: "conv4_1/Softmax" op: "Softmax" input: "conv4_1/Reshape" ...

so is the last one really my output nodes name? Thanks

flankechen avatar Mar 17 '18 07:03 flankechen

@zorancupic hi, how you find your input node and output node name?

styap94 avatar Apr 05 '18 21:04 styap94

I have same problem. It's quite difficult to guess which is output/input node name.

lechatthecat avatar May 01 '18 06:05 lechatthecat

https://developer.arm.com/technologies/machine-learning-on-arm/developer-material/how-to-guides/optimizing-neural-networks-for-mobile-and-embedded-devices-with-tensorflow/determine-the-names-of-input-and-output-nodes

u may use this to find out the node name. @lechatthecat

styap94 avatar May 02 '18 12:05 styap94

ok, thank you.

lechatthecat avatar May 02 '18 12:05 lechatthecat

I used the ARM page as suggested by hanako94 and found the names "num_detections,detection_classes,detection_boxes,detection_scores" as my outputs. However when I load the same into the freeze_graph.py script it says that these names were not in the graph.

aejaex avatar Nov 10 '18 16:11 aejaex

Hi There, We are checking to see if you still need help on this, as this seems to be considerably old issue. Please update this issue with the latest information, code snippet to reproduce your issue and error you are seeing. If we don't hear from you in the next 7 days, this issue will be closed automatically. If you don't need help on this issue any more, please consider closing this.

tensorflowbutler avatar Jan 29 '20 23:01 tensorflowbutler