caffe:用自己的数据训练网络mnist
画黑底白字的软件:KolourPaint。
假设所有“1”的图片放到名字为1的文件夹下。(0-9类似)。。获取每个数字的名称文件后,手动表上标签。然后合成train。txt
1、获取文件夹内全部图像的名称:
find ./1 -name '*.png'>1.txt
//此时的1.txt文件中的图像名称包括路劲信息,要把前面的路径信息去掉。
$ sudo sed -i "s/.\/1\///g" 1.txt //(\表示转义,所以这里用双引号而不是单引号)
2、要在1.txt 内的每个名称后面加上标签
1.txt:
1101.png 1
1102.png 1
.....(如此)
3、将图片数据转换为lmdb格式的数据
caffe/examples下建一个文件保存训练用的文件:sd_mnist
3.1 sd_mnist下创建一个sd_create_lmdb.sh用来转换图片格式:
sudo vim sd_create_lmdb.sh ,内容如下:
#!/usr/bin/env sh
# Create the imagenet lmdb inputs
# N.B. set the path to the imagenet train + val data dirs
EXAMPLE=examples/sd_mnist (!注意:这是你在examples下创建的目录)
DATA=data/sd_mnist (!注意:就是你在data文件夹下新建目录,里面有两个图片集(训练和测试训练集)及上面所说的两个txt)
TOOLS=build/tools
TRAIN_DATA_ROOT=data/sd_mnist/train/ (!注意:就是训练图片集路径)
VAL_DATA_ROOT=data/sd_mnist/test/ (!注意:就是测试图片集路径)
# Set RESIZE=true to resize the images to 256x256. Leave as false if images have
# already been resized using another tool.
RESIZE=true
if $RESIZE; then
RESIZE_HEIGHT=28
RESIZE_WIDTH=28
else
RESIZE_HEIGHT=0
RESIZE_WIDTH=0
fi
if [ ! -d "$TRAIN_DATA_ROOT" ]; then
echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"
echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \
"where the ImageNet training data is stored."
exit 1
fi
if [ ! -d "$VAL_DATA_ROOT" ]; then
echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"
echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \
"where the ImageNet validation data is stored."
exit 1
fi
echo "Creating train lmdb..."
GLOG_logtostderr=1 $TOOLS/convert_imageset \
--resize_height=$RESIZE_HEIGHT \
--resize_width=$RESIZE_WIDTH \
--shuffle \
$TRAIN_DATA_ROOT \
$DATA/train.txt \ (!注意路劲)
$EXAMPLE/mnist_train_lmdb
echo "Creating test lmdb..."
GLOG_logtostderr=1 $TOOLS/convert_imageset \
--resize_height=$RESIZE_HEIGHT \
--resize_width=$RESIZE_WIDTH \
--shuffle \
$VAL_DATA_ROOT \
$DATA/test.txt \ (!注意路劲)
$EXAMPLE/mnist_test_lmdb
echo "Done."
-----------------------------------------------------------------------
3.2 运行sh example/sd_mnist/sd_create_lmdb.sh
如果成功的话,终端返回的信息中,图片是有大小的而不是0kb。并且在examples/sd_mnist下会有两个文件:mnist_train_lmdb,mnist_test_lmdb它们里面都是data.mdb和lock.mdb。
4、对我们的数据集进行训练:下面的文件都是从caffe\examples\mnist下复制到caffe\examples\sd_mnist下来进行修改的。主要是修改路径信息,整个网络保持不变。
4.1第一个sh文件是train_lenet,sh
#!/usr/bin/env sh
set -e
./build/tools/caffe train --solver=examples/sd_mnist/lenet_solver.prototxt $@
4.2、复制lenet_solver.prototxt文件,并修改:
# The train/test net protocol buffer definition
net: "examples/sd_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/sd_mnist/lenet"
# solver mode: CPU or GPU
solver_mode: CPU
4.3、lenet_train_test.prototxt复制从mnist文件夹到当前文件夹下
修改路径
name: "LeNet"
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
source: "examples/sd_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/sd_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"
}
4.4 lenet.prototxt复制从mnist文件夹到当前文件夹下,不用修改
4.5 运行 sh example/sd_mnist/train_lenet.sh
没报错,出来accuracy loss这些,说明成功!!
参考:http://blog.csdn.net/xiaoxiao_huitailang/article/details/51361036
caffe:用自己的数据训练网络mnist的更多相关文章
- 用caffe跑自己的数据,基于WINDOWS的caffe
本文详细介绍,如何用caffe跑自己的图像数据用于分类. 1 首先需要安装过程见 http://www.cnblogs.com/love6tao/p/5706830.html 同时依据上面教程,生成了 ...
- Windows下用Caffe跑自己的数据(遥感影像)
1 前言 Caffe对于像我这样的初学者来说是一款非常容易上手的深度学习框架.关于用Caffe跑自己的数据这样的博客已经非常多,感谢前辈们为我们提供的这么好的学习资源.这里我主要结合我所在的行业,说下 ...
- 『计算机视觉』Mask-RCNN_训练网络其三:训练Model
Github地址:Mask_RCNN 『计算机视觉』Mask-RCNN_论文学习 『计算机视觉』Mask-RCNN_项目文档翻译 『计算机视觉』Mask-RCNN_推断网络其一:总览 『计算机视觉』M ...
- 『计算机视觉』Mask-RCNN_训练网络其二:train网络结构&损失函数
Github地址:Mask_RCNN 『计算机视觉』Mask-RCNN_论文学习 『计算机视觉』Mask-RCNN_项目文档翻译 『计算机视觉』Mask-RCNN_推断网络其一:总览 『计算机视觉』M ...
- 『计算机视觉』Mask-RCNN_训练网络其一:数据集与Dataset类
Github地址:Mask_RCNN 『计算机视觉』Mask-RCNN_论文学习 『计算机视觉』Mask-RCNN_项目文档翻译 『计算机视觉』Mask-RCNN_推断网络其一:总览 『计算机视觉』M ...
- Caffe Blob针对图像数据在内存中的组织方式
Caffe使用Blob结构在CNN网络中存储.传递数据.对于批量2D图像数据,Blob的维度为 图像数量N × 通道数C × 图像高度H × 图像宽度W 显然,在此种场景下,Blob使用4维坐标定位数 ...
- Caffe学习系列(12):训练和测试自己的图片--linux平台
Caffe学习系列(12):训练和测试自己的图片 学习caffe的目的,不是简单的做几个练习,最终还是要用到自己的实际项目或科研中.因此,本文介绍一下,从自己的原始图片到lmdb数据,再到训练和测 ...
- Ubuntu16.04下caffe CPU版的图片训练和测试
一 数据准备 二.转换为lmdb格式 1.首先,在examples下面创建一个myfile的文件夹,来用存放配置文件和脚本文件.然后编写一个脚本create_filelist.sh,用来生成train ...
- AI:拿来主义——预训练网络(一)
我们已经训练过几个神经网络了,识别手写数字,房价预测或者是区分猫和狗,那随之而来就有一个问题,这些训练出的网络怎么用,每个问题我都需要重新去训练网络吗?因为程序员都不太喜欢做重复的事情,因此答案肯定是 ...
随机推荐
- 如何评价苹果中国官网 iOS 8 介绍页面的文案「开发者的大事、大快所有人心的大好事」?[转自知乎]
在什么是「苹果式中文」答案中,小七得出了这个结论: 「苹果式中文」是指句子结构破碎,经常缺乏主语,滥用排比,顶真,偏正短语,和不恰当四字词的广告文体. (有关什么是苹果式中文,小七原来贴错地方了TAT ...
- Apache commons-client authentication(授权)
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswo ...
- C#基础——winform应用上传图片到SQLServer数据库
前言 之前通过winform与SQL Server的交互一直局限于文本.数字等信息,都可以通过string的方式来传输,但是比如音乐.图片等特殊格式的文件要如何与SQL Server数据库进行交互呢? ...
- 面试时被问到js的绑定事件,我居然不知道怎么回答。回来查了下,做个笔记
事件绑定是几种方法 以下为例: <button id='btn'>click me</button> function Btn(){ alert('click'); } 1.直 ...
- NOIP2014提高组解方程
其实没有太难 但是不知道的话想不到 考场上大概有50分吧 #include <iostream> #include <stdio.h> #include <queue&g ...
- Android驱动开发前的准备(一)
Android系统移植与驱动开发概述 1.1 Android 系统架构 1.2 Android系统移植的主要工作 1.3 查看linux内核版本 1.4 linux内核版本号的定义规则 1.5 lin ...
- rigidbody2D.velocity 提示缺少using?用的unity5?
请用 GetComponent<Rigidbody2D>().velocity
- dandelion datatables : Page index must not be less than zero!
java.lang.IllegalArgumentException: Page index must not be less than zero! at org.springframework.da ...
- bash获取properties文件资源
#!/bin/sh prop_value="" function getProperty() { file=$ prop_key=$ prop_value=`cat $file | ...
- NOIP2016报零记
其实,NOIP2016已经于10天之前就结束了,但是由于种种原因,没有写总结. 现在就来填上这个坑吧. DAY1: T1:一道简(kun)单(nan)的模拟,虽然ac,但是考试的时候总觉得怪怪的.并且 ...