-
Notifications
You must be signed in to change notification settings - Fork 38
/
data.py
165 lines (155 loc) · 6.69 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
161
162
163
164
165
from __future__ import print_function
from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
import numpy as np
import os
import skimage.io as io
import skimage.transform as trans
import cv2
import warnings
warnings.filterwarnings("ignore")
BackGround = [255, 255, 255]
road = [0, 0, 0]
# COLOR_DICT = np.array([BackGround, road])
one = [128, 128, 128]
two = [128, 0, 0]
three = [192, 192, 128]
four = [255, 69, 0]
five = [128, 64, 128]
six = [60, 40, 222]
seven = [128, 128, 0]
eight = [192, 128, 128]
nine = [64, 64, 128]
ten = [64, 0, 128]
eleven = [64, 64, 0]
twelve = [0, 128, 192]
COLOR_DICT = np.array([one, two,three,four,five,six,seven,eight,nine,ten,eleven,twelve])
class data_preprocess:
def __init__(self, train_path=None, image_folder=None, label_folder=None,
valid_path=None,valid_image_folder =None,valid_label_folder = None,
test_path=None, save_path=None,
img_rows=512, img_cols=512,
flag_multi_class=False,
num_classes = 2):
self.img_rows = img_rows
self.img_cols = img_cols
self.train_path = train_path
self.image_folder = image_folder
self.label_folder = label_folder
self.valid_path = valid_path
self.valid_image_folder = valid_image_folder
self.valid_label_folder = valid_label_folder
self.test_path = test_path
self.save_path = save_path
self.data_gen_args = dict(rotation_range=0.2,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
zoom_range=0.05,
vertical_flip=True,
horizontal_flip=True,
fill_mode='nearest')
self.image_color_mode = "rgb"
self.label_color_mode = "rgb"
self.flag_multi_class = flag_multi_class
self.num_class = num_classes
self.target_size = (512, 512)
self.img_type = 'png'
def adjustData(self, img, label):
if (self.flag_multi_class):
img = img / 255.
label = label[:, :, :, 0] if (len(label.shape) == 4) else label[:, :, 0]
new_label = np.zeros(label.shape + (self.num_class,))
for i in range(self.num_class):
new_label[label == i, i] = 1
label = new_label
elif (np.max(img) > 1):
img = img / 255.
label = label / 255.
label[label > 0.5] = 1
label[label <= 0.5] = 0
return (img, label)
def trainGenerator(self, batch_size, image_save_prefix="image", label_save_prefix="label",
save_to_dir=None, seed=7):
'''
can generate image and label at the same time
use the same seed for image_datagen and label_datagen to ensure the transformation for image and label is the same
if you want to visualize the results of generator, set save_to_dir = "your path"
'''
image_datagen = ImageDataGenerator(**self.data_gen_args)
label_datagen = ImageDataGenerator(**self.data_gen_args)
image_generator = image_datagen.flow_from_directory(
self.train_path,
classes=[self.image_folder],
class_mode=None,
color_mode=self.image_color_mode,
target_size=self.target_size,
batch_size=batch_size,
save_to_dir=save_to_dir,
save_prefix=image_save_prefix,
seed=seed)
label_generator = label_datagen.flow_from_directory(
self.train_path,
classes=[self.label_folder],
class_mode=None,
color_mode=self.label_color_mode,
target_size=self.target_size,
batch_size=batch_size,
save_to_dir=save_to_dir,
save_prefix=label_save_prefix,
seed=seed)
train_generator = zip(image_generator, label_generator)
for (img, label) in train_generator:
img, label = self.adjustData(img, label)
yield (img, label)
def testGenerator(self):
filenames = os.listdir(self.test_path)
for filename in filenames:
img = io.imread(os.path.join(self.test_path, filename), as_gray=False)
img = img / 255.
img = trans.resize(img, self.target_size, mode='constant')
img = np.reshape(img, img.shape + (1,)) if (not self.flag_multi_class) else img
img = np.reshape(img, (1,) + img.shape)
yield img
def validLoad(self, batch_size,seed=7):
image_datagen = ImageDataGenerator(**self.data_gen_args)
label_datagen = ImageDataGenerator(**self.data_gen_args)
image_generator = image_datagen.flow_from_directory(
self.valid_path,
classes=[self.valid_image_folder],
class_mode=None,
color_mode=self.image_color_mode,
target_size=self.target_size,
batch_size=batch_size,
seed=seed)
label_generator = label_datagen.flow_from_directory(
self.valid_path,
classes=[self.valid_label_folder],
class_mode=None,
color_mode=self.label_color_mode,
target_size=self.target_size,
batch_size=batch_size,
seed=seed)
train_generator = zip(image_generator, label_generator)
for (img, label) in train_generator:
img, label = self.adjustData(img, label)
yield (img, label)
# return imgs,labels
def saveResult(self, npyfile, size, name,threshold=127):
for i, item in enumerate(npyfile):
img = item
img_std = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
if self.flag_multi_class:
for row in range(len(img)):
for col in range(len(img[row])):
num = np.argmax(img[row][col])
img_std[row][col] = COLOR_DICT[num]
else:
for k in range(len(img)):
for j in range(len(img[k])):
num = img[k][j]
if num < (threshold/255.0):
img_std[k][j] = road
else:
img_std[k][j] = BackGround
img_std = cv2.resize(img_std, size, interpolation=cv2.INTER_CUBIC)
cv2.imwrite(os.path.join(self.save_path, ("%s_predict." + self.img_type) % (name)), img_std)