基于TF-Slim使用MobilenetV2进行图像分类

Environment

  • ubuntu16.04
  • python2.7
  • tensorflow

Github

Preparing the datasets

  • Prepare your own data and divide it into train(90%) and validation(10%) sets
  • Put in "data_prepare/pic/train" and "data_prepare/pic/validation" 
pic
--train
  --class 0
  --class 1
  ...
--validation
  --class 0
  --class 1
  ...

Converting to TFRecord format

  • Execute the "convert.sh" file 
python data_convert.py -t pic/ \
  --train-shards 2 \
  --validation-shards 2 \
  --num-threads 2 \
  --dataset-name satellite
  • Will get 4 tf-record files and 1 label file
  • Move these 5 files to "slim\satellite\data"

Modify the "slim\datasets\satellite.py" file

  • FILE_PATTERN = ‘satellite%s_*.tfrecord’ ( not recommend to modify)
  • SPLITS_TO_SIZES = {‘train’: 16, ‘validation’: 4} (Total number of train and validation set )(The sum of all categories)
  • _NUM_CLASSES = 2 (Number of categories)
  • ‘image/format’: tf.FixedLenFeature((), tf.string, default_value=’jpg’) (Image format, here is jpg)

Download the pre-training model Mobilenet V2

Execute "model_train.sh" in the "slim/" folder to train:

python train_image_classifier.py \
  --train_dir=satellite/train_log \
  --dataset_name=satellite \
  --train_image_size=500 \
  --dataset_split_name=train \
  --dataset_dir=satellite/data \
  --model_name="mobilenet_v2_140" \
  --checkpoint_path=satellite/pretrained/mobilenet_v2_1.4_224.ckpt \
  --checkpoint_exclude_scopes=MobilenetV2/Logits,MobilenetV2/AuxLogits \
  --trainable_scopes=MobilenetV2/Logits,MobilenetV2/AuxLogits \
  --max_number_of_steps=1000 \
  --batch_size=16 \
  --learning_rate=0.001 \
  --learning_rate_decay_type=fixed \
  --log_every_n_steps=10 \
  --optimizer=rmsprop \
  --weight_decay=0.00004 \
  --label_smoothing=0.1 \
  --num_clones=1 \
  --num_epochs_per_decay=2.5 \
  --moving_average_decay=0.9999 \
  --learning_rate_decay_factor=0.98 \
  --preprocessing_name="inception_v2" 

Execute "model_test.sh" in the "slim/" folder to evaluate the model:

python eval_image_classifier.py \
  --checkpoint_path=satellite/train_log \
  --eval_dir=satellite/eval_log \
  --dataset_name=satellite \
  --dataset_split_name=validation \
  --dataset_dir=satellite/data \
  --model_name="mobilenet_v2_140" \
  --batch_size=32 \
  --num_preprocessing_threads=2 \
  --eval_image_size=500
  • Or execute "start_train_test.sh" at the project root directory to train and evaluate the model.

Export trained models

  • Execute "export_inference_graph.py" in the "slim/" folder to exporting the Inference Graph
python export_inference_graph.py \
  --alsologtostderr \
  --model_name="mobilenet_v2_140" \
  --image_size=500 \
  --output_file=slim/satellite/export/mobilenet_v2_140_inf_graph.pb \
  --dataset_name satellite
  • Execute "freeze_graph.py" at the project root directory to freezing the exported Graph
  • Remember to modify the train steps "-****" in "--input_checkpoint"
python freeze_graph.py \
  --input_graph slim/satellite/export/mobilenet_v2_140_inf_graph.pb \
  --input_checkpoint slim/satellite/train_log/model.ckpt-****(8000) \
  --input_binary true \
  --output_node_names MobilenetV2/Predictions/Reshape_1 \
  --output_graph slim/satellite/freeze/mobilenet_v2_140.pb

  • Or execute "export_freeze.sh" at the project root directory to export and freeze Graph.

 

Reference:

猜你喜欢

转载自blog.csdn.net/weixin_41803874/article/details/81567791
今日推荐