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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

Windows下Caffe的学习与应用(二)——优化自己训练的模型(fine-tuning)

發(fā)布時(shí)間:2025/3/21 windows 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows下Caffe的学习与应用(二)——优化自己训练的模型(fine-tuning) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

在前面的博文中,我們也看到,在進(jìn)行訓(xùn)練的樣本比較少,每個(gè)種類的樣本只有120張,迭代次數(shù)為100代,訓(xùn)練得到的模型在測(cè)試那些特征不那么明顯圖像時(shí),準(zhǔn)確率達(dá)不到我們想要的效果。如果下圖:


測(cè)試的結(jié)果如下:


這個(gè)測(cè)試結(jié)果是不準(zhǔn)確的,像這種概率是根本無(wú)法在項(xiàng)目中應(yīng)用。一般深度學(xué)習(xí)的庫(kù)都要求樣本量盡量在在1萬(wàn)個(gè)以上,但有時(shí)候我們的樣本又達(dá)不到這個(gè)數(shù)量,什么辦呢,這里就要用到別的用戶的訓(xùn)練好的成熟的數(shù)據(jù)模型進(jìn)行微調(diào)(fine-tuning),借用訓(xùn)練好的參數(shù),可以更快的收斂,達(dá)到比原來(lái)樣本小的時(shí)候訓(xùn)練效果要好一些。但使用的網(wǎng)絡(luò)必須一樣。
圖像分類這塊,caffe團(tuán)隊(duì)用ImageNet進(jìn)行訓(xùn)練,迭代30多萬(wàn)次,訓(xùn)練出來(lái)一個(gè)model。這個(gè)model將圖片分為1000類,應(yīng)該是目前為止最好的圖片分類model了,這里就會(huì)用caffe的官方模型來(lái)進(jìn)行遷移學(xué)習(xí)。

一、數(shù)據(jù)準(zhǔn)備

在data這個(gè)文件夾下新建立一個(gè),把上次轉(zhuǎn)換的數(shù)據(jù),均值文件文件都移到到這個(gè)文件夾,然后把caffe的圖像分類官方模型下載到當(dāng)前文件夾,下載地址是:http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel。
當(dāng)前文件夾所包含的文件如下圖:

所有包含的文件,不能少,如果沒(méi)有這些文件,可以往回看我之前寫的博文,如何生成這幾個(gè)文件的。
2.數(shù)據(jù)處理

二、更改代碼

修改配置三個(gè)文件的,這里我貼出我的文件,路徑是我電腦上的,可能參考,是于參數(shù)的說(shuō)明,可能看上一個(gè)博文。
1.修改solever.prototxt文件

net: "E:/LIB/caffe-windows/data/fine_tuning/train_val.prototxt" test_iter: 4 test_interval: 8 base_lr: 0.001 lr_policy: "step" gamma: 0.1 stepsize: 160 display: 20 max_iter: 800 momentum: 0.9 weight_decay: 0.0005 snapshot: 5000 snapshot_prefix: "E:/LIB/caffe-windows/data/fine_tuning/train" solver_mode: CPU

2.train_val.prototxt文件

name: "CaffeNet" layer {name: "data"type: "Data"top: "data"top: "label"include {phase: TRAIN}transform_param {mirror: truecrop_size: 227mean_file: "data/fine_tuning/train_mean.binaryproto" #均值文件路徑} # mean pixel / channel-wise mean instead of mean image # transform_param { # crop_size: 227 # mean_value: 104 # mean_value: 117 # mean_value: 123 # mirror: true # }data_param {source: "data/fine_tuning/train_leveldb" #轉(zhuǎn)換文件路徑batch_size: 60backend: LEVELDB #數(shù)據(jù)類型} } layer {name: "data"type: "Data"top: "data"top: "label"include {phase: TEST}transform_param {mirror: falsecrop_size: 227mean_file: "data/fine_tuning/test_mean.binaryproto" #均值文件路徑} # mean pixel / channel-wise mean instead of mean image # transform_param { # crop_size: 227 # mean_value: 104 # mean_value: 117 # mean_value: 123 # mirror: false # }data_param {source: "data/fine_tuning/test_leveldb" #轉(zhuǎn)換文件路徑batch_size: 25backend: LEVELDB} } layer {name: "conv1"type: "Convolution"bottom: "data"top: "conv1"param {lr_mult: 1decay_mult: 1}param {lr_mult: 2decay_mult: 0}convolution_param {num_output: 96kernel_size: 11stride: 4weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}} } layer {name: "relu1"type: "ReLU"bottom: "conv1"top: "conv1" } layer {name: "pool1"type: "Pooling"bottom: "conv1"top: "pool1"pooling_param {pool: MAXkernel_size: 3stride: 2} } layer {name: "norm1"type: "LRN"bottom: "pool1"top: "norm1"lrn_param {local_size: 5alpha: 0.0001beta: 0.75} } layer {name: "conv2"type: "Convolution"bottom: "norm1"top: "conv2"param {lr_mult: 1decay_mult: 1}param {lr_mult: 2decay_mult: 0}convolution_param {num_output: 256pad: 2kernel_size: 5group: 2weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 1}} } layer {name: "relu2"type: "ReLU"bottom: "conv2"top: "conv2" } layer {name: "pool2"type: "Pooling"bottom: "conv2"top: "pool2"pooling_param {pool: MAXkernel_size: 3stride: 2} } layer {name: "norm2"type: "LRN"bottom: "pool2"top: "norm2"lrn_param {local_size: 5alpha: 0.0001beta: 0.75} } layer {name: "conv3"type: "Convolution"bottom: "norm2"top: "conv3"param {lr_mult: 1decay_mult: 1}param {lr_mult: 2decay_mult: 0}convolution_param {num_output: 384pad: 1kernel_size: 3weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}} } layer {name: "relu3"type: "ReLU"bottom: "conv3"top: "conv3" } layer {name: "conv4"type: "Convolution"bottom: "conv3"top: "conv4"param {lr_mult: 1decay_mult: 1}param {lr_mult: 2decay_mult: 0}convolution_param {num_output: 384pad: 1kernel_size: 3group: 2weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 1}} } layer {name: "relu4"type: "ReLU"bottom: "conv4"top: "conv4" } layer {name: "conv5"type: "Convolution"bottom: "conv4"top: "conv5"param {lr_mult: 1decay_mult: 1}param {lr_mult: 2decay_mult: 0}convolution_param {num_output: 256pad: 1kernel_size: 3group: 2weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 1}} } layer {name: "relu5"type: "ReLU"bottom: "conv5"top: "conv5" } layer {name: "pool5"type: "Pooling"bottom: "conv5"top: "pool5"pooling_param {pool: MAXkernel_size: 3stride: 2} } layer {name: "fc6"type: "InnerProduct"bottom: "pool5"top: "fc6"param {lr_mult: 1decay_mult: 1}param {lr_mult: 2decay_mult: 0}inner_product_param {num_output: 4096weight_filler {type: "gaussian"std: 0.005}bias_filler {type: "constant"value: 1}} } layer {name: "relu6"type: "ReLU"bottom: "fc6"top: "fc6" } layer {name: "drop6"type: "Dropout"bottom: "fc6"top: "fc6"dropout_param {dropout_ratio: 0.5} } layer {name: "fc7"type: "InnerProduct"bottom: "fc6"top: "fc7"param {lr_mult: 1decay_mult: 1}param {lr_mult: 2decay_mult: 0}inner_product_param {num_output: 4096weight_filler {type: "gaussian"std: 0.005}bias_filler {type: "constant"value: 1}} } layer {name: "relu7"type: "ReLU"bottom: "fc7"top: "fc7" } layer {name: "drop7"type: "Dropout"bottom: "fc7"top: "fc7"dropout_param {dropout_ratio: 0.5} } layer {name: "fc8-re" #要改的位置type: "InnerProduct"bottom: "fc7"top: "fc8"param {lr_mult: 1decay_mult: 1}param {lr_mult: 2decay_mult: 0}inner_product_param {num_output: 4 #訓(xùn)練的種類weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"value: 0}} } layer {name: "accuracy"type: "Accuracy"bottom: "fc8"bottom: "label"top: "accuracy"include {phase: TEST} } layer {name: "loss"type: "SoftmaxWithLoss"bottom: "fc8"bottom: "label"top: "loss" }

3.deploy.prototxt文件

name: "CaffeNet" layer {name: "data"type: "Input"top: "data"input_param { shape: { dim: 10 #一批的數(shù)量(bach of image)dim: 3 #圖像通道數(shù)量(channels 彩色圖是3通道RGB)dim: 227 #圖像的高度dim: 227 #圖像的寬度} } } layer {name: "conv1" #層的名稱type: "Convolution" #層的類型bottom: "data" #層的輸入(對(duì)應(yīng)上面的data)top: "conv1" #層的輸出(對(duì)應(yīng)的是本層卷積的結(jié)果)convolution_param { #卷積的參數(shù)num_output: 96 #過(guò)濾器的個(gè)數(shù),可以看做的是卷積和的個(gè)數(shù)吧kernel_size: 11 #卷積核的大小stride: 4 #圖像中的間隔多少進(jìn)行卷積(一次窗口滑動(dòng)的步長(zhǎng))} } layer {name: "relu1"type: "ReLU"bottom: "conv1"top: "conv1" } layer {name: "pool1"type: "Pooling"bottom: "conv1"top: "pool1"pooling_param {pool: MAX #使用最大池化kernel_size: 3stride: 2} } layer {name: "norm1"type: "LRN"bottom: "pool1"top: "norm1"lrn_param {local_size: 5alpha: 0.0001beta: 0.75} } layer {name: "conv2"type: "Convolution"bottom: "norm1"top: "conv2"convolution_param {num_output: 256pad: 2 #邊界處補(bǔ)2個(gè)行和2個(gè)列kernel_size: 5group: 2 #卷積分組} } layer {name: "relu2"type: "ReLU" #激活函數(shù)bottom: "conv2"top: "conv2" } layer {name: "pool2"type: "Pooling"bottom: "conv2"top: "pool2"pooling_param {pool: MAXkernel_size: 3stride: 2} } layer {name: "norm2"type: "LRN" #側(cè)抑制bottom: "pool2"top: "norm2"lrn_param { #主要是LRN的三個(gè)主要的參數(shù)local_size: 5alpha: 0.0001beta: 0.75} } layer {name: "conv3"type: "Convolution"bottom: "norm2"top: "conv3"convolution_param {num_output: 384pad: 1kernel_size: 3} } layer {name: "relu3"type: "ReLU"bottom: "conv3"top: "conv3" } layer {name: "conv4"type: "Convolution"bottom: "conv3"top: "conv4"convolution_param {num_output: 384pad: 1kernel_size: 3group: 2} } layer {name: "relu4"type: "ReLU"bottom: "conv4"top: "conv4" } layer {name: "conv5"type: "Convolution"bottom: "conv4"top: "conv5"convolution_param {num_output: 256pad: 1 #對(duì)圖像進(jìn)行補(bǔ)充像素的設(shè)置(在圖像的高和寬進(jìn)行補(bǔ)充)kernel_size: 3group: 2} } layer {name: "relu5"type: "ReLU"bottom: "conv5"top: "conv5" } layer {name: "pool5"type: "Pooling"bottom: "conv5"top: "pool5"pooling_param {pool: MAXkernel_size: 3stride: 2} } layer {name: "fc6"type: "InnerProduct"bottom: "pool5"top: "fc6"inner_product_param {num_output: 4096} } layer {name: "relu6"type: "ReLU"bottom: "fc6"top: "fc6" } layer {name: "drop6"type: "Dropout"bottom: "fc6"top: "fc6"dropout_param {dropout_ratio: 0.5 #使用的drop進(jìn)行網(wǎng)絡(luò)的參數(shù)的隱藏時(shí)的參數(shù)} } layer {name: "fc7"type: "InnerProduct"bottom: "fc6"top: "fc7"inner_product_param {num_output: 4096 #過(guò)濾器的個(gè)數(shù)(輸出的個(gè)數(shù))} } layer {name: "relu7"type: "ReLU" #relu的激活函數(shù)bottom: "fc7"top: "fc7" } layer {name: "drop7"type: "Dropout" #dropout將一部分的權(quán)重置零不參與運(yùn)算bottom: "fc7"top: "fc7"dropout_param {dropout_ratio: 0.5} } layer {name: "fc8-re" #要改的位置type: "InnerProduct" #內(nèi)積(全連接層)bottom: "fc7"top: "fc8"inner_product_param {num_output: 4 #輸出的類別(要改成對(duì)應(yīng)的)} } layer {name: "prob"type: "Softmax" #Softmax分類層bottom: "fc8"top: "prob" }

三、開始訓(xùn)練

1.編寫腳本train.bat

cd ../../ E:/LIB/caffe-windows/build/tools/Release/caffe.exe train --solver=data/fine_tuning/solver.prototxt --weights=data/fine_tuning/bvlc_reference_caffenet.caffemodel pause

2.運(yùn)行腳本

完成之后會(huì)多出兩個(gè)文件,如下,代表訓(xùn)練成功。

四、測(cè)試數(shù)據(jù)

1.新建labels.txt文件,寫上以下內(nèi)容:

0 cat 1 dog 2 flower 3 cartoon

2.新建data_test.bat文件

E:\LIB\caffe-windows\build\examples\cpp_classification\Release\classification.exe ..\..\data\fine_tuning\deploy.prototxt ..\..\data\fine_tuning\train_iter_800.caffemodel ..\..\data\fine_tuning\test_mean.binaryproto ..\..\data\fine_tuning\labels.txt ..\..\data\fine_tuning\test\5127.jpg pause

保存,點(diǎn)擊運(yùn)行,還是測(cè)試文章開頭的那張圖像,我們來(lái)看看測(cè)試結(jié)果明顯提高了.

后記

1.以上所有的圖像分類的訓(xùn)練已經(jīng)完成,之后就是如何在在項(xiàng)目中使用訓(xùn)練好的模型。
2.有興趣討論學(xué)習(xí)可以加群:487350510。

總結(jié)

以上是生活随笔為你收集整理的Windows下Caffe的学习与应用(二)——优化自己训练的模型(fine-tuning)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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