hdf5 matlab,hdf5格式的matlab读写操作
最近要用caffe处理一个multi-label的回归问题,就是输出是一个向量,不是一个具体的数值,这个时候之前的leveldb格式就不凑效了,因为caffe源代码里面默认label是一个数值,网上搜了下,都说hdf5格式可以解决这个问题
在caffe里面,有一个hdf5的datalayer作为数据输入,从源代码来看,对于label的维数没做限制,剩下的问题就是如何生成hdf5的数据,目前只是找到了github上的一个人共享的用matlab写的hdf5数据的读写操作,在这我把代码粘贴出来
testHDF5.m
%% WRITING TO HDF5
filename='trial.h5';
num_total_samples=10000;
% to simulate data being read from disk / generated etc.
data_disk=rand(5,5,1,num_total_samples);
label_disk=rand(10,num_total_samples);
chunksz=100;
created_flag=false;
totalct=0;
for batchno=1:num_total_samples/chunksz
fprintf('batch no. %d\n', batchno);
last_read=(batchno-1)*chunksz;
% to simulate maximum data to be held in memory before dumping to hdf5 file
batchdata=data_disk(:,:,1,last_read+1:last_read+chunksz);
batchlabs=label_disk(:,last_read+1:last_read+chunksz);
% store to hdf5
startloc=struct('dat',[1,1,1,totalct+1], 'lab', [1,totalct+1]);
curr_dat_sz=store2hdf5(filename, batchdata, batchlabs, ~created_flag, startloc, chunksz);
created_flag=true;% flag set so that file is created only once
totalct=curr_dat_sz(end);% updated dataset size (#samples)
end
% display structure of the stored HDF5 file
h5disp(filename);
%% READING FROM HDF5
% Read data and labels for samples #1000 to 1999
data_rd=h5read(filename, '/data', [1 1 1 1000], [5, 5, 1, 1000]);
label_rd=h5read(filename, '/label', [1 1000], [10, 1000]);
fprintf('Testing ...\n');
try
assert(isequal(data_rd, single(data_disk(:,:,:,1000:1999))), 'Data do not match');
assert(isequal(label_rd, single(label_disk(:,1000:1999))), 'Labels do not match');
fprintf('Success!\n');
catch err
fprintf('Test failed ...\n');
getReport(err)
end
%delete(filename);
% CREATE list.txt containing filename, to be used as source for HDF5_DATA_LAYER
FILE=fopen('list.txt', 'w');
fprintf(FILE, '%s', filename);
fclose(FILE);
fprintf('HDF5 filename listed in %s \n', 'list.txt');
% NOTE: In net definition prototxt, use list.txt as input to HDF5_DATA as:
% layers {
% name: "data"
% type: HDF5_DATA
% top: "data"
% top: "labelvec"
% hdf5_data_param {
% source: "/path/to/list.txt"
% batch_size: 64
% }
% }
store2hdf5.m
function [curr_dat_sz, curr_lab_sz] = store2hdf5(filename, data, labels, create, startloc, chunksz)
% *data* is W*H*C*N matrix of images should be normalized (e.g. to lie between 0 and 1) beforehand
% *label* is D*N matrix of labels (D labels per sample)
% *create* [0/1] specifies whether to create file newly or to append to previously created file, useful to store information in batches when a dataset is too big to be held in memory (default: 1)
% *startloc* (point at which to start writing data). By default,
% if create=1 (create mode), startloc.data=[1 1 1 1], and startloc.lab=[1 1];
% if create=0 (append mode), startloc.data=[1 1 1 K+1], and startloc.lab = [1 K+1]; where K is the current number of samples stored in the HDF
% chunksz (used only in create mode), specifies number of samples to be stored per chunk (see HDF5 documentation on chunking) for creating HDF5 files with unbounded maximum size - TLDR; higher chunk sizes allow faster read-write operations
% verify that format is right
dat_dims=size(data);
lab_dims=size(labels);
num_samples=dat_dims(end);
assert(lab_dims(end)==num_samples, 'Number of samples should be matched between data and labels');
if ~exist('create','var')
create=true;
end
if create
%fprintf('Creating dataset with %d samples\n', num_samples);
if ~exist('chunksz', 'var')
chunksz=1000;
end
if exist(filename, 'file')
fprintf('Warning: replacing existing file %s \n', filename);
delete(filename);
end
h5create(filename, '/data', [dat_dims(1:end-1) Inf], 'Datatype', 'single', 'ChunkSize', [dat_dims(1:end-1) chunksz]); % width, height, channels, number
h5create(filename, '/label', [lab_dims(1:end-1) Inf], 'Datatype', 'single', 'ChunkSize', [lab_dims(1:end-1) chunksz]); % width, height, channels, number
if ~exist('startloc','var')
startloc.dat=[ones(1,length(dat_dims)-1), 1];
startloc.lab=[ones(1,length(lab_dims)-1), 1];
end
else % append mode
if ~exist('startloc','var')
info=h5info(filename);
prev_dat_sz=info.Datasets(1).Dataspace.Size;
prev_lab_sz=info.Datasets(2).Dataspace.Size;
assert(prev_dat_sz(1:end-1)==dat_dims(1:end-1), 'Data dimensions must match existing dimensions in dataset');
assert(prev_lab_sz(1:end-1)==lab_dims(1:end-1), 'Label dimensions must match existing dimensions in dataset');
startloc.dat=[ones(1,length(dat_dims)-1), prev_dat_sz(end)+1];
startloc.lab=[ones(1,length(lab_dims)-1), prev_lab_sz(end)+1];
end
end
if ~isempty(data)
h5write(filename, '/data', single(data), startloc.dat, size(data));
h5write(filename, '/label', single(labels), startloc.lab, size(labels));
end
if nargout
info=h5info(filename);
curr_dat_sz=info.Datasets(1).Dataspace.Size;
curr_lab_sz=info.Datasets(2).Dataspace.Size;
end
end
hdf5 matlab,hdf5格式的matlab读写操作相关推荐
- matlab对nii文件处理,使用Nibabel库对nii格式图像的读写操作
因为后期主要的研究方向是医学图像处理,而现有手头的大部分数据都是nii格式或者是hdr,img格式的数据,所以首先第一步我们需要解决图像的读写问题. 其实使用OpenCV也可以方便的进行图像读取,但是 ...
- 理解h5文件并使用python对h5格式文件进行读写操作
(一)HDF与h5 HDF(Hierarchical Data Format层次数据格式)是一种设计用于存储和组织大量数据的文件格式,最开始由美国国家超算中心研发,后来由一个非盈利组织HDF Gr ...
- matlab 数值格式转换,Matlab字符串转换及数值格式转换
Matlab字符串转换及数值格式转换 字符串转换函数 abs 字符串到ASCII转换 dec2hex 十进制数到十六进制字符串转换 fprintf 把格式化的文本写到文件中或显示屏上 hex2dec ...
- matlab eps格式,【MATLAB】论文图片处理(各种数据图转换成eps格式)
最近跟eps图片杠上了..转载一个人人网上matlab到eps图片输出格式较好的方法 随着Latex在论文编辑和排版的日益普及,越来越多的人写文章都用Latex,数据图表作为论文不可缺少的一部分,如何 ...
- matlab读取格式重复,matlab - 为什么Xlsread以字符串形式读取(日期时间) - 堆栈内存溢出...
我正在尝试阅读Excel工作表. 在同一台计算机上,我得到不同的结果. 我应该怎么做才能将日期时间读为日期而不是字符串? 我知道以后可以更改它,但是我需要为Octave和Matlab使用相同的代码. ...
- hdf5格式的matlab读写操作
最近要用caffe处理一个multi-label的回归问题,就是输出是一个向量,不是一个具体的数值,这个时候之前的leveldb格式就不凑效了,因为caffe源代码里面默认label是一个数值,网上搜 ...
- matlab segy文件,利用matlab实现segy格式数据的读写研究和分析.ppt
利用matlab实现segy格式数据的读写研究和分析 2.SEGY数据格式 地震数据一般以地震道为单位进行组织,采用SEG-Y文件格式存储.SEG-Y格式是由SEG (Society of Explo ...
- PLY文件格式及其MATLAB读写操作
PLY是一种电脑档案格式,全名为多边形档案(Polygon File Format)或 斯坦福三角形档案(Stanford Triangle Format). 史丹佛大学的 The Digital M ...
- matlab用excel的数据,使用MATLAB对excel文件数据的读写操作
最近建模,刚好用matlab对excel中的数据进行处理,由于自己是个菜鸟,基本都是边问度娘边动手操作.感觉以后还会用得到,在此做个简单的总结,为下一次省点力... excel读写操作:xlsread ...
最新文章
- 逼自己玩命学了3个多月,吃透了Python技术核心!分享给你,让你今年进个大厂!...
- 做科研发论文一直找不到研究热点?硕博导师都在关注的平台你还不快来?
- 你是否真的了解全局解析锁(GIL)
- ZOJ 3781 Paint the Grid Reloaded
- 一位同学想通过用计算机编程解决韩信点兵,高中信息技术《算法与程序设计模块》练习题...
- 趣学python3(7)-循环语句(1)
- kafka自带没web ui界面,怎么办?安装个第三方的
- 《C++ Primer 第五版》(第1~6章总结)
- 无法从ftp服务器上复制文件格式,ftp服务器上复制不了文件格式
- 真正解决ASP.NET每一个页面首次访问超级慢的问题
- OpenCV精进之路(二十三):实例——Bag of Features(BoF)图像分类实践
- Java Map是否有序?
- SSH框架搭建和整合(struts2、spring4、hibernate5)
- 布谷鸟哈希函数的参数_CuckooHash(布谷鸟散列)
- Spring 团队开源 nohttp;西部数据将中止与华为的战略合作
- 时差怎么理解_时差是怎么形成的?
- l10n php download,在PHP,WordPress,phpMyAdmin,C,i18n,L10n等双下划线?
- python多变量相关性分析_多变量相关性分析(一个因变量与多个自变量)
- 【python】统计正负数个数
- Big Mart Sales:预测销售结果 |Python