多标签caffe重新编译
说明:
Caffe自带的图像转LMDB接口只支持单label,对于多label的任务,可以使用HDF5的格式,也可以通过修改caffe代码来实现.本篇文章介绍怎么通过修改DataLayer来实现带Multilabel的lmdb格式数据输入的分类任务.
--- 本文参考自 :
1. 总体介绍
共修改5个文件:
- $CAFFE_ROOT/src/caffe/proto/caffe.proto
- $CAFFE_ROOT/src/caffe/layers/data_layer.cpp
- $CAFFE_ROOT/src/caffe/util/io.cpp
- $CAFFE_ROOT/include/caffe/util/io.hpp
- $CAFFE_ROOT/tools/convert_imageset.cpp
其中$CAFFE_ROOT为caffe根目录,即git clone 目录, 如图:
修改完成后,执行:
make clean
make all –j8
-------______
具体的文件修改内容如下:
2.1 caffe.proto
vim /src/caffe/proto/caffe.proto
在message Datum { }里中添加一行代码,即添加一个labels,是repeated类型的,以便接受多标签数据集。
repeated float labels = 8;
message Datum {
optional int32 channels = 1;
optional int32 height = 2;
optional int32 width = 3;
// the actual image data, in bytes
optional bytes data = 4;
optional int32 label = 5;
// Optionally, the datum could also hold float data.
repeated float float_data = 6;
// If true data contains an encoded image that need to be decoded
optional bool encoded = 7 [default = false];
//////////////////////////////////
repeated float labels = 8;
//////////////////////////////////
}
2.2 data_layer.cpp (两处)
// label
/*
if (this->output_labels_) {
vector<int> label_shape(1, batch_size);
top[1]->Reshape(label_shape);
for (int i = 0; i < this->prefetch_.size(); ++i) {
this->prefetch_[i]->label_.Reshape(label_shape);
}
}
*/
/////////////////////////////////////////////////
if (this->output_labels_){
top[1]->Reshape(batch_size, 4, 1, 1);
for (int i = 0; i < this->prefetch_.size(); ++i) {
this->prefetch_[i]->label_.Reshape(batch_size, 4, 1, 1);
}
}
//////////////////////////////////////////////////
// Copy label.
/*
if (this->output_labels_) {
Dtype* top_label = batch->label_.mutable_cpu_data();
top_label[item_id] = datum.label();
}
*/
///////////////////////////////////////////////
if (this->output_labels_) {
Dtype* top_label = batch->label_.mutable_cpu_data();
for (int i = 0; i < 4; i++)
top_label[item_id * 4 + i] = datum.labels(i);
}
///////////////////////////////////////////////
2.3 io.cpp(两处)
修改两个函数,替换成下面修改后的代码即可,
vim /src/caffe/util/io.cpp
ReadImageToDatum()
bool ReadImageToDatum(const string& filename, const vector<float> label,
const int height, const int width, const bool is_color,
const std::string & encoding, Datum* datum) {
cv::Mat cv_img = ReadImageToCVMat(filename, height, width, is_color);
if (cv_img.data) {
if (encoding.size()) {
if ( (cv_img.channels() == 3) == is_color && !height && !width &&
matchExt(filename, encoding) )
return ReadFileToDatum(filename, label, datum);
std::vector<uchar> buf;
cv::imencode("."+encoding, cv_img, buf);
datum->set_data(std::string(reinterpret_cast<char*>(&buf[0]),
buf.size()));
datum->clear_labels();
for (int i = 0; i < label.size(); i++){
datum->add_labels(label[i]);
}
datum->set_encoded(true);
return true;
}
CVMatToDatum(cv_img, datum);
datum->clear_labels();
for (int i = 0; i < label.size(); i++){
datum->add_labels(label[i]);
}
return true;
} else {
return false;
}
}
ReadFileToDatum()
//////////////////////////////////////////////////////////////////////
bool ReadFileToDatum(const string& filename, const vector<float> label,
Datum* datum) {
std::streampos size;
fstream file(filename.c_str(), ios::in|ios::binary|ios::ate);
if (file.is_open()) {
size = file.tellg();
std::string buffer(size, ' ');
file.seekg(0, ios::beg);
file.read(&buffer[0], size);
file.close();
datum->set_data(buffer);
datum->clear_labels();
for (int i = 0; i < label.size(); i++){
datum->add_labels(label[i]);
}
datum->set_encoded(true);
return true;
} else {
return false;
}
}
2.4 io.hpp
在其中新加入/////// ..... ///////内的两个成员函数声明,不删除原来的任何代码,下面的前两个函数声明是原来文件中就有的,可以看到,原来代码中的label参数是int类型,只能处理单标签字符;新增的两个成员函数就是参考上面两个函数,将const int label参数改成了std::vector labels,以接受多标签字符。
bool ReadImageToDatum(const string& filename, const int label,
const int height, const int width, const bool is_color,
const std::string & encoding, Datum* datum);
bool ReadFileToDatum(const string& filename, const int label, Datum* datum);
//////////////////////////////////////////
bool ReadImageToDatum(const string& filename, std::vector<float> labels,
const int height, const int width, const bool is_color,
const std::string & encoding, Datum* datum);
bool ReadFileLabelsToDatum(const string& filename, std::vector<float> labels,
Datum* datum);
///////////////////////////////////
2.5 convert_imageset.cpp
/*
std::ifstream infile(argv[2]);
std::vector<std::pair<std::string, int> > lines;
std::string line;
size_t pos;
int label;
while (std::getline(infile, line)) {
pos = line.find_last_of(' ');
label = atoi(line.substr(pos + 1).c_str());
lines.push_back(std::make_pair(line.substr(0, pos), label));
}
*/
////////////////////////////
std::ifstream infile(argv[2]);
std::vector<std::pair<std::string, vector<float> > > lines;
std::string filename;
vector<float> labels(4);
while (infile >> filename >> labels[0] >> labels[1] >> labels[2] >> labels[3]){
lines.push_back(std::make_pair(filename, labels));
}
///////////////////////////
多标签caffe重新编译的更多相关文章
- 深度学习框架Caffe的编译安装
深度学习框架caffe特点,富有表达性.快速.模块化.下面介绍caffe如何在Ubuntu上编译安装. 1. 前提条件 安装依赖的软件包: CUDA 用来使用GPU模式计算. 建议使用 7.0 以上最 ...
- 64位win10+cuda8.0+vs2013+cuDNN V5下Caffe的编译安装教程并配置matlab2014a 接口
一.需要安装的软件 1)vs2013,我是在http://www.52pojie.cn/thread-492326-1-1.html这个网址安装的.我之前用的是vs2012,按照网上的配置教程会爆各种 ...
- caffe windows编译
MicroSoft维护的caffe已经作为官方的caffe分支了,编译方式也改了,刚好最近重装了一次caffe windows, 记录一下里面的坑 https://github.com/BVLC/ca ...
- caffe make 编译
其实嘛,出现这个的原因在于,已经编译过啦,没有任何改动,那还烦劳编译啥呢. 那Linux又是如何知道已经编译过了呢? 那就要看makefile的规则啦.makefile的规则是所想产生的文件需要依赖很 ...
- Caffe: gflag编译出现问题汇总
1. 使用Unicode字符集: 出现问题 E:\CodeBase\ML\Caffe\ThirdPartySrc\gflags-master\src\gflags.cc(1340): error C2 ...
- caffe安装编译问题-ImportError: No module named google.protobuf.internal
问题描述 ~/Downloads/caffe$ python Python (default, Dec , ::) [GCC ] on linux2 Type "help", &q ...
- caffe安装编译问题-ImportError: No module named skimage.io
问题描述 >>> import caffe Traceback (most recent call last): File , in <module> File , in ...
- caffe安装编译问题-ImportError: libopencv_core.so.3.4: cannot open shared object file: No such file or directory
问题描述 >>> import caffe Traceback (most recent call last): File , in <module> File , in ...
- caffe安装编译问题-ImportError: No module named caffe
问题描述 ~/Downloads/caffe$ python Python (default, Dec , ::) [GCC ] on linux2 Type "help", &q ...
随机推荐
- 安装windows server 2008r2服务器操作系统
R4900 G2 UEFI使用RAID-1000配置阵列 http://123.57.42.60/12755.html 首先进入到进到UEFI中阵列配置界面, 1Legacy BIOS Mode进入阵 ...
- 序列化 反序列化 MessagePack for C#
阅读目录 快速序列化组件MessagePack介绍 简介 使用 快速开始 分析器 内置的支持类型 对象序列化 DataContract兼容性 序列化不可变对象(序列化构造器) 序列化回调 Union ...
- [源码分析]StringBuilder
[源码分析]StringBuilder StringBuilder是继承自AbstractStringBuilder的. 这里附上另外两篇文章的连接: AbstractStringBuilder : ...
- CSS部分语法2
<!-- 第1部分 尺寸与框模型 略 第2部分背景设置 2.1 背景颜色:background-color:value 2.2 背景图片:background-image body{ backg ...
- [Jetty] Debugging With Eclipse
There are a number of options available to debug your application in Eclipse. If not done already pr ...
- .NET尝试访问某方法失败
今天发现了一个错误: xxxx.xxxx尝试访问xxxx.xxxx方法失败. 调试无果,经过分析后得到这是.NET引用的问题.果然有了这个方向后,发现了引用不匹配的问题,问题随之解决. 记录一下.
- C# 泛型单例
不支持非公共的无参构造函数的 public abstract class BaseInstance<T> where T : class,new() { private readonly ...
- 【vue】组件使用Deferred特性
延迟加载组件 defer的意思是"延迟",所以deferred对象的含义就是"延迟"到未来某个点再执行. <template> <div> ...
- vue 前端框架
什么是vue.js 1.vue是目前最火的一个前端框架,react 是最流行的前端框架(react除了开发网站,还可以开发手机APP,vue语法也是可以进行手机app开发的,需要借助于weex) 2. ...
- IAR中如何定向把数组和函数放在指定的地址单元
1. 指定数组到特定的Flash单元#pragma location = 0x000FFF00 __root const char Flash_config[] = {0x0,0x1,0x2,0x3, ...
