mortenjust / trainer-mac

Trains a model, then generates a complete Xcode project that uses it - no code necessary
124 stars 9 forks source link

Script for retrained_graph_stripped.pb #3

Open BFMarks opened 7 years ago

BFMarks commented 7 years ago

I've been having trouble retraining the model. It mostly tries to use the same folder types from a previous training group. I have Tensorflow for Poets working correctly for in my docker container. Do you happen to have the script to turn that model into the stripped model for iOS? Just curious as it seems the model trainer is broken.

mortenjust commented 7 years ago

Is this the right way to think about it?

You've trained a model for app 1. Now you want to train a model for app 2. But it keeps using the labels (folder names) from app 1.

It sounds like you have some old bottleneck files hanging around. Try and delete the bottlenecks folder, and the *.txt log files.

Right now this is how it does the stripping. You will need to compile the stripper first, though. Check out this thread.

python /tensorflow/tensorflow/examples/image_retraining/retrain.py \
--bottleneck_dir=$TFBASE/bottlenecks \
--model_dir=$TFBASE/inception \
--output_graph=$TFBASE/model/retrained_graph.pb \
--output_labels=$TFBASE/model/retrained_labels.txt \
--image_dir $TFBASE/images/resized

/tensorflow/bazel-bin/tensorflow/python/tools/strip_unused \
--input_graph=$TFBASE/model/retrained_graph.pb \
--output_graph=$TFBASE/model/retrained_graph_stripped.pb \
--input_node_names=Mul \
--output_node_names=final_result \
--input_binary=true
scm-ns commented 7 years ago

After stripping as the model as per @mortenjust .There i also a memory consumption error that will happen due to the tf model taking up a lot of space on iOS. You will have to convert the model using the following :

bazel build tensorflow/contrib/util:convert_graphdef_memmapped_format && \
bazel-bin/tensorflow/contrib/util/convert_graphdef_memmapped_format \
--in_graph=tensorflow/contrib/ios_examples/camera/data/tensorflow_inception_graph.pb \
--out_graph=tensorflow/contrib/ios_examples/camera/inception_mmap.pb

There is a flag called model_uses_memory_mapping, which has to be set to true for this to work. (in the ios code) Other things to keep in mind when using a model obtained from transfer learning on the inception model (Using the image retraining example on tf) , is: 1) to set change the input layer to Mul , and output layer to final_result 2) Change the model height = width = 299 and input_mean = input_std = 128

More details here https://github.com/tensorflow/tensorflow/issues/2883 and here https://github.com/tensorflow/tensorflow/issues/4255.

Thanks a lot to @petewarden for all these solutions.