-
Notifications
You must be signed in to change notification settings - Fork 0
/
augment_data.py
executable file
·141 lines (131 loc) · 4.36 KB
/
augment_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
"""Raw data augmentation script.
"""
from keras.preprocessing.image import (
ImageDataGenerator,
array_to_img,
img_to_array,
load_img,
)
import numpy as np
import os
import glob
import cv2
import matplotlib.pyplot as plt
class myAugmentation(object):
"""
A class used to augmentate images using keras.
"""
def __init__(
self,
train_path="./data/train_aug/image",
label_path="./data/train_aug/label",
merge_path="./data/merge",
aug_merge_path="data/aug_merge",
aug_train_path="data/aug_train",
aug_label_path="data/aug_label",
img_type="tif",
):
"""
Using glob to get all .img_type form path
"""
self.train_imgs = glob.glob(train_path + "/*." + img_type)
self.label_imgs = glob.glob(label_path + "/*." + img_type) # label
self.train_path = train_path
self.label_path = label_path
self.merge_path = merge_path
self.img_type = img_type
self.aug_merge_path = aug_merge_path
self.aug_train_path = aug_train_path
self.aug_label_path = aug_label_path
self.slices = len(self.train_imgs)
self.datagen = ImageDataGenerator(
rotation_range=0.2,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=10,
zoom_range=0.05,
vertical_flip=True,
horizontal_flip=True,
fill_mode="nearest",
)
def Augmentation(self):
print("Augmentation")
"""
Start augmentation.....
"""
trains = self.train_imgs
labels = self.label_imgs
path_train = self.train_path
path_label = self.label_path
path_merge = self.merge_path
imgtype = self.img_type
path_aug_merge = self.aug_merge_path
print(len(trains), len(labels))
if len(trains) != len(labels) or len(trains) == 0 or len(trains) == 0:
print("trains can't match labels")
return 0
for i in range(len(trains)):
img_t = load_img(path_train + "/" + str(i) + "." + imgtype)
img_l = load_img(path_label + "/" + str(i) + "." + imgtype)
x_t = img_to_array(img_t)
x_l = img_to_array(img_l)
x_t[:, :, 2] = x_l[:, :, 0]
img_tmp = array_to_img(x_t)
img_tmp.save(path_merge + "/" + str(i) + "." + imgtype)
img = x_t
img = img.reshape((1,) + img.shape) # shape(1, 512, 512, 3)
savedir = path_aug_merge + "/" + str(i)
if not os.path.lexists(savedir):
os.mkdir(savedir)
self.doAugmentate(img, savedir, str(i))
def doAugmentate(
self, img, save_to_dir, save_prefix, batch_size=1, save_format="tif", imgnum=0
):
"""
augmentate one image
"""
datagen = self.datagen
i = 0
for batch in datagen.flow(
img,
batch_size=batch_size,
save_to_dir=save_to_dir,
save_prefix=save_prefix,
save_format=save_format,
):
i += 1
if i > imgnum:
break
def splitMerge(self):
print("splitMerge")
"""
split merged image apart
"""
path_merge = self.aug_merge_path
path_train = self.aug_train_path
path_label = self.aug_label_path
for i in range(self.slices):
path = path_merge + "/" + str(i)
print(path)
train_imgs = glob.glob(path + "/*." + "tif")
savedir = path_train
if not os.path.lexists(savedir):
os.mkdir(savedir)
savedir = path_label
if not os.path.lexists(savedir):
os.mkdir(savedir)
for imgname in train_imgs:
midname = imgname[imgname.rindex("/") + 1 : imgname.rindex("." + "tif")]
img = cv2.imread(imgname)
img_train = img[:, :, 2]
img_label = img[:, :, 0]
cv2.imwrite(
path_train + "/" + midname + "." + "tif", img_train
) # label
cv2.imwrite(
path_label + "/" + midname + "_mask" + "." + "tif", img_label
)
if __name__ == "__main__":
aug = myAugmentation()
aug.Augmentation()
aug.splitMerge()