博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
caffe神经网络框架的辅助工具(将图片转换为leveldb格式)
阅读量:6029 次
发布时间:2019-06-20

本文共 4058 字,大约阅读时间需要 13 分钟。

caffe中负责整个网络输入的datalayer是从leveldb里读取数据的,是一个google实现的很高效的kv数据库。

因此我们训练网络必须先把数据转成leveldb的格式。

这里我实现的是把一个目录的全部图片转成leveldb的格式。

工具使用命令格格式:convert_imagedata src_dir dst_dir attach_dir channel width height

例子:./convert_imagedata.bin /home/linger/imdata/collar_train/ /home/linger/linger/testfile/crop_train_db/ /home/linger/linger/testfile/crop_train_attachment/ 3 50 50

源码:

#include 
#include
#include
#include
#include
// NOLINT(readability/streams)#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "caffe/proto/caffe.pb.h"#include
#include
#include
using std::string;using namespace std;set
all_class_name;map
class2id;/** * path:文件夹 * files:用于保存文件名称的vector * r:是否须要遍历子文件夹 * return:文件名称,不包括路径 */void list_dir(const char *path,vector
& files,bool r = false){ DIR *pDir; struct dirent *ent; char childpath[512]; pDir = opendir(path); memset(childpath, 0, sizeof(childpath)); while ((ent = readdir(pDir)) != NULL) { if (ent->d_type & DT_DIR) { if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { continue; } if(r) //假设须要遍历子文件夹 { sprintf(childpath, "%s/%s", path, ent->d_name); list_dir(childpath,files); } } else { files.push_back(ent->d_name); } } sort(files.begin(),files.end());//排序}string get_classname(string path){ int index = path.find_last_of('_'); return path.substr(0, index);}int get_labelid(string fileName){ string class_name_tmp = get_classname(fileName); all_class_name.insert(class_name_tmp); map
::iterator name_iter_tmp = class2id.find(class_name_tmp); if (name_iter_tmp == class2id.end()) { int id = class2id.size(); class2id.insert(name_iter_tmp, std::make_pair(class_name_tmp, id)); return id; } else { return name_iter_tmp->second; }}void loadimg(string path,char* buffer){ cv::Mat img = cv::imread(path, CV_LOAD_IMAGE_COLOR); string val; int rows = img.rows; int cols = img.cols; int pos=0; for (int c = 0; c < 3; c++) { for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { buffer[pos++]=img.at
(row,col)[c]; } } }}void convert(string imgdir,string outputdb,string attachdir,int channel,int width,int height){ leveldb::DB* db; leveldb::Options options; options.create_if_missing = true; options.error_if_exists = true; caffe::Datum datum; datum.set_channels(channel); datum.set_height(height); datum.set_width(width); int image_size = channel*width*height; char buffer[image_size]; string value; CHECK(leveldb::DB::Open(options, outputdb, &db).ok()); vector
filenames; list_dir(imgdir.c_str(),filenames); string img_log = attachdir+"image_filename"; ofstream writefile(img_log.c_str()); for(int i=0;i
Put(leveldb::WriteOptions(),string(buffer),value); //printf("%d %s\n",i,fileNames[i].c_str()); assert(writefile.is_open()); writefile<
<<" "<
<<"\n"; } delete db; writefile.close(); img_log = attachdir+"image_classname"; writefile.open(img_log.c_str()); set
::iterator iter = all_class_name.begin(); while(iter != all_class_name.end()) { assert(writefile.is_open()); writefile<<(*iter)<<"\n"; //printf("%s\n",(*iter).c_str()); iter++; } writefile.close();}int main(int argc, char** argv){ if (argc < 6) { LOG(ERROR) << "convert_imagedata src_dir dst_dir attach_dir channel width height"; return 0; }//./convert_imagedata.bin /home/linger/imdata/collarTest/ /home/linger/linger/testfile/dbtest/ /home/linger/linger/testfile/test_attachment/ 3 250 250 // ./convert_imagedata.bin /home/linger/imdata/collar_train/ /home/linger/linger/testfile/crop_train_db/ /home/linger/linger/testfile/crop_train_attachment/ 3 50 50 google::InitGoogleLogging(argv[0]); string src_dir = argv[1]; string src_dst = argv[2]; string attach_dir = argv[3]; int channel = atoi(argv[4]); int width = atoi(argv[5]); int height = atoi(argv[6]); //for test /* src_dir = "/home/linger/imdata/collarTest/"; src_dst = "/home/linger/linger/testfile/dbtest/"; attach_dir = "/home/linger/linger/testfile/"; channel = 3; width = 250; height = 250; */ convert(src_dir,src_dst,attach_dir,channel,width,height);}

转载地址:http://yhbhx.baihongyu.com/

你可能感兴趣的文章
在Oracle中删除所有强制性外键约束
查看>>
dhcp
查看>>
【R】R语言使用命令行参数 - [编程技巧(Program Skill)]
查看>>
经典算法题每日演练——第二题 五家共井
查看>>
存储过程中拼接的变量和点的问题
查看>>
ASP.NET那点不为人知的事(一)
查看>>
HTML 表格
查看>>
VMware 虚拟化编程(7) — VixDiskLib 虚拟磁盘库详解之三
查看>>
php 未实例化类调用方法的问题
查看>>
Anaconda jupyter notebook 出现 kernel error 解决办法
查看>>
我对读计算机软件专业硕士的几点看法
查看>>
【枚举】bzoj1800 [Ahoi2009]fly 飞行棋
查看>>
用JS写CSS
查看>>
TOJ4537: n阶行列式
查看>>
算法刷题笔记-stack-四则运算
查看>>
3.16
查看>>
Linux下arp用法
查看>>
表单文件上传与文件下载
查看>>
jquery 中prop()的使用方法
查看>>
下午考
查看>>