最近剛接觸caffe弄了一個caffe多標籤遇到各種蛋疼的問題跟大家分享分享。
一 準備資料這裡用的驗證碼0-9+26個字母字母產生4位元的驗證碼
二 修改caffe源碼涉及到修改的檔案有
caffe.proto ,
convert_imageset.cpp,
data_layer.cpp,
io.cpp,
data_layer.hpp,
io.hpp
具體修改就不介紹了去下面地址下載修改後的檔案然後替換掉原有caffe中的
檔案下載地址:https://pan.baidu.com/s/1eSP1RUi
這裡我們理解一下,caffe原本不支援多標籤分類任務,這裡的主要修改是為了是的caffe支援多標籤分類,我們知道caffe的label原版指定的是整數型且只有1個,看caffe.proto裡面寫的:Datum就是我們的資料層,那麼資料層的label是int32,這就限制了資料label的輸入必須是一個整數,那麼修改他的起點就是從proto裡面開始,加一個labels,數群組類型
修改完瞭然後接著修改caffe使用Datum部分代碼,實現對labels的支援
最後修改convert_imageset.cpp,讓他實現對例如如下:
imgs/abc.jpg 1 2 3 4 5
這種類型的多標籤做支援
最終完成這次修改,再編譯一遍就好了
三 製作資料標籤
圖片路徑 + 對應的標籤 samples/MYL1.bmp 22 34 21 1
四 寫個多分類網路
結構如下:
name:"LeNet" layer{ name:"mnist" type:"Data" top:"data" top:"label" include{ phase: TRAIN } transform_param{ scale:0.003921568627451 } data_param{ source:"train_lmdb" batch_size:64 backend: LMDB } } layer{ name:"mnist" type:"Data" top:"data" top:"label" include{ phase: TEST } transform_param{ scale:0.003921568627451 } data_param{ source:"val_lmdb" batch_size:64 backend: LMDB } } layer{ name:"conv1" type:"Convolution" bottom:"data" top:"conv1" param{ lr_mult:1 } param{ lr_mult:2 } convolution_param{ num_output:128 kernel_size:7 stride:1 weight_filler{ type:"xavier" } bias_filler{ type:"constant" } } } layer{ name:"pool1" type:"Pooling" bottom:"conv1" top:"pool1" pooling_param{ pool: MAX kernel_size:2 stride:2 } } layer{ name:"conv2" type:"Convolution" bottom:"pool1" top:"conv2" param{ lr_mult:1 } param{ lr_mult:2 } convolution_param{ num_output:128 kernel_size:5 stride:1 weight_filler{ type:"xavier" } bias_filler{ type:"constant" } } } layer{ name:"pool2" type:"Pooling" bottom:"conv2" top:"pool2" pooling_param{ pool: MAX kernel_size:2 stride:1 } } layer{ name:"Relu" type:"ReLU" bottom:"pool2" top:"pool2" } layer{ name:"conv3" type:"Convolution" bottom:"pool2" top:"conv3" param{ lr_mult:1 } param{ lr_mult:2 } convolution_param{ num_output:128 kernel_size:3 stride:1 weight_filler{ type:"xavier" } bias_filler{ type:"constant" } } } layer{ name:"Relu2" type:"ReLU" bottom:"conv3" top:"conv3" } layer{ name:"conv4" type:"Convolution" bottom:"conv3" top:"conv4" param{ lr_mult:1 } param{ lr_mult:2 } convolution_param{ num_output:128 kernel_size:3 stride:1 weight_filler{ type:"xavier" } bias_filler{ type:"constant" } } } layer{ name:"Relu3" type:"ReLU" bottom:"conv4" top:"conv4" } layer{ name:"conv5" type:"Convolution" bottom:"conv4" top:"conv5" param{ lr_mult:1 } param{ lr_mult:2 } convolution_param{ num_output:128 kernel_size:3 stride:1 weight_filler{ type:"xavier" } bias_filler{ type:"constant" } } } layer{ name:"fc81" type:"InnerProduct" bottom:"conv5" top:"fc81" param{ lr_mult:1 } param{ lr_mult:2 } inner_product_param{ num_output:36 weight_filler{ type:"xavier" } bias_filler{ type:"constant" } } } layer{ name:"fc82" type:"InnerProduct" bottom:"conv5" top:"fc82" param{ lr_mult:1 } param{ lr_mult:2 } inner_product_param{ num_output:36 weight_filler{ type:"xavier" } bias_filler{ type:"constant" } } } layer{ name:"fc83" type:"InnerProduct" bottom:"conv5" top:"fc83" param{ lr_mult:1 } param{ lr_mult:2 } inner_product_param{ num_output:36 weight_filler{ type:"xavier" } bias_filler{ type:"constant" } } } layer{ name:"fc84" type:"InnerProduct" bottom:"conv5" top:"fc84" param{ lr_mult:1 } param{ lr_mult:2 } inner_product_param{ num_output:36 weight_filler{ type:"xavier" } bias_filler{ type:"constant" } } } layer{ name:"slice2" type:"Slice" bottom:"label" top:"l1" top:"l2" top:"l3" top:"l4" slice_param{ axis:1 slice_point:1 slice_point:2 slice_point:3 } } layer{ name:"accuracy1" type:"Accuracy" bottom:"fc81" bottom:"l1" top:"accuracy1" include{ phase: TEST } } layer{ name:"accuracy2" type:"Accuracy" bottom:"fc82" bottom:"l2" top:"accuracy2" include{ phase: TEST } } layer{ name:"accuracy3" type:"Accuracy" bottom:"fc83" bottom:"l3" top:"accuracy3" include{ phase: TEST } } layer{ name:"accuracy4" type:"Accuracy" bottom:"fc84" bottom:"l4" top:"accuracy4" include{ phase: TEST } } layer{ name:"loss1" type:"SoftmaxWithLoss" bottom:"fc81" bottom:"l1" top:"loss1" loss_weight:0.1 } layer{ name:"loss2" type:"SoftmaxWithLoss" bottom:"fc82" bottom:"l2" top:"loss2" loss_weight:0.1 } layer{ name:"loss3" type:"SoftmaxWithLoss" bottom:"fc83" bottom:"l3" top:"loss3" loss_weight:0.1 } layer{ name:"loss4" type:"SoftmaxWithLoss" bottom:"fc84" bottom:"l4" top:"loss4" loss_weight:0.1 }
然後就可以開始訓練了
結果如下: