[AI] Python3 uses TF-Slim for image classification

machine environment

  • win10
  • python3.6
  • tensorflow==1.7.0

Github address

Prepare image data

  • Prepare custom image data
  • Put it in data_prepare/pic/train and data_prepare/pic/validation
  • Create a category folder by yourself, the folder name is the category label name

Convert image data to TF-Record format file

  • Under data_prepare/, execute
python data_convert.py -t pic/ \
  --train-shards 2 \
  --validation-shards 2 \
  --num-threads 2 \
  --dataset-name satellite
  • 4 tf-record files and 1 label file will be generated

Copy the 5 files generated by the conversion to slim\satellite\data

Modify the slim\datasets\satellite.py file

  • FILE_PATTERN = 'satellite %s_*.tfrecord' (tf-record file name format)
  • SPLITS_TO_SIZES = {'train': 16, 'validation': 4} (total number of train and test files)
  • _NUM_CLASSES = 2 (total number of classification categories)
  • 'image/format': tf.FixedLenFeature((), tf.string, default_value='jpg') (image format, here is jpg)

Download the pretrained model Inception V3

Execute the following command in the slim/ folder to train:

python train_image_classifier.py \
  --train_dir=satellite/train_dir \
  --dataset_name=satellite \
  --dataset_split_name=train \
  --dataset_dir=satellite/data \
  --model_name=inception_v3 \
  --checkpoint_path=satellite/pretrained/inception_v3.ckpt \
  --checkpoint_exclude_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \
  --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \
  --max_number_of_steps=100000 \
  --batch_size=32 \
  --learning_rate=0.001 \
  --learning_rate_decay_type=fixed \
  --save_interval_secs=300 \
  --save_summaries_secs=2 \
  --log_every_n_steps=10 \
  --optimizer=rmsprop \
  --weight_decay=0.00004

Execute the following command in the slim/ folder to evaluate the model capability:

python eval_image_classifier.py \
  --checkpoint_path=satellite/train_dir \
  --eval_dir=satellite/eval_dir \
  --dataset_name=satellite \
  --dataset_split_name=validation \
  --dataset_dir=satellite/data \
  --model_name=inception_v3

Export the trained model

  • Execute the following command in the slim/ folder:
python export_inference_graph.py \
  --alsologtostderr \
  --model_name=inception_v3 \
  --output_file=satellite/inception_v3_inf_graph.pb \
  --dataset_name satellite
  • Execute the following command in the project root directory (need to change 5271 to the actual model training steps saved in train_dir)
python freeze_graph.py \
  --input_graph slim/satellite/inception_v3_inf_graph.pb \
  --input_checkpoint slim/satellite/train_dir/model.ckpt-5271 \
  --input_binary true \
  --output_node_names InceptionV3/Predictions/Reshape_1 \
  --output_graph slim/satellite/frozen_graph.pb

Predict a single image

  • Execute the following command in the project root directory
python classify_image_inception_v3.py \
  --model_path slim/satellite/frozen_graph.pb \
  --label_path data_prepare/pic/label.txt \
  --image_file test_image.jpg

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325993710&siteId=291194637