f554b87e5f633fa734513fb953af1be2f6678ab1
[platform/upstream/caffeonacl.git] / matlab / caffe / hdf5creation / demo.m
1 %% WRITING TO HDF5
2 filename='trial.h5';
3
4 num_total_samples=10000;
5 % to simulate data being read from disk / generated etc.
6 data_disk=rand(5,5,1,num_total_samples); 
7 label_disk=rand(10,num_total_samples); 
8
9 chunksz=100;
10 created_flag=false;
11 totalct=0;
12 for batchno=1:num_total_samples/chunksz
13   fprintf('batch no. %d\n', batchno);
14   last_read=(batchno-1)*chunksz;
15
16   % to simulate maximum data to be held in memory before dumping to hdf5 file 
17   batchdata=data_disk(:,:,1,last_read+1:last_read+chunksz); 
18   batchlabs=label_disk(:,last_read+1:last_read+chunksz);
19
20   % store to hdf5
21   startloc=struct('dat',[1,1,1,totalct+1], 'lab', [1,totalct+1]);
22   curr_dat_sz=store2hdf5(filename, batchdata, batchlabs, ~created_flag, startloc, chunksz); 
23   created_flag=true;% flag set so that file is created only once
24   totalct=curr_dat_sz(end);% updated dataset size (#samples)
25 end
26
27 % display structure of the stored HDF5 file
28 h5disp(filename);
29
30 %% READING FROM HDF5
31
32 % Read data and labels for samples #1000 to 1999
33 data_rd=h5read(filename, '/data', [1 1 1 1000], [5, 5, 1, 1000]);
34 label_rd=h5read(filename, '/label', [1 1000], [10, 1000]);
35 fprintf('Testing ...\n');
36 try 
37   assert(isequal(data_rd, single(data_disk(:,:,:,1000:1999))), 'Data do not match');
38   assert(isequal(label_rd, single(label_disk(:,1000:1999))), 'Labels do not match');
39
40   fprintf('Success!\n');
41 catch err
42   fprintf('Test failed ...\n');
43   getReport(err)
44 end
45
46 %delete(filename);
47
48 % CREATE list.txt containing filename, to be used as source for HDF5_DATA_LAYER
49 FILE=fopen('list.txt', 'w');
50 fprintf(FILE, '%s', filename);
51 fclose(FILE);
52 fprintf('HDF5 filename listed in %s \n', 'list.txt');
53
54 % NOTE: In net definition prototxt, use list.txt as input to HDF5_DATA as: 
55 % layers {
56 %   name: "data"
57 %   type: HDF5_DATA
58 %   top: "data"
59 %   top: "labelvec"
60 %   hdf5_data_param {
61 %     source: "/path/to/list.txt"
62 %     batch_size: 64
63 %   }
64 % }