caffe神经网络框架的辅助工具(将图片转换为leveldb格式)
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 <google/protobuf/text_format.h>
#include <glog/logging.h>
#include <leveldb/db.h> #include <stdint.h>
#include <fstream> // NOLINT(readability/streams)
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include "caffe/proto/caffe.pb.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc.hpp> using std::string;
using namespace std; set<string> all_class_name;
map<string,int> class2id; /**
* path:文件夹
* files:用于保存文件名称的vector
* r:是否须要遍历子文件夹
* return:文件名称,不包括路径
*/
void list_dir(const char *path,vector<string>& 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<string,int>::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<cv::Vec3b>(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<string> filenames;
list_dir(imgdir.c_str(),filenames);
string img_log = attachdir+"image_filename";
ofstream writefile(img_log.c_str());
for(int i=0;i<filenames.size();i++)
{
string path= imgdir;
path.append(filenames[i]);//算出绝对路径 loadimg(path,buffer); int labelid = get_labelid(filenames[i]); datum.add_label(labelid);
datum.set_data(buffer,image_size);
datum.SerializeToString(&value);
snprintf(buffer, image_size, "%05d", i);
printf("\nclassid:%d classname:%s abspath:%s",labelid,get_classname(filenames[i]).c_str(),path.c_str());
db->Put(leveldb::WriteOptions(),string(buffer),value);
//printf("%d %s\n",i,fileNames[i].c_str()); assert(writefile.is_open());
writefile<<i<<" "<<filenames[i]<<"\n"; }
delete db;
writefile.close(); img_log = attachdir+"image_classname";
writefile.open(img_log.c_str());
set<string>::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); }
caffe神经网络框架的辅助工具(将图片转换为leveldb格式)的更多相关文章
- windows+caffe(二)——图片转换为levedb格式
借鉴于langb2014的 http://blog.csdn.net/langb2014/article/details/50458520 与liukailun09的 http://blog.cs ...
- Ubuntu 下将 svg 图片转换为其他格式 (如 png)
参考 How to Convert SVG Files to other Image Formats on Ubuntu 12.04/11.10 Ubuntu 下将 svg 图片转换为其他格式 (如 ...
- 将图片转换为base64 格式
1.页面上的图片,转换成base64格式,可以通过canvas 的 toDataURL 例子:给定图片的url 将图片转换为base64 var imageSrc = "../images/ ...
- python 使用pillow将图片转换为webp格式
1.webp格式 webp格式是谷歌开发的一种旨在加快图片加载速度的格式,将图片转为webp格式后,体积约为原来的2/3,这可以节省大量的服务器带宽,微信公众号文章里的图片就是这种格式的. 2.使用p ...
- s2sh框架搭建(辅助工具:MyEclipse)及解决一些遇到的问题
1.新建一个web project 2.首先生成Hibernate Facet 3.Hibernate Facet 安装步骤 4.然后是spring facet安装步骤 5.最后是struts fac ...
- js小工具---本地图片转换为base64编码数据
今天用jmeter对图片对比进行性能测试,post请求为json请求,图片为Base64编码后的图片数据.所以需要将一张本地图片生成base64编码,找到一个js小工具,记录在这儿便于以后复用. 效果 ...
- Java图片转换为base64格式
/** * @Descriptionmap 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 * @author temdy * @Date 2015-01-26 * @param pa ...
- Caffe初试(三)使用caffe的cifar10网络模型训练自己的图片数据
由于我涉及一个车牌识别系统的项目,计划使用深度学习库caffe对车牌字符进行识别.刚开始接触caffe,打算先将示例中的每个网络模型都拿出来用用,当然这样暴力的使用是不会有好结果的- -||| ,所以 ...
- PHP将图片转base64编码以及base64图片转换为图片并保存代码
图片转base64编码 /*图片转换为 base64格式编码*/ $img = 'uploads/01.png'; $base64_img = base64EncodeImage($img); ech ...
随机推荐
- ASP.NET性能监视参数详解
性能监视器- Performance Monitor 性能监视器是Windows自带的系统资源和性能监视工具. 性能监视器能够量化地提供CPU使用率, 内存分配状况, 异常派发情况, 线程调度频率等信 ...
- hdu acm 2154(多解取一解)
//题目中结果有一条限制就是最后必须跳回A,如果我们的思想框在这个条件上就很容易卡住,因为这样的条件下的路径很难有规律的罗列,然而我们说这个图形中有三个区域,我们算出每个区域的第n-1次的种类数,然后 ...
- BZOJ 1114 Number theory(莫比乌斯反演+预处理)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=71738 题意:给你一个整数序列a1, a2, a3, ... , ...
- MysqlHelp
using System.Configuration;using MySql.Data: public class MySqlHelp { //链接字符串 private static string ...
- OptiScroll 公共例子(只修改了滚动条颜色)
地址:https://github.com/wilsonfletcher/Optiscroll <!DOCTYPE html> <html> <head> < ...
- 制作一个vagrant的win7 box
准备: 1.win7镜像文件 2.vagrant安装文件 3.virtual box安装文件 步骤: 1.先在本机上安装virtualbox和vagrant,本机为win7,安装虚机也为win7 2. ...
- codeforces 622E. Ants in Leaves
题目链接 给一棵有根树, 每个叶子节点上有一只蚂蚁. 在0时刻蚂蚁开始向上爬, 同一时刻, 除了根节点以外, 一个节点上面不能有2个蚂蚁. 问所有的蚂蚁都爬到根节点需要的最短时间. 因为除了根节点, ...
- A Byte of Python 笔记(9) 面向对象编程
第11章 面向对象编程 面向过程:根据操作数据的函数或语句块来设计程序. 面向对象(OOP, object-oriented programming):把数据和功能结合起来,用对象包裹组织程序. 类 ...
- IOS 页面之间的传值(主讲delegate)
IOS的Delegate,通俗一点说就是页面之间的传值. 总结一下现在知道的IOS页面之间传值的方式有三种 1.使用NSNotification发送通知的传值 主要是通过NSNotificationC ...
- web压缩gzip响应
String data = "ggrgrgw4gergergregerge"; byte b[] = data.getBytes(); String gzipValue = req ...