东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 4891|回复: 2
打印 上一主题 下一主题

[课堂笔记] 运行caffe源码自带的mnits和cifar10项目实验

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14438
QQ
跳转到指定楼层
楼主
发表于 2020-7-9 10:34:08 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
运行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





  1. # The train/test net protocol buffer definition
  2. net: "examples/mnist/lenet_train_test.prototxt"
  3. # test_iter specifies how many forward passes the test should carry out.
  4. # In the case of MNIST, we have test batch size 100 and 100 test iterations,
  5. # covering the full 10,000 testing images.
  6. test_iter: 100
  7. # Carry out testing every 500 training iterations.
  8. test_interval: 500
  9. # The base learning rate, momentum and the weight decay of the network.
  10. base_lr: 0.01
  11. momentum: 0.9
  12. weight_decay: 0.0005
  13. # The learning rate policy
  14. lr_policy: "inv"
  15. gamma: 0.0001
  16. power: 0.75
  17. # Display every 100 iterations
  18. display: 100
  19. # The maximum number of iterations
  20. max_iter: 10000
  21. # snapshot intermediate results
  22. snapshot: 5000
  23. snapshot_prefix: "examples/mnist/lenet"
  24. # solver mode: CPU or GPU
  25. solver_mode: GPU
复制代码
  1. name: "LeNet"
  2. layer {
  3.   name: "mnist"
  4.   type: "Data"
  5.   top: "data"
  6.   top: "label"
  7.   include {
  8.     phase: TRAIN
  9.   }
  10.   transform_param {
  11.     scale: 0.00390625
  12.   }
  13.   data_param {
  14.     source: "examples/mnist/mnist_train_lmdb"
  15.     batch_size: 64
  16.     backend: LMDB
  17.   }
  18. }
  19. layer {
  20.   name: "mnist"
  21.   type: "Data"
  22.   top: "data"
  23.   top: "label"
  24.   include {
  25.     phase: TEST
  26.   }
  27.   transform_param {
  28.     scale: 0.00390625
  29.   }
  30.   data_param {
  31.     source: "examples/mnist/mnist_test_lmdb"
  32.     batch_size: 100
  33.     backend: LMDB
  34.   }
  35. }
  36. layer {
  37.   name: "conv1"
  38.   type: "Convolution"
  39.   bottom: "data"
  40.   top: "conv1"
  41.   param {
  42.     lr_mult: 1
  43.   }
  44.   param {
  45.     lr_mult: 2
  46.   }
  47.   convolution_param {
  48.     num_output: 20
  49.     kernel_size: 5
  50.     stride: 1
  51.     weight_filler {
  52.       type: "xavier"
  53.     }
  54.     bias_filler {
  55.       type: "constant"
  56.     }
  57.   }
  58. }
  59. layer {
  60.   name: "pool1"
  61.   type: "Pooling"
  62.   bottom: "conv1"
  63.   top: "pool1"
  64.   pooling_param {
  65.     pool: MAX
  66.     kernel_size: 2
  67.     stride: 2
  68.   }
  69. }
  70. layer {
  71.   name: "conv2"
  72.   type: "Convolution"
  73.   bottom: "pool1"
  74.   top: "conv2"
  75.   param {
  76.     lr_mult: 1
  77.   }
  78.   param {
  79.     lr_mult: 2
  80.   }
  81.   convolution_param {
  82.     num_output: 50
  83.     kernel_size: 5
  84.     stride: 1
  85.     weight_filler {
  86.       type: "xavier"
  87.     }
  88.     bias_filler {
  89.       type: "constant"
  90.     }
  91.   }
  92. }
  93. layer {
  94.   name: "pool2"
  95.   type: "Pooling"
  96.   bottom: "conv2"
  97.   top: "pool2"
  98.   pooling_param {
  99.     pool: MAX
  100.     kernel_size: 2
  101.     stride: 2
  102.   }
  103. }
  104. layer {
  105.   name: "ip1"
  106.   type: "InnerProduct"
  107.   bottom: "pool2"
  108.   top: "ip1"
  109.   param {
  110.     lr_mult: 1
  111.   }
  112.   param {
  113.     lr_mult: 2
  114.   }
  115.   inner_product_param {
  116.     num_output: 500
  117.     weight_filler {
  118.       type: "xavier"
  119.     }
  120.     bias_filler {
  121.       type: "constant"
  122.     }
  123.   }
  124. }
  125. layer {
  126.   name: "relu1"
  127.   type: "ReLU"
  128.   bottom: "ip1"
  129.   top: "ip1"
  130. }
  131. layer {
  132.   name: "ip2"
  133.   type: "InnerProduct"
  134.   bottom: "ip1"
  135.   top: "ip2"
  136.   param {
  137.     lr_mult: 1
  138.   }
  139.   param {
  140.     lr_mult: 2
  141.   }
  142.   inner_product_param {
  143.     num_output: 10
  144.     weight_filler {
  145.       type: "xavier"
  146.     }
  147.     bias_filler {
  148.       type: "constant"
  149.     }
  150.   }
  151. }
  152. layer {
  153.   name: "accuracy"
  154.   type: "Accuracy"
  155.   bottom: "ip2"
  156.   bottom: "label"
  157.   top: "accuracy"
  158.   include {
  159.     phase: TEST
  160.   }
  161. }
  162. layer {
  163.   name: "loss"
  164.   type: "SoftmaxWithLoss"
  165.   bottom: "ip2"
  166.   bottom: "label"
  167.   top: "loss"
  168. }
复制代码


caffe_minist.png (29.29 KB, 下载次数: 119)

caffe_minist.png

mnist_lmdb.png (41.51 KB, 下载次数: 123)

mnist_lmdb.png

caffe_train_gpu.png (46.61 KB, 下载次数: 121)

caffe_train_gpu.png

caffe_train_end.png (118.08 KB, 下载次数: 118)

caffe_train_end.png

lenet_dfy.png (94.34 KB, 下载次数: 122)

lenet_dfy.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14438
QQ
沙发
 楼主| 发表于 2020-7-9 11:18:35 | 只看该作者
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图像
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14438
QQ
板凳
 楼主| 发表于 2020-7-10 14:46:51 | 只看该作者
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配置文件中指定
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-7 04:52 , Processed in 0.186159 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表