在之前的caffe配置中,我们已经下载好了cifar10的数据。这里我就用cifar10的example进行一些修改,建立自己的训练脚本。
进入caffe目录,在/example/cifar10下可以看到一些示例脚本
在这里插入图片描述
这里我们复制cifar10_full_sigmoid_train_test.prototxt并重新命名,然后将网络结构修改成我们需要的样子。
注意:将里面的路径修改成绝对路径

name: "CIFAR10_full"
layer {
  name: "cifar"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    mean_file: "/home/kzj/CNN/caffe/examples/cifar10/mean.binaryproto"
  }
  data_param {
    source: "/home/kzj/CNN/caffe/examples/cifar10/cifar10_train_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "cifar"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    mean_file: "/home/kzj/CNN/caffe/examples/cifar10/mean.binaryproto"
  }
  data_param {
    source: "/home/kzj/CNN/caffe/examples/cifar10/cifar10_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 32
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "gaussian"
      std: 0.0001
    }
    bias_filler {
      type: "constant"
    }
  }
}


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

layer {
  name: "conv2"
  type: "Convolution"
  bottom: "conv1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 32
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
    }
  }
}


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

layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "conv2"
  top: "ip1"
  param {
    lr_mult: 1
    decay_mult: 250
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "gaussian"
      std: 0.01
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip1"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip1"
  bottom: "label"
  top: "loss"
}

再复制cifar10_full_solver.prototxt,并重命名,修改内容。

# reduce learning rate after 120 epochs (60000 iters) by factor 0f 10
# then another factor of 10 after 10 more epochs (5000 iters)

# The train/test net protocol buffer definition
net: "/home/kzj/CNN/caffe/kzj_test/two_conv_train_ex.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of CIFAR10, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
# Carry out testing every 1000 training iterations.
test_interval: 400
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.0005
momentum: 0.9
weight_decay: 0.004
# The learning rate policy
lr_policy: "fixed"
# Display every 200 iterations
display: 200
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 2000
snapshot_format: HDF5
snapshot_prefix: "/home/kzj/CNN/caffe/kzj_test/two_conv_train"
# solver mode: CPU or GPU
solver_mode: CPU

最后再建立一个执行脚本文件

TOOLS=~/CNN/caffe/build/tools

$TOOLS/caffe train \
	--solver=/home/kzj/CNN/caffe/kzj_test/two_conv_train_ex_solver.prototxt

完成后可以新建一个文件夹, 把三个文件放到一起,但三个文件中的路径一定要是绝对路径,否则训练过程中会出现错误。
在这里插入图片描述
现在执行训练脚本即可开始训练,训练完成后会在目录下保存训练好的模型文件。
在这里插入图片描述

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐