1. 程式人生 > >使用SSD基於caffe框架訓練自己的資料

使用SSD基於caffe框架訓練自己的資料

宣告:本文僅以自己的實驗過程編寫,如若轉載,請與博主聯絡。建議讀者在做本文中的嘗試之前,先訓練一編官網論文中的例子,按照SSD教程跑一邊,或者參看博主博文:http://blog.csdn.net/xunan003/article/details/78427446    配置SSD並完整除錯一遍。

如果文章中連結點選錯誤,可複製該連結至位址列中訪問!!!

一、總體介紹

        SSD基本基於python實現。使用VGGNET做為基礎網路。執行一個完整SSD過程,需要做完成以下幾個準備工作:

1、資料的標註。即將待訓練驗證和測試影象做標註,確定識別哪幾類目標,標註後生成xml檔案供訓練測試時提供位置資訊。

2、資料集的轉換。SSD訓練與分類網路類似同樣需要將圖片資料轉化成為lmdb格式,此過程轉換之前,要根據標註的位置資訊生成相應的trainval.txt和test.txt檔案。

3、修改訓練執行指令碼。根據我們自己的資料和內容修改SSD訓練執行指令碼ssd_pasical.py檔案。

4、執行訓練模型。

5、使用訓練出的模型進行測試。

二、資料的標註

我們的資料使用INRIA資料集中train和test中的person_and_bike組合而成,共159張圖片,我們使用前面120張做trainval,最後39張做test。

1、首先在ssd-caffe目錄下建立資料夾image,image資料夾下建立資料夾INRIA_TRAIN_part用來儲存我們的資料。該資料夾下類似於VOC資料格式,新建三個必須子資料夾:Annotations、ImageSets、PNGImage分別用來儲存我們的xml檔案、txt檔案、原始圖片資料。

2、將159張原始圖片資料存放入PNGImg資料夾下,此資料夾下最好再新建兩個資料夾trainval和test,將前120張圖片複製一份放入trainval備份,將後39張圖片放入test中備份,這樣做的原因雖然多佔用了儲存容量,但是在後續可以利用python指令碼容易生成txt檔案,免去更改create_list.sh指令碼生成txt的煩擾。如圖1所示。


圖1. 我的是全部執行完畢之後的狀況

3、使用labelImg標註工具標註影象目標。本文中我們檢測兩個目標person和bike,開啟labelImg工具使用快捷鍵選中PNGImg資料夾做影象原始檔,選中xml儲存資料夾為Annotations,然後開始159張圖片的標註,標註完成後如圖2。


圖2.標註工具使用和標註後生成的xml檔案

三、資料集的轉換

在轉換db檔案之前,首先要先生成txt檔案。按照博文http://blog.csdn.net/u014696921/article/details/53353896 的方法需要兩組txt檔案,一組是不帶路徑和字尾的純圖片名稱存放在ImgeSets目錄中,二組是根據一組而生成帶有路徑、字尾以及相對應的xml檔案的路徑檔名放在一起的txt檔案。我們的方法不需要第一組txt檔案,但是還是講解一下生成第一組txt檔案生成的指令碼,但是訓練過程中這部分txt檔案沒有用到,所以讀者可不用考慮。

1、第一組txt檔案生成方法(此部分可以不做考慮)

        我們為了方便起見使用matlab實現,如果caffe編譯裡matlab介面,可以使用matlab介面直接執行.m檔案執行生成。如果沒有編譯matlab介面則需要使用matlab軟體,windows系統或ubuntu系統安裝的matlab均可以。博主使用的是matlab r2012b版本。博主是在windows系統下生成的,因為博主有兩臺電腦。

首先將159張圖片放在一個單獨的資料夾pos下,使用matlab編譯程式。指令碼如下,ubuntu系統類似,只需更改路徑即可。

%% 
clear all; close all; clc;

%%
Dataset = 'E:\目標檢測\練習\INRIA\img';%159張圖片儲存的位置
Folder = dir(fullfile(Dataset,'*.png'));%dir()返回資料夾中的所有png檔案所組成的列表,結構體名為Floder
%Folder(1:2) = [];%把陣列a的第一個和第二個元素取空
NumCls = length(Folder);%length()表示向量長度,即圖片個數

%% 
fid = fopen('E:\目標檢測\練習\MIT\trainval.txt','w');%生成的trainval.txt檔案儲存位置,自己指定。如果需要生成test.txt檔案,則把trainval相應改為test即可。
fid = fopen('E:\目標檢測\練習\MIT\trainval.txt','a');
for iCls = 1:120    %這裡是取的圖片個數,我們是生成trainval.txt所以取前120個圖片。如果要生成test.txt則改為121:NumCls即可。
    ClsName = Folder(iCls).name;%結構體Floder中第iCLs個檔案的名字
    newClsName = ClsName(1:end-4);%取消檔名中的.png字尾,如果字尾為.jpeg則需要改為end-5。
        ImgName_Label = sprintf('%s\n',newClsName);  %寫入檔名
        fprintf(fid, ImgName_Label);  
end
fclose(fid);
       生成的txt檔案如圖3所示。

 圖3.將生成的trainval.txt和test.txt文字拷貝存放在目錄ImgeSets下即可。

2、生成第二組txt檔案,必須生成,使用lmdb檔案時需要使用。

       此步驟需要生成三個txt檔案——trainval.txt、test.txt以及test_name_size.txt檔案,注意此處trainval.txt和test.txt是不同的檔案,所以不能儲存在相同的路徑下,我們儲存在image根目錄下。test_name_size.txt檔案儲存了39張測試圖片的名稱和圖片大小資訊。參考博文中和ssd框架預設的都是使用的檔案create_list.sh指令碼生成這三個txt檔案,但是博主使用此指令碼更改了很久只能生成trainval.txt和test.txt文字,但是測試圖片大小文字始終無法生成,錯誤不斷,易讀性較差,必須將我們的所有檔案包括圖片、xml檔案等完全按照VOC實驗時的資料存放路徑放,不能像本文這樣自己指定存放自己的路徑,不然始終有錯誤。另外指令碼所使用的get_image_size工具有缺陷,其對jpg圖片使用較適合,但是對png等其它格式圖片有缺陷,有可能使得所獲取的圖片大小完全離譜且每次執行的結果都不同。針對以上缺陷,我們使用python指令碼完成此步驟,簡單易讀。

1)、生成trainval.txt和test.txt檔案的指令碼(此指令碼需要提前將39張test圖片和120張trainval圖片分開存放,這也就是我們在二中將圖片分開備份一份的目的。)

#! /usr/bin/python

import os, sys
import glob

trainval_dir = "/home/xn/caffe/image/INRIA_TRAIN_part/PNGImages/trainval"  #trainval圖片儲存路徑
test_dir = "/home/xn/caffe/image/INRIA_TRAIN_part/PNGImages/test"  #test圖片儲存路徑

trainval_img_lists = glob.glob(trainval_dir + '/*.png')    #獲取trainval中所有.png的檔案
trainval_img_names = []    #獲取名稱
for item in trainval_img_lists:
    temp1, temp2 = os.path.splitext(os.path.basename(item))
    trainval_img_names.append(temp1)

test_img_lists = glob.glob(test_dir + '/*.png')   #獲取test中所有.png檔案
test_img_names = []
for item in test_img_lists:
    temp1, temp2 = os.path.splitext(os.path.basename(item))
    test_img_names.append(temp1)
#dist_img_dir1 = "INRIA_TRAIN_part/PNGImages/trainval"
#dist_img_dir2 = "INRIA_TRAIN_part/PNGImages/test"
dist_img_dir = "INRIA_TRAIN_part/PNGImages"    #需要寫入txt的trainval和test路徑,因為我們在PNGImges目錄下除了有trainval和test資料夾外還有所有159張圖片,而資料夾trainval和test資料夾只是用於生成txt之用
dist_anno_dir = "INRIA_TRAIN_part/Annotations" #需要寫入的xml路徑

trainval_fd = open("/home/xn/caffe/image/trainval.txt", 'w')
test_fd = open("/home/xn/caffe/image/test.txt", 'w')

for item in trainval_img_names:
    trainval_fd.write(dist_img_dir + '/' + str(item) + '.png' + ' ' + dist_anno_dir + '/' + str(item) + '.xml\n')

for item in test_img_names:
    test_fd.write(dist_img_dir + '/' + str(item) + '.png' + ' ' + dist_anno_dir + '/' + str(item) + '.xml\n')
        生成後的txt檔案如圖4。儲存在INRIA_TRAIN_part根目錄下。


圖4.生成的trainval.txt和test.txt文字。

2)、生成test_name_size.txt,所用python指令碼如下。

#! /usr/bin/python

import os, sys
import glob
from PIL import Image

img_dir = "/home/xn/caffe/image/INRIA_TRAIN_part/PNGImages/test/"  #39張test圖片儲存路徑

img_lists = glob.glob(img_dir + '/*.png')

test_name_size = open('/home/xn/caffe/image/test_name_size.txt', 'w')

for item in img_lists:
    img = Image.open(item)
    width, height = img.size
    temp1, temp2 = os.path.splitext(os.path.basename(item))
    test_name_size.write(temp1 + ' ' + str(height) + ' ' + str(width) + '\n')
       生成的txt檔案如圖5。

圖5.test_name_size.txt檔案包含圖片名和圖片大小,從左到右分別為:名稱   高(height)   寬(width)

3、生成lmdb檔案

       生成了相應的txt檔案之後生成訓練資料lmdb格式。

       首先在ssd-caffe/data/VOC0712目錄下拷貝create_data.sh至目錄image下並重命名為create_INRIA_data.sh。修改其中的內容,主要為路徑。如下

cur_dir=$(cd $( dirname ${BASH_SOURCE[0]} ) && pwd )
root_dir=$cur_dir/../..

cd $root_dir

redo=1
data_root_dir="$HOME/caffe/image"
dataset_name="INRIA"
mapfile="$HOME/caffe/image/labelmap_INRIA.prototxt"   #此檔案我們在下面4中補充。
anno_type="detection"
db="lmdb"
min_dim=0
max_dim=0
width=0
height=0

extra_cmd="--encode-type=png --encoded"
if [ $redo ]
then
  extra_cmd="$extra_cmd --redo"
fi
for subset in test trainval
do   #下面的路徑需要根據自己的情況修改,我們的就是這樣
  python $HOME/caffe/scripts/create_annoset.py --anno-type=$anno_type --label-map-file=$mapfile --min-dim=$min_dim --max-dim=$max_dim --resize-width=$width --resize-height=$height --check-label $extra_cmd $data_root_dir $HOME/caffe/image/$subset.txt $data_root_dir/INRIA_TRAIN_part/$db/$dataset_name"_"$subset"_"$db /home/xn/caffe/examples/INRIA_TRAIN_part
done
       以上指令碼生成了兩份lmdb檔案,一份在INRIA_TRAIN_part目錄下,一份備份到examples/INRIA_TRAIN_part目錄下。

4、指令碼3中所需的labelmap_INRIA.prototxt檔案在ssd-caffe/data/VOC0712目錄下,拷貝一份至image目錄下並改名為labelmap_INRIA.prototxt

根據自己的類別(我們的是person和bike)修改其中的內容(第一部分不能改變)為

item {
  name: "none_of_the_above"
  label: 0
  display_name: "background"
}
item {
  name: "person"
  label: 1
  display_name: "person"
}
item {
  name: "bike"
  label: 2
  display_name: "bike"
}

並儲存即可。如下圖


四、修改訓練執行指令碼

       訓練時使用ssd demo中提供的預訓練好的VGGnet model : VGG_ILSVRC_16_layers_fc_reduced.caffemodel
       將該模型儲存到$CAFFE_ROOT/models/VGGNet下。

       ssd訓練執行指令碼主要是儲存在ssd-caffe/examples/ssd路徑下的ssd_pascal.py,此指令碼為綜合指令碼,其包含了生成訓練檔案train.prototxt、test.prototxt、solver.prototxt,以及最終訓練命令都包含在內,所以說要想熟練的使用ssd框架,此指令碼是我們必須要仔細研讀和熟悉的。我們拷貝一份至image目錄下並修改其中的內容。修改部分有標記,修改後的指令碼如下

from __future__ import print_function
import caffe
from caffe.model_libs import *
from google.protobuf import text_format

import math
import os
import shutil
import stat
import subprocess
import sys

# Add extra layers on top of a "base" network (e.g. VGGNet or Inception).
def AddExtraLayers(net, use_batchnorm=True, lr_mult=1):
    use_relu = True

    # Add additional convolutional layers.
    # 19 x 19
    from_layer = net.keys()[-1]

    # TODO(weiliu89): Construct the name using the last layer to avoid duplication.
    # 10 x 10
    out_layer = "conv6_1"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 1, 0, 1,
        lr_mult=lr_mult)

    from_layer = out_layer
    out_layer = "conv6_2"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 512, 3, 1, 2,
        lr_mult=lr_mult)

    # 5 x 5
    from_layer = out_layer
    out_layer = "conv7_1"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 128, 1, 0, 1,
      lr_mult=lr_mult)

    from_layer = out_layer
    out_layer = "conv7_2"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 3, 1, 2,
      lr_mult=lr_mult)

    # 3 x 3
    from_layer = out_layer
    out_layer = "conv8_1"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 128, 1, 0, 1,
      lr_mult=lr_mult)

    from_layer = out_layer
    out_layer = "conv8_2"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 3, 0, 1,
      lr_mult=lr_mult)

    # 1 x 1
    from_layer = out_layer
    out_layer = "conv9_1"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 128, 1, 0, 1,
      lr_mult=lr_mult)

    from_layer = out_layer
    out_layer = "conv9_2"
    ConvBNLayer(net, from_layer, out_layer, use_batchnorm, use_relu, 256, 3, 0, 1,
      lr_mult=lr_mult)

    return net


### Modify the following parameters accordingly ###
# The directory which contains the caffe code.
# We assume you are running the script at the CAFFE_ROOT.
caffe_root = os.getcwd()

# Set true if you want to start training right after generating all files.
run_soon = True
# Set true if you want to load from most recently saved snapshot.
# Otherwise, we will load from the pretrain_model defined below.
resume_training = True
# If true, Remove old model files.
remove_old_models = False

# The database file for training data. Created by data/VOC0712/create_data.sh
train_data = "examples/INRIA_TRAIN_part/INRIA_trainval_lmdb"             #改成自己lmdb的路徑
# The database file for testing data. Created by data/VOC0712/create_data.sh
test_data = "examples/INRIA_TRAIN_part/INRIA_test_lmdb"            #改成自己lmdb的路徑
# Specify the batch sampler.
resize_width = 300           # 圖片大小可以改變
resize_height = 300
resize = "{}x{}".format(resize_width, resize_height)
batch_sampler = [
        {
                'sampler': {
                        },
                'max_trials': 1,
                'max_sample': 1,
        },
        {
                'sampler': {
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        },
                'sample_constraint': {
                        'min_jaccard_overlap': 0.1,
                        },
                'max_trials': 50,
                'max_sample': 1,
        },
        {
                'sampler': {
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        },
                'sample_constraint': {
                        'min_jaccard_overlap': 0.3,
                        },
                'max_trials': 50,
                'max_sample': 1,
        },
        {
                'sampler': {
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        },
                'sample_constraint': {
                        'min_jaccard_overlap': 0.5,
                        },
                'max_trials': 50,
                'max_sample': 1,
        },
        {
                'sampler': {
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        },
                'sample_constraint': {
                        'min_jaccard_overlap': 0.7,
                        },
                'max_trials': 50,
                'max_sample': 1,
        },
        {
                'sampler': {
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        },
                'sample_constraint': {
                        'min_jaccard_overlap': 0.9,
                        },
                'max_trials': 50,
                'max_sample': 1,
        },
        {
                'sampler': {
                        'min_scale': 0.3,
                        'max_scale': 1.0,
                        'min_aspect_ratio': 0.5,
                        'max_aspect_ratio': 2.0,
                        },
                'sample_constraint': {
                        'max_jaccard_overlap': 1.0,
                        },
                'max_trials': 50,
                'max_sample': 1,
        },
        ]
train_transform_param = {
        'mirror': True,
        'mean_value': [104, 117, 123],      #均值可以跟據分類任務中的均值計算方法計算修改
        'resize_param': {
                'prob': 1,
                'resize_mode': P.Resize.WARP,
                'height': resize_height,
                'width': resize_width,
                'interp_mode': [
                        P.Resize.LINEAR,
                        P.Resize.AREA,
                        P.Resize.NEAREST,
                        P.Resize.CUBIC,
                        P.Resize.LANCZOS4,
                        ],
                },
        'distort_param': {
                'brightness_prob': 0.5,
                'brightness_delta': 32,
                'contrast_prob': 0.5,
                'contrast_lower': 0.5,
                'contrast_upper': 1.5,
                'hue_prob': 0.5,
                'hue_delta': 18,
                'saturation_prob': 0.5,
                'saturation_lower': 0.5,
                'saturation_upper': 1.5,
                'random_order_prob': 0.0,
                },
        'expand_param': {
                'prob': 0.5,
                'max_expand_ratio': 4.0,
                },
        'emit_constraint': {
            'emit_type': caffe_pb2.EmitConstraint.CENTER,
            }
        }
test_transform_param = {
        'mean_value': [104, 117, 123],
        'resize_param': {
                'prob': 1,
                'resize_mode': P.Resize.WARP,
                'height': resize_height,
                'width': resize_width,
                'interp_mode': [P.Resize.LINEAR],
                },
        }

# If true, use batch norm for all newly added layers.
# Currently only the non batch norm version has been tested.
use_batchnorm = False
lr_mult = 1
# Use different initial learning rate.
if use_batchnorm:
    base_lr = 0.0004
else:
    # A learning rate for batch_size = 1, num_gpus = 1.
    base_lr = 0.000004         #基礎學習慮的修改,因為我們的資料在原始的0.00004下會發散,因此縮小10倍,最終生成的solver.prototxt檔案中基礎學習率為0.0001,如不修改則為0.001

# Modify the job name if you want.
job_name = "SSD_{}".format(resize)
# The name of the model. Modify it if you want.
model_name = "VGG_INRIA_{}".format(job_name)   #修改我們的模型名稱

# Directory which stores the model .prototxt file.
save_dir = "models/VGGNet/INRIA/{}".format(job_name)   #修改此檔案生成的所有prototxt檔名稱及儲存路徑
# Directory which stores the snapshot of models.
snapshot_dir = "models/VGGNet/INRIA/{}".format(job_name)    #訓練得到的快照模型儲存路徑
# Directory which stores the job script and log file.
job_dir = "jobs/VGGNet/INRIA/{}".format(job_name)       #改為自己的路徑
# Directory which stores the detection results.
output_result_dir = "{}/xn/image/results/INRIA/{}/Main".format(os.environ['HOME'], job_name)    #輸出的結果路徑

# model definition files.
train_net_file = "{}/train.prototxt".format(save_dir)
test_net_file = "{}/test.prototxt".format(save_dir)
deploy_net_file = "{}/deploy.prototxt".format(save_dir)
solver_file = "{}/solver.prototxt".format(save_dir)
# snapshot prefix.
snapshot_prefix = "{}/{}".format(snapshot_dir, model_name)
# job script path.
job_file = "{}/{}.sh".format(job_dir, model_name)

# Stores the test image names and sizes. Created by data/VOC0712/create_list.sh
name_size_file = "xn/image/test_name_size.txt"          #自己的test_name_size .txt檔案路徑
# The pretrained model. We use the Fully convolutional reduced (atrous) VGGNet.
pretrain_model = "models/VGGNet/VGG_ILSVRC_16_layers_fc_reduced.caffemodel"    #下載的微調預訓練模型位置
# Stores LabelMapItem.
label_map_file = "xn/image/labelmap_INRIA.prototxt"     #我們的labelmap_INRIA.prototxt位置

# MultiBoxLoss parameters.
num_classes = 3                      #此處需要注意,修改為自己的訓練類別數+1,我們的資料為person和bike共2類,所以這裡設定為3
share_location = True
background_label_id=0
train_on_diff_gt = True
normalization_mode = P.Loss.VALID
code_type = P.PriorBox.CENTER_SIZE
ignore_cross_boundary_bbox = False
mining_type = P.MultiBoxLoss.MAX_NEGATIVE
neg_pos_ratio = 3.
loc_weight = (neg_pos_ratio + 1.) / 4.
multibox_loss_param = {
    'loc_loss_type': P.MultiBoxLoss.SMOOTH_L1,
    'conf_loss_type': P.MultiBoxLoss.SOFTMAX,
    'loc_weight': loc_weight,
    'num_classes': num_classes,
    'share_location': share_location,
    'match_type': P.MultiBoxLoss.PER_PREDICTION,
    'overlap_threshold': 0.5,
    'use_prior_for_matching': True,
    'background_label_id': background_label_id,
    'use_difficult_gt': train_on_diff_gt,
    'mining_type': mining_type,
    'neg_pos_ratio': neg_pos_ratio,
    'neg_overlap': 0.5,
    'code_type': code_type,
    'ignore_cross_boundary_bbox': ignore_cross_boundary_bbox,
    }
loss_param = {
    'normalization': normalization_mode,
    }

# parameters for generating priors.
# minimum dimension of input image
min_dim = 300
# conv4_3 ==> 38 x 38
# fc7 ==> 19 x 19
# conv6_2 ==> 10 x 10
# conv7_2 ==> 5 x 5
# conv8_2 ==> 3 x 3
# conv9_2 ==> 1 x 1
mbox_source_layers = ['conv4_3', 'fc7', 'conv6_2', 'conv7_2', 'conv8_2', 'conv9_2']
# in percent %
min_ratio = 20
max_ratio = 90
step = int(math.floor((max_ratio - min_ratio) / (len(mbox_source_layers) - 2)))
min_sizes = []
max_sizes = []
for ratio in xrange(min_ratio, max_ratio + 1, step):
  min_sizes.append(min_dim * ratio / 100.)
  max_sizes.append(min_dim * (ratio + step) / 100.)
min_sizes = [min_dim * 10 / 100.] + min_sizes
max_sizes = [min_dim * 20 / 100.] + max_sizes
steps = [8, 16, 32, 64, 100, 300]
aspect_ratios = [[2], [2, 3], [2, 3], [2, 3], [2], [2]]
# L2 normalize conv4_3.
normalizations = [20, -1, -1, -1, -1, -1]
# variance used to encode/decode prior bboxes.
if code_type == P.PriorBox.CENTER_SIZE:
  prior_variance = [0.1, 0.1, 0.2, 0.2]
else:
  prior_variance = [0.1]
flip = True
clip = False

# Solver parameters.
# Defining which GPUs to use.
gpus = "0"
gpulist = gpus.split(",")
num_gpus = len(gpulist)

# Divide the mini-batch to different GPUs.
batch_size = 16            #根據GPU及電腦記憶體的容量來修改批次大小,太大會導致記憶體爆炸無法訓練
accum_batch_size = 16
iter_size = accum_batch_size / batch_size         #這裡根據設定的批次的大小計算出迭代次數,不需要修改
solver_mode = P.Solver.CPU
device_id = 0
batch_size_per_device = batch_size
if num_gpus > 0:
  batch_size_per_device = int(math.ceil(float(batch_size) / num_gpus))
  iter_size = int(math.ceil(float(accum_batch_size) / (batch_size_per_device * num_gpus)))
  solver_mode = P.Solver.GPU
  device_id = int(gpulist[0])

if normalization_mode == P.Loss.NONE:    #用if做條件語句
  base_lr /= batch_size_per_device            #根據程式碼起始處註釋的basr_lr計算最終訓練的base_lr
elif normalization_mode == P.Loss.VALID:
  base_lr *= 25. / loc_weight    #根據起始處註釋的basr_lr計算最終訓練的base_lr

elif normalization_mode == P.Loss.FULL:
# Roughly there are 2000 prior bboxes per image. 
# TODO(weiliu89): Estimate the exact # of priors.
 base_lr *= 2000. #根據起始處註釋的basr_lr計算最終訓練的base_lr
# Evaluate on whole test set.
num_test_image = 39 #修改為自己的test圖片數量,我們的為39
test_batch_size = 8 #可以修改test批次大小,必要時需要改小
# Ideally test_batch_size should be divisible by num_test_image,
# otherwise mAP will be slightly off the true value.
test_iter = int(math.ceil(float(num_test_image) / test_batch_size)) #根據上兩條設定計算出每test一次所需要的迭代次數
solver_param = {
 # Train parameters
       'base_lr': base_lr, 
       'weight_decay': 0.0005,
       'lr_policy': "multistep",
       'stepvalue': [20000, 40000, 60000], #此處可以根據自己資料量的大小修改,學習慮衰減的迭代次數
       'gamma': 0.1,
       'momentum': 0.9,
       'iter_size': iter_size,
       'max_iter': 80000, #根據自己的資料量修改
       'snapshot': 80000, #根據自己的資料量修改
       'display': 20,
       'average_loss': 20,
       'type': "SGD",
       'solver_mode': solver_mode,
       'device_id': device_id,
       'debug_info': False,
       'snapshot_after_train': True,
  # Test parameters
       'test_iter': [test_iter],
       'test_interval': 200, #根據自己的情況修改
       'eval_type': "detection",
       'ap_version': "11point",
       'test_initialization': False, 
}
# parameters for generating detection output.
det_out_param = { 
       'num_classes': num_classes,
       'share_location': share_location,
       'background_label_id': background_label_id,
       'nms_param': {'nms_threshold': 0.45, 'top_k': 400},
       'save_output_param': {
               'output_directory': output_result_dir,
               'output_name_prefix': "comp4_det_test_",
               'output_format': "INRIA",
               'label_map_file': label_map_file,
               'name_size_file': name_size_file,
               'num_test_image': num_test_image, 
        },
       'keep_top_k': 200,
       'confidence_threshold': 0.01,
       'code_type': code_type, 
}

# parameters for evaluating detection results.
det_eval_param = {
       'num_classes': num_classes,
       'background_label_id': background_label_id,
       'overlap_threshold': 0.5,
       'evaluate_difficult_gt': False,
       'name_size_file': name_size_file, 
}

### Hopefully you don't need to change the following ###
# Check file.
check_if_exist(train_data)
check_if_exist(test_data)
check_if_exist(label_map_file)
check_if_exist(pretrain_model)
make_if_not_exist(save_dir)
make_if_not_exist(job_dir)
make_if_not_exist(snapshot_dir)

# Create train net.
net = caffe.NetSpec()
net.data, net.label = CreateAnnotatedDataLayer(train_data, batch_size=batch_size_per_device,
             train=True, output_label=True, label_map_file=label_map_file,
             transform_param=train_transform_param, batch_sampler=batch_sampler)

VGGNetBody(net, from_layer='data', fully_conv=True, reduced=True, dilated=True, dropout=False)

AddExtraLayers(net, use_batchnorm, lr_mult=lr_mult)

mbox_layers = CreateMultiBoxHead(net, data_layer='data', from_layers=mbox_source_layers,
        use_batchnorm=use_batchnorm, min_sizes=min_sizes, max_sizes=max_sizes,
        aspect_ratios=aspect_ratios, steps=steps, normalizations=normalizations,
        num_classes=num_classes, share_location=share_location, flip=flip, clip=clip,
        prior_variance=prior_variance, kernel_size=3, pad=1, lr_mult=lr_mult)

# Create the MultiBoxLossLayer.
name = "mbox_loss"
mbox_layers.append(net.label)
net[name] = L.MultiBoxLoss(*mbox_layers, multibox_loss_param=multibox_loss_param,
      loss_param=loss_param, include=dict(phase=caffe_pb2.Phase.Value('TRAIN')),
      propagate_down=[True, True, False, False])
with open(train_net_file, 'w') as f: 
    print('name: "{}_train"'.format(model_name), file=f)
    print(net.to_proto(), file=f)
shutil.copy(train_net_file, job_dir)

# Create test net.
net = caffe.NetSpec()
net.data, net.label = CreateAnnotatedDataLayer(test_data, batch_size=test_batch_size,
        train=False, output_label=True, label_map_file=label_map_file,
        transform_param=test_transform_param)

VGGNetBody(net, from_layer='data', fully_conv=True, reduced=True, dilated=True,
    dropout=False)

AddExtraLayers(net, use_batchnorm, lr_mult=lr_mult)

mbox_layers = CreateMultiBoxHead(net, data_layer='data', from_layers=mbox_source_layers,
        use_batchnorm=use_batchnorm, min_sizes=min_sizes, max_sizes=max_sizes,
        aspect_ratios=aspect_ratios, steps=steps, normalizations=normalizations,
        num_classes=num_classes, share_location=share_location, flip=flip, clip=clip,
        prior_variance=prior_variance, kernel_size=3, pad=1, lr_mult=lr_mult)

conf_name = "mbox_conf"
if multibox_loss_param["conf_loss_type"] == P.MultiBoxLoss.SOFTMAX:
  reshape_name = "{}_reshape".format(conf_name)
  net[reshape_name] = L.Reshape(net[conf_name], shape=dict(dim=[0, -1, num_classes]))
  softmax_name = "{}_softmax".format(conf_name)
  net[softmax_name] = L.Softmax(net[reshape_name], axis=2)
  flatten_name = "{}_flatten".format(conf_name)
  net[flatten_name] = L.Flatten(net[softmax_name], axis=1)
  mbox_layers[1] = net[flatten_name]
elif multibox_loss_param["conf_loss_type"] == P.MultiBoxLoss.LOGISTIC:
  sigmoid_name = "{}_sigmoid".format(conf_name)
  net[sigmoid_name] = L.Sigmoid(net[conf_name])
  mbox_layers[1] = net[sigmoid_name]

net.detection_out = L.DetectionOutput(*mbox_layers,
    detection_output_param=det_out_param,
    include=dict(phase=caffe_pb2.Phase.Value('TEST')))
net.detection_eval = L.DetectionEvaluate(net.detection_out, net.label,
    detection_evaluate_param=det_eval_param,
    include=dict(phase=caffe_pb2.Phase.Value('TEST')))

with open(test_net_file, 'w') as f:
    print('name: "{}_test"'.format(model_name), file=f)
    print(net.to_proto(), file=f)
shutil.copy(test_net_file, job_dir)# Create deploy net.

# Remove the first and last layer from test net.
deploy_net = net
with open(deploy_net_file, 'w') as f:
    net_param = deploy_net.to_proto()
    # Remove the first (AnnotatedData) and last (DetectionEvaluate) layer from test net.
    del net_param.layer[0]
    del net_param.layer[-1]
    net_param.name = '{}_deploy'.format(model_name)
    net_param.input.extend(['data'])
    net_param.input_shape.extend([
        caffe_pb2.BlobShape(dim=[1, 3, resize_height, resize_width])])
    print(net_param, file=f)
shutil.copy(deploy_net_file, job_dir)

# Create solver.
solver = caffe_pb2.SolverParameter(
        train_net=train_net_file,
        test_net=[test_net_file],
        snapshot_prefix=snapshot_prefix,
        **solver_param)

with open(solver_file, 'w') as f:
    print(solver, file=f)
shutil.copy(solver_file, job_dir)

max_iter = 0
# Find most recent snapshot.
for file in os.listdir(snapshot_dir):
  if file.endswith(".solverstate"):
  basename = os.path.splitext(file)[0]
  iter = int(basename.split("{}_iter_".format(model_name))[1])
  if iter > max_iter:
    max_iter = iter

train_src_param = '--weights="{}" \\\n'.format(pretrain_model)
if resume_training:
  if max_iter > 0:
    train_src_param = '--snapshot="{}_iter_{}.solverstate" \\\n'.format(snapshot_prefix, max_iter)

if remove_old_models:
  # Remove any snapshots smaller than max_iter.
  for file in os.listdir(snapshot_dir):
  if file.endswith(".solverstate"):
    basename = os.path.splitext(file)[0]
    iter = int(basename.split("{}_iter_".format(model_name))[1])
    if max_iter > iter:
      os.remove("{}/{}".format(snapshot_dir, file))
  if file.endswith(".caffemodel"):
    basename = os.path.splitext(file)[0]
    iter = int(basename.split("{}_iter_".format(model_name))[1])
    if max_iter > iter:
      os.remove("{}/{}".format(snapshot_dir, file))

# Create job file.
with open(job_file, 'w') as f:
    f.write('cd {}\n'.format(caffe_root))
    f.write('./build/tools/caffe train \\\n')
    f.write('--solver="{}" \\\n'.format(solver_file))
    f.write(train_src_param)
    if solver_param['solver_mode'] == P.Solver.GPU:
      f.write('--gpu {} 2>&1 | tee {}/{}.log\n'.format(gpus, job_dir, model_name)) #日誌檔案儲存的路徑
    else:
      f.write('2>&1 | tee {}/{}.log\n'.format(job_dir, model_name))

# Copy the python script to job_dir.
py_file = os.path.abspath(__file__)
shutil.copy(py_file, job_dir)

# Run the job.
os.chmod(job_file, stat.S_IRWXU)
if run_soon: 
  subprocess.call(job_file, shell=True)
       修改完成之後使用命令:python ssd_pascal_INRIA.py

相關推薦

使用SSD基於caffe框架訓練自己資料

宣告:本文僅以自己的實驗過程編寫,如若轉載,請與博主聯絡。建議讀者在做本文中的嘗試之前,先訓練一編官網論文中的例子,按照SSD教程跑一邊,或者參看博主博文:http://blog.csdn.net/xunan003/article/details/78427446    配

Caffe-Windows訓練自己資料 + 遷移學習

一:目的 用配置好的Windows版本Caffe(no GPU),使用caffe自帶的ImageNet網路結構進行訓練和測試。訓練自己的資料; 用caffe團隊採用imagenet圖片進行訓練的引數結果,進行遷移學習; 二:訓練與測試 1. 資料集下載與處理 (

SSD框架訓練自己資料

一份非常簡單易懂的SSD tensorflow訓練教程,初學者也可以看得懂。當初自學的時候走了很多彎路,所以把自己的經驗和踩過的坑分享給大家,如果文中有什麼寫的不對的地方歡迎大家在評論區和我討論。 https://blog.csdn.net/Echo_Harring

SSD框架訓練自己資料

from __future__ import print_function import caffe from caffe.model_libs import * from google.protobuf import text_format import math import os i

caffe-MobileNet-ssd環境搭建及訓練自己資料集模型

PROJECT := caffe CONFIG_FILE := Makefile.config # Explicitly check for the config file, otherwise make -k will proceed anyway. ifeq ($(wildcard $(CONFIG_

Ubuntu 16.04 Caffe SSD 環境配置以及訓練自己資料

這篇文章我一開始寫在簡書,歡迎關注我的簡書賬號,會寫一些論文翻譯以及模型實現過程。「我的簡書文章地址」 這篇文章都儘量寫的詳盡。如果你有任何問題,歡迎在這裡提問,或者到我點簡書提問 總的來說,Caffe 是一個比較難上手的框架。這次嘗試訓練 Caffe 框架

caffe + win10基於CaffeNet網路框架訓練自己的圖片進行分類(實踐篇)

  接觸caffe一段時間了 ,一直沒有自己完整的跑過自己的資料,現在使用在win10系統上配置好的caffe環境,使用caffeNet網路框架,對自己準備的圖片資料集進行訓練,並使用生成的模型對圖片進行類別預測。接觸時間比較短,有的地方理解不到位,現在整理下我

Windows平臺基於Caffe框架的LeNet網路訓練

在Windows平臺下使用Caffe的確不如Linux來的方便,至少人家把Shell都已經寫好了。但是像我這種VS重度依賴者,還是離不開微軟大腿呀…廢話不多說,一步步來吧 0. 為了後續檔案路徑訪問的便利,我們先將$CAFFE_ROOT根目錄新增到作業系統環境變數PATH中,

MaskRCNN-Benchmark框架訓練自己資料

          Facebook AI Research 開源了 Faster R-CNN 和 Mask R-CNN 的 PyTorch 1.0 實現基準:MaskRCNN-Benchmark。相比 Detectron 和 mmdetecti

基於Darknet框架訓練分類器(cifar10資料集)+windows

參考  https://pjreddie.com/darknet/train-cifar/ 1 下載資料集 https://pjreddie.com/media/files/cifar.tgz  在該網址下下載cifar資料集,並解壓在darknet.exe

SSD-Tensorflow測試及訓練自己資料_錯誤錦集

深度學習小白一枚~入門初級,開始跑些實驗,一路遇到很多很多問題,在此僅記錄其中一部分,謝謝~環境:Ubuntu16.04+CUDA8.0+Cudnn8.0v6.0+python3.5+tensorflow1.4一、    SSD測試1.  下載SSD-Tensorflow原始

windows10+GPU下caffe資料集Lmdb格式製作+訓練自己資料

最近做人臉識別專案,想用到caffe訓練自己的資料,電腦作業系統為win10+GPU,這裡對caffe-windows配置、資料集製作、訓練資料都做一些介紹。(無GPU配置的看我這個部落格)。如果你用的是vs2015,那麼下面介紹的caffe不適合,要用BVLC wind

SSD-Tensorflow測試及訓練自己資料錯誤錦集

深度學習小白一枚~入門初級,開始跑些實驗,一路遇到很多很多問題,在此僅記錄其中一部分,謝謝~環境:Ubuntu16.04+CUDA8.0+Cudnn8.0v6.0+python3.5+tensorflow1.4一、    SSD測試1.  下載SSD-Tensorflow原始

如何利用caffe訓練自己資料

這篇博文主要參考了另一位博主https://blog.csdn.net/hellohaibo,在此向他表示感謝 首先,博主今天的caffe崩了,毫無徵兆的崩了,具體表現為博主想做一個更大的資料集,但是在生成lmbd檔案時永遠生成的是一個沒有名字的資料夾,可是博主已經在指定的example目錄裡寫了

SSD訓練自己資料遇到的問題

1在windows上面訓練和測試ssd的方法很少,而且配置也相當之麻煩,搞了幾天才除錯成功。現在寫一下過程分享到部落格上,順便也可以作為今後再次安裝的參考。我的配置環境: windows: 10  gpu: Titan X  cuda: 8.0  caffe: caffe

caffe訓練自己資料集的步驟

1. 準備資料,正負樣本的原圖和正負樣本的路徑; 例如:資料夾face,資料夾noface,訓練和驗證的檔名路徑txt 2. 建立資料庫 在caffe/example目錄下新建目錄myself。並將caffe/examples/imagenet目錄下create_i

使用pytorch版faster-rcnn訓練自己資料

使用pytorch版faster-rcnn訓練自己資料集 引言 faster-rcnn pytorch程式碼下載 訓練自己資料集 接下來工作 參考文獻 引言 最近在復現目標檢測程式碼(師兄強烈推薦F

《錯誤手記-01》 facenet使用預訓練模型fine-tune重新訓練自己資料集報錯

環境資訊:windows10+python3.5+tensorflow1.6.0 問題描述: 在自己的訓練集上跑train_softmax.py.  引數: --logs_base_dir F:/work/runspace/log/  --models_base_

Kaldi中thchs30訓練自己資料集的步驟

一、資料準備過程 網上下載的thchs-openslr資料集需要換成自己的資料集,包含兩個資料夾:data_thchs30和resource。下面講解如何搞定這一部分。 資料集在data_thchs30檔案中,包含四個部分(data、train、dev、test)。 data資料夾中包

yolov3訓練自己資料集可參考文章

參考部落格原址: https://blog.csdn.net/u012966194/article/details/80004647 這篇文章將介紹編譯darknet框架開始,到整理資料集,到用yolo網路實現一個內部資料集中號碼簿的定