-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcreate_aug_night.py
executable file
·146 lines (111 loc) · 4.94 KB
/
create_aug_night.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
import numpy as np
import pandas as pd
from keras.preprocessing import image
from os.path import join
import matplotlib.pyplot as plt
from PIL import Image
import scipy.misc
input_size = 300
data_dir = './dataset/'
NO_OF_IMAGES = 115
img_ids = []
for i in range(NO_OF_IMAGES):
if len(str(i+1)) == 3:
item = str(i+1)
elif len(str(i+1)) == 2:
item = '0' + str(i+1)
elif len(str(i+1)) == 1:
item = '00' + str(i+1)
img_ids.append(item)
def get_image_and_mask(img_id):
my_image = data_dir + 'SWINSEG/' + 'images/' + str(img_id) + '.jpg'
my_GT = data_dir + 'SWINSEG/' + 'GTmaps/' + str(img_id) + '_GT.jpg'
img = image.load_img(my_image,
target_size=(input_size, input_size))
img = image.img_to_array(img)
mask = image.load_img(my_GT,
grayscale=True, target_size=(input_size, input_size))
mask = image.img_to_array(mask)
img, mask = img / 255., mask / 255.
return img, mask
# Different augmentation techniques
def random_flip(img, mask, u=0.5):
if np.random.random() < u:
img = image.flip_axis(img, 1)
mask = image.flip_axis(mask, 1)
return img, mask
def rotate(x, theta, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest', cval=0.):
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1]])
h, w = x.shape[row_axis], x.shape[col_axis]
transform_matrix = image.transform_matrix_offset_center(rotation_matrix, h, w)
x = image.apply_transform(x, transform_matrix, channel_axis, fill_mode, cval)
return x
def random_rotate(img, mask, rotate_limit=(-20, 20), u=0.5):
if np.random.random() < u:
theta = np.pi / 180 * np.random.uniform(rotate_limit[0], rotate_limit[1])
img = rotate(img, theta)
mask = rotate(mask, theta)
return img, mask
def shift(x, wshift, hshift, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest', cval=0.):
h, w = x.shape[row_axis], x.shape[col_axis]
tx = hshift * h
ty = wshift * w
translation_matrix = np.array([[1, 0, tx],
[0, 1, ty],
[0, 0, 1]])
transform_matrix = translation_matrix # no need to do offset
x = image.apply_transform(x, transform_matrix, channel_axis, fill_mode, cval)
return x
def random_shift(img, mask, w_limit=(-0.1, 0.1), h_limit=(-0.1, 0.1), u=0.5):
if np.random.random() < u:
wshift = np.random.uniform(w_limit[0], w_limit[1])
hshift = np.random.uniform(h_limit[0], h_limit[1])
img = shift(img, wshift, hshift)
mask = shift(mask, wshift, hshift)
return img, mask
def zoom(x, zx, zy, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest', cval=0.):
zoom_matrix = np.array([[zx, 0, 0],
[0, zy, 0],
[0, 0, 1]])
h, w = x.shape[row_axis], x.shape[col_axis]
transform_matrix = image.transform_matrix_offset_center(zoom_matrix, h, w)
x = image.apply_transform(x, transform_matrix, channel_axis, fill_mode, cval)
return x
def random_zoom(img, mask, zoom_range=(0.8, 1), u=0.5):
if np.random.random() < u:
zx, zy = np.random.uniform(zoom_range[0], zoom_range[1], 2)
img = zoom(img, zx, zy)
mask = zoom(mask, zx, zy)
return img, mask
def shear(x, shear, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest', cval=0.):
shear_matrix = np.array([[1, -np.sin(shear), 0],
[0, np.cos(shear), 0],
[0, 0, 1]])
h, w = x.shape[row_axis], x.shape[col_axis]
transform_matrix = image.transform_matrix_offset_center(shear_matrix, h, w)
x = image.apply_transform(x, transform_matrix, channel_axis, fill_mode, cval)
return x
def random_shear(img, mask, intensity_range=(-0.5, 0.5), u=0.5):
if np.random.random() < u:
sh = np.random.uniform(-intensity_range[0], intensity_range[1])
img = shear(img, sh)
mask = shear(mask, sh)
return img, mask
def random_augmentation(img, mask):
img, mask = random_rotate(img, mask, rotate_limit=(-20, 20), u=0.5)
img, mask = random_shear(img, mask, intensity_range=(-0.3, 0.3), u=0.2)
img, mask = random_flip(img, mask, u=0.3)
img, mask = random_shift(img, mask, w_limit=(-0.1, 0.1), h_limit=(-0.1, 0.1), u=0.3)
img, mask = random_zoom(img, mask, zoom_range=(0.8, 1), u=0.3)
return img, mask
NO_OF_Xs = 5
for img_id in img_ids:
img, mask = get_image_and_mask(img_id)
print (['Processing input image for ', img_id])
print (['Augmentation for ', img_id])
for i in range(NO_OF_Xs):
img_aug, mask_aug = random_augmentation(img, mask)
scipy.misc.imsave(data_dir + 'aug_SWINSEG/images/'+img_id+'_'+str(i)+'.jpg', img_aug)
scipy.misc.imsave(data_dir + 'aug_SWINSEG/GTmaps/'+img_id+'_'+str(i)+'.jpg', mask_aug[:, :, 0])