caffe convert_image 初略解析

该博客介绍了如何使用Caffe将图像数据转换为LevelDB格式,涉及基础知识如OpenCV和LevelDB。通过分析convert_imageset.sh脚本和ReadImageToDatum函数,解释了数据转换过程,强调了图片标签连续且从0开始编号的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要的基础知识: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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值