Caffe学习笔记[基础篇:Cifar10网络训练 190730]

Caffe自带实例Cifar10

目录

Caffe自带实例Cifar10

一. 数据集简介:

二. 数据准备

三. 训练网络

四. 测试网络

五. 测试自己的图片


一. 数据集简介:

二. 数据准备

1. caffe自带了一个脚本下载数据集,运行此脚本即可下载,下载速度较慢,大约需要5h,运行结束后会在data/cifar10路径下出现许多文件

cd ~caffe
~/caffe/data/cifar10/get_cifar10.sh

2. 将原始数据转换为lmdb格式,运行结束后会在examples/cifar10//下出现两个文件夹,其中包含了制作好的数据库:~/caffe/examples/cifar10/cifar10_test_lmdb/data.mdb
~/caffe/examples/cifar10/cifar10_test_lmdb/lock.mdb

~/caffe/examples/cifar10/cifar10_train_lmdb/data.mdb
~/caffe/examples/cifar10/cifar10_train_lmdb/lock.mdb

/home/snake/caffe/examples/cifar10/mean.binaryproto

cd ~/caffe
sh ~/caffe/examples/cifar10/create_cifar10.sh 

三. 训练网络

1. 训练网络可以采用脚本:~/caffe/examples/cifar10/train_quick.sh

#!/usr/bin/env sh

# set -e表示一旦脚本中有命令的返回值为非0,则脚本立即退出,后续命令不再执行;
set -e

TOOLS=./build/tools

$TOOLS/caffe train \
  --solver=examples/cifar10/cifar10_quick_solver.prototxt $@
# $@ 表示所有参数列表

# reduce learning rate by factor of 10 after 8 epochs
$TOOLS/caffe train \
  --solver=examples/cifar10/cifar10_quick_solver_lr1.prototxt \
  --snapshot=examples/cifar10/cifar10_quick_iter_4000.solverstate.h5 $@

2. 脚本解析

在这个脚本中,进行了分阶段的训练,

a . 阶段一

$TOOLS/caffe train  --solver=examples/cifar10/cifar10_quick_solver.prototxt $@ 

在这个solver文件(超参数文件)中,指定了使用的网络为:

net: "examples/cifar10/cifar10_quick_train_test.prototxt"

每500次训练迭代为一个epoch(test_interval: 500),一个epoch测试一次;

最大迭代次数为4000次,总共8个epoch;

测试迭代次数为100;

基础学习率0.001,策略为固定;

每100次训练迭代输出一次结果在屏幕上;

迭代结束后,生成hdf5格式的中间结果文件。

# reduce the learning rate after 8 epochs (4000 iters) by a factor of 10

# The train/test net protocol buffer definition
net: "examples/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: "examples/cifar10/cifar10_quick"
# solver mode: CPU or GPU
solver_mode: GPU

b. 阶段二

# reduce learning rate by factor of 10 after 8 epochs
$TOOLS/caffe train \
  --solver=examples/cifar10/cifar10_quick_solver_lr1.prototxt \
  --snapshot=examples/cifar10/cifar10_quick_iter_4000.solverstate.h5 $@

基于前4000次的迭代,继续训练(第二阶段(迭代1000次)),

此时使用的solver文件变为examples/cifar10/cifar10_quick_solver_lr1.prototxt:

# reduce the learning rate after 8 epochs (4000 iters) by a factor of 10

# The train/test net protocol buffer definition
net: "examples/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.0001
# 学习率变为原来的10分之一
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: 5000
# 包含了前4000次
# snapshot intermediate results
snapshot: 5000
snapshot_format: HDF5
snapshot_prefix: "examples/cifar10/cifar10_quick"
# solver mode: CPU or GPU
solver_mode: GPU

c. 分阶段的改进:

实际上,对caffe熟悉之后,可以直接指定策略为multistep:

base_lr: 0.001
momentum: 0.9
weight_decay: 0.004
lr_policy: "multistep"
gamma: 0.1
stepvalue: 4000
stepvalue: 5000

d. 训练结果:

GPU+Cudnn 训练时间45秒左右,准确度:0.7485 loss:0.748655。

e. 尝试更改训练迭代的次数

第二次3000+第一次4000:准确度: 0.7623 loss:0.729898  (略有提升)

四. 测试网络

脚本 /home/snake/caffe/examples/cifar10/test_cifar10_sun.sh

#!/usr/bin/env sh
./build/tools/caffe test -model examples/cifar10/cifar10_quick_train_test.prototxt -weights examples/cifar10/cifar10_quick_iter_5000.caffemodel.h5 -gpu 0 \ 
-iterations 100

 

五. 测试自己的图片

5.1 使用的是python接口测试,使用caffe提供的$CAFFE_ROOT/python/classify.py文件.

修改两个地方:

a. 在该py文件中,找到:

if args.mean_file:
        mean = np.load(args.mean_file)

在下面加上一行:

mean=mean.mean(1).mean(1) 

b. 添加结果显示 ,最简单的方法是,在CAFFE_ROOT/python/classify.py添加:

print("Predictions:%s" % predictions)

5.2 准备测试图片

在CAFFE_ROOT/examples/images下有cat.jpg等图片,也可以自己添加其他类别,如狗、鸟等。

python python/classify.py --model_def examples/cifar10/cifar10_quick.prototxt --pretrained_model examples/cifar10/cifar10_quick_iter_5000.caffemodel.h5 --center_only  examples/images/cat.jpg foo

官方自带的脚本是不输出在屏幕上的。最好基于此脚本文件,做一些可视化的修改,比如在图片上标出是什么类别。

在[4]中,有一个写的不错的脚本deploy.py,调用方法:

python deploy.py --model_def=cifar10_quick.prototxt  --pretrained_model=./cifar10_quick_iter_50000.caffemodel air2.jpg 

 

参考文献:

[1] https://manutdzou.github.io/2016/05/15/Caffe-Usage.html

[2] https://blog.csdn.net/ibelieveican2015/article/details/79052133

[3] https://blog.csdn.net/u014380165/article/details/77493943

[4] https://github.com/MacroXJTU/caffe_cifar10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值