caffe:自己搭建網絡來訓練


1.准備樣本

  要訓練自己的樣本,首先需要把樣本准備好,需要准備的是訓練集和測試集,caffe支持直接使用圖片,當然把樣本轉換為leveldb或lmdb格式的話訓練起來會更快一點。這里我先偷個懶,直接使用圖片吧 [尷尬.jpg]

  訓練集和測試集是一樣的,不過樣本不要重疊。首先我把訓練集的圖片都放在一個目錄,然后shift+右鍵選擇該目錄,打開cmd,使用命令 dir /s/b >train.txt ,這樣就在該目錄下生成了一份所有圖片的列表,效果如下

  然后使用查找替換功能把它修改成下面這個樣子,后面的0,1,,序號是為每個類別的樣本分配的標簽,需要從0開始:

位置/../xx1.jpg 0
位置/../xx2.jpg 0
位置/../xx3.jpg 0
位置/../xx4.jpg 1
位置/../xx5.jpg 1
位置/../xx6.jpg 1
.....................

  這里樣本的准備已經差不多了,最后一步是需要生成均值文件 binaryproto,生成均值文件之前需要先將圖片轉換為lmdb,這里可以使用caffe自帶的工具來生成(vs編譯后會在bin文件夾下生成comput_image_mean.exe convert_imageset.exe

  打開cmd,然后運行:

convert_imageset -shuffle -resize_height=150 -resize_width=150 J:/Caffe/train/ J:/Caffe/train/train.txt J:/Caffe/train/lmdb

  使用命令生成均值文件:

compute_image_mean J:/Caffe/train/lmdb J:/Caffe/train/train.binaryproto

  到這里訓練集已經准備好了,然后測試集同上,最后我把它們全放在了data文件夾下,該文件夾下包含訓練和測試所需的所有圖片圖片列表train.txt和test.txt均值文件train.binaryproto(我的測試也是使用這個均值文件)

2. 編寫配置文件solver,我在caffe目錄下的face_example目錄下新建了my_solver.prototxt,編寫如下:

net: "face_example/my_train.prototxt" # 網絡結構文件的位置
test_iter:1200 # 迭代次數,根據batch大小來
test_interval:100 # 測試間隔

base_lr:0.001 # 學習率
lr_policy: "multistep" 
gamma: 0.1

stepvalue: 200 # 設置學習率什么時候減小
stepvalue: 400
stepvalue: 700
stepvalue: 1000
max_iter: 2000 # 最大迭代次數

display: 100 # 每訓練100次顯示一次
momentum: 0.9 # 設置沖量
weight_decay: 0.0005 
snapshot: 200 # 每200次保存一個快照文件
snapshot_prefix: "face_example/face_snapshot" # 快照文件保存位置

solver_mode:GPU # 使用GPU訓練

3. 編寫網絡定義文件,新建 my_train.prototxt ,編寫如下:

name: "my_caffe_test"

layer{
    name: "data"
    type: "ImageData"
    top: "data"
    top: "label"
    include{
        phase:TRAIN
    }
    transform_param{
        mean_file:"face_example/train.binaryproto"
        scale: 0.0078125
        mirror:true
    }
    image_data_param{
        source:"face_example/data/train.txt"
        batch_size:1
        shuffle:true
    }
}

layer{
    name: "data"
    type: "ImageData"
    top: "data"
    top: "label"
    include{
        phase:TEST
    }
    transform_param{
        mean_file:"face_example/train.binaryproto"
        scale: 0.0078125
        mirror:true
    }
    image_data_param{
        source:"face_example/data/test.txt"
        batch_size:1
        shuffle:true
    }
}

layer{
    name: "conv1"
    type: "Convolution"
    bottom: "data"
    top: "conv1"
    param{
        lr_mult: 1 # 和base_lr相乘
        decay_mult: 1
    }
    convolution_param{
        num_output: 32
        kernel_size: 3
        stride: 1
        weight_filler{
            type: "xavier"
        }
        bias_filler{
            type: "constant"
            value: 0
        }
    }
}

layer{
    name: "relu1"
    type: "PReLU"
    bottom: "conv1"
    top: "conv1"
}

layer{
    name: "conv2"
    type: "Convolution"
    bottom: "conv1"
    top: "conv2"
    param{
        lr_mult: 1
        decay_mult: 1
    }
    convolution_param{
        num_output: 64
        kernel_size: 3
        stride: 1 # 步長
        weight_filler{
            type: "xavier"
        }
        bias_filler{
            type: "constant"
            value: 0
        }
    }
}

layer{
    name: "relu2"
    type: "PReLU"
    bottom: "conv2"
    top: "conv2"
}

layer{
    name: "pool1"
    type: "Pooling"
    bottom: "conv2"
    top: "pool1"
    pooling_param{
        pool: MAX
        kernel_size: 2
        stride: 2
    }
}

layer{
    name: "conv3"
    type: "Convolution"
    bottom: "pool1"
    top: "conv3"
    param{
        lr_mult: 1
        decay_mult: 1
    }
    convolution_param{
        num_output: 128
        kernel_size: 3
        stride: 1
        weight_filler{
            type: "xavier"
        }
        bias_filler{
            type: "constant"
            value: 0
        }
    }
}

layer{
    name: "relu3"
    type: "PReLU"
    bottom: "conv3"
    top: "conv3"
}

layer{
    name: "fc1"
    type: "InnerProduct"
    bottom: "conv3"
    top: "fc1"
    param{
        lr_mult: 1
        decay_mult:10
    }
    inner_product_param{
        num_output: 256
        weight_filler{
            type: "xavier"
        }
        bias_filler{
            type: "constant"
            value: 0
        }
    }
}

layer{
    name: "fc2"
    type: "InnerProduct"
    bottom: "fc1"
    top: "fc2"
    param{
        lr_mult: 1
        decay_mult:10
    }
    inner_product_param{
        num_output: 12
        weight_filler{
            type: "xavier"
        }
        bias_filler{
            type: "constant"
            value: 0
        }
    }
}

layer{
    name: "softmax_loss"
    type: "SoftmaxWithLoss"
    bottom: "fc2"
    bottom: "label"
    top: "softmax_loss"
}

4. 然后開始訓練,打開cmd,輸入命令:

caffe train -solver=face_example/my_solver.prototxt

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM