caffe源码阅读(一)convert_imageset.cpp注释
PS:本系列为本人初步学习caffe所记,由于理解尚浅,其中多有不足之处和错误之处,有待改正。
一、实现方法
首先,将文件名与它对应的标签用 std::pair 存储起来,其中first存储文件名,second存储标签,
其次,数据通过 Datum datum来存储,将图像与标签转为Datum 需要通过函数ReadImageToDatum() 来完成,
再次, Datum 数据又是通过datum.SerializeToString(&out)把数据序列化为字符串 string out;,
最后, 将字符串 string out ,通过txn->Put(string(key_cstr, length), out)写入数据库DB。
二、convert_imageset.cpp解析
1.头文件解析
#include <algorithm> //输出数组的内容、对数组进行升幂排序、反转数组内容、复制数组内容等操作
#include <fstream> // NOLINT(readability/streams)
#include <string>
#include <utility> //utility头文件定义了一个pair类型,pair类型用于存储一对数据
#include <vector> //会自动扩展容量的数组 #include "boost/scoped_ptr.hpp" //智能指针头文件
#include "gflags/gflags.h" //Google的一个命令行参数库
#include "glog/logging.h" //日志头文件 #include "caffe/proto/caffe.pb.h"
#include "caffe/util/db.hpp" //引入包装好的lmdb操作函数
#include "caffe/util/format.hpp"
#include "caffe/util/io.hpp" //引入opencv的图像操作函数
#include "caffe/util/rng.hpp" using namespace caffe; // NOLINT(build/namespaces)引入全部caffe命名空间
using std::pair; //引入pair对命名空间
using boost::scoped_ptr;
2.使用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, , "Width images are resized to"); //定义resize的尺寸,默认为0,不转换尺寸
DEFINE_int32(resize_height, , "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',...)."); //是否转换图像格式
注:gflags是google的一个开源处理命令行参数库,具体使用方法可以参照http://blog.csdn.net/lezardfu/article/details/23753741。
3.main函数
int main(int argc, char** argv) {
#ifdef USE_OPENCV
::google::InitGoogleLogging(argv[]); //初始化log
// Print output to stderr (while still logging)
FLAGS_alsologtostderr = ; //log写入stderr文件
#ifndef GFLAGS_GFLAGS_H_
namespace gflags = google;
#endif
gflags::SetUsageMessage("Convert a set of images to the leveldb/lmdb\n"
"format used as input for Caffe.\n"
"Usage:\n"
" convert_imageset [FLAGS] ROOTFOLDER/ LISTFILE DB_NAME\n"
"The ImageNet dataset for the training demo is at\n"
" http://www.image-net.org/download-images\n");
gflags::ParseCommandLineFlags(&argc, &argv, true); //初始化Flags,从命令行传入参数
if (argc < ) {
gflags::ShowUsageWithFlagsRestrict(argv[], "tools/convert_imageset");
return ;
}
const bool is_color = !FLAGS_gray; //通过gflags把宏定义变量的值,赋值给常值变量
const bool check_size = FLAGS_check_size;//检查图像的size
const bool encoded = FLAGS_encoded; //是否编译(转换)图像格式
const string encode_type = FLAGS_encode_type; //要编译的图像格式
std::ifstream infile(argv[]); //创建指向train.txt的文件读入流
std::vector<std::pair<std::string, int> > lines; //定义向量变量,向量中每个元素为一个pair对,pair对有两个成员变量,
//一个为string类型,一个为int类型;其中string类型用于存储文件名,int类型,感觉用于存数对应类别的id
std::string line;
size_t pos;
int label;
//读取train.txt中的文本行内容,将图片路径与label分离,存储在lines中
while (std::getline(infile, line)) { //读取train.txt中每行的内容,存在line中
pos = line.find_last_of(' '); //找出line中最后一个''所在的位置
label = atoi(line.substr(pos + ).c_str()); //获取line字符串中对应的label字符,并将其转换为整型
lines.push_back(std::make_pair(line.substr(, pos), label)); //将图片地址与label存在vector变量lines中
}
if (FLAGS_shuffle) {
// randomly shuffle data(打乱数据,发现lines的成员顺序和值发生了变化)
LOG(INFO) << "Shuffling data"; //LOG(INFO)日志输出
shuffle(lines.begin(), lines.end());
}
LOG(INFO) << "A total of " << lines.size() << " images."; //输出图片数据的数量
if (encode_type.size() && !encoded)
LOG(INFO) << "encode_type specified, assuming encoded=true.";
int resize_height = std::max<int>(, FLAGS_resize_height);
int resize_width = std::max<int>(, FLAGS_resize_width);
// Create new DB(以智能指针的方式创建db::DB类型的对象 db)
scoped_ptr<db::DB> db(db::GetDB(FLAGS_backend)); // 智能指针的创建方式类似泛型的格式,
//上面通过db.cpp内定义的命名的子命名空间中db的“成员函数”GetDB函数来初始化db对象
db->Open(argv[], db::NEW); //argv[3]的文件夹下创建并打开lmdb的操作环境
scoped_ptr<db::Transaction> txn(db->NewTransaction());//创建lmdb文件的操作句柄txn
// Storing to db(源数据中提取图像数据)
std::string root_folder(argv[]);
Datum datum;
int count = ;
int data_size = ;
bool data_size_initialized = false;
for (int line_id = ; line_id < lines.size(); ++line_id) {
bool status;
std::string enc = encode_type;
if (encoded && !enc.size()) {
// Guess the encoding type from the file name
string fn = lines[line_id].first; //读取图片的绝对路径
size_t p = fn.rfind('.');
if ( p == fn.npos )
LOG(WARNING) << "Failed to guess the encoding of '" << fn << "'";
enc = fn.substr(p);
std::transform(enc.begin(), enc.end(), enc.begin(), ::tolower);
}
//通过ReadImageToDatum把图片和标签转换为Datum
status = ReadImageToDatum(root_folder + lines[line_id].first,
lines[line_id].second, resize_height, resize_width, is_color,
enc, &datum); //通过ReadImageToDatum把图片和标签转换为Datum
if (status == false) continue;
if (check_size) {
if (!data_size_initialized) {
data_size = datum.channels() * datum.height() * datum.width();
data_size_initialized = true;
} else {
const std::string& data = datum.data();
CHECK_EQ(data.size(), data_size) << "Incorrect data field size "
<< data.size();
}
}
// sequential
string key_str = caffe::format_int(line_id, ) + "_" + lines[line_id].first; //序列化键值
// Put in db
string out;
CHECK(datum.SerializeToString(&out)); //把数据序列转换为string out
txn->Put(key_str, out); //把键值放入到数据库
//批量提交到lmdb文件
if (++count % == ) {
// Commit db
txn->Commit(); //保存到lmdb类型的文件
txn.reset(db->NewTransaction()); //重新初始化操作句柄
LOG(INFO) << "Processed " << count << " files.";
}
}
// write the last batch
if (count % != ) {
txn->Commit();
LOG(INFO) << "Processed " << count << " files.";
}
#else
LOG(FATAL) << "This tool requires OpenCV; compile with USE_OPENCV.";
#endif // USE_OPENCV
return ;
}
注:main函数中的主要函数为
ReadImageToDatum(root_folder + lines[line_id].first,lines[line_id].second, resize_height, resize_width, is_color,enc, &datum);
该函数内部有两个子函数:ReadImageToCVMat(filename, height, width, is_color)和CVMatToDatum(cv_img, datum)。
ReadImageToCVMat(filename, height, width, is_color)的主要功能是将图像的存储格式由3通道存储形式转换为cv空间存储形式,我的理解是由一个指针指向的存储空间,里面依次存放每个通道的像素值。
CVMatToDatum(cv_img, datum)的功能事将cv空间存储的像素值传递到datum.data。
参考:http://blog.csdn.net/whiteinblue/article/details/45330801
caffe源码阅读(一)convert_imageset.cpp注释的更多相关文章
- Caffe源码阅读(1) 全连接层
Caffe源码阅读(1) 全连接层 发表于 2014-09-15 | 今天看全连接层的实现.主要看的是https://github.com/BVLC/caffe/blob/master/src ...
- caffe源码阅读
参考网址:https://www.cnblogs.com/louyihang-loves-baiyan/p/5149628.html 1.caffe代码层次熟悉blob,layer,net,solve ...
- caffe源码阅读(1)-数据流Blob
Blob是Caffe中层之间数据流通的单位,各个layer之间的数据通过Blob传递.在看Blob源码之前,先看一下CPU和GPU内存之间的数据同步类SyncedMemory:使用GPU运算时,数据要 ...
- caffe源码阅读(3)-Datalayer
DataLayer是把数据从文件导入到网络的层,从网络定义prototxt文件可以看一下数据层定义 layer { name: "data" type: "Data&qu ...
- caffe源码阅读(1)_整体框架和简介(摘录)
原文链接:https://www.zhihu.com/question/27982282 1.Caffe代码层次.回答里面有人说熟悉Blob,Layer,Net,Solver这样的几大类,我比较赞同. ...
- caffe源码阅读(2)-Layer
神经网络是由层组成的,深度神经网络就是层数多了.layer对应神经网络的层.数据以Blob的形式,在不同的layer之间流动.caffe定义的神经网络已protobuf形式定义.例如: layer { ...
- caffe 源码阅读
bvlc:Berkeley Vision and Learning Center. 1. 目录结构 models(四个文件夹均有四个文件构成,deploy.prototxt, readme.md, s ...
- caffe中batch norm源码阅读
1. batch norm 输入batch norm层的数据为[N, C, H, W], 该层计算得到均值为C个,方差为C个,输出数据为[N, C, H, W]. <1> 形象点说,均值的 ...
- caffe-windows中classification.cpp的源码阅读
caffe-windows中classification.cpp的源码阅读 命令格式: usage: classification string(模型描述文件net.prototxt) string( ...
随机推荐
- Zookeeper基本配置
前面两篇文章介绍了Zookeeper是什么和可以干什么,那么接下来我们就实际的接触一下Zookeeper这个东西,看看具体如何使用,有个大体的感受,后面再描述某些地方的时候也能在大脑中有具体的印象.本 ...
- SQL存在一个表而不在另一个表中的数据, 更新字段为随机时间
--更新字段为随机时间 86400秒=1天 UPDATE dl_robot ), ,GETDATE()) ) SQL存在一个表而不在另一个表中的数据 方法一 使用 not in ,容易理解,效 ...
- Linux下使用ping快速检测存活主机
该shell脚本主要是通过ping来批量检测网段内存活主机,执行结果保存在脚本当前目录下的IPinfor目录中,写的比较匆忙,希望大家留下更好的建议. #!/usr/bin/env bash##### ...
- DSP中.gel文件的作用
GEL是CCS提供的一种解释语言,使用该语言写出的GEL,函数具有两在功能,一是配置CCS工作环境,二是直接访问目标处理器DSP(包括DSP软/硬仿真器).用户可以使用GEL函数完成类似于宏操作的自动 ...
- Delphi、C C++、Visual Basic数据类型的对照 转
Delphi.C C++.Visual Basic数据类型的对照 变量类型 Delphi C/C++ Visual Basic 位有符号整数 ShortInt char -- 位无符号整数 Byte ...
- python3-day1-python简介及入门
python简介及入门 python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为 ...
- flex 布局 初次接触这个好使又不是特别好用的布局方法
刚开始学前端的童鞋们应该也是一样先学习的table然后再学习了盒子模型,感觉终于学会了简单的网页布局,使用各种display,float,position绞尽脑汁给页面布局成自己想要的页面样式,然而, ...
- mysql:查询结果添加序列号
select (@i:=@i+1) as i,table_name.* from table_name,(select @i:=0) as it
- org.apache.catalina.LifecycleException tomcat 启动 maven 处处都是坑!!!
问题1:tomcat不识别maven工程解决办法:project右击->Properties->Project Facets,选择Dynamic Web Module及其版本(tomcat ...
- sevice__属性介绍: android:exported
http://blog.csdn.net/lhf0000/article/details/6576327 http://blog.csdn.net/berry666/article/details/2 ...