-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_generator.py
216 lines (168 loc) · 6.45 KB
/
data_generator.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import glob
import os
import cv2
import numpy as np
from imgaug import augmenters as iaa
from tensorflow.keras.preprocessing.image import ImageDataGenerator
Road = [128, 64, 128]
Noroad = [255, 73, 95]
COLORS = np.array([Road, Noroad])
aug_blurer = iaa.MotionBlur(k=(3, 7))
aug_fogger = iaa.Fog()
aug_brightness = iaa.imgcorruptlike.Brightness(severity=2)
def normalize_image(img, colorspace='rgb'):
if colorspace == 'rgb':
return img / 255
# return img / 127.5 - 1
elif colorspace == 'hsv':
# cv2 rgb to hsv returns H value in range [0, 180] if the image
# is of type int. In case of float, the range is [0, 360]
img = img.astype(np.float32)
if len(img.shape) == 4:
for i in range(img.shape[0]):
img[i] = cv2.cvtColor(img[i], cv2.COLOR_RGB2HSV)
c = img
else:
c = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
return c / [360, 1, 255]
def fix_mask(mask):
m = mask.copy()
m[m < 30] = 0.0
m[m > 0] = 1.0
return m
def train_generator(batch_size, train_path, image_folder, mask_folder,
img_target_size=(480, 640), augs={}, tohsv=False,
aug=False):
image_datagen = ImageDataGenerator(**augs)
masks_datagen = ImageDataGenerator(preprocessing_function=fix_mask, **augs)
image_generator = image_datagen.flow_from_directory(
train_path,
class_mode=None,
classes=[image_folder],
color_mode='rgb',
target_size=img_target_size,
batch_size=batch_size,
seed=1
)
mask_generator = masks_datagen.flow_from_directory(
train_path,
class_mode=None,
classes=[mask_folder],
color_mode='grayscale',
target_size=img_target_size,
batch_size=batch_size,
seed=1
)
colorspace = 'rgb'
if tohsv:
colorspace = 'hsv'
generator = zip(image_generator, mask_generator)
for (img, mask) in generator:
if aug:
# blur augmentation
img_aug = aug_blurer.augment_images(img)
img_aug = normalize_image(img_aug, colorspace=colorspace)
yield (img_aug, mask)
# fog augmentation
# img_aug = aug_fogger.augment_images(img)
# img_aug = normalize_image(img_aug, colorspace=colorspace)
# yield (img_aug, mask)
# brightness augmentation
img_aug = aug_brightness.augment_images(img.astype(np.uint8))
img_aug = normalize_image(img_aug, colorspace=colorspace)
yield (img_aug, mask)
img = normalize_image(img, colorspace=colorspace)
yield (img, mask)
def test_data_generator(test_path, image_folder, img_target_size=(480, 640),
tohsv=False):
augs = {}
test_datagen = ImageDataGenerator(**augs)
image_gen = test_datagen.flow_from_directory(
test_path,
class_mode=None,
classes=[image_folder],
color_mode='rgb',
target_size=img_target_size,
batch_size=1,
shuffle=False
)
colorspace = 'rgb'
if tohsv:
colorspace = 'hsv'
for img in image_gen:
img = normalize_image(img, colorspace=colorspace)
yield (img,)
def eval_generator(batch_size, test_path, image_folder, mask_folder,
img_target_size=(480, 640), tohsv=False, aug=True):
return train_generator(batch_size, test_path, image_folder, mask_folder,
img_target_size, augs={}, tohsv=tohsv, aug=aug)
def load_data_memory(train_paths, image_folder, mask_folder, resize=(640, 480),
tohsv=False, aug=True):
X = []
Y = []
colorspace = 'rgb'
if tohsv:
colorspace = 'hsv'
for train_path in train_paths:
for i in sorted(glob.glob(os.path.join(train_path, image_folder, '*.png'))):
img = cv2.imread(i).astype(np.float32)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, resize)
if aug:
img_aug = aug_blurer.augment_image(img)
img_aug = normalize_image(img_aug, colorspace=colorspace)
X.append(img_aug)
# img_aug = aug_fogger.augment_images(img)
# img_aug = normalize_image(img_aug, colorspace=colorspace)
# X.append(img_aug)
img_aug = aug_brightness.augment_image(img.astype(np.uint8))
img_aug = normalize_image(img_aug, colorspace=colorspace)
X.append(img_aug)
img = normalize_image(img, colorspace=colorspace)
X.append(img)
for i in sorted(glob.glob(os.path.join(train_path, mask_folder, '*.png'))):
mask = cv2.imread(i, cv2.IMREAD_GRAYSCALE)
mask = cv2.resize(mask, resize)
mask = mask.reshape(resize[1], resize[0], 1)
mask = fix_mask(mask)
Y.append(mask)
if aug:
Y.append(mask)
# Y.append(mask)
Y.append(mask)
X = np.array(X)
Y = np.array(Y)
return (X, Y)
def load_single_image(path, resize=(640, 480), tohsv=False):
colorspace = 'rgb'
if tohsv:
colorspace = 'hsv'
img = cv2.imread(path).astype(np.float32)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, resize)
img = normalize_image(img, colorspace=colorspace)
return img
def save_predicted_images(path, test_image_folder, predictions, resize_to, save_mask):
os.makedirs(path, exist_ok=True)
test_imgs = sorted(glob.glob(os.path.join(test_image_folder, '*.png')))
COLORIZED_ONES = np.ones((480, 640, 3)) * (1, 0, 1)
for p, t in zip(predictions, test_imgs):
p[p <= 0.5] = 0
p[p > 0.5] = 255
img = cv2.imread(t)
img = cv2.resize(img, resize_to)
basename = os.path.basename(t)
if save_mask:
# save predicted mask
cv2.imwrite(os.path.join(path, 'mask_{}'.format(basename)), p)
mask = cv2.cvtColor(p, cv2.COLOR_GRAY2BGR) * COLORIZED_ONES
out = cv2.addWeighted(img, 1, mask.astype(np.uint8), 0.7, 0)
cv2.imwrite(os.path.join(path, '{}'.format(basename)), out)
if __name__ == '__main__':
data_gen_args = dict(horizontal_flip=True)
gen = train_generator(2, 'data/train', 'image',
'masks',
augs=data_gen_args,
tohsv=False)
for (img, mask) in gen:
print(img)