I am new to Tensorflow and have followed this simple flower image classifier guide https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0. So now I have a graph file "retrained_graph.pb" and a text file "retrained_labels.txt", I want to continue to train that model to also distinguish more flowers (and other objects) and train with more images in old flower categories. How can I continue training the model without retrain the entire last layer. Again, I am very new to Python and Tensorflow so if I have said something wrong, please correct me.
1 Answers
First of all, this is kind of an answer for beginners so I will include everything I did, as clear as possible. This solution to image classification is called transfer learning, which takes a trained model and then re-trains only the last layer of it, this not only will take less time than building your own model but also maintain a high accuracy. (Bear in mind that this process will lower your images resolution to 300x300 or lower before training, so if you want to work with high resolution images, I suggest reading this Train High Definition images with Tensorflow and inception V3 pre trained model).
I use Tensorflow image classification retraining example (because the tensorflow for poets script will not save checkpoints for you and checkpoints are important for further training), after completing the tutorial, you will have several files, checkpoint and graph, which are saved in /tmp folder (make sure to copy them to somewhere else since tmp will be deleted when your computer shuts down; to navigate to /tmp folder, type in terminal "open /tmp"): https://www.tensorflow.org/hub/tutorials/image_retraining.
Here are the explanation to checkpoint files: https://stackoverflow.com/a/45033500/10917154.
After some time, I figured out that you can not continue to train a graph with new images: Saving model in tensorflow.
But instead, you have to save your progress with checkpoint (which the script already did it for you) and then load the checkpoint to continue your training, in the script retrain.py change the directory to where you save your checkpoint and do this: https://github.com/tensorflow/hub/issues/37#issuecomment-384237524.
Sadly, I have not found a way to add more class to my trained model and can only train more data with old classes.
In this section of the tutorial, you will also see there is two variables called "input_layer" and "output_layer", to know your model input/output layer, run this: List of tensor names in graph in Tensorflow, the first and last line should be your model's in/out layer.
I took another step to convert graph to tflite to use it on mobile device. To convert it you must freeze your graph (graph->frozen_graph->tflite) How to convert a retrained model to tflite format?, luckily, the script in the tutorial already freezes the graph for us. So now we only have to convert it (you can change .tflite to .lite or vice versa).
$ tflite_convert \
--output_file=/.../output_graph.tflite \
--graph_def_file=/.../output_graph.pb \
--input_arrays=Placeholder \
--output_arrays=final_result \
--output_format=TFLITE \
--inference_type=FLOAT \
--inference_input_type=FLOAT
For some other graphs, it will ask us to provide input shape, we can add 1 more line to the command (224,224 is the resolution of the image that the model is trained with, change it respectively to your model; 3 is for RGB):
--input_shape=1,224,224,3
Once you got the tflite, you can add it to the Tensorflow example and run it: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/ios/camera

- 1
- 5