东方耀AI技术分享
标题:
运行caffe源码自带的mnits和cifar10项目实验
[打印本页]
作者:
东方耀
时间:
2020-7-9 10:34
标题:
运行caffe源码自带的mnits和cifar10项目实验
运行caffe源码自带的mnits和cifar10项目实验
彻底查看是否能在gpu上运行
必须是源码编译安装好caffe才行:
http://www.ai111.vip/thread-1062-1-1.html
http://www.ai111.vip/thread-782-1-1.html
1. mnits
在caffe/examples/mnist目录下可以找到LeNet模型的具体实现
数据下载:运行./data/mnist/get_mnist.sh
在./data/mnist/目录下下载了4个*.gz格式的数据压缩包,train-*是训练数据,t10k-*是测试数据,下载后会利用gunzip命令解压缩[其中源文件会默认被移除]
train-labels-idx1-ubyte.gz => train-labels-idx1-ubyte
train-images-idx3-ubyte.gz => train-images-idx3-ubyte
t10k-labels-idx1-ubyte.gz => t10k-labels-idx1-ubyte
t10k-images-idx3-ubyte.gz => t10k-images-idx3-ubyte
数据格式转换
将上一步下载解压后的文件转换为lmdb文件. Caffe支持多种数据格式输入网络:leveldb,lmdb,HDF5等,可以根据自己需要选择.
./examples/mnist/create_mnist.sh
在 ./examples/mnist/目录下会创建2个文件夹:mnist_train_lmdb和mnist_test_lmdb,其中分别包含data.mdb和lock.mdb两个文件
网络配置
LeNet网络定义在examples/mnist/lenet_train_test.prototxt文件中,需要关注的是里面source参数文件路径.
关注: TRAIN 的data_param的source: "examples/mnist/mnist_train_lmdb", TEST 的data_param的source: "examples/mnist/mnist_test_lmdb"
训练网络
./examples/mnist/train_lenet.sh
# 迭代10000次, 输出 "Optimization Done."
# 准确率达到99%
参数配置examples/mnist/lenet_solver.prototxt
默认是使用GPU完成运算,如果使用的是CPU,则需要修改配置文件
3. 生成网络组织模型图形
下面以打印mnist网络模型为例,利用的是caffe内提供的draw_net.py ,在caffe/python目录下,需要make pycaffe之后才可以使用
在当前目录为caffe下,执行
python ./python/draw_net.py ./examples/mnist/lenet_train_test.prototxt lenet_dfy.png
jpg jpeg 都支持的!
在会在当前路径caffe下生成一张lennet_dfy.png图像
ImportError: No module named pydot
pip install pydot
OSError: [Errno 2] "dot" not found in path
sudo apt-get install graphviz
完美解决! 参考:
http://www.ai111.vip/thread-934-1-1.html
# 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
# 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.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
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: GPU
复制代码
name: "LeNet"
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
source: "examples/mnist/mnist_train_lmdb"
batch_size: 64
backend: LMDB
}
}
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
backend: LMDB
}
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 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"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
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"
}
复制代码
作者:
东方耀
时间:
2020-7-9 11:18
cifar10
基本过程同mnist, 执行命令如下:
./data/cifar10/get_cifar10.sh
./examples/cifar10/create_cifar10.sh
./examples/cifar10/train_quick.sh
网络配置examples/cifar10/cifar10_quick_train_test.prototxt
参数配置examples/cifar10/cifar10_quick_solver.prototxt
运行结果[迭代了5000次,准确路74.8%]:
Resuming from examples/cifar10/cifar10_quick_iter_4000.solverstate
这里面还有模型的恢复 迁移学习
在当前目录为caffe下,执行
python ./python/draw_net.py ./examples/cifar10/cifar10_quick_train_test.prototxt cifar10_dfy.png
jpg jpeg 都支持的!
在会在当前路径caffe下生成一张cifar10_dfy.png图像
作者:
东方耀
时间:
2020-7-10 14:46
caffe的运行提供三种接口:c++接口(命令行)、python接口和matlab接口。
用c++接口(命令行)训练
caffe <command> <args>
<command>有这样四种:train test device_query time
train----训练或finetune模型(model),
./build/tools/caffe train -solver examples/mnist/lenet_solver_adam.prototxt
用两块或多块GPU来平行运算,这样速度会快很多。但是如果你只有一块或没有gpu, 就不要加-gpu参数了,加了反而慢
./build/tools/caffe train -solver examples/mnist/lenet_solver_adam.prototxt -gpu 0,1
在linux下,本身就有一个time命令,因此可以结合进来使用,因此我们运行mnist例子的最终命令是(一块gpu):
time ./build/tools/caffe train -solver examples/mnist/lenet_solver_adam.prototxt
test-----测试模型
./build/tools/caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_dfy_iter_10000.caffemodel -gpu 0 -iterations 100
100个Batch 测试100次
device_query---显示gpu信息
./build/tools/caffe device_query -gpu 0
time-----显示程序执行时间
./build/tools/caffe time -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_dfy_iter_10000.caffemodel -gpu 0 -iterations 10
这个例子用来在屏幕上显示lenet模型gpu上迭代10次所使用的时间
包括每次迭代的forward和backward所用的时间,也包括每层forward和backward所用的平均时间。
其中的<args>参数有:-solver -gpu -snapshot -weights -iteration -model -sighup_effect -sigint_effect
-solver:必选参数。一个protocol buffer类型的文件,即模型的配置文件(超参和网络等)
-gpu: 可选参数。该参数用来指定用哪一块gpu运行,根据gpu的id进行选择
-snapshot:可选参数。该参数用来从快照(snapshot)中恢复训练
./build/tools/caffe train -solver examples/mnist/lenet_solver.prototxt
-snapshot examples/mnist/lenet_iter_5000.solverstate
-weights:可选参数。用预先训练好的权重来fine-tuning模型,需要一个caffemodel,不能和-snapshot同时使用
-weights models/bvlc_reference_caffenet.caffemodel
-iterations: 可选参数,迭代次数,默认为50。 如果在配置文件文件中没有设定迭代次数,则默认迭代50次
-model:可选参数,定义在protocol buffer文件中的模型。也可以在solver配置文件中指定
欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/)
Powered by Discuz! X3.4