关闭

caffe多标签装hdf5接口

1337人阅读 评论(12) 收藏 举报
分类:

这个代码的作用是输入图像的标签,在caffe里面,由于用自带的lmda和leveldb接口的标签是一个,并且这个标签只能是整数,如果想进行回归或者多标签的话就没法使用自带的接口,因此,我提供了这个接口可以实现任意标签数。
首先介绍一个这个接口的使用:
对于argv[1],里面的txt要求,每一行对应一张图片,每一行的开口对应图片名称,后面接着是标签。

#include<string>
#include<vector>
#include "H5Cpp.h"
#include "hdf5.h"
#include "hdf5_hl.h"
#include<fstream>
#include <iostream>
#include <stdlib.h>
#include<stdio.h>
#include <fstream>  // NOLINT(readability/streams)
#include <string>
#include <utility>
#include <vector>
using namespace H5;
using namespace std;
#define IMAGE_NUM_MAX 10000

//DEFINE_string(lable_type, "double", "support type of int and double");

int main(int argc, char** argv){
    //argv[1] 是label的txt
    //argv[2] 是hdf5存放的位置以及名称
    //argv[3] 是每个样本label的个数
    if (argc != 4){
        //LOG(ERROR) << "input less!" << endl;
        printf("input less\n");
        return false;
    }
    int label_size = atoi(argv[3]);
    std::ifstream in(argv[1]);
    string image_path;
    double* label = new double[label_size*IMAGE_NUM_MAX];
    int image_num = 0;
    while (in >> image_path){
        for (int i = 0; i < label_size; ++i){
            in >> label[i+image_num*label_size];
        }
        ++image_num;
    }//while 标签数组的大小为image_num*label_size
    in.close();

    H5File* file = new H5File(argv[2], H5F_ACC_TRUNC);

    double fill_val = 0.0;
    DSetCreatPropList plist;
    plist.setFillValue(PredType::NATIVE_DOUBLE, &fill_val);

    hsize_t fdim[] = {image_num, label_size};
    DataSpace dataspace(2, fdim);

    DataSet* dataset = new DataSet(file->createDataSet("label", PredType::NATIVE_DOUBLE, dataspace, plist));

    hsize_t start[2] = {0, 0};
    hsize_t count[2] = {image_num, label_size};
    dataspace.selectHyperslab(H5S_SELECT_SET, count, start);

    hsize_t mdim[] = {image_num*label_size};
    DataSpace mspace(1, mdim);

    start[0] = 0;
    count[0] = image_num*label_size;
    mspace.selectHyperslab(H5S_SELECT_SET, count, start);

    dataset->write(label, PredType::NATIVE_DOUBLE, mspace, dataspace);

    delete dataset;
    delete file;
    cout << "process " << image_num << " images " << endl;
    return true;
}

makefile

all:
    g++ -o convert_labels_hdf5 convert_labels_hdf5.cpp  -L/usr/lib -I/usr/include -lhdf5 -lhdf5_hl -lhdf5_cpp

查看生成的文件可以通过matlab,代码如下

label = h5read('filepath','/label')
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:141609次
    • 积分:2488
    • 等级:
    • 排名:第15727名
    • 原创:101篇
    • 转载:10篇
    • 译文:1篇
    • 评论:162条
    文章分类
    最新评论