需要的基础知识:OpenCV(建议去看官网的图文教程), LevelDB(http://dblab.cs.toronto.edu/courses/443/2014/tutorials/leveldb.html 这个是我学习的教程)
今天在看caffe的代码,发现所用到的的数据都是leveldb的格式,而如果我们要是有形如imagenet的图片和标签的数据的话,就需要将他们给转化成leveldb的格式,caffe的代码中给了例子,在create_imagenet.sh 中,而这个shell文件主要就是调用了build/tools/convert_imageset.bin
举个例子
其TRAIN_DATA_ROOT,就是图片的路径, $DATA/train.txt,里面存放的是图片的名字,和图片的label(注意,这里的label应当从0开始编号,而且要连续,例如有20个类就是0到19,而不要0-10,12-20,这样会出错的), ilsvrc12_train_leveldb 输出名, 1(乱序读取, 0 顺序读取), 后面是图片归一化的大小,应为整数,为0表示不缩放
好了我们进代码来看
// This program converts a set of images to a leveldb by storing them as Datum
// proto buffers.
// Usage:
// convert_imageset [-g] ROOTFOLDER/ LISTFILE DB_NAME RANDOM_SHUFFLE[0 or 1]
// [resize_height] [resize_width]
// where ROOTFOLDER is the root folder that holds all the images, and LISTFILE
// should be a list of files as well as their labels, in the format as
// subfolder1/file1.JPEG 7
// ....
// if RANDOM_SHUFFLE is 1, a random shuffle will be carried out before we
// process the file lines.
// Optional flag -g indicates the images should be read as
// single-channel grayscale. If omitted, grayscale images will be
// converted to color.
#include <glog/logging.h>
#include <leveldb/db.h>
#include <leveldb/write_batch.h>
#include <lmdb.h>
#include <sys/stat.h>
#include <algorithm>
#include <fstream> // NOLINT(readability/streams)
#include <string>
#include <utility>
#include <vector>
#include "caffe/proto/caff