記錄在unbantu14.04, caffe框架下對MobileNet的自有數據集fine tune。
首先git clone一下caffe版本的mobilenet https://github.com/shicai/MobileNet-Caffe.git
然后把deploy.prototxt文件修改一下
Modify deploy.prototxt
and save it as your train.prototxt
as follows: Remove the first 5 input
/input_dim
lines, and add Image Data
layer in the beginning like this:
layer { name: "data" type: "ImageData" top: "data" top: "label" include { phase: TRAIN } transform_param { scale: 0.017 mirror: true crop_size: 224 mean_value: [103.94, 116.78, 123.68] } image_data_param { source: "your_list_train_txt" batch_size: 32 # your batch size new_height: 256 new_width: 256 root_folder: "your_path_to_training_data_folder" } }
Remove the last prob
layer, and add Loss
and Accuracy
layers in the end like this:
layer { name: "loss" type: "SoftmaxWithLoss" bottom: "fc7" bottom: "label" top: "loss" } layer { name: "top1/acc" type: "Accuracy" bottom: "fc7" bottom: "label" top: "top1/acc" include { phase: TEST } } layer { name: "top5/acc" type: "Accuracy" bottom: "fc7" bottom: "label" top: "top5/acc" include { phase: TEST } accuracy_param { top_k: 5 } }
然后包括了train_val.prototxt, deploy.prototxt,
通過createDB.py來生成帶label的txt文件:代碼如下:
第二個方法為生成LMDB的文件
# -*- coding: UTF-8 -*- import os import re import commands def createFileList(images_path, txt_save_path): fw = open(txt_save_path,"w") images_name = os.listdir(images_path) for eachname in images_name: pattern_cat = r'(^cat.\d{0,10}.jpg$)' pattern_dog = r'(^dog.\d{0,10}.jpg$)' cat_name = re.search(pattern_cat, eachname) dog_name = re.search(pattern_dog, eachname) if cat_name != None: fw.write(cat_name.group(0) + ' n16000001\n') if dog_name != None: fw.write(dog_name.group(0) + ' n16000002\n') print "done with txt generation!" fw.close() def create_db(caffe_root, images_path, txt_save_path): lmdb_name = 'img_train.lmdb' lmdb_save_path = caffe_root + 'models/MobileNet-Caffe/' + lmdb_name convert_imageset_path = caffe_root + 'build/tools/convert_imageset' cmd = """%s --shuffle --resize_height=256 --resize_width=256 %s %s %s""" status, output = commands.getstatusoutput(cmd % (convert_imageset_path, images_path, txt_save_path, lmdb_save_path)) print output if(status == 0): print "lmbd is done!" if __name__ == '__main__': caffe_root = '/home/wy/ssd-caffe/caffe/' my_caffe_project = caffe_root + 'models/MobileNet-Caffe/' images_path = caffe_root + 'models/MobileNet-Caffe/val/' txt_name = 'label_val.txt' txt_save_path = my_caffe_project + txt_name createFileList(images_path, txt_save_path) #create_db(caffe_root, images_path, txt_save_path)
生成的txt文件如下,帶labels:
對於自己的solver,由於懷疑測試集是imagenet的分支,所以,把lr調的很低,我的solver文件如下:
調參的過程很復雜,因為收斂非常快,最后總結出一個辦法,要么把lr調低,要么在提前fc一層就開始fine tune。
tips:在fine tune的時候,要改layer中的名字,這樣會使得caffemodel的weight參數跳過賦值。
最后訓練的結果如下
有時間再把整個過程整理下,今天權當記錄一下。
最終accuracy nearly 98.5%