forked from akirasosa/mobile-semantic-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
160 lines (133 loc) · 4.42 KB
/
data.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import argparse
import os
from glob import glob
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from scipy.misc import imresize
from scipy.ndimage import imread
from sklearn.model_selection import train_test_split
seed = 1
def standardize(images, mean=None, std=None):
if mean is None:
# These values are available from all images.
mean = [[[29.24429131, 29.24429131, 29.24429131]]]
if std is None:
# These values are available from all images.
std = [[[69.8833313, 63.37436676, 61.38568878]]]
x = (images - np.array(mean)) / (np.array(std) + 1e-7)
return x
def _create_datagen(images, masks, img_gen, mask_gen):
img_iter = img_gen.flow(images, seed=seed)
# only hair
mask_iter = mask_gen.flow(np.expand_dims(masks[:, :, :, 0], axis=4),
# use same seed to apply same augmentation with image
seed=seed)
def datagen():
while True:
img = img_iter.next()
mask = mask_iter.next()
yield img, mask
return datagen
def load_data(img_file, mask_file):
images = np.load(img_file)
masks = np.load(mask_file)
X_train, X_val, Y_train, Y_val = train_test_split(images,
masks,
test_size=0.2,
random_state=seed)
train_img_gen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
# rescale=1. / 255,
rotation_range=20,
shear_range=0.2,
zoom_range=0.2,
# vertical_flip=True, # debug
horizontal_flip=True,
)
train_img_gen.fit(images)
train_mask_gen = ImageDataGenerator(
rescale=1. / 255,
rotation_range=20,
shear_range=0.2,
zoom_range=0.2,
# vertical_flip=True, # debug
horizontal_flip=True,
)
train_gen = _create_datagen(X_train, Y_train,
img_gen=train_img_gen,
mask_gen=train_mask_gen)
validation_img_gen = ImageDataGenerator(
featurewise_center=True,
featurewise_std_normalization=True,
horizontal_flip=True,
)
validation_img_gen.fit(images)
validation_mask_gen = ImageDataGenerator(
rescale=1. / 255,
horizontal_flip=True,
)
validation_gen = _create_datagen(X_val, Y_val,
img_gen=validation_img_gen,
mask_gen=validation_mask_gen)
return train_gen, validation_gen, images.shape[1:3]
def create_data(data_dir, out_dir, img_size):
"""
It expects following directory layout in data_dir.
images/
0001.jpg
0002.jpg
masks/
0001.ppm
0002.ppm
Mask image has 3 colors R, G and B. R is hair. G is face. B is bg.
Finally, it will create images.npy and masks.npy in out_dir.
:param data_dir:
:param out_dir:
:param img_size:
:return:
"""
img_files = sorted(glob(data_dir + '/images/*.jpg'))
mask_files = sorted(glob(data_dir + '/masks/*.ppm'))
X = []
Y = []
for img_path, mask_path in zip(img_files, mask_files):
img = imread(img_path)
img = imresize(img, (img_size, img_size))
mask = imread(mask_path)
mask = imresize(mask, (img_size, img_size), interp='nearest')
# debug
if False:
import matplotlib.pyplot as plt
plt.subplot(1, 2, 1)
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.imshow(mask)
plt.show()
X.append(img)
Y.append(mask)
if not os.path.exists(out_dir):
os.makedirs(out_dir)
np.save(out_dir + '/images-{}.npy'.format(img_size), np.array(X))
np.save(out_dir + '/masks-{}.npy'.format(img_size), np.array(Y))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--data_dir',
type=str,
default='data/raw',
help='directory in which images and masks are placed.'
)
parser.add_argument(
'--out_dir',
type=str,
default='data',
help='directory to put outputs.'
)
parser.add_argument(
'--img_size',
type=int,
default=192,
)
args, _ = parser.parse_known_args()
create_data(**vars(args))