TensorFlow Hub profile

TensorFlow Hub is a library designed to facilitate the re-exploration and re-publication of reusable parts of machine learning models, primarily reusing pre-trained TensorFlow model fragments for new tasks. (Can be understood as doing transfer learning)

To use the TensorFlow Hub, you need to have TensorFlow installed at least 1.7 times.

To install the TensorFlow Hub, run the following command

pip install tensorflow-hub

The downloaded version information of TensorFlow Hub is shown in the following figure


TensorFlow Hub using

For convenience, we use the flower photo set officially provided by TensorFlow Hub as our data set. If network conditions permit, readers can use the following command to download the photo set

cd ~

curl -LO http://download.tensorflow.org/example_images/flower_photos.tgz

tar xzf flower_photos.tgz

Download not down also all right, I am close to the baidu cloud link link: https://pan.baidu.com/s/1NVl8uUU7iVktxE0g7Oa5jw password: ivt8

Download it and unzip it.

When unpacked, the flower_photos folder contains the following subfolders, each with a name that represents the label of the image in it. For example, all images in the Dandelion folder are dandelion images.

With the photo set, we also need training code for transfer learning. Similarly, you can download the training code for migration learning by running the following command

mkdir ~/example_code

cd ~/example_code

curl -LO https://github.com/tensorflow/hub/raw/master/examples/image_retraining/retrain.py

You can also download it from TensorFlow Hub’s GitHub repository.

What we need to use is the retrain.py file under the hub-master\examples\image_retraining path. This py file is officially prepared for us to learn the template code for image classification migration.

Here’s the big part

How to implement complex transfer learning with one line of code

Since my TensorFlow-GPU library is installed in a separate environment, I need to enable Anaconda Prompt and then activate the specific environment I want to use

activate my_special_env

(If python is in the system Path, skip the previous step.)

Then put the path CD into the path of your local retrain.py file (the specific path can be changed according to your actual situation, the following is my path address).

Finally, the most important line of code comes:

python retrain.py --image_dir ~/flower_photos

(~/flower_photos indicates the path of the downloaded and decompressed photo sets. Please modify it according to the actual situation.)

Then, the transfer learning training is running ~!


TensorFlow Hub tips

1. Why do we keep stuck in the downloading position?

Because TensorFlow Hub is a model published online in the form of a URL, if you are lucky enough to run the entire training, You can see a folder and a TXT text in C:\Users\ your username \AppData\Local\Temp\tfhub_modules where the TXT text is as follows:

Module: https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1

The Download Time: 2018-07-02 18:21:36. 051380

Downloader Hostname: WSJ-LAPTOP (PID:18172)

TensorFlow Hub uses the inception_v3 model downloaded from the Internet as a pre-training model for our migration learning (as you can see by reading the retrain.py source code), so if the network is not working well or the wall is not working well, That naturally is not downloaded down……

2. Where is the model file saved after the training?

By default, it is saved in TMP /, because the drive letter of my retrain.py file is E, so all bottleneck files, CKPT files, PB files, label files generated by training are in E:\ TMP.

3. What is the tensor of input and output in the trained model?

According to the official documentation, the input tensor is “Placeholder”, the output tensor is “final_result”. The reader can test the trained model using the official image classification prediction code. This article does not describe the specific steps of the test, the reader can refer to

4. Besides the default model, what other pre-training models can we use?

The default value of tfhub_moduled in retrain.py is the URL of the inception_v3 model. To replace the model, refer to the official documentation, which lists all available official pre-training models published.


If you want to know more information, such as super parameter setting configuration details, please consult the official making warehouse TensorFlow Hub of the latest Chinese website: https://tensorflow.google.cn/hub/



If you find this article useful to you, please point a careful heart for me to show support. Thank you!