Caffe中对cifar10执行train操作
生活随笔
收集整理的這篇文章主要介紹了
Caffe中对cifar10执行train操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考Caffe source中examples/cifar10目錄下內容。
cifar10是一個用于普通物體識別的數據集,cifar10被分為10類,分別為airplane、automobile、bird、cat、deer、dog、frog、horse、ship、truck,關于cifar10的詳細介紹可以參考:?http://blog.csdn.net/fengbingchun/article/details/53560637
調整后的cifar10_quick_solver.prototxt內容如下:
# reduce the learning rate after 8 epochs (4000 iters) by a factor of 10# The train/test net protocol buffer definition
net: "E:/GitCode/Caffe_Test/test_data/model/cifar10/cifar10_quick_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
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.001
momentum: 0.9
weight_decay: 0.004
# The learning rate policy
lr_policy: "fixed"
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 4000
# snapshot intermediate results
snapshot: 4000
snapshot_format: HDF5
snapshot_prefix: "E:/GitCode/Caffe_Test/test_data/model/cifar10/cifar10_quick"
# solver mode: CPU or GPU
#solver_mode: GPU
調整后的cifar10_quick_train_test.prototxt內容如下:
name: "CIFAR10_quick"
layer {name: "cifar"type: "Data"top: "data"top: "label"include {phase: TRAIN}transform_param {mean_file: "E:/GitCode/Caffe_Test/test_data/model/cifar10/mean.binaryproto"}data_param {source: "E:/GitCode/Caffe_Test/test_data/cifar10/cifar10_train_lmdb"batch_size: 100backend: LMDB}
}
layer {name: "cifar"type: "Data"top: "data"top: "label"include {phase: TEST}transform_param {mean_file: "E:/GitCode/Caffe_Test/test_data/model/cifar10/mean.binaryproto"}data_param {source: "E:/GitCode/Caffe_Test/test_data/cifar10/cifar10_test_lmdb"batch_size: 100backend: LMDB}
}
layer {name: "conv1"type: "Convolution"bottom: "data"top: "conv1"param {lr_mult: 1}param {lr_mult: 2}convolution_param {num_output: 32pad: 2kernel_size: 5stride: 1weight_filler {type: "gaussian"std: 0.0001}bias_filler {type: "constant"}}
}
layer {name: "pool1"type: "Pooling"bottom: "conv1"top: "pool1"pooling_param {pool: MAXkernel_size: 3stride: 2}
}
layer {name: "relu1"type: "ReLU"bottom: "pool1"top: "pool1"
}
layer {name: "conv2"type: "Convolution"bottom: "pool1"top: "conv2"param {lr_mult: 1}param {lr_mult: 2}convolution_param {num_output: 32pad: 2kernel_size: 5stride: 1weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"}}
}
layer {name: "relu2"type: "ReLU"bottom: "conv2"top: "conv2"
}
layer {name: "pool2"type: "Pooling"bottom: "conv2"top: "pool2"pooling_param {pool: AVEkernel_size: 3stride: 2}
}
layer {name: "conv3"type: "Convolution"bottom: "pool2"top: "conv3"param {lr_mult: 1}param {lr_mult: 2}convolution_param {num_output: 64pad: 2kernel_size: 5stride: 1weight_filler {type: "gaussian"std: 0.01}bias_filler {type: "constant"}}
}
layer {name: "relu3"type: "ReLU"bottom: "conv3"top: "conv3"
}
layer {name: "pool3"type: "Pooling"bottom: "conv3"top: "pool3"pooling_param {pool: AVEkernel_size: 3stride: 2}
}
layer {name: "ip1"type: "InnerProduct"bottom: "pool3"top: "ip1"param {lr_mult: 1}param {lr_mult: 2}inner_product_param {num_output: 64weight_filler {type: "gaussian"std: 0.1}bias_filler {type: "constant"}}
}
layer {name: "ip2"type: "InnerProduct"bottom: "ip1"top: "ip2"param {lr_mult: 1}param {lr_mult: 2}inner_product_param {num_output: 10weight_filler {type: "gaussian"std: 0.1}bias_filler {type: "constant"}}
}
layer {name: "accuracy"type: "Accuracy"bottom: "ip2"bottom: "label"top: "accuracy"include {phase: TEST}
}
layer {name: "loss"type: "SoftmaxWithLoss"bottom: "ip2"bottom: "label"top: "loss"
}
cifar10_quick_train_test.prototxt可視化結果如下圖(http://ethereon.github.io/netscope/quickstart.html):
訓練代碼如下: 它既可以在CPU也可以在GPU模式下運行,分別對應的工程為Caffe_Test和Caffe_GPU_Test,在GPU模式下耗時較少。
#include "funset.hpp"
#include "common.hpp"int cifar10_train()
{
#ifdef CPU_ONLYcaffe::Caffe::set_mode(caffe::Caffe::CPU);
#elsecaffe::Caffe::set_mode(caffe::Caffe::GPU);
#endifconst std::string filename{ "E:/GitCode/Caffe_Test/test_data/model/cifar10/cifar10_quick_solver.prototxt" };caffe::SolverParameter solver_param;if (!caffe::ReadProtoFromTextFile(filename.c_str(), &solver_param)) {fprintf(stderr, "parse solver.prototxt fail\n");return -1;}cifar10_convert(); // convert cifar10 to LMDBif (cifar10_compute_image_mean() != 0) { // compute cifar10 image mean, generate mean.binaryprotofprintf(stderr, "compute cifar10 image mean fail\n");return -1;}caffe::SGDSolver<float> solver(solver_param);solver.Solve();fprintf(stderr, "cifar10 train finish\n");return 0;
}
執行結果如下圖所示:
默認最大迭代次數為4000,此時accuracy為0.7062;如果設置最大迭代次數為10000,則此時accuracy約為0.72。
GitHub:https://github.com/fengbingchun/Caffe_Test
總結
以上是生活随笔為你收集整理的Caffe中对cifar10执行train操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++/C++11中std::list双
- 下一篇: 使用Caffe基于cifar10进行物体