Skip to content

Commit 44d7607

Browse files
authored
Complete Readme.md ver1
1 parent 4714b95 commit 44d7607

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

README.md

+98-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ TensorFlow is an open source library for numerical computation, specializing in
2424
### Prepare data:
2525
You need one dataset for tranning. If you want to classifier flower, flag, car,... you need 1 dataset of them. <br>
2626
Guide:
27-
* Fatkun downloader chrome extension: go google, type what you need, click tab images (there are many images), then click Fatkun logo, you NEED CLEAN the DATASET, delete all images not involve, download all of extractly you need.
27+
* Fatkun downloader chrome extension: go google, type what you need, click tab images (there are many images), then click Fatkun logo, you NEED CLEAN the DATASET, delete all images not involve, download all of extractly you need. Copy all of images into 1 folder that's name is comom name of label of them.
2828

2929
### Prepare environment:
3030
To work with tensorflow and android, you need:
@@ -45,8 +45,103 @@ Install and run docker (all in one) every you need at here:
4545

4646
When you downloaded, run this command to install docker container:
4747

48-
<b>docker run -it -p 8888:8888 -v $HOME/tf_files:/tf_files tensorflow/tensorflow:nightly-devel</b>
48+
> docker run -it -p 8888:8888 -v $HOME/tf_files:/tf_files tensorflow/tensorflow:nightly-devel</b>
4949
50-
It share folder tf_files at your main computer with docker container
50+
It share folder tf_files at your main computer with docker container. Update tensor:
51+
52+
> pip install --upgrade "tensorflow==1.7.*"
53+
54+
Your structure folder:
55+
56+
tf_files
57+
58+
---|-----script
59+
60+
---|-----photos
61+
62+
---|-----tf_files
63+
64+
tf_files at root of apps, script contains files .py that train and optimize the model, file photos contains folders of images input, tf_files contains result of next steps (files model, label, bottleneck,...)
65+
66+
## Train model
67+
You need download script at:
68+
* retrain.py: https://github.com/googlecodelabs/tensorflow-for-poets-2/blob/master/scripts/retrain.py
69+
* optimize_for_inference.py: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/optimize_for_inference.py
70+
* label_images.py: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/label_image/label_image.py
71+
* quantize_graph.py: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/quantize/python/quantize_graph.py
72+
73+
Puts them into script folder in tf_files folder.
74+
75+
Investigate the retraining script
76+
The retrain script is from the TensorFlow Hub repo, but it is not installed as part of the pip package. So for simplicity I've included it in the codelab repository. You can run the script using the python command. Take a minute to skim its "help".
77+
78+
> python -m scripts.retrain -h
79+
80+
Start your retraining with one big command (note the --summaries_dir option, sending training progress reports to the directory that tensorboard is monitoring) :
81+
82+
> python -m scripts.retrain \
83+
> --bottleneck_dir=tf_files/bottlenecks \
84+
> --how_many_training_steps=500 \
85+
> --model_dir=tf_files/models/ \
86+
> --summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}" \
87+
> --output_graph=tf_files/retrained_graph.pb \
88+
> --output_labels=tf_files/retrained_labels.txt \
89+
> --architecture="${ARCHITECTURE}" \
90+
> --image_dir=tf_files/name_photos_folder
91+
92+
Note that: --how_many_training_steps=500, you can increase this number to get higher accuracy, ${ARCHITECTURE} you change it into inception_v3 (i use inceptionv3) or mobilenet, softmax,... --image_dir=tf_files/flower_photo, you must change this folder name by the folder name of photos you download at before step.
93+
94+
retrained_graph.pb & retrained_labels.txt is the most important file of this program.
95+
96+
Next, verify that the model is producing sane results before starting to modifying it.
97+
98+
The scripts/ directory contains a simple command line script, label_image.py, to test the network. Now we'll test:
99+
100+
>python -m scripts.label_image \
101+
> --graph=tf_files/retrained_graph.pb \
102+
> --image=tf_files/photos/folder_name/photo_name.jpg
103+
104+
Optimize for inference: To avoid problems caused by unsupported training ops, the TensorFlow installation includes a tool, optimize_for_inference, that removes all nodes that aren't needed for a given set of input and outputs.
105+
106+
The script also does a few other optimizations that help speed up the model, such as merging explicit batch normalization operations into the convolutional weights to reduce the number of calculations. This can give a 30% speed up, depending on the input model. Here's how you run the script:
107+
108+
> python -m tensorflow.python.tools.optimize_for_inference \
109+
> --input=tf_files/retrained_graph.pb \
110+
> --output=tf_files/optimized_graph.pb \
111+
> --input_names="input" \
112+
> --output_names="final_result"
113+
114+
Quantize the network weights: Applying an almost identical process to your neural network weights has a similar effect. It gives a lot more repetition for the compression algorithm to take advantage of, while reducing the precision by a small amount (typically less than a 1% drop in precision).
115+
116+
It does this without any changes to the structure of the network, it simply quantizes the constants in place.
117+
118+
Now use the quantize_graph script to apply these changes:
119+
120+
> python -m scripts.quantize_graph \
121+
> --input=tf_files/optimized_graph.pb \
122+
> --output=tf_files/rounded_graph.pb \
123+
> --output_node_names=final_result \
124+
> --mode=weights_rounded
125+
126+
## Add your model files to the project
127+
The demo project is configured to search for a graph.pb, and a labels.txt files in the android/tfmobile/assets directory. Replace those two files with your versions. The following command accomplishes this task:
128+
129+
> cp tf_files/rounded_graph.pb android/tfmobile/assets/graph.pb
130+
> cp tf_files/retrained_labels.txt android/tfmobile/assets/labels.txt
131+
132+
Structure demo i share in this repo, you can download and test it.
51133

52134
## Demo
135+
136+
## Full source code
137+
All of step you need at here: https://bit.ly/2ILVkfv
138+
139+
## Reference:
140+
* Tensorflow-for-poets-2: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0
141+
* Tensorflow mobile: https://www.tensorflow.org/mobile/ & https://www.tensorflow.org/tutorials/image_recognition
142+
* Pete Warden's blog: https://petewarden.com/2016/09/27/tensorflow-for-mobile-poets/
143+
144+
## Thanks for your reading. It help? Vote star? :star:
145+
See another repo and get issues if you get any question.
146+
147+
Made by Damminhtien from HUST :heart:

0 commit comments

Comments
 (0)