-
Notifications
You must be signed in to change notification settings - Fork 26
/
write_hdf5.py
54 lines (43 loc) · 2 KB
/
write_hdf5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from hdf5_util import *
output_filename_prefix = 'volume_data'
def write_tensor_label_hdf5(mat_filelist, volume_size, pad_size):
""" We will use constant label (class 0) for the test data """
tensor_filenames = [line.rstrip() for line in open(mat_filelist, 'r')]
labels = [0 for _ in range(len(tensor_filenames))]
N = len(tensor_filenames)
assert(N<=10000)
# =============================================================================
# Specify what data and label to write, CHNAGE this according to your needs...
vox_size = volume_size + pad_size * 2
data_dim = [1,vox_size, vox_size, vox_size]
label_dim = [1]
data_dtype = 'uint8'
label_dtype = 'uint8'
# =============================================================================
h5_batch_size = N
# set batch buffer
batch_data_dim = [min(h5_batch_size,N)] + data_dim
batch_label_dim = [min(h5_batch_size,N)] + label_dim
h5_batch_data = np.zeros(batch_data_dim)
h5_batch_label = np.zeros(batch_label_dim)
for k in range(N):
mat = sio.loadmat(tensor_filenames[k])
d = mat[mat.keys()[0]]
l = labels[k]
h5_batch_data[k%h5_batch_size, ...] = d
h5_batch_label[k%h5_batch_size, ...] = l
if (k+1)%h5_batch_size == 0 or k==N-1:
print '[%s] %d/%d' % (datetime.datetime.now(), k+1, N)
print 'batch data shape: ', h5_batch_data.shape
h5_filename = output_filename_prefix+str(k/h5_batch_size)+'.h5'
print h5_filename
print np.shape(h5_batch_data)
print np.shape(h5_batch_label)
begidx = 0
endidx = min(h5_batch_size, (k%h5_batch_size)+1)
print h5_filename, data_dtype, label_dtype
save_h5(h5_filename, h5_batch_data[begidx:endidx,:,:,:,:], h5_batch_label[begidx:endidx,:], data_dtype, label_dtype)
write_tensor_label_hdf5('mat_filelist.txt', 26, 3)
(d,l) = load_h5(output_filename_prefix+'0.h5')
print d.shape
print l.shape