-
Notifications
You must be signed in to change notification settings - Fork 23
/
infer_image_all.py
251 lines (206 loc) · 7.48 KB
/
infer_image_all.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import os
import random
import numpy as np
import cv2
from tqdm import tqdm
from glob import glob
import tifffile as tif
from sklearn.model_selection import train_test_split
from utils import *
from albumentations import (
PadIfNeeded,
HorizontalFlip,
VerticalFlip,
CenterCrop,
Crop,
Compose,
Transpose,
RandomRotate90,
ElasticTransform,
GridDistortion,
OpticalDistortion,
RandomSizedCrop,
OneOf,
CLAHE,
RandomBrightnessContrast,
RandomGamma,
HueSaturationValue,
RGBShift,
RandomBrightness,
RandomContrast,
MotionBlur,
MedianBlur,
GaussianBlur,
GaussNoise,
ChannelShuffle,
CoarseDropout
)
def augment_data(images, masks, save_path, augment=True):
""" Performing data augmentation. """
crop_size = (256, 256)
size = (256, 256)
for image, mask in tqdm(zip(images, masks), total=len(images)):
image_name = image.split("/")[-1].split(".")[0]
mask_name = mask.split("/")[-1].split(".")[0]
x, y = read_data(image, mask)
h, w, c = x.shape
if augment == True:
## Center Crop
aug = CenterCrop(p=1, height=crop_size[0], width=crop_size[0])
augmented = aug(image=x, mask=y)
x1 = augmented['image']
y1 = augmented['mask']
## Crop
x_min = 0
y_min = 0
x_max = x_min + size[1]
y_max = y_min + size[0]
aug = Crop(p=1, x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max)
augmented = aug(image=x, mask=y)
x2 = augmented['image']
y2 = augmented['mask']
## Random Rotate 90 degree
aug = RandomRotate90(p=1)
augmented = aug(image=x, mask=y)
x3 = augmented['image']
y3 = augmented['mask']
## Transpose
aug = Transpose(p=1)
augmented = aug(image=x, mask=y)
x4 = augmented['image']
y4 = augmented['mask']
## ElasticTransform
aug = ElasticTransform(p=1, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03)
augmented = aug(image=x, mask=y)
x5 = augmented['image']
y5 = augmented['mask']
## Grid Distortion
aug = GridDistortion(p=1)
augmented = aug(image=x, mask=y)
x6 = augmented['image']
y6 = augmented['mask']
## Optical Distortion
aug = OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5)
augmented = aug(image=x, mask=y)
x7 = augmented['image']
y7 = augmented['mask']
## Vertical Flip
aug = VerticalFlip(p=1)
augmented = aug(image=x, mask=y)
x8 = augmented['image']
y8 = augmented['mask']
## Horizontal Flip
aug = HorizontalFlip(p=1)
augmented = aug(image=x, mask=y)
x9 = augmented['image']
y9 = augmented['mask']
## Grayscale
x10 = cv2.cvtColor(x, cv2.COLOR_RGB2GRAY)
y10 = y
## Grayscale Vertical Flip
aug = VerticalFlip(p=1)
augmented = aug(image=x10, mask=y10)
x11 = augmented['image']
y11 = augmented['mask']
## Grayscale Horizontal Flip
aug = HorizontalFlip(p=1)
augmented = aug(image=x10, mask=y10)
x12 = augmented['image']
y12 = augmented['mask']
## Grayscale Center Crop
aug = CenterCrop(p=1, height=crop_size[0], width=crop_size[0])
augmented = aug(image=x10, mask=y10)
x13 = augmented['image']
y13 = augmented['mask']
##
aug = RandomBrightnessContrast(p=1)
augmented = aug(image=x, mask=y)
x14 = augmented['image']
y14 = augmented['mask']
aug = RandomGamma(p=1)
augmented = aug(image=x, mask=y)
x15 = augmented['image']
y15 = augmented['mask']
aug = HueSaturationValue(p=1)
augmented = aug(image=x, mask=y)
x16 = augmented['image']
y16 = augmented['mask']
aug = RGBShift(p=1)
augmented = aug(image=x, mask=y)
x17 = augmented['image']
y17 = augmented['mask']
aug = RandomBrightness(p=1)
augmented = aug(image=x, mask=y)
x18 = augmented['image']
y18 = augmented['mask']
aug = RandomContrast(p=1)
augmented = aug(image=x, mask=y)
x19 = augmented['image']
y19 = augmented['mask']
aug = MotionBlur(p=1, blur_limit=7)
augmented = aug(image=x, mask=y)
x20 = augmented['image']
y20 = augmented['mask']
aug = MedianBlur(p=1, blur_limit=10)
augmented = aug(image=x, mask=y)
x21 = augmented['image']
y21 = augmented['mask']
aug = GaussianBlur(p=1, blur_limit=10)
augmented = aug(image=x, mask=y)
x22 = augmented['image']
y22 = augmented['mask']
aug = GaussNoise(p=1)
augmented = aug(image=x, mask=y)
x23 = augmented['image']
y23 = augmented['mask']
aug = ChannelShuffle(p=1)
augmented = aug(image=x, mask=y)
x24 = augmented['image']
y24 = augmented['mask']
aug = CoarseDropout(p=1, max_holes=8, max_height=32, max_width=32)
augmented = aug(image=x, mask=y)
x25 = augmented['image']
y25 = augmented['mask']
images = [
x, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10,
x11, x12, x13, x14, x15, x16, x17, x18, x19, x20,
x21, x22, x23, x24, x25
]
masks = [
y, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10,
y11, y12, y13, y14, y15, y16, y17, y18, y19, y20,
y21, y22, y23, y24, y25
]
else:
images = [x]
masks = [y]
idx = 0
for i, m in zip(images, masks):
i = cv2.resize(i, size)
m = cv2.resize(m, size)
tmp_image_name = f"{image_name}_{idx}.jpg"
tmp_mask_name = f"{mask_name}_{idx}.jpg"
image_path = os.path.join(save_path, "image/", tmp_image_name)
mask_path = os.path.join(save_path, "mask/", tmp_mask_name)
cv2.imwrite(image_path, i)
cv2.imwrite(mask_path, m)
idx += 1
def load_data(path, split=0.1):
""" Load all the data and then split them into train and valid dataset. """
img_path = glob(os.path.join(path, "images/*"))
msk_path = glob(os.path.join(path, "masks/*"))
train_x, valid_x = train_test_split(img_path, test_size=split, random_state=42)
train_y, valid_y = train_test_split(msk_path, test_size=split, random_state=42)
return (train_x, train_y), (valid_x, valid_y)
def main():
np.random.seed(42)
path = "../../../ml_dataset/Kvasir-SEG/"
(train_x, train_y), (valid_x, valid_y) = load_data(path, split=0.2)
create_dir("new_data/train/image/")
create_dir("new_data/train/mask/")
create_dir("new_data/valid/image/")
create_dir("new_data/valid/mask/")
augment_data(train_x, train_y, "new_data/train/", augment=True)
augment_data(valid_x, valid_y, "new_data/valid/", augment=False)
if __name__ == "__main__":
main()