1. 程式人生 > >caffe網路配置檔案欄位意義詳解

caffe網路配置檔案欄位意義詳解

解決方案:lenet_solver.prototxt

# The train/test net protocol buffer definition
net: "examples/mnist/lenet_train_test.prototxt"
//網路協議具體定義
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
//test迭代次數 如果batch_size =100,則100張圖一批,訓練100次,則可以覆蓋10000張圖的需求
# Carry out testing every 500 training iterations.
test_interval: 500
//訓練迭代500次,測試一次
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
//網路引數:學習率,動量,權重的衰減
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
//學習策略:有固定學習率和每步遞減學習率
# Display every 100 iterations
display: 100
//每迭代100次顯示一次
# The maximum number of iterations
max_iter: 10000
//最大迭代次數
# snapshot intermediate results
snapshot: 5000
//每5000次迭代儲存一次資料,路徑字首是examples/mnist/lenet
snapshot_prefix: "examples/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: GPU
//是否使用GPU還是CPU

網路構造:lenet_train_test.prototxt

name: "LeNet"           網路名
layer {
  name: "mnist"         本層名稱
  type: "Data"              層型別
  top: "data"               下一層介面
  top: "label"              下一層介面
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625           #1/256,預處理如減均值,尺寸變換,隨機剪,映象等
  }
  data_param {
    source: "examples/mnist/mnist_train_lmdb"   訓練資料位置
    batch_size: 64                  一次訓練的樣本數
    backend: LMDB                   讀入的訓練資料格式,預設leveldb
  }
}

layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_test_lmdb"
    batch_size: 100                 一次測試使用100個數據
    backend: LMDB
  }
}

layer {
  name: "conv1"
  type: "Convolution"               卷積層
  bottom: "data"                上一層名“data”
  top: "conv1"                  下一層介面“conv1”
  param {
    lr_mult: 1                  (weights的學習率與全域性相同)
  }
  param {
    lr_mult: 2                  (biases的學習率是全域性的2倍)
  }
  convolution_param {
    num_output: 20              卷積核20個
    kernel_size: 5              卷積核尺寸5×5
    stride: 1                   步長1
    weight_filler {
      type: "xavier"                (隨機的初始化權重和偏差)
    }
    bias_filler {
      type: "constant"              bias用0初始化
    }
  }
}

layer {
  name: "pool1"
  type: "Pooling"               池化層
  bottom: "conv1"               上層“conv1”
  top: "pool1"                  下層介面“pool1”
  pooling_param {
    pool: MAX                   池化函式用MAX
    kernel_size: 2              池化核函式大小2×2
    stride: 2                   步長2
  }
}

layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50              卷積核50個
    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: 2
  }
}

layer {
  name: "ip1"
  type: "InnerProduct"              全連線層
  bottom: "pool2"               上層連線“pool2”
  top: "ip1"                    “下層輸出介面ip1”
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500             輸出數量500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}

layer {
  name: "relu1"
  type: "ReLU"              啟用函式
  bottom: "ip1"
  top: "ip1"    (這個地方還是ip1,底層與頂層相同減少開支,下一層全連線層的輸入也還是ip1)
}

layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10              輸出結果10個
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}

layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"         上層連線ip2全連線層
  bottom: "label"           上層連線label層
  top: "accuracy"           輸出介面為accuracy
  include {
    phase: TEST        
  }
}

layer {
  name: "loss"
  type: "SoftmaxWithLoss"       損失函式
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

訓練網路模型:

# -*- coding: utf-8 -*-

import caffe
#caffe.set_device(0)
#caffe.set_mode_gpu()
caffe.set_mode_cpu()
solver = caffe.SGDSolver('/root/AI/lenet/solver.prototxt')
solver.solve()