This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgen_camvid_hdf5.py
94 lines (76 loc) · 2.99 KB
/
gen_camvid_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# load the camvid data sets and save in a ndarray,
# output to a pickle file for use witht he ArrayIterator
import argparse
import os
import pickle
import numpy as np
from glob import glob
from scipy.misc import imresize
from scipy.ndimage import imread
import h5py
np.random.seed(1)
pth_to_camvid = 'SegNet-Tutorial/CamVid'
pth_to_camvid = os.path.abspath(pth_to_camvid)
if not os.path.isdir(pth_to_camvid):
raise ValueError('Set path to SegNet-Tutorial repo CamVid dir')
H = 256
W = 512
C = 3
NCLASS = 12
new_shape = (C, H, W)
new_shape_annot = (NCLASS, H, W)
mns = np.zeros((C,))
for settype in ['train', 'val', 'test']:
print settype
# load up the image and the corresponding annotation
fns = glob(os.path.join(pth_to_camvid, settype, '*.png'))
# randomize image order
inds = np.random.permutation(len(fns))
imgs = np.zeros((len(fns), np.prod(new_shape)))
imgs_annot = np.zeros((len(fns), np.prod(new_shape_annot)))
im_annot_out = np.zeros(new_shape_annot)
# the settype + 'files' file in the CamVid directory will
# have the output files in order
fid2 = open(os.path.join(pth_to_camvid, settype + 'files'), 'w')
for cnt in range(len(fns)):
fn = fns[inds[cnt]]
fid2.write(fn + '\n')
# load the image (shape is [H, W, C]
im = imread(fn)
fn_annot = os.path.join(pth_to_camvid, settype + 'annot', os.path.basename(fn))
im_annot = imread(fn_annot)
# resize images to 256 x 512
im = imresize(im, new_shape[1:3])
im = im.transpose((2,0,1))
im_annot = imresize(im_annot, new_shape_annot[1:3], interp='nearest')
# map the im_annot to channel
im_annot_out[:] = 0
for h in range(new_shape_annot[1]):
for w in range(new_shape_annot[2]):
chan_ind = int(im_annot[h, w])
im_annot_out[chan_ind, h, w] = 1.0
# Nimages x (flattened shape)
imgs[cnt, :] = im.flatten()
imgs_annot[cnt, :] = im_annot_out.flatten()
# quick check
annot_test = imgs_annot.reshape((cnt+1, new_shape_annot[0], -1))
uvals = np.unique(np.sum(annot_test, axis=1))
assert len(uvals) == 1
assert uvals[0] == 1.0
fid2.close()
# calculate the mean pixel vals for each channel
# for the training set
if settype == 'train':
mnview = imgs.reshape((imgs.shape[0], C, -1))
mns = np.mean(np.mean(mnview, axis=2), axis=0)
mns_array = np.zeros(new_shape)
for c in range(C):
mns_array[c, :, :] = mns[c]
mns_array = mns_array.flatten().copy()
imgs = imgs - mns_array
with h5py.File(os.path.join(pth_to_camvid, settype + '_images.h5'), mode='w') as out_file:
out_file.create_dataset("input", data=imgs.astype(np.float32))
out_file['input'].attrs['lshape'] = new_shape
out_file['input'].attrs['mean'] = mns
out_file.create_dataset("output", data=imgs_annot.astype(np.uint8), dtype='uint8')
out_file['output'].attrs['lshape'] = new_shape_annot