1.Install Tensorflow,
if installed,upgrade to most recent stable branch with pip install --upgrade tensorflow
the model url is https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/image_retraining
2.mkdir tensorlfowdemo cd tensorlfowdemo
git clone https://github.com/googlecodelabs/tensorflow-for-poets-2
3.Download the training images
curl http://download.tensorflow.org/example_images/flower_photos.tgz | tar xz -C tf_files
4.(Re) training the network
Configure your MobileNet
The retain script can retain either Inception V3 model or a MobileNet. 设置环境变量
IMAGE_SIZE=224 ARCHITECTURE="mobilenet_1.0_${IMAGE_SIZE}"(选择模型是
inception_v3还是mobilenet_v1_1.0)
Start TensorBoard
launch tensorboard in the background
tensorboard --logdir tf_files/trainning_summaries &
this command will fail with the following error if you already have a tensorboard process running
ERROR:tensorflow:tensorBoard attempted to bind to port 6006,but it was already in use,you can kill all existing TensorBoard instances with:
pkill -f "tensorboard"
if we want to see graph in tensorboard,we should mkdir tf_files/trainning_summaries and
python -m scripts.graph_pb2tb tf_files/training_summaries/retrained tf_files/retrained_graph.pb
see python help in command:python -m scripts.retrain -h
Run the training
python -m scripts.retrain --bottleneck_dir=tf_files/bottlenecks --how_many_training_steps=500 --model_dir=tf_files/models/ --summaries_dirs=tf_files/training_summaries/"${ARCHITECTURE}" --output_graph=tf_files/retrained_graph.pb --output_labels=tf_files/retrained_labels.txt --architecture="$ARCHITECTURE" --image_dir=tf_files/flower_photos/
if you are using Docker and the above command fails reporting ERRO[xxx] error getting events from daemon:EOF increase your Docker cpu allocation to 4 or more
bottlenecks is an informal term we often use for the layer just before the final output layer that actually does the classification.
5.Using the Retained Model
in android we use tf_files/retrained_graph.pb and tf_files/retrained_labels.txt in assets
The codelab repo also contrains a copy of tensorflow's label_image.py example, python -m scripts.label_image -h
(检验模型是否预测准确,执行如下命令)
python -m scripts.label_image --graph=tf_files/retrained_graph.pb --image=tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg
if you want to optimized the model .pb ,then do it
python -m tensorflow.python.tools.optimize_for_inference --input=tf_files/retrained_graph.pb --output=tf_files/optimized_graph.pb --input_names="input" --output_names="final_result"
******************************************************************************************************************************************************************************************************************
6.Training on Your Own Categories
In theory,all you need to do is run the tool,specifying a particular set of sub-folders.Each sub-folder is named after one of your categories and contains only images from that category.
PS
in source code ,we can train in following command
bazel build tensorflow/examples/image_retraining:retrain
If you have a machine which suports the AVX instruction set,you can improve the running speed of the retraining by building for that architecture. bazel build --config opt tensorflow/examples/image_retraining:retrain
The retrainer can then be run like this
bazel -bin/tensorflow/examples/image_retraining:retrain --image_dir ~/flower_photos
参考 https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/index.html#0