日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Caffe-SSD相关源码说明和调试记录

發(fā)布時(shí)間:2025/3/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Caffe-SSD相关源码说明和调试记录 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

1????? 對(duì)Blob的理解及其操作:

Blob是一個(gè)四維的數(shù)組。維度從高到低分別是:

(num_,channels_,height_,width_)

對(duì)于圖像數(shù)據(jù)來說就是:圖片個(gè)數(shù),彩色通道個(gè)數(shù),寬,高

?

Blob中數(shù)據(jù)是row-major存儲(chǔ)的,W是變化最快的維度,例如在(n, k, h, w)處的數(shù)據(jù),其物理偏移量計(jì)算方式為:

?

?

Blob的常用方法:

blob.data() ? ? // 返回?cái)?shù)據(jù)

blob.diff() ? ? // 返回梯度

blob.shape() ? ?// 返回樣本的形狀

blob.num() ? ? ?// 返回樣本的個(gè)數(shù)(常用)

blob.channels() // 返回通道的個(gè)數(shù)(常用)

blob.height() ? // 返回樣本維度一,對(duì)于圖像而言是高度(常用)

blob.width() ? ?// 返回樣本維度二,對(duì)于圖像而言是寬度(常用)

?

2???? Layer:*.prototxt文件中各參數(shù)的說明

?

Data layer參數(shù)說明如下:

message ImageDataParameter {

? // 指定圖片數(shù)據(jù) txt 路徑.

? optional string source = 1;

?

// batch size.

? optional uint32 batch_size = 4 [default = 1];

?

// 隨機(jī)跳過部分?jǐn)?shù)據(jù)樣本,以避免所有同步 sgd 客戶端開始的樣本相同.

? // 其中,跳過的點(diǎn)設(shè)置為 rand_skip * rand(0,1).

? // rand_skip 小于等于數(shù)據(jù)集樣本數(shù).

? optional uint32 rand_skip = 7 [default = 0];

?

// 每個(gè) epoch 后打亂數(shù)據(jù)順序

? optional bool shuffle = 8 [default = false];

?

// resize 圖片到指定的 new_height 和 new_width 尺寸.

? optional uint32 new_height = 9 [default = 0];

? optional uint32 new_width = 10 [default = 0];

?

// 圖片是彩色還是灰度圖 color or gray

? optional bool is_color = 11 [default = true];

?

// DEPRECATED. See TransformationParameter.

? // 數(shù)據(jù)預(yù)處理時(shí),可以進(jìn)行簡(jiǎn)單的縮放(scale) 和減均值處理

? // 減均值是在縮放處理前進(jìn)行.

? optional float scale = 2 [default = 1];

? optional string mean_file: "mean.proto";

?

// DEPRECATED. See TransformationParameter.

? // 從圖片隨機(jī)裁剪.

// 剪裁一個(gè) 227*227的圖塊,在訓(xùn)練階段隨機(jī)剪裁,在測(cè)試階段從中間裁剪

? optional uint32 crop_size = 227 [default = 0];

?

// DEPRECATED. See TransformationParameter.

? // 隨機(jī)水平翻轉(zhuǎn).

? optional bool mirror = 6 [default = false];

? optional string root_folder = 12 [default = ""];

}

?

? window_data_param {

??? source: "window_data_train.txt"

??? batch_size: 128

??? crop_size: 256 # 要把bounding box warp到的大小

??? fg_threshold: 0.5 # 與ground truth 大于 fg_threshold 的bbox才作為正陽本

??? bg_threshold: 0.5 # 與ground truth 小于 bg_threshold 的bbox才作為正陽本

??? fg_fraction: 0.25 # 一個(gè)batch中正陽本數(shù)量的比例

??? crop_mode: "warp"

? }

?

負(fù)樣本的label是任意的, 但是overlap要小于threshold (絕對(duì)負(fù)樣本可以將overlap 設(shè)置為 0)

?

卷積層參數(shù)說明:

layer {

? name: "conv"

? type: "Convolution"

? bottom: "data"

? top: "conv"

? param {?

??? lr_mult: 1 #權(quán)重的學(xué)習(xí)率 該層lr=lr_mult*base_lr

??? decay_mult: 1 #權(quán)重的衰減值

? }

? param {?

??? lr_mult: 2 #偏置項(xiàng)的學(xué)習(xí)率

??? decay_mult: 0 #偏置項(xiàng)的衰減值

? }

? convolution_param {

??? num_output: 96 #該層輸出的filter的個(gè)數(shù)。四維數(shù)組N*C*H*W中的N

??? kernel_size: 11 #卷積核大小11*11。可設(shè)定長(zhǎng)kernel_h與寬kernel_w

??? stride: 4 #步長(zhǎng),也就是卷積核滑動(dòng)的距離

??? weight_filler { #卷積核初始化方式

????? type: "gaussian" #高斯分布

????? std: 0.01 #標(biāo)準(zhǔn)差為0.01

??? }

??? bias_filler { #偏置項(xiàng)初始化方式

????? type: "constant" #連續(xù)分布

????? value: 0

??? }

? }

}

?

3????? Caffe中幾個(gè)layer的區(qū)別:

Data對(duì)應(yīng)數(shù)據(jù)庫格式

WindowData對(duì)應(yīng)存儲(chǔ)在硬盤上的圖片

DummyData用于開發(fā)與調(diào)試

?

5????? caffe中的學(xué)習(xí)率的衰減機(jī)制

學(xué)習(xí)率更新,現(xiàn)在step exp 和multistep用的比較多。

根據(jù)? caffe/src/caffe/proto/caffe.proto 里的文件,可以看到它有以下幾種學(xué)習(xí)率的衰減速機(jī)制:

(1). fixed:?? 在訓(xùn)練過程中,學(xué)習(xí)率不變;

(2). step:? 它的學(xué)習(xí)率的變化就像臺(tái)價(jià)一樣;step by step 的; 其中的 gamma 與 stepsize需要設(shè)置的;

?

?

?

(3).exp :表示指數(shù)型的,其中參數(shù)?gamma?需要設(shè)置;

?

?

?

(4). inv :?其中參數(shù)?gamma?與?power?都需要設(shè)置;

?

?

?

(5).multistep: 可以設(shè)置多個(gè) stepvalue的值, 在prototxt里面也沒有具體介紹, 它由參數(shù) stepsize 與 stepvalue 決定; 它不是每時(shí)第刻都去計(jì)算 學(xué)習(xí)率,而是達(dá)到我們?cè)O(shè)定的stepvalue的時(shí)候,才去計(jì)算(根據(jù)方法2中的公式),然后更新學(xué)習(xí)率; stepvalue 可以設(shè)置多個(gè)的,下面是 stepvalue的定義;

183?? repeated int32 stepvalue = 34;

?

(6).poly :多項(xiàng)式衰減?,當(dāng)?shù)竭_(dá)最大次數(shù)時(shí), 學(xué)習(xí)率變?yōu)榱?;

?

?

?

(7).sigmoid形的:

?

?

?

6????? Caffe的solver.prototxt配置文件參數(shù)說明:

?

solver.prototxt文件是用來告訴caffe如何訓(xùn)練網(wǎng)絡(luò)的。solver.prototxt的各個(gè)參數(shù)的解釋如下:

?base_lr

這個(gè)參數(shù)是用來表示網(wǎng)絡(luò)的初始學(xué)習(xí)率的。這個(gè)值是一個(gè)浮點(diǎn)型實(shí)數(shù)。

lr_policy

這個(gè)參數(shù)是用來表示學(xué)習(xí)率隨著時(shí)間是如何變化的。值是字符串,需要加""。學(xué)習(xí)率變化的可選參數(shù)有:

“step”——需要設(shè)置stepsize。根據(jù)gamma參數(shù)和stepsize參數(shù)來降低學(xué)習(xí)率,base_lr * gamma ^ (floor(iter / stepsize))。iter是當(dāng)前迭代次數(shù)。學(xué)習(xí)率每迭代stepsize次變化一次。

“multistep”——與step類似,需要設(shè)置stepvalue,學(xué)習(xí)率根據(jù)stepvalue進(jìn)行變化。

“fixed”——學(xué)習(xí)率base_lr保持不變。

“inv”——學(xué)習(xí)率變化公式為base_lr * (1 + gamma * iter) ^ (- power)

“exp”——學(xué)習(xí)率變化公式為base_lr * gamma ^ iter}

“poly”——學(xué)習(xí)率以多項(xiàng)式形式衰減,到最大迭代次數(shù)時(shí)降為0。學(xué)習(xí)率變化公式為base_lr * (1 - iter/max_iter) ^ (power)。

“sigmoid”——學(xué)習(xí)率以S型曲線形式衰減,學(xué)習(xí)率變化公式為base_lr * (1 / (1 + exp(-gamma * (iter - stepsize))))。

gamma

這個(gè)參數(shù)表示學(xué)習(xí)率每次的變化程度,值為實(shí)數(shù)。

stepsize

這個(gè)參數(shù)表示什么時(shí)候應(yīng)該進(jìn)行訓(xùn)練的下一過程,值為正整數(shù)。主要用在lr_policy為step的情況。

stepvalue

這個(gè)參數(shù)表示什么時(shí)候應(yīng)該進(jìn)行訓(xùn)練的下一過程,值為正整數(shù)。主要用在lr_policy為multistep的情況。

max_iter

這個(gè)參數(shù)表示訓(xùn)練神經(jīng)網(wǎng)絡(luò)迭代的最大次數(shù),值為正整數(shù)。

momentum

這個(gè)參數(shù)表示在新的計(jì)算中要保留的前面的權(quán)重?cái)?shù)量,值為真分?jǐn)?shù),通常設(shè)為0.9。

weight_decay

這個(gè)參數(shù)表示對(duì)較大權(quán)重的懲罰(正則化)因子。值為真分?jǐn)?shù)。

This parameter indicates the factor of (regularization) penalization of large weights. This value is a often a real fraction.

solver_mode

這個(gè)參數(shù)用來表示求解神經(jīng)網(wǎng)絡(luò)的模式——值為CPU or GPU。

snapshot

這個(gè)參數(shù)用來表示每迭代多少次就應(yīng)該保存snapshot的model和solverstate,值為正整數(shù)。

snapshot_prefix:

這個(gè)參數(shù)用來表示保存snapshot時(shí)model和solverstate的前綴,值為帶引號(hào)的字符串。

net:

這個(gè)參數(shù)表示訓(xùn)練網(wǎng)絡(luò)所在的位置,值為帶引號(hào)的字符串。

test_iter

這個(gè)參數(shù)表示

這個(gè)參數(shù)表示每個(gè)test_interval進(jìn)行多少次test迭代,值為正整數(shù)。

test_interval

這個(gè)參數(shù)表示什么時(shí)候進(jìn)行數(shù)據(jù)的測(cè)試,值為正整數(shù)。

display

這個(gè)參數(shù)用來表示什么時(shí)候?qū)⑤敵鼋Y(jié)果打印到屏幕上,值為正整數(shù),表示迭代次數(shù)。

type

這個(gè)參數(shù)表示訓(xùn)練神經(jīng)網(wǎng)絡(luò)采用的反向傳播算法,值為帶引號(hào)的字符串。可選的值有:

Stochastic Gradient Descent “SGD”——隨機(jī)梯度下降,默認(rèn)值。

AdaDelta “AdaDelta”——一種”魯棒的學(xué)習(xí)率方法“,是基于梯度的優(yōu)化方法。

Adaptive Gradient “AdaGrad”——自適應(yīng)梯度方法。

Adam “Adam”——一種基于梯度的優(yōu)化方法。

Nesterov’s Accelerated Gradient “Nesterov”——Nesterov的加速梯度法,作為凸優(yōu)化中最理想的方法,其收斂速度非常快。

RMSprop “RMSProp”——一種基于梯度的優(yōu)化方法。

?

2. Demo

lr_policy

# lr_policy為multisetp

base_lr: 0.01

momentum: 0.9

lr_policy: "multistep"

gamma: 0.9

stepvalue: 1000

stepvalue: 2000

stepvalue: 3000

stepvalue: 4000

stepvalue: 5000

?

# lr_policy為step

base_lr: 0.01

momentum: 0.9

lr_policy: "step"

gamma: 0.9

stepsize: 1000

?

solver.prototxt

net: "models/bvlc_alexnet/train_val.prototxt"

# 每次測(cè)試時(shí)進(jìn)行1000次迭代

test_iter: 1000

# 每進(jìn)行1000次訓(xùn)練執(zhí)行一次測(cè)試

test_interval: 1000

base_lr: 0.01

lr_policy: "step"

gamma: 0.1

stepsize: 100000

display: 20

max_iter: 450000

momentum: 0.9

weight_decay: 0.0005

snapshot: 10000

snapshot_prefix: "models/bvlc_alexnet/caffe_alexnet_train"

solver_mode: GPU

?

?

參考資料

http://www.cnblogs.com/denny402/p/5074049.html

https://github.com/BVLC/caffe/wiki/Solver-Prototxt

http://stackoverflow.com/questions/30033096/what-is-lr-policy-in-caffe

?

7????? Caffe中的SSD:

SSD 300在conv4_3生成prior?box的conv4_3_norm_priorbox層prototxt定義如下:

  • layer?{??
  • ??name:?"conv4_3_norm_mbox_priorbox"??
  • ??type:?"PriorBox"??
  • ??bottom:?"conv4_3_norm"??
  • ??bottom:?"data"??
  • ??top:?"conv4_3_norm_mbox_priorbox"??
  • ??prior_box_param?{??
  • ????min_size:?30.0??
  • ????max_size:?60.0??
  • ????aspect_ratio:?2??
  • ????flip:?true??
  • ????clip:?false??
  • ????variance:?0.1??
  • ????variance:?0.1??
  • ????variance:?0.2??
  • ????variance:?0.2??
  • ????step:?8??
  • ????offset:?0.5??
  • ??}??
  • }??
  • 8????? 多GPU支持:

    盡量一個(gè)實(shí)驗(yàn)用一塊卡,caffe的多GPU并行做的不好。

    不要-gpu 0,1,2. 用一個(gè)gpu即可

    ?

    9????? Tmux用法

    (Ctrl+B) + (Shift+5) 打開一個(gè)新窗口

    (Ctrl+B) + right/left 在不同窗口之間切換

    (Ctrl+B) + [ 或 ] 進(jìn)入復(fù)制模式,查看歷史記錄

    tmux ls 列出當(dāng)前會(huì)話列表

    tmux attach 進(jìn)入會(huì)話窗口

    Ctrl+B+X 結(jié)束會(huì)話

    ?

    10? Caffe-SSD安裝配置記錄

    Github代碼地址如下:

    https://github.com/weiliu89/caffe/tree/ssd

    ?

    安裝步驟如下(github):

    Installation

  • Get the code. We will call the directory that you cloned Caffe into?$CAFFE_ROOT
  • Build the code. Please follow?Caffe instruction?to install all necessary packages and build it.
  • Download?fully convolutional reduced (atrous) VGGNet. By default, we assume the model is stored in?$CAFFE_ROOT/models/VGGNet/
  • Download VOC2007 and VOC2012 dataset. By default, we assume the data is stored in?$HOME/data/
  • Create the LMDB file.
  • git clone https://github.com/weiliu89/caffe.git cd caffe git checkout ssd # Modify Makefile.config according to your Caffe installation. cp Makefile.config.example Makefile.config make -j8 # Make sure to include $CAFFE_ROOT/python to your PYTHONPATH. make py make test -j8 # (Optional) make runtest -j8

    Preparation

    # Download the data. cd $HOME/data wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar # Extract the data. tar -xvf VOCtrainval_11-May-2012.tar tar -xvf VOCtrainval_06-Nov-2007.tar tar -xvf VOCtest_06-Nov-2007.tar cd $CAFFE_ROOT # Create the trainval.txt, test.txt, and test_name_size.txt in data/VOC0712/ ./data/VOC0712/create_list.sh # You can modify the parameters in create_data.sh if needed. # It will create lmdb files for trainval and test with encoded original image: #?? - $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_trainval_lmdb #?? - $HOME/data/VOCdevkit/VOC0712/lmdb/VOC0712_test_lmdb # and make soft links at examples/VOC0712/ ./data/VOC0712/create_data.sh

    Train/Eval

  • Train your model and evaluate the model on the fly.
  • # It will create model definition files and save snapshot models in: #?? - $CAFFE_ROOT/models/VGGNet/VOC0712/SSD_300x300/ # and job file, log file, and the python script in: #?? - $CAFFE_ROOT/jobs/VGGNet/VOC0712/SSD_300x300/ # and save temporary evaluation results in: #?? - $HOME/data/VOCdevkit/results/VOC2007/SSD_300x300/ # It should reach 77.* mAP at 120k iterations. python examples/ssd/ssd_pascal.py

    If you don't have time to train your model, you can download a pre-trained model at?here.

  • Evaluate the most recent snapshot.
  • Test your model using a webcam. Note: press?esc?to stop.
  • # If you would like to test a model you trained, you can do: python examples/ssd/score_ssd_pascal.py # If you would like to attach a webcam to a model you trained, you can do: python examples/ssd/ssd_pascal_webcam.py

    Here?is a demo video of running a SSD500 model trained on?MSCOCO?dataset.

  • Check out?examples/ssd_detect.ipynb?or?examples/ssd/ssd_detect.cpp?on how to detect objects using a SSD model. Check out?examples/ssd/plot_detections.py?on how to plot detection results output by ssd_detect.cpp.
  • To train on other dataset, please refer to data/OTHERDATASET for more details. We currently add support for COCO and ILSVRC2016. We recommend using?examples/ssd.ipynb?to check whether the new dataset is prepared correctly.
  • Models

    We have provided the latest models that are trained from different datasets. To help reproduce the results in?Table 6, most models contain a pretrained?.caffemodel?file, many?.prototxt?files, and python scripts.

  • PASCAL VOC models:
    • 07+12:?SSD300*,?SSD512*
    • 07++12:?SSD300*,?SSD512*
    • COCO[1]:?SSD300*,?SSD512*
    • 07+12+COCO:?SSD300*,?SSD512*
    • 07++12+COCO:?SSD300*,?SSD512*
  • COCO models:
  • ILSVRC models:
    • trainval35k:?SSD300*,?SSD512*
    • trainval1:?SSD300*,?SSD500

    [1]We use?examples/convert_model.ipynb?to extract a VOC model from a pretrained COCO model.

    ?

    ?

    13? Mean_file的生成:

    sudo build/tools/compute_image_mean examples/mnist/mnist_train_lmdb examples/mnist/mean.binaryproto

    ?

    帶兩個(gè)參數(shù):

    第一個(gè)參數(shù):examples/mnist/mnist_train_lmdb, 表示需要計(jì)算均值的數(shù)據(jù),格式為lmdb的訓(xùn)練數(shù)據(jù)。

    第二個(gè)參數(shù):examples/mnist/mean.binaryproto, 計(jì)算出來的結(jié)果保存文件。

    ?

    14? ssd_detect.py的參數(shù)

    def parse_args():

    ??? '''parse args'''

    ??? parser = argparse.ArgumentParser()

    ??? parser.add_argument('--gpu_id', type=int, default=0, help='gpu id')

    ??? parser.add_argument('--labelmap_file',

    ??????????????????????? default='data/VOC0712/labelmap_voc.prototxt')

    ??? parser.add_argument('--model_def',

    ??????????????????????? default='models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt')

    ??? parser.add_argument('--image_resize', default=300, type=int)

    ??? parser.add_argument('--model_weights',

    ??????????????????????? default='models/VGGNet/VOC0712/SSD_300x300/'

    ??????????????????????? 'VGG_VOC0712_SSD_300x300_iter_120000.caffemodel')

    ??? parser.add_argument('--image_file', default='examples/images/fish-bike.jpg')

    ??? return parser.parse_args()

    ?

    15? plot_detections.py的參數(shù)

    if __name__ == "__main__":

    ??? parser = argparse.ArgumentParser(

    ??????????? description = "Plot the detection results output by ssd_detect.")

    ??? parser.add_argument("resultfile",

    ??????????? help = "A file which contains all the detection results.")

    ??? parser.add_argument("imgdir",

    ??????????? help = "A directory which contains the images.")

    ??? parser.add_argument("--labelmap-file", default="",

    ??????????? help = "A file which contains the LabelMap.")

    ??? parser.add_argument("--visualize-threshold", default=0.01, type=float,

    ??????????? help = "Display detections with score higher than the threshold.")

    ??? parser.add_argument("--save-dir", default="",

    ??????????? help = "A directory which saves the image with detection results.")

    ??? parser.add_argument("--display-classes", default=None,

    ??????????? help = "If provided, only display specified class. Separate by ','")

    ?

    16? ssd_detect.bin(及ssd_detect.cpp)的參數(shù)

    Usage:

    ??? ssd_detect [FLAGS] model_file weights_file list_file

    ?

    ?

    ? Flags from examples/ssd/ssd_detect.cpp:

    ??? -confidence_threshold (Only store detections with score higher than the

    ????? threshold.) type: double default: 0.01

    ??? -file_type (The file type in the list_file. Currently support image and

    ????? video.) type: string default: "image"

    ??? -mean_file (The mean file used to subtract from the input image.)

    ????? type: string default: ""

    ??? -mean_value (If specified, can be one value or can be same as image

    ????? channels - would subtract from the corresponding channel). Separated by

    ????? ','.Either mean_file or mean_value should be provided, not both.)

    ????? type: string default: "104,117,123"

    ??? -out_file (If provided, store the detection results in the out_file.)

    ????? type: string default: ""

    ?

    17? 生成檢測(cè)框并繪圖

    Step 1 使用訓(xùn)練好的模型檢測(cè)圖片:

    build/examples/ssd/ssd_detect.bin models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt /home/daisida/ssd/models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel /home/daisida/ssd/list.txt? --out_file tmp/output.txt

    ?

    參數(shù)說明:

    ssd_detect.bin:是ssd_detect.cpp的編譯文件,類似于caffe中的classifier.bin,主要用于ssd訓(xùn)練模型的檢測(cè),相當(dāng)main函數(shù)。

    deploy.prototxt:網(wǎng)絡(luò)結(jié)構(gòu)參數(shù)

    VGG_VOC0712_SSD_300×300_iter_120000.caffemodel:訓(xùn)練模型

    test.txt:測(cè)試數(shù)據(jù),可以是圖片,可以是視頻,每一行為圖片或視頻的路徑

    --file_type:文件類型,圖片用image,視頻用video

    --out_file:檢測(cè)結(jié)果輸出文件

    --condidence_threshold:閾值參數(shù),這里設(shè)置為0.5,參數(shù)范圍(0,1)

    ?

    Step 2 結(jié)果可視化:

    保存結(jié)果前需要將output.txt中的路徑進(jìn)行修改,去掉每行前邊的路徑,避免重復(fù)。

    python examples/ssd/plot_detections.py tmp/output.txt /home/daisida/data/testset --labelmap-file data/VOC0712/labelmap_voc.prototxt --save-dir tmp/

    ?

    ?

    ?

    報(bào)錯(cuò):

    /home/daisida/data/testset///home/daisida/data/testset/3.jpg does not exist

    /home/daisida/data/testset///home/daisida/data/testset/9.jpg does not exist

    /home/daisida/data/testset///home/daisida/data/testset/7.jpg does not exist

    /home/daisida/data/testset///home/daisida/data/testset/2.jpg does not exist

    /home/daisida/data/testset///home/daisida/data/testset/8.jpg does not exist

    ?

    解決方案:

    修改output.txt文件,去掉每行前邊的路徑,避免重復(fù)。

    ?

    18? protobuf協(xié)議

    定義一個(gè)消息類型

    ?

    假設(shè)你想定義一個(gè)“搜索請(qǐng)求”的消息格式,每一個(gè)請(qǐng)求含有一個(gè)查詢字符串、你感興趣的查詢結(jié)果所在的頁數(shù),以及每一頁多少條查詢結(jié)果。可以采用如下的方式來定義消息類型的.proto文件了:

    message SearchRequest {

    ??required string query = 1;

    ??optional int32 page_number = 2;

    ??optional int32 result_per_page = 3;

    }

    ?

    SearchRequest消息格式有3個(gè)字段,在消息中承載的數(shù)據(jù)分別對(duì)應(yīng)于每一個(gè)字段。其中每個(gè)字段都有一個(gè)名字和一種類型。

    ?

    指定字段類型

    ?

    在上面的例子中,所有字段都是標(biāo)量類型:兩個(gè)整型(page_number和result_per_page),一個(gè)string類型(query)。當(dāng)然,你也可以為字段指定其他的合成類型,包括枚舉(enumerations)或其他消息類型。

    ?

    分配標(biāo)識(shí)號(hào)

    ?

    正如上述文件格式,在消息定義中,每個(gè)字段都有唯一的一個(gè)標(biāo)識(shí)符。這些標(biāo)識(shí)符是用來在消息的二進(jìn)制格式中識(shí)別各個(gè)字段的,一旦開始使用就不能夠再改變。注:[1,15]之內(nèi)的標(biāo)識(shí)號(hào)在編碼的時(shí)候會(huì)占用一個(gè)字節(jié)。[16,2047]之內(nèi)的標(biāo)識(shí)號(hào)則占用2個(gè)字節(jié)。所以應(yīng)該為那些頻繁出現(xiàn)的消息元素保留[1,15]之內(nèi)的標(biāo)識(shí)號(hào)。切記:要為將來有可能添加的、頻繁出現(xiàn)的標(biāo)識(shí)號(hào)預(yù)留一些標(biāo)識(shí)號(hào)。

    ?

    最小的標(biāo)識(shí)號(hào)可以從1開始,最大到229?- 1, or 536,870,911。不可以使用其中的[19000-19999]的標(biāo)識(shí)號(hào), Protobuf協(xié)議實(shí)現(xiàn)中對(duì)這些進(jìn)行了預(yù)留。如果非要在.proto文件中使用這些預(yù)留標(biāo)識(shí)號(hào),編譯時(shí)就會(huì)報(bào)警。

    ?

    指定字段規(guī)則

    ?

    所指定的消息字段修飾符必須是如下之一:

    required:一個(gè)格式良好的消息一定要含有1個(gè)這種字段。表示該值是必須要設(shè)置的;

    optional:消息格式中該字段可以有0個(gè)或1個(gè)值(不超過1個(gè))。

    repeated:在一個(gè)格式良好的消息中,這種字段可以重復(fù)任意多次(包括0次)。重復(fù)的值的順序會(huì)被保留。表示該值可以重復(fù),相當(dāng)于java中的List。

    ?

    由于一些歷史原因,基本數(shù)值類型的repeated的字段并沒有被盡可能地高效編碼。在新的代碼中,用戶應(yīng)該使用特殊選項(xiàng)[packed=true]來保證更高效的編碼。如:

    repeated int32 samples = 4 [packed=true];

    ?

    required是永久性的:在將一個(gè)字段標(biāo)識(shí)為required的時(shí)候,應(yīng)該特別小心。如果在某些情況下不想寫入或者發(fā)送一個(gè)required的字段,將原始該字段修飾符更改為optional可能會(huì)遇到問題——舊版本的使用者會(huì)認(rèn)為不含該字段的消息是不完整的,從而可能會(huì)無目的的拒絕解析。在這種情況下,你應(yīng)該考慮編寫特別針對(duì)于應(yīng)用程序的、自定義的消息校驗(yàn)函數(shù)。Google的一些工程師得出了一個(gè)結(jié)論:使用required弊多于利;他們更愿意使用optional和repeated而不是required。當(dāng)然,這個(gè)觀點(diǎn)并不具有普遍性。

    ?

    19? Caffe擴(kuò)展層——ROIPooling的caffe實(shí)現(xiàn)

    https://blog.csdn.net/jiongnima/article/details/80016683

    ?

    20? 多版本Python共存時(shí)的啟動(dòng)方式:

    python2 *.py??? #啟動(dòng)Python2

    python *.py????? #啟動(dòng)Python3

    sudo python???? #啟動(dòng)系統(tǒng)Python,不加sudo,則啟動(dòng)用戶bashrc中的Python

    ?

    21? OpenCV的visualstudio2015(VC14)配置過程

    ?

    (1)?????? 下載并解壓

    (2)?????? 配置環(huán)境變量:

    D:\Program Files\opencv\build\x64\vc14\bin

    (3)?????? 新建控制臺(tái)應(yīng)用程序——空項(xiàng)目

    (4)?????? 選擇編譯模式為x64:項(xiàng)目右鍵——屬性——連接器——高級(jí)——目標(biāo)計(jì)算機(jī):Machine64——右上角配置管理器——平臺(tái):新建——win32和x64

    (5)?????? 接下來有兩種方案,(64位系統(tǒng)要選擇debug模式為x64)一是此項(xiàng)目上右鍵屬性,則只改變當(dāng)前項(xiàng)目,二是視圖——其他窗口——屬性管理器——修改全局配置

    (6)?????? 工程上右鍵屬性——包含目錄:

    D:\Program Files\opencv\build\include\opencv2

    D:\Program Files\opencv\build\include\opencv

    D:\Program Files\opencv\build\include

    (7)?????? 庫目錄:

    D:\Program Files\opencv\build\x64\vc14\lib

    (8)?????? 鏈接器——輸入——附加依賴項(xiàng):

    opencv_world345d.lib

    opencv_world345.lib?

    (9)?????? 測(cè)試代碼:

    #include<iostream>

    #include <opencv2/core/core.hpp>

    #include <opencv2/highgui/highgui.hpp>

    ?

    using namespace cv;

    ?

    int main()

    {

    ??? // 讀入一張圖片(游戲原畫)

    ??? Mat img = imread("pic.jpg");

    ??? // 創(chuàng)建一個(gè)名為 "游戲原畫"窗口

    ??? namedWindow("游戲原畫");

    ??? // 在窗口中顯示游戲原畫

    ??? imshow("游戲原畫", img);

    ??? // 等待6000 ms后窗口自動(dòng)關(guān)閉

    ??? waitKey(6000);

    }

    ?

    22? 更新服務(wù)器CUDA版本

    1. 刪除所有服務(wù)器上的/home/public/local/cuda-7.5文件夾,只保留8.0文件夾,如果這臺(tái)服務(wù)器已經(jīng)安裝過7.5的驅(qū)動(dòng),需要先卸載driver

    卸載方法:

    cd /home/public/local/cuda-7.5/bin

    sudo ./uninstall_cuda_7.5.pl

    (新安裝機(jī)器只裝了cuda8.0的直接刪了cuda-7.5文件夾就可以)

    2. 修改/home/bashrc文件里面的cuda路徑

    3. 修改自己的Makefile.config里面的cuda路徑

    4. 把10.90.41.188上/home/public/local/cuda-8.0.tar 和OpenCV-3.0-cuda8.0.tar 拷貝到每臺(tái)機(jī)器上的/home/public/local下,并解壓(沒有顯卡的機(jī)器也拷貝cuda8.0.tar,方便以后安裝)

    5. 把10.90.41.188上/home/public/local/anaconda2/lib/python2.7/site-packages/cv2.so 拷貝到對(duì)應(yīng)機(jī)器上對(duì)應(yīng)路徑/home/public/local/anaconda2/lib/python2.7/site-packages/

    ?

    重新make caffe和OpenCV

    ?

    ?

    測(cè)試:

    對(duì)于有顯卡的機(jī)器,測(cè)試在python中import cv2

    ?

    Boost 的修改過程:

    為運(yùn)行caffe,cd /usr/local/lib

    將libboost_thread.so -> libboost_thread.so.1.68.0重命名為libboost_thread.so_bak

    新建libboost_thread.so -> libboost_thread.so.1.66.0

    ?

    23? OUT_OF_MEMORY

    ps -aux | grep python | grep -v grep | awk '{print $2}' | xargs kill -9

    ?

    24? Importing caffe results in ImportError: “No module named google.protobuf.internal” (import enum_type_wrapper)

    解決方案:

    pip install protobuf

    ?

    25? 每次運(yùn)行代碼之前需要考慮修改的地方

    Train階段:

    (1)?????? train.sh中的solver.prototxt路徑

    (2)?????? train.sh中的caffemodel路徑

    (3)?????? train.sh中的日志名稱

    (4)?????? solver.prototxt中的net名稱(也就是train.prototxt路徑)

    (5)?????? solver.prototxt中的snapshot_prefix名稱(包括路徑+文件名)

    (6)?????? train.prototxt中的data層(有兩個(gè),TRAIN和TEST)輸入文件的路徑,即list.txt或者lmdb的路徑

    Test階段:

    (1)?????? test.py腳本中的caffemodel改成訓(xùn)練好的caffemodel

    (2)?????? test.py中的prototxt改成deploy.prototxt

    (3)?????? test.py中的labelfile(即list.txt文件的路徑)

    ?

    26? 從train.prototxt生成deploy.prototxt需要修改的地方

    (1)?????? 去掉輸入層(有兩個(gè),phase=TRAIN或TEST),換成Input層:

    layer {

    ? name: "data_exp"

    ? type: "ImageData"

    ? top: "data_exp"

    ? top: "label_exp"

    ? include {

    ??? phase: TRAIN

    ? }

    ? transform_param {

    ??? mirror: true

    ??? mean_value: 104

    ??? mean_value: 117

    ??? mean_value: 123

    ? }

    ? image_data_param {

    ??? source: "list.txt"

    ??? batch_size: 32

    ??? shuffle: true

    ? }

    }

    改成:

    layer {

    ? name: "data"

    ? type: "Input"

    ? top: "data"

    ? input_param { shape: { dim: 1 dim: 3 dim: 144? dim: 144 } }

    }

    ?

    (2)?????? 第一個(gè)卷積層的bottom變量要和輸入層一致

    (3)?????? Loss層:從SoftmaxWithLoss改成Softmax

    layer {

    ? name: "loss_expression"

    ? type: "SoftmaxWithLoss"

    ? bottom: "pool10_expression"

    ? bottom: "label_exp"

    ? top: "loss_exp"

    ? loss_weight: 1

    ? include {

    ??? phase: TRAIN

    ? }

    }

    改成:

    layer {

    ? name: "prob_expression"

    ? type: "Softmax"

    ? bottom: "pool10_expression"

    ? top: "prob_expression"

    }

    (4)?????? 去掉Accuracy層

    (5)?????? 對(duì)于多任務(wù)情況,還要去掉concat層(deploy階段每次test只輸入一張圖片,同理也要去掉slice)

    27? Caffe多任務(wù)訓(xùn)練

    添加concat層以及slice層。

    Concat用于拼接不同數(shù)據(jù)源的data,concat之后的數(shù)據(jù)經(jīng)過網(wǎng)絡(luò)傳播,由slice層將feature分開,分別用于不同人物的訓(xùn)練,生成不同的loss。Slice_point就是batchsize。

    ?

    train.prototxt需要修改的位置:

    (1)?????? 有幾項(xiàng)任務(wù),就要有幾份輸入數(shù)據(jù)(每項(xiàng)任務(wù)都要有各自的TRAIN和TEST數(shù)據(jù)輸入層);同時(shí)注意batchsized的大小;

    (2)?????? 修改Concat層如下(TRAIN和TEST各有一個(gè))

    layer {

    ? name: "concat"

    ? bottom: "data_blink"

    ? bottom: "data_obstacle"

    ? bottom: "data_blur"

    ? top: "data"

    ? type: "Concat"

    ? include {

    ??? phase: TRAIN

    ? }

    ? concat_param {

    ??? axis: 0

    ? }

    }

    其中bottom是三項(xiàng)任務(wù)各自的輸入數(shù)據(jù)層。

    (3)?????? 修改slice層(TRAIN和TEST各有一個(gè)):

    layer {

    ? name: "slice"

    ? type: "Slice"

    ? bottom: "fire9/concat"

    ? top: "feature_blink"

    ? top: "feature_obstacle"

    ? top: "feature_blur"

    ? include {

    ??? phase: TRAIN

    ? }

    ? slice_param {

    ??? axis: 0

    ??? slice_point: 128

    ??? slice_point: 256

    ? }

    }

    (4)?????? Slice層之后接不同任務(wù)的輸出層:

    Eg. Conv、relu、pooling、SoftmaxWithLoss、accuracy_smile

    (5)?????? 修改deploy.prototxt:

    轉(zhuǎn)載于:https://www.cnblogs.com/sddai/p/10613478.html

    總結(jié)

    以上是生活随笔為你收集整理的Caffe-SSD相关源码说明和调试记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。