转自:http://www.jianshu.com/p/9644f7ec0a03

和 http://www.cnblogs.com/denny402/p/5083300.html

Caffe训练自己的数据集并用Python接口预测

本教程作者是清华大学在读硕士金天童鞋,在当地较为英俊的男子,大家对教程有任何疑问欢迎联系我:WeChat
jintianiloveu,同时也欢迎大家转载评论,不过请保留这段版权信息,桑口~

之前写了一个博客讲Caffe训练自己的数据集,但是是英文版的,为了防止大家说我有装逼的嫌疑我还是写个中文版吧,而且这次是分五类,简洁明了,caffe入门必备,大家有任何疑问直接在下面评论吧,最后马上过年了大家给我的github repo挨个赞一个呗,地址在此我拯救世界的梦想就靠你们了!

Caffe安装

首先还是简要的说一下Caffe在Ubuntu下的安装过程,具体安装过程如下:

  • 安装前置依赖
    前置依赖项较多,好在Ubuntu下都可以通过apt下载安装,这个命令一阵复制粘帖吧:
sudo apt-get upgrade
sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y --no-install-recommends libboost-all-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev

然后安装OpenCV3.1,这个安装已经轻车熟路了。大家对此有和疑问的查看我这个博客,找到Ubuntu下安装OpenCV的教程进行安装。
接下来就简单了,不过为了防止后面导入caffe出问题,建议软链接一下这个东西:

sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python3.so

接着clone项目:

git clone https://github.com/BVLC/caffe.git

然后在我的Repo里面下载MakefileMakefile.config来替换原有的。接着就是安装python依赖,这里不要按照其他教程里面的安装官方的caffe/python下的requirement.txt,instead,下载我的,然后运行:

sudo pip3 install --upgrade -r python/requirements_python3.txt

后面的requirements_python3.txt也可以从上面的Repo下载。
接着直接安装就行了:

make all -j
make pycaffe

测试一下是否安装成功:

./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh
./examples/mnist/train_lenet.sh

如果成功你就会看到caffe在训练mnist了,大概几千次之后准确度就可以达到99%,非常好。

Caffe生成并训练自己的数据集

本教程所有的代码,文件目录结构可以在我的github上下载,点这里,下载之后你会看到一个完整的caffe训练架构,但是数据还得另外下载,我在这里只是上传了5类图片的样张图片,真正的数据集大概有600张的样子。下载地址为这里。
接下来要生成caffe需要的数据了,步骤如下,大家先不要方,生成数据是整个过程最复杂的部分,不过这个部分也不是很麻烦,我精简一下步骤:

Step 1 Generate the image name file

Run caffe_path_gen.py in your terminal, just type:

python3 caffe_path_gen.py -train=/home/jfg/caffe_tiny5/tiny5/train -shuffle=True

-shuffle is optional, because caffe can do this too.
In this tutorial we only have train data in image_data folder, we don't have test image, so we just generate the train image path, and manully divide them into train and test. But if you have test data folder, you also can type:

python3 caffe_path_gen.py -train=/home/jfg/Documents/PythonSpace/caffe_tiny5/tiny5/train -test=/home/jfg/Documents/PythonSpace/caffe_tiny5/tiny5/test -valid=/home/jfg/Documents/PythonSpace/caffe_tiny5/tiny5/valid

the valid data only generate image path without labels.
After this, you gonna have train.txt, words.txt.

Step 2 Split train.txt into to train.txt and test.txt

Before split please remove the path just make it like this:

/flower/683.jpg 3
/bus/334.jpg 0
/bus/336.jpg 0
/dinosaur/481.jpg 2
/dinosaur/436.jpg 2
/bus/327.jpg 0
/elephant/595.jpg 4
/bus/357.jpg 0
/bus/393.jpg 0
/bus/375.jpg 0
/dinosaur/453.jpg 2
/flower/654.jpg 3
/dinosaur/491.jpg 2
/bus/365.jpg 0
/flower/636.jpg 3
/flower/629.jpg 3
/bus/347.jpg 0
/bus/398.jpg 0
/horse/761.jpg 1
/elephant/560.jpg 4
/dinosaur/449.jpg 2
/elephant/531.jpg 4
/horse/794.jpg 1
/horse/743.jpg 1
/elephant/586.jpg 4

But stay with the class prefix

Step 3 Generate Caffe LMDB data file
First mkdir a data folder just inside the project directory, and place train.txt and test.txt into it.
And then open caffe_create_lmdb.sh and just edit the following two lines:

TRAIN_DATA_ROOT=/home/jfg/Documents/PythonSpace/caffe_tiny5/tiny5/train
VAL_DATA_ROOT=/home/jfg/Documents/PythonSpace/caffe_tiny5/tiny5/valid

在这一步中,确保TRAIN_DATA_ROOT和上面的train.txt中的文件名能够组成完整的路径即可。

Simply edit two lines, leave other along.
Then type:

bash caffe_create_lmdb.sh

Tip: Anything wrong, check you mkdir a data folder, and have train.txt and test.txt in it.
OK, after this, you gonna have two new folder in your data folder, that is:
caffe_train_lmdb
caffe_val_lmdb
this is what we need to feed into caffe net and it is nothing with your original image anymore! It's complete and clean! Do not warry about path wrong anymore! Very nice!

Step 4 Generate Mean Binary File
This step is very easy, don't change anything ,just type this in your terminal:

bash caffe_make_mean.sh

And you gonna have caffe_mean.binaryproto file in your data folder.

开始Caffe训练

you already have your data, just finish 80% work. 20% to go. Next, we gonna using solver folder. In this folder we have a solver.prototxt and a train_test.prototxt.
solver.prototxt is the net pramas setting file.
train_test.prototxt is the net structure setting file and your lmdb data feed into net in here.

layer {name: "cifar"type: "Data"top: "data"top: "label"include {phase: TRAIN}transform_param {mean_file: "/home/jfg/caffe_tiny5/data/caffe_mean.binaryproto"}data_param {source: "/home/jfg/caffe_tiny5/data/caffe_train_lmdb"batch_size: 100backend: LMDB}
}
layer {name: "cifar"type: "Data"top: "data"top: "label"include {phase: TEST}transform_param {mean_file: "/home/jfg/caffe_tiny5/data/caffe_mean.binaryproto"}data_param {source: "/home/jfg/caffe_tiny5/data/caffe_val_lmdb"batch_size: 100backend: LMDB}
}

This 2 layer is data feed layer, so you have to change your data path in here.Make sure it correct.
Open solver.prototxt, find this 2 and edit it:

net: "/home/jfg/caffe_tiny5/solver/caffenet_train_valid.prototxt"
snapshot_prefix: "/home/jfg/caffe_tiny5/model/caffenet"

net: it is your train_test.prototxt file postion, snapshot_prefix is your save model path and prefix name, we place all saved models into a model_snapshot folder with prefix cifar10.

训练

只需要一行命令:

bash train_caffe.sh

重点来了,得到模型之后怎么预测

这里我们使用python接口,直接上代码吧,实际上该的地方也不多:

# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
caffe_test.py
http://www.lewisjin.coding.me
~~~~~~~~~~~~~~~
This script implement by Jin Fagang.
: copyright: (c) 2017 Didi-Chuxing.
: license: Apache2.0, see LICENSE for more details.
"""
import numpy as np
import sys
import os
import cv2caffe_root = '/home/jfg/caffe/'
sys.path.insert(0, caffe_root + 'python')
import caffenet_file = '/home/jfg/Documents/PythonSpace/caffe_tiny5/solver/caffenet_deploy.prototxt'
caffe_model = '/home/jfg/Documents/PythonSpace/caffe_tiny5/solver/model/caffenet_iter_4500.caffemodel'
mean_file = '/home/jfg/Documents/PythonSpace/caffe_tiny5/data/caffe_mean.binaryproto'
print('Params loaded!')caffe.set_mode_gpu()
net = caffe.Net(net_file,caffe_model,caffe.TEST)mean_blob = caffe.proto.caffe_pb2.BlobProto()
mean_blob.ParseFromString(open(mean_file, 'rb').read())
mean_npy = caffe.io.blobproto_to_array(mean_blob)
a = mean_npy[0, :, 0, 0]print(net.blobs['data'].data.shape)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))transformer.set_mean('data', a)
transformer.set_raw_scale('data', 255.0)
transformer.set_channel_swap('data', (2, 1, 0))test_img = 'elephant.jpeg'
im = caffe.io.load_image(test_img)
net.blobs['data'].data[...] = transformer.preprocess('data', im)predict = net.forward()
names = []
with open('words.txt', 'r+') as f:for l in f.readlines():names.append(l.split(' ')[1].strip())print(names)
prob = net.blobs['prob'].data[0].flatten()
print('prob: ', prob)
print('class: ', names[np.argmax(prob)])img = cv2.imread(test_img)
cv2.imshow('Image', img)
cv2.waitKey(0)

OK,本教程到此结束,欢迎乘坐本次老司机列车。这几天一直没有更新博客不是没有写,而且写了没有上传,还是得发时间整理整理,笔耕不啜才是一代文豪应该做的事情啊。

近期计划

这是一个花絮,最近我还是决定干点事情了,接下来我会研究一下如何用目标检测算法来识别交通信号灯,这个在无人驾驶领域是肯定会遇到的一个问题,具体来说它有几个难点:

  • 检测信号灯的位置

要检测信号灯就需要训练图片,标注信号灯的方框,这个好办,现有的网络可以做到很好,接下来有个问题就是第二个点。

  • 检测信号等每个路口转向的信号等情况

比如我检测到了信号灯,但是这个信号灯是左转通行还是右转通行呢?是左转通行直行禁止右转禁止还是其他情况呢?而且我检测算法可能在一个视角中检测多个红绿灯,但是我真正感兴趣的只是我前方的红绿灯,那么如何来却分识别出来的红绿灯也是个问题。

这两个问题如何解决还得看数据标注的情况。
训练接口

作者:Nicholas_Jela
链接:http://www.jianshu.com/p/9644f7ec0a03
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

caffe 训练自己的数据相关推荐

  1. win10下使用caffe训练自己的数据,车牌二分类

    caffe编译参考:http://blog.csdn.net/cym1990/article/details/72630584 1.数据格式转换 Caffe采用leveldb或者lmdb的数据格式. ...

  2. win10用caffe训练自己的数据

    1.将数据按照种类放在不同的文件夹下,每个文件夹下新建一个bat文件,内容类似 dir/s/on/b>d:/train.txt 或者dir /b/s/p/w *.jpg > train.t ...

  3. caffe 训练自己的数据 参考博客网址

    参考网址: http://blog.csdn.net/qqlu_did/article/details/47131549 http://www.docin.com/p-871820919.html 转 ...

  4. 利用Caffe实现mnist的数据训练

    阿里云的参考文档:https://help.aliyun.com/document_detail/49571.html 在文档里提供了caffe的一个案例,利用Caffe实现mnist的数据训练.准备 ...

  5. Caffe实践】如何利用Caffe训练ImageNet分类网络

    Caffe实践]如何利用Caffe训练ImageNet分类网络 源文章:https://github.com/BVLC/caffe/tree/master/examples/imagenet 由于要使 ...

  6. Caffe训练ImageNet

    Caffe训练ImageNet使用的是NIPS 2012 paper论文的算法. 1.准备数据. 假设已经下载好数据集和验证集,存储路径为: /path/to/imagenet/train/n0144 ...

  7. Caffe 训练时loss等于87.3365且保持不变的原因及解决方法

    如题,在caffe训练时,遇到这个特殊的数字之后,loss会一直就是这个数字. 网上虽然有很多针对这个问题调参的trick,但少有详细的分析,因此,有必要研究一下caffe的源代码. softmax的 ...

  8. caffe训练输入数据格式介绍(LMDB/imagelist)

    目录 引言 LMDB数据格式 imagelist数据 LMDB数据生成 引言 这里介绍caffe训练数据的两种输入格式,分别是LMDB,以及普通的imagelist图片输入.主要贴出两种输入数据格式的 ...

  9. caffe训练技巧总结

    一 数据集的制作 我们经常用到的原始数据是图片文件,如jpg,jpeg,png,tif等格式的,而且有可能图片的大小还不一致,而在caffe中经常使用的数据类型是lmdb或leveldb. 1. 数据 ...

最新文章

  1. hive删除hbase数据_Hive进阶:Hive通过外部表操作Hbase数据
  2. C#之windows桌面软件第一课:倒时器软件
  3. JFinal开发环境搭建,JFinal开发案例
  4. 嵌入式烤箱能不能放台面上_2021年开放式厨房怎么设计?先来做做嵌入式家电的功课吧!...
  5. 优酷开放sdk-setOnerroListener
  6. mysql查看数据库和表的占用空间大小
  7. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第22篇]如何用蒙哥马利算法表示一个数字和多个相乘的数字
  8. 给力!使用MacPilot解锁macOS 1200多个隐藏功能!(支持M1处理器mac)
  9. 查找目录下固定文件目录以及路径(自然排序)
  10. python猜数字统计游戏次数_猜数字游戏(Python)
  11. spring使用@Value标签读取*.properties文件的中文乱码问题的解决
  12. Mac 查看jdk版本
  13. 2021年 全网最细大数据学习笔记(一):初识 Hadoop
  14. xp系统开机自检很久_xp系统电脑开机硬盘自检时间很长如何处理
  15. 追书神器 三星s8+ 语音朗读锁屏后中断问题解决办法
  16. 如何判断用户是否关注微信订阅号,亲测实战步骤
  17. vue使用print.js打印插件
  18. 中兴olt-c300常用命令
  19. matlab网络通讯消息响应,nevoVI FIRE——实现PC与CAN和LIN网络通信
  20. 【虚拟语气练习题】对现在的虚拟

热门文章

  1. 一些常用的网址,分享给需要的朋友
  2. LaTex学习(三)LaTex文档类与宏包
  3. 最新发布:2022斯坦福AI指数报告
  4. 浅谈航飞重叠度,利用小工具计算大疆精灵、M300、垂起固定翼航飞重叠度——拒绝航飞空洞
  5. 洛谷P1055题解 [NOIP2008 普及组] ISBN 号码
  6. echarts地图api series_echarts实现中国地图数据展示
  7. 《经济学通识》七、医患关系,毒奶和产品质量
  8. 【云计算与大数据概述 】课堂笔记
  9. iOS版橘花伪春菜开发笔记1.3之shell的pna文件的处理
  10. 2021年度电脑CPU电梯图