Caffe生成的数据分为2种格式:Lmdb 和 Leveldb

  • 它们都是键/值对(Key/Value Pair)嵌入式数据库管理系统编程库。
  • 虽然lmdb的内存消耗是leveldb的1.1倍,但是lmdb的速度比leveldb快10%至15%,更重要的是lmdb允许多种训练模型同时读取同一组数据集。
  • 因此lmdb取代了leveldb成为Caffe默认的数据集生成格式。

create_babyface.sh调用的convertData的源代码如下:

#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include <stdio.h>
#include<string.h> #include <fstream> // NOLINT(readability/streams)
#include <string>
#include <vector> #include "boost/scoped_ptr.hpp"
#include "glog/logging.h"
#include "google/protobuf/text_format.h"
#include "stdint.h" #include "caffe/proto/caffe.pb.h"
#include "caffe/util/db.hpp" #include <opencv/cv.h>
#include <opencv/highgui.h> using caffe::Datum;
using boost::scoped_ptr;
using std::string;
namespace db = caffe::db;
using namespace std; const int kCIFARSize = 32;
const int kCIFARChannelBytes = 1024;
const int kCIFARImageNBytes = 3072;
const int kCIFARBatchSize = 1000;//1000 for a batch!
const int kCIFARTrainBatches = 5; void read_image(std::ifstream* file, int* label, char* buffer) {
char label_char;
file->read(&label_char, 1);
*label = label_char;
file->read(buffer, kCIFARImageNBytes);
return;
} //Read IPLimage to the buffer
void read_image(
IplImage* out, char* buffer,
char* RC, char* GC, char* BC)
{
int x,y;
int idx =0;
for(y = 0; y<out->height; y++){
char *ptr= out->imageData + y * out->widthStep;
for( x = 0;x< out->width;x++){
idx =y*out->height + x;
BC[idx]= ptr[3*x];
GC[idx]= ptr[3*x+1];
RC[idx]= ptr[3*x+2]; //这样就可以添加自己的操作,这里我使三通道颜色一样,就彩色图转黑白图了
}
}
memcpy( buffer ,RC, kCIFARChannelBytes*sizeof(char) );
memcpy( buffer+ kCIFARChannelBytes*sizeof(char) , GC,kCIFARChannelBytes*sizeof(char) );
memcpy( buffer+ kCIFARChannelBytes*sizeof(char) *2, BC,kCIFARChannelBytes*sizeof(char) );
return;
} //Travel the folder and load the filelist!
//使用linux dirent遍历目录
 int traveldir(char* path ,int depth, vector<string > &FileList)
{
DIR* d;// a
struct dirent *file; struct stat sb; if( !(d=opendir(path ) ) ){
printf("Read path %s error,wishchin! ", path);
return -1;
} while( (file= readdir(d ) ) != NULL ) {
if(0== strncmp(file->d_name, ".", 1 ) ) continue;
char filename[256];
strcpy( filename , file->d_name ); string Sfilename(filename);string Spath(path);
Spath.append(Sfilename);
FileList.push_back(Spath);
} if( stat(file->d_name, &sb)>=0 && S_ISDIR(sb.st_mode) && depth <=4 )
traveldir(file->d_name,depth+1,FileList); closedir(d);
return 1;
} // convert the data to the lmdb format !
void convert_dataset(
const string& input_folder,
const string& output_folder,
const string& db_type) {  scoped_ptr<db::DB> train_db(db::GetDB(db_type));
train_db->Open(output_folder + "/babyface_train_" + db_type, db::NEW);
scoped_ptr<db::Transaction> txn(train_db->NewTransaction()); char* path=new char[256];
int depth=2;
vector<string > FileList(0); // Data buffer
int label;
IplImage* ImageS;
char str_buffer[kCIFARImageNBytes];
char* RC=new char[kCIFARChannelBytes];
char* GC=new char[kCIFARChannelBytes];
char* BC=new char[kCIFARChannelBytes];
Datum datum;
datum.set_channels(3);
datum.set_height(kCIFARSize);
datum.set_width(kCIFARSize); //"Writing Training data"//载入训练数据
LOG(INFO) << "Writing Training data"; strcpy(path,( input_folder+(string)("train1") ).c_str() );
traveldir( path , depth, FileList); for (int fileid = 0; fileid < kCIFARTrainBatches; ++fileid) {
// Open files
LOG(INFO) << "Training Batch " << fileid + 1;
snprintf(str_buffer, kCIFARImageNBytes, "/data_batch_%d.bin", fileid + 1);
//CHECK(data_file) << "Unable to open train file #" << fileid + 1; label=1;//The Batch has 10000 pics!
for (int itemid = 0; itemid < kCIFARBatchSize; ++itemid) {
ImageS =cvLoadImage( (FileList[ fileid*kCIFARTrainBatches + itemid] ).c_str() );
read_image( ImageS, str_buffer, RC, GC, BC); datum.set_label(label);//datum.set_label(label);
datum.set_data(str_buffer, kCIFARImageNBytes); int length = snprintf(str_buffer, kCIFARImageNBytes,
"%05d", fileid * kCIFARBatchSize + itemid);
string out;
CHECK(datum.SerializeToString( &out) ) ;
txn->Put(string(str_buffer, length), out);//The main sentence ,put data to the txn!
}
} strcpy(path,( input_folder+(string)("train0") ).c_str() );
traveldir( path , depth, FileList);
for (int fileid = 0; fileid < kCIFARTrainBatches; ++fileid) {
LOG(INFO) << "Training Batch " << fileid + 1;
snprintf(str_buffer, kCIFARImageNBytes, "/data_batch_%d.bin", fileid + 1);
//CHECK(data_file) << "Unable to open train file #" << fileid + 1; label=0;//The Batch has 10000 pics!
for (int itemid = 0; itemid < kCIFARBatchSize; ++itemid) {
ImageS =cvLoadImage( (FileList[ fileid*kCIFARTrainBatches + itemid] ).c_str() );
read_image( ImageS, str_buffer, RC, GC, BC); datum.set_label(label);//datum.set_label(label);
datum.set_data(str_buffer, kCIFARImageNBytes); int length = snprintf(str_buffer, kCIFARImageNBytes,
"%05d", fileid * kCIFARBatchSize + itemid);
string out;
CHECK(datum.SerializeToString( &out) ) ;
txn->Put(string(str_buffer, length), out);//The main sentence ,put data to the txn!
}
} txn->Commit();
train_db->Close(); //写入测试数据!
LOG(INFO) << "Writing Testing data";
scoped_ptr<db::DB> test_db(db::GetDB(db_type));
test_db->Open(output_folder + "/babyface_test_" + db_type, db::NEW);
txn.reset(test_db->NewTransaction()); strcpy(path,( input_folder+(string)("test1") ).c_str() );
traveldir( path , depth, FileList);
for (int fileid = 0; fileid < 2; ++fileid) {
LOG(INFO) << "Training Batch " << fileid + 1;
snprintf(str_buffer, kCIFARImageNBytes, "/data_batch_%d.bin", fileid + 1); label=1;//The Batch has 10000 pics!
for (int itemid = 0; itemid < kCIFARBatchSize; ++itemid) {
ImageS =cvLoadImage( (FileList[ fileid*2 + itemid] ).c_str() );
read_image( ImageS, str_buffer, RC, GC, BC); datum.set_label(label);//datum.set_label(label);
datum.set_data(str_buffer, kCIFARImageNBytes); int length = snprintf(str_buffer, kCIFARImageNBytes,
"%05d", fileid * kCIFARBatchSize + itemid);
string out;
CHECK(datum.SerializeToString( &out) ) ;
txn->Put(string(str_buffer, length), out);//The main sentence ,put data to the txn!
}
} strcpy(path,( input_folder+(string)("test0") ).c_str() );
traveldir( path , depth, FileList);
for (int fileid = 0; fileid < 2; ++fileid) { LOG(INFO) << "Training Batch " << fileid + 1;
snprintf(str_buffer, kCIFARImageNBytes, "/data_batch_%d.bin", fileid + 1); label=0;//The Batch has 10000 pics!
for (int itemid = 0; itemid < kCIFARBatchSize; ++itemid) {
ImageS =cvLoadImage( (FileList[ fileid*2 + itemid] ).c_str() );
read_image( ImageS, str_buffer, RC, GC, BC); datum.set_label(label);//datum.set_label(label);
datum.set_data(str_buffer, kCIFARImageNBytes); int length = snprintf(str_buffer, kCIFARImageNBytes,
"%05d", fileid * kCIFARBatchSize + itemid);
string out;
CHECK(datum.SerializeToString( &out) ) ;
txn->Put(string(str_buffer, length), out);//The main sentence ,put data to the txn!
}
} txn->Commit();
test_db->Close(); cvReleaseImage(&ImageS);
delete [] RC;delete [] GC;delete [] BC;
} int main(int argc, char** argv) {
if (argc != 4) {
printf("This script converts the CIFAR dataset to the leveldb format used\n"
"by caffe to perform classification.\n"
"Usage:\n"
" convert_cifar_data input_folder output_folder db_type\n"
"Where the input folder should contain the binary batch files.\n"
"The CIFAR dataset could be downloaded at\n"
" http://www.cs.toronto.edu/~kriz/cifar.html\n"
"You should gunzip them after downloading.\n");
} else {
google::InitGoogleLogging(argv[0]);
convert_dataset(string(argv[1]), string(argv[2]), string(argv[3]));
}
return 0;
}

后记:目的是载入32×32的三通道图像,直接输入3072维的char向量进行训练,至于怎样训练网络,还得仔细查看一下。

后记:代码出现 coredump 问题,利用 gcc path/...bin  -o coredemo -g ,出现caffe.pb.h 包含丢失现象,why???

caffe特征提取/C++数据格式转换的更多相关文章

  1. zw版【转发·台湾nvp系列Delphi例程】Delphi 使用 HALCON库件COM控件数据格式转换

    zw版[转发·台湾nvp系列Delphi例程]Delphi 使用 HALCON库件COM控件数据格式转换 Delphi 使用 HALCON库件COM控件数据格式转换,与IHObjectX接口有关 va ...

  2. Java将其他数据格式转换成json字符串格式

    package com.wangbo.util; import java.beans.IntrospectionException; import java.beans.Introspector; i ...

  3. 完善GDAL与OpenCV间的数据格式转换与影像分块读写

    本博客为原创内容,未经博主允许禁止转载,商用,谢谢. 一.前言 关于GDAL与openCV间的数据格式转换,在我之前的博客中已有简要说明,这里,由于最近工作上经常用到openCV里的函数进行图像处理, ...

  4. 页面输入的数据格式转换类:BaseAction(经常使用于Struts框架中)

    在我们接收页面传来的数据时,这些数据都是以String类型接收的,所以要进行数据格式转换,这时候就能够统一为它们进行转换,并且在处理这些数据的类中能够继承ActionSupport类,然后让每个接收数 ...

  5. SBC数据格式转换软件

    北京博信施科技有限公司是一家专业从事数据格式转换.数据处理领域研发软件产品和解决方案实施的技术型公司.在当今信息时代,PDF文档格式是在Internet上进行电子文档发行和数字化信息传播的理想文档格式 ...

  6. 【C#/WPF】图像数据格式转换时,透明度丢失的问题

    问题:工作中涉及到图像的数据类型转换,经常转着转着发现,到了哪一步图像的透明度丢失了! 例如,Bitmap转BitmapImage的经典代码如下: public static BitmapImage ...

  7. NetworkX系列教程(11)-graph和其他数据格式转换

    小书匠 Graph 图论  学过线性代数的都了解矩阵,在矩阵上的文章可做的很多,什么特征矩阵,单位矩阵等.grpah存储可以使用矩阵,比如graph的邻接矩阵,权重矩阵等,这节主要是在等到graph后 ...

  8. python pandas数据分析基础入门2——(数据格式转换、排序、统计、数据透视表)

    //2019.07.18pyhton中pandas数据分析学习——第二部分2.1 数据格式转换1.查看与转换表格某一列的数据格式:(1)查看数据类型:某一列的数据格式:df["列属性名称&q ...

  9. 【转】在Python的struct模块中进行数据格式转换的方法

    这篇文章主要介绍了在Python的struct模块中进行数据格式转换的方法,文中还给出了C语言和Python语言的数据类型比较,需要的朋友可以参考下 Python是一门非常简洁的语言,对于数据类型的表 ...

随机推荐

  1. 【剑指Offer】47、求1+2+3+4+···+n

      题目描述:   求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C).   解题思路:   本题本身没有太多 ...

  2. C++引用、类型转换、类和对象(day03)

    十 C++的引用(Reference) 引用型函数参数 )将引用用于函数的参数,可以修改实参变量的值,同时也能减小函数调用的开销. )引用参数有可能意外修饰实参的值,如果不希望修改实参变量本身,可以将 ...

  3. [luogu4162 SCOI2009] 最长距离(最短路)

    传送门 Solution 题目是最长路,其实是最短路ヽ(ー_ー)ノ 把进入障碍点的边设为1,其他为0.枚举每个点为起点找距离<=T的点,更新答案 Code //By Menteur_Hxy #i ...

  4. ExtJs之Ext.comboBox的远程数据源读取程序

    既然可以测试本地AJAX,那就把书前面的代码作一次学习吧. <!DOCTYPE html> <html> <head> <title>ExtJs< ...

  5. Spring深入理解(一)

    Spring 框架的设计理念与设计模式分析 Spring核心组件 Spring 框架中的核心组件只有三个:Core.Context 和 Beans Spring 的设计理念 前面介绍了 Spring ...

  6. How to run Java main class and pass application arguments in Maven?

    原文: http://www.logicbig.com/how-to/maven/mvn-java-exec-args/ --------------------------------------- ...

  7. POJ2391 Ombrophobic Bovines 网络流拆点+二分+floyed

    题目链接: id=2391">poj2391 题意: 有n块草地,每块草地上有一定数量的奶牛和一个雨棚,并给出了每一个雨棚的容(牛)量. 有m条路径连接这些草地  ,这些路径是双向的, ...

  8. D 分组背包

    <span style="color:#3333ff;">/* ---------------------------------------------------- ...

  9. 热修复JSPatch之实战教程

      接上篇<热修复JSPatch之接口设计>,在这篇文章主要给大家讲述一下怎样高速具备热修复能力,当然了假设有人有志于把JSPatch系统的学习,甚至用JSPatch进行开发的.就没有必要 ...

  10. 什么是鸭子类型(duck typing)

    "当看到一仅仅鸟走起来像鸭子.游泳起来像鸭子.叫起来也像鸭子,那么这仅仅鸟就能够被称为鸭子." 我们并不关心对象是什么类型,究竟是不是鸭子,仅仅关心行为. 比方在python中.有 ...