Caffe框架下的图像回归测试

参考资料:

1. http://stackoverflow.com/questions/33766689/caffe-hdf5-pre-processing

2. http://corpocrat.com/2015/02/24/facial-keypoints-extraction-using-deep-learning-with-caffe/

3. http://stackoverflow.com/questions/31774953/test-labels-for-regression-caffe-float-not-allowed/31808324#31808324

在caffe的框架上,如何根据现有的图像及其分值、标准差对图像进行回归,caffe_model是Imagenet训练得到模型、solver.prototxt和train_val.prototxt。本次的数据来源为:http://live.ece.utexas.edu/。 注意:所有的工程建立都是在caffe的根目录下执行。

Author: jinxj

Email:

Data:     2016.3.30

Version: First Version

一、环境:

编码成功的caffe;图像数据库。

二、数据预处理:

在data下建立文件夹“face_key_point”,之后所有的数据处理均在“face_key_point”文件夹下,该文件夹下包含images文件夹和其他文件(.py和.txt文件)。

1. images文件夹里包含所有的train、test和val图像;

2. 其他文件

(1) meancreat_hdf5.py

用于产生train.h5和train_h5_list.txt(注意检查该文件中train.h5的路径是否是绝对路径,如果不是,请改一下),可以用HDF5View查看train.h5检查数据是否正确,ImageNum*3*227*227(SIZE,最好和Imagenet模型的保持一致),适合所有类型的图像(.JPG和.bmp同时处理),代码里对图像已经做过均值处理,具体代码如下:

######################################################################################

import h5py, os

import caffe

import numpy as np

SIZE = 227 # fixed size to all images

with open( 'train.txt', 'r' ) as T :

lines = T.readlines()

with open( 'num.txt', 'r' ) as M :

lines_Num = M.readlines()

# If you do not have enough memory split data into

# multiple batches and generate multiple separate h5 files

X = np.zeros( (len(lines), 3, SIZE, SIZE), dtype='f4' )

XMean = np.zeros( (len(lines), 3, 1, 1), dtype='f4' )

y = np.zeros( (len(lines_Num)), dtype='f4' )

print len(lines)

for i,l in enumerate(lines):

sp = l.split('\r\n')

img = caffe.io.load_image( sp[0] )

img = caffe.io.resize_image( img, (SIZE, SIZE), 3 ) # resize to fixed size

img = np.transpose(img, (2, 0, 1))

# you may apply other input transformations here...

X[i] = img

# Compute the mean of all images

XMean = sum(X)/len(X)

X = X - XMean

print 'image OK'

for j, eachLine in enumerate(lines_Num):

y[j] = float(float(eachLine)/92.432)

print 'Num OK'

with h5py.File('train.h5','w') as H:

H.create_dataset( 'X', data=X ) # note the name X given to the dataset!

H.create_dataset( 'y', data=y ) # note the name y given to the dataset!

with open('train_h5_list.txt','w') as L:

L.write( '/home2/xj_jin/experiment/caffe-master/data/face_key_point/train.h5' ) # list all h5 files you are going to use

######################################################################################

(2) meancreat_hdf5test.py

同理,用于产生test.h5和test_h5_list.txt(检查路径),代码里对图像已经做过均值处理,具体代码如下:

######################################################################################

import h5py, os

import caffe

import numpy as np

SIZE = 227 # fixed size to all images

with open( 'test.txt', 'r' ) as T :

lines = T.readlines()

with open( 'num_test.txt', 'r' ) as M :

lines_Num = M.readlines()

# If you do not have enough memory split data into

# multiple batches and generate multiple separate h5 files

X = np.zeros( (len(lines), 3, SIZE, SIZE), dtype='f4' )

XMean = np.zeros( (len(lines), 3, 1, 1), dtype='f4' )

y = np.zeros( (len(lines_Num)), dtype='f4' )

print len(lines)

for i,l in enumerate(lines):

sp = l.split('\r\n')

img = caffe.io.load_image( sp[0] )

img = caffe.io.resize_image( img, (SIZE, SIZE), 3 ) # resize to fixed size

img = np.transpose(img, (2, 0, 1))

# you may apply other input transformations here...

X[i] = img

# Compute the mean of all images

XMean = sum(X)/len(X)

X = X - XMean

print 'image OK'

for j, eachLine in enumerate(lines_Num):

y[j] = float(float(eachLine)/87.0656)

print 'Num OK'

with h5py.File('test.h5','w') as H:

H.create_dataset( 'X', data=X ) # note the name X given to the dataset!

H.create_dataset( 'y', data=y ) # note the name y given to the dataset!

with open('train_h5_list.txt','w') as L:

L.write( '/home2/xj_jin/experiment/caffe-master/data/face_key_point/test.h5' ) # list all h5 files you are going to use

######################################################################################

(3) train.txt和num.txt

train.txt存放着待训练的图片,num.txt存放待训练图片的分值。具体格式如下:

train.txt:(建议检查路径是否存在空格)

/home2/xj_jin/experiment/caffe-master/data/face_key_point/images/00t1.bmp

/home2/xj_jin/experiment/caffe-master/data/face_key_point/images/00t2.bmp

/home2/xj_jin/experiment/caffe-master/data/face_key_point/images/00t3.bmp

/home2/xj_jin/experiment/caffe-master/data/face_key_point/images/00t4.bmp

/home2/xj_jin/experiment/caffe-master/data/face_key_point/images/00t5.bmp

num.txt:

63.9634

25.3353

48.9366

35.8863

66.5092

(4) test.txt和num_test.txt

test.txt存放着待训练的图片,num_test.txt存放待训练图片的分值。具体格式和上述一致。

3. 生成train.h5和test.h5数据

在caffe/data/face_key_point下执行:

命令: python meancreat_hdf5.py和 python meancreat_hdf5test.py

三、模型修改

在caffe/model下建立自己的文件夹“face_key_point”,将下载得到的Imagenet的模型和网络结构bvlc_reference_caffenet.caffemodel、train_val.prototxt、solver.prototxt放入该文件夹下。

1. 修改solver.prototxt

#######################################################################################

net: "models/face_key_point/train_val.prototxt"                             -----------------------路径要改

test_iter: 100

test_interval: 1000

# lr for fine-tuning should be lower than when starting from scratch

base_lr: 0.001                                                                                    -----------------------基础学习率根据实际情况改

lr_policy: "step"

gamma: 0.1

# stepsize should also be lower, as we're closer to being done

stepsize: 20000

display: 20

max_iter: 100000

momentum: 0.9

weight_decay: 0.0005

snapshot: 10000

snapshot_prefix: "models/face_key_point/finetune_face_key_point"     ---------------------------路径+自己取名新生成的模型名称

# uncomment the following to default to CPU mode solving

# solver_mode: CPU                                                                                 ---------------------------根据自己的实际情况设置

#######################################################################################

2. 修改train_val.prototxt网络结构

这里只提供部分的网络结构。

#######################################################################################

name: "FaceKeyPointCaffeNet"                                                                   ---------------------------自己训练的网络模型名称

layer {                                                                                                            ---------------------------处理.h5数据的data层

name: "data"

type: "HDF5Data"

top: "X"                                                                             ---------------------------和meancreat_hdf5test.py中最后的输出保持一致,原来是data,现在是X

top: "y"                                                                              ---------------------------和meancreat_hdf5test.py中最后的输出保持一致,原来是label,现在是y

include {

phase: TRAIN

}

hdf5_data_param {

source: "data/face_key_point/train_h5_list.txt"            ---------------------------保存的train.h5的绝对路径,可以处理多个.h5数据

batch_size: 64

}

}

layer {

name: "data"

type: "HDF5Data"

top: "X"

top: "y"

include {

phase: TEST

}

hdf5_data_param {

source: "data/face_key_point/test_h5_list.txt"

batch_size: 100

}

}

卷积层、池化层、全连接层、损失等

#######################################################################################

最后,因为本次处理的是一个回归问题,所以最后的损失函数(loss)应该是一个回归,而不是分类,保证最后几层的bottom是y,而不是label。

四、训练

注意改掉对应路径即可:

./build/tools/caffe train --solver=models/face_key_point/solver.prototxt --weights=models/face_key_point/bvlc_reference_caffenet.caffemodel -gpu 0

Caffe框架下的图像回归测试的更多相关文章

  1. caffe框架下目标检测——faster-rcnn实战篇操作

    原有模型 1.下载fasrer-rcnn源代码并安装 git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git 1) ...

  2. Caffe使用step by step:caffe框架下的基本操作和分析

    caffe虽然已经安装了快一个月了,但是caffe使用进展比较缓慢,果然如刘老师说的那样,搭建起来caffe框架环境比较简单,但是完整的从数据准备->模型训练->调参数->合理结果需 ...

  3. 【神经网络与深度学习】Caffe使用step by step:caffe框架下的基本操作和分析

    caffe虽然已经安装了快一个月了,但是caffe使用进展比较缓慢,果然如刘老师说的那样,搭建起来caffe框架环境比较简单,但是完整的从数据准备->模型训练->调参数->合理结果需 ...

  4. caffe框架下目标检测——faster-rcnn实战篇问题集锦

    1.问题 解决方案:没编译好,需要在lib下编译make 需要在caffe-fast-rcnn下编译make或者make all -j16  ,还需要make pycaffe 2.问题 解决方案:/p ...

  5. 转 Yolov3转化Caffe框架详解

    转自https://blog.csdn.net/watermelon1123/article/details/82083522 前些日子因工程需求,需要将yolov3从基于darknet转化为基于Ca ...

  6. Caffe框架,图像数据转换成LMDB数据格式

    小码农最近在研究深度学习,对所学知识做点记录,以供以后翻阅.在Caffe框架中,数据的格式都是LMDB的,如何将图像数据转换成这个格式呢? 首先,将图像数据和标签生成txt文档,执行一下代码: fin ...

  7. 【机器学习PAI实践十】深度学习Caffe框架实现图像分类的模型训练

    背景 我们在之前的文章中介绍过如何通过PAI内置的TensorFlow框架实验基于Cifar10的图像分类,文章链接:https://yq.aliyun.com/articles/72841.使用Te ...

  8. Caffe学习笔记4图像特征进行可视化

    Caffe学习笔记4图像特征进行可视化 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hit201 ...

  9. Caffe windows下安装攻略

    Caffe 是一个高效的深度学习框架,鉴于不想折腾装个双系统,最近鼓捣了下用caffe源码在windows进行编译.非常感谢Yangqing Jia博士的caffe开源代码.Neil Z.Shao's ...

随机推荐

  1. html()、text()、val()、innerHTML、value()的区分

    以上的方法可用于一般的html标签(div)与input中分别进行讨论 1.html(): jQuery方法,用于一般标签中,可读写,可以获得写入html标签. 2.text(): jQuery方法, ...

  2. .Net 4.5 的async 和await 的简单理解使用

    原文地址:http://www.cnblogs.com/HJL-Blog/p/4432632.html 所谓的异步编程是利用CPU空闲时间和多核的特性,它所返回的Task或Task<TResul ...

  3. Learn ZYNC (6)

    最近在关注的问题是怎么样从ps端丢数据到ram, 然后用ip核进行处理后再输送到ram,ps端可以读取. 参考文献:[OpenHW参赛手记]AXI-Stream接口开发详细流程 首先按照作者的探索思路 ...

  4. CI框架入门2

    文件目录与布局 1.user_guide    用户手册,可删 2.readme.rst    说明,可删 3.license.txt     证书,可删 4..gitignore composer. ...

  5. spring mvc@RequestParam根据参数名获取传入参数值

    在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取.这里主要 ...

  6. C#快捷键和注释

    C#注释    //单行注释    /*....*/快注释    ///说明注释,一般用于封装的属性和方法上   #region和#endregion折叠注释,可以将代码折叠 static void ...

  7. 【ipv6惹的祸】curl 超时

    偶然发现 最近在公司日志平台 总是可以看到很多关于php curl的错误信息 Operation timed out after 0 milliseconds with 0 out of 0 byte ...

  8. 设置一个顺手的Xcode

    授权转载,作者:吴白(微博) 手指在键盘上飞速跳跃,终端上的代码也随着飞舞,是的这确实很酷.优秀的程序员总是这么一群人,他们不拘于现状,不固步自封,他们喜欢新奇的事,他们把自己发挥到极致. 指法攻略 ...

  9. android必须要进行为不同分辨率设备切图

    以分辨率为1920×1080的android设备为例.在项目中加载资源的位置为xxhdpi文件夹: 例如将图片放入mdpi文件夹中就会出现,图片的横纵尺寸分别乘3被的后果,因为它认为在这个文件夹中是低 ...

  10. oracle 中的cascade

    级联删除,比如你删除某个表的时候后面加这个关键字,会在删除这个表的同时删除和该表有关系的其他对象