caffe学习之convert_imageset:图片格式转lmdb/leveld格式

来源:互联网 发布:mac 中文字体 ttf 编辑:程序博客网 时间:2024/05/20 12:49

caffe中data_layer的输入数据的格式是lmdb或者leveld格式的。(hdf5_data_layer等layer暂不考虑)但是我们面对的原始数据往往是.jpg/.png格式的,所以需要进行一定的处理才能作为输入数据。

在这里主要用到的工具便是caffe源码中已经写好的一个工具caffe/tools/convert_imageset.cpp,只需对图片做一些处理便可自动帮你将图片格式转成lmdb或leveld格式。
下面就介绍下如何使用这个工具。

第一步,生成train.txt文件

根据自己的图片数据集,生成对应的train.txt文件。文件里的内容,每一行有两个字段,分别为:图片名和label,比如:
train.txt里面放着你的训练集

pic1.jpg 1pic2.jpg 2pic3.jpg 2pic4.jpg 3...

至于怎么生成train.txt需要你自己写对应的脚本生成,这里变不做介绍。

第二步,输入相关命令生成lmdb或leveld文件

先上一个例子,比如命令行下输入以下命令:

CAFFEROOT$ ./build/tools/convert_imageset data/img/ train.txt data/lmdb_data/my_lmdb

将会把CAFFEROOT/data/img/CAFFEROOT /train.txt(路径要对)中所定义的所有图片,全都转化成lmdb(默认)格式,并保存在CAFFEROOT$/data/lmdb_data/my_lmdb目录下,运行后,你会发现在此目录下多了data.mdb和lock.mdb两个文件。

规范的格式如下:

convert_imageset [FLAGS] ROOTFOLDER/ LISTFILE DB_NAME

convert_imageset : 可执行文件的命令
[FLAGS]: 可选参数,下面会讲
ROOTFOLDER/ : 图片保存的目录(绝对路径或者相对路径对应上即可)
LISTFILE .txt : 文件的位置(绝对路径或者相对路径对应上即可)
DB_NAME : 保存lmdb之后的文件位置(绝对路径或者相对路径对应上即可)

[FLAGS]参数:

DEFINE_bool(gray, false,    "When this option is on, treat images as grayscale ones");DEFINE_bool(shuffle, false,    "Randomly shuffle the order of images and their labels");DEFINE_string(backend, "lmdb",        "The backend {lmdb, leveldb} for storing the result");DEFINE_int32(resize_width, 0, "Width images are resized to");DEFINE_int32(resize_height, 0, "Height images are resized to");DEFINE_bool(check_size, false,    "When this option is on, check that all the datum have the same size");DEFINE_bool(encoded, false,    "When this option is on, the encoded image will be save in datum");DEFINE_string(encode_type, "",    "Optional: What type should we encode the image as ('png','jpg',...).");

从源代码中即可看到参数一共有gray、shuffle、backend、resize_width、resize_height、check_size、encoded、encode_type几种。

gray: 是否是灰度图,默认不是
shuffle: 是否需要对图片数据打乱,默认不需要
backend: 保存的格式,默认lmdb
resize_width: resize图片的宽度,因为caffe对于每个batch的图片size大小需要一致
resize_height: resize图片的高度
check_size: 是否检查图片大小是否一致,默认不检查
encoded: 是否将原始的图片放入lmdb或者leveld中,默认否
encode_type: encoded为ture时,需要注明原始图片的格式是什么,‘.jpg,.png’等等

阅读全文
0 0
原创粉丝点击