1.mnist实例

##1.数据下载 获得mnist的数据包,在caffe根目录下执行./data/mnist/get_mnist.sh脚本。 get_mnist.sh脚本先下载样本库并进行解压缩,得到四个文件。 

2.生成LMDB

成功解压缩下载的样本库后,然后执行./examples/mnist/create_mnist.sh。 create_mnist.sh脚本先利用caffe-master/build/examples/mnist/目录下的convert_mnist_data.bin工具,将mnist data转化为caffe可用的lmdb格式文件,然后将生成的mnist-train-lmdb和mnist-test-lmdb两个文件放在caffe-master/example/mnist目录下面。

3.网络配置

LeNet网络定义在./examples/mnist/lenet_train_test.prototxt 文件中。

name: "LeNet"
layer {
name: "mnist" //输入层的名称mnist
type: "Data" //输入层的类型为Data层
top: "data" //本层下一场连接data层和label blob空间
top: "label"
include {
phase: TRAIN //训练阶段
}
transform_param {
scale: 0.00390625 //输入图片像素归一到[0,1].1除以256为0.00390625
}
data_param {
source: "examples/mnist/mnist_train_lmdb" //从mnist_train_lmdb中读入数据
batch_size: 64 //batch大小为64,一次训练64条数据
backend: LMDB
}
}
layer {
name: "mnist" //输入层的名称mnist
type: "Data" //输入层的类型为Data层
top: "data" //本层下一场连接data层和label blob空间
top: "label"
include {
phase: TEST //测试阶段
}
transform_param {
scale: 0.00390625 //输入图片像素归一到[0,1].1除以256为0.00390625
}
data_param {
source: "examples/mnist/mnist_test_lmdb" //从mnist_test_lmdb中读入数据
batch_size: 100 //batch大小为100,一次训练100条数据
backend: LMDB
}
}
layer {
name: "conv1" //卷积层名称conv1
type: "Convolution" //层类型为卷积层
bottom: "data" //本层使用上一层的data,生成下一层conv1的blob
top: "conv1"
param {
lr_mult: 1 //权重参数w的学习率倍数
}
param {
lr_mult: 2 //偏置参数b的学习率倍数
}
convolution_param {
num_output: 20 //输出单元数20
kernel_size: 5 //卷积核大小为5*5
stride: 1 //步长为1
weight_filler { //允许用随机值初始化权重和偏置值
type: "xavier" //使用xavier算法自动确定基于输入—输出神经元数量的初始规模
}
bias_filler {
type: "constant" //偏置值初始化为常数,默认为0
}
}
}
layer {
name: "pool1" //层名称为pool1
type: "Pooling" //层类型为pooling
bottom: "conv1" //本层的上一层是conv1,生成下一层pool1的blob
top: "pool1"
pooling_param { //pooling层的参数
pool: MAX //pooling的方式是MAX
kernel_size: 2 //pooling核是2*2
stride: 2 //pooling步长是2
}
}
layer {
name: "conv2" //第二个卷积层,同第一个卷积层相同,只是卷积核为50
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" //第二个pooling层,与第一个pooling层相同
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer { //全连接层
name: "ip1" //全连接层名称ip1
type: "InnerProduct" //层类型为全连接层
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param { //全连接层的参数
num_output: 500 //输出500个节点
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu1" //ReLU层
type: "ReLU" //层名称为relu1
bottom: "ip1" //层类型为ReLU
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 //输出10个单元
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
layer { //loss层,softmax_loss层实现softmax和多项Logistic损失
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
top: "loss"
}

4.训练网络

运行./examples/mnist/train_lenet.sh。 执行此脚本是,实际运行的是lenet_solver.prototxt中的定义。

# 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 //test迭代次数,若batch_size=100,则100张图一批,训练100次,可覆盖1000张图
# Carry out testing every 500 training iterations.
test_interval: 500 //训练迭代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 //每迭代100次显示一次
display: 100
# The maximum number of iterations //最大迭代数
max_iter: 10000
# snapshot intermediate results //每5000次迭代存储一次数据
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: CPU //本例用CPU训练

数据训练结束后,会生成以下四个文件: 

5.测试网络

运行./build/tools/caffe.bin test -model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel

test:表示对训练好的模型进行Testing,而不是training。其他参数包括train, time, device_query。

-model=XXX:指定模型prototxt文件,这是一个文本文件,详细描述了网络结构和数据集信息。 

从上面的打印输出可看出,测试数据中的accruacy平均成功率为98%。

mnist手写测试

手写数字的图片必须满足以下条件:

  • 必须是256位黑白色
  • 必须是黑底白字
  • 像素大小必须是28*28
  • 数字在图片中间,上下左右没有过多的空白。

测试图片

    

手写数字识别脚本

import os
import sys
import numpy as np
import matplotlib.pyplot as plt caffe_root = '/home/lynn/caffe/'
sys.path.insert(0, caffe_root + 'python')
import caffe MODEL_FILE = '/home/lynn/caffe/examples/mnist/lenet.prototxt'
PRETRAINED = '/home/lynn/caffe/examples/mnist/lenet_iter_10000.caffemodel' IMAGE_FILE = '/home/lynn/test.bmp'
input_image = caffe.io.load_image(IMAGE_FILE, color=False) #print input_image
net = caffe.Classifier(MODEL_FILE, PRETRAINED)
prediction = net.predict([input_image], oversample = False)
caffe.set_mode_cpu()
print 'predicted class: ', prediction[0].argmax()

测试结果

 
 

caffe mnist实例 --lenet_train_test.prototxt 网络配置详解的更多相关文章

  1. Vmware在NAT模式下网络配置详解

    Vmware在NAT模式下网络配置详解 Linux中的网络配置对于接触Linux不久的小白菜来说,还是小有难度的,可能是不熟悉这种与windows系列迥然不同的命令行操作,也可能是由于对Linux的结 ...

  2. Docker基础 :网络配置详解

    本篇文章将讲述 Docker 的网络功能,包括使用端口映射机制来将容器内应用服务提供给外部网络,以及通过容器互联系统让多个容器之间进行快捷的网络通信,有兴趣的可以了解下. 大量的互联网应用服务包含多个 ...

  3. CentOS网络配置详解

    转载于CentOS中文站:http://www.centoscn.com/CentOS/2015/0507/5376.html一.配置文件详解 在RHEL或者CentOS等Redhat系的Linux系 ...

  4. linux网络配置详解

    一:相关网络配置的文件 1.网卡名配置相关文件 网卡名命名规则文件: /etc/udev/rules.d/70-persistent-net.rules # PCI device 0x8086:0x1 ...

  5. VMware虚拟机网络配置详解

    VMware网络配置:三种网络模式简介 安装好虚拟机以后,在网络连接里面可以看到多了两块网卡: 其中VMnet1是虚拟机Host-only模式的网络接口,VMnet8是NAT模式的网络接口,这些后面会 ...

  6. 虚拟机网络配置详解(NAT、桥接、Hostonly)

    VirtualBox中有四种网络连接方式: NAT Bridged Adapter Internal Host-only Adapter VMWare中有三种,其实它跟VMWare的网络连接方式都是一 ...

  7. 虚拟机网络配置详解(NAT、桥接、Hostonly) z

    http://www.cnblogs.com/beginmind/p/6379881.html VirtualBox中有四种网络连接方式: NAT Bridged Adapter Internal H ...

  8. CentOS 7 网络配置详解

    今天在一台PC上安装了CentOS 7,当时选择了最小安装模式,安装完成后马上用ifconfig查看本机的ip地址(局域网已经有DHCP),发现报错,提示ifconfig命令没找到. ? 1 2 3 ...

  9. Linux下MongoDB单实例的安装和配置详解

    推荐网站 MongoDB官网:http://www.mongodb.org/ MongoDB学习网站:http://www.runoob.com/mongodb 一.创建MongoDB的资源目录和安装 ...

随机推荐

  1. TypeScript简单的代码片段

    TypeScript中,接口.接口实现.函数重载: interface IThing{ name:string; age:number; sayHello:{ (name:string):string ...

  2. DataGridView属性设置汇总

    1.标题列居中 外观  ColumnHeadersDefaultCellStyle - Alignment - MiddleCenter 2.表格内容居中 外观  DefaultCellStyle - ...

  3. VMware ESXi定制版(OEM ISO)资源下载

    一.VMware ESXi 5.1.0 update03 链接: https://pan.baidu.com/s/1nvQ4CGD 密码: acc1 1.VMware-ESXi-5.1.0-Updat ...

  4. 动态规划——独立任务最优调度(Independent Task Scheduling)

    题目链接 题目描述 用2 台处理机A 和B 处理n 个作业.设第i 个作业交给机器A 处理时需要时间i a ,若由机器B 来处理,则需要时间i b .由于各作业的特点和机器的性能关系,很可能对于某些i ...

  5. nyoj13-Fibonacci数

    Fibonacci数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1 描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递归地 ...

  6. NOI 2016 循环之美 (莫比乌斯反演+杜教筛)

    题目大意:略 洛谷传送门 鉴于洛谷最近总崩,附上良心LOJ链接 任何形容词也不够赞美这一道神题 $\sum\limits_{i=1}^{N}\sum\limits_{j=1}^{M}[gcd(i,j) ...

  7. JavaScript中的XMLDOM对象

    测试: demo.xml中的内容: js文件内容: window.onload=function(){ //var v=returnXMLDOM(); //v.loadXML('<root> ...

  8. ElasticSearch[v6.2] 在实际项目中的应用

    摘要:本文所讲述的内容,为ElasticSearch(以下简称ES)全文搜索引擎在实际大数据项目的应用:ES的底层是开源库 Lucene.但是,你没法直接用 Lucene,必须自己写代码去调用它的接口 ...

  9. HTTP中的重定向和请求转发的区别(转)

    时间长有些忘了,转篇文章加深一下印象: 一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下:request.getRequestDispatcher("new.jsp&qu ...

  10. CF894A QAQ

    CF894A QAQ 题意翻译 题目大意: 给定一个字符串,字符串长度<=100,现在要求出字符串中'QAQ'的个数,注意,'QAQ'可以不连续,只要有顺序就可以了,例如QABQ也有一个QAQ ...