深度學習之遷移學習(打造自己的圖像識別模型)

 

一、概論

       遷移學習(打造自己的圖像識別模型)其實就是利用已有的深度神經網絡(VGG16,AlexNet , GoogLeNet等)進行簡單的微調。一般有如下幾種方式:

  1. 只訓練全連接層。
  2. 全部網絡重新訓練(使用已有參數/從頭開始)
  3. 只訓練部分網絡。

二、數據準備

    原始的圖片數據和標籤需要轉換成tfrecord格式的文件,tfrecord, 這是一種將圖像數據和標籤放在一起的二進制文件,能更好的利用內存,在tensorflow中快速的複製,移動,讀取,存儲 等等。

    1.原始圖片保存在 data_prepare/pic中      

   2. 在data_prepare中執行如下命令:

    python data_convert.py -t pie/ \
    --train-shards 2 \
    --validation-shards 2 \
    --num-threads 2 \
    --dataset-name satellite

三、使用 TensorFlow Slim 微調模型

1.下載 TensorFlow Slim 的源代碼

   git clone http://github.com/tensorflow/models.git

找到 models/research/ 目錄中的 slim 文件夾 , 這就是要用到的 TensorFlow Slim的源代碼 。

2.準備數據 

    2.1  新建satellite.py

     在 datasets/ 目錄下新建一個文件 satellite.py,並將 flowers.py 文件
中的內容複製到 satellite. py 中 。

    2.2 修改是 FILE PATTERN 、 SPLITS_TO_ SIZES 、NUM CLASSES

     _FILE PATTERN = ’ satellite_%s_ *. tfrecord'

     SPLITS_TO_SIZES = {'train':4800,'validation':1200}

     _NUM_CLASSES = 6

    2.3 修改 image/format 部分

       將“'image/format': tf.FixedLenFeature((), tf.string, default_value='png'),”修改爲“'image/format': tf.FixedLenFeature((), tf.string, default_value='jpg'),”

    2.4 在dataset_factory. py 文件中註冊satellite數據庫

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from datasets import cifar10
from datasets import flowers
from datasets import imagenet
from datasets import mnist
from datasets import satellite

datasets_map = {
    'cifar10': cifar10,
    'flowers': flowers,
    'imagenet': imagenet,
    'mnist': mnist,
    'satellite': satellite,
}

3.準備訓練文件夾

   3.1 在 slim 文件夾下再新建一個 satellite

   3.2 在satellite文件夾中新建一個 data 目錄 ,並將 5 個轉換好格式的tfrecord格式訓練數據複製進去。

   3.3  在satellite文件夾中新建一個空的 train_dir 目錄,用來保存訓練過程中的日誌和模型。

   3.4  在satellite文件夾中新建一個 pretrained 目錄,在 slim 的 GitHub 頁面找到 Inception V3 模型的下載地址 http://download. tensorflow.org/models/inception_v3_2016_08_28.tar.gz ,下載並解壓後,會得到一個 inception_v3 .ckpt 文件,將該文件複製到 pretrained 目錄下。

4.開始訓練

   1.(在slim文件夾下運行)訓練Logits層

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

2.訓練所有層

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 \
  --max_number_of_steps=100000 \
  --batch_size=32 \
  --learning_rate=0.001 \
  --learning_rate_decay_type=fixed \
  --save_interval_secs=300 \
  --save_summaries_secs=10 \
  --log_every_n_steps=1 \
  --optimizer=rmsprop \
  --weight_decay=0.00004

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章