-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCustomCallback.py
331 lines (232 loc) · 10.7 KB
/
MyCustomCallback.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from myMetrics import *
from Utils import *
from tqdm import tqdm
from tensorflow.python.keras.callbacks import Callback
from sklearn.metrics import confusion_matrix
from scipy.special import softmax
"""
Calculate mean Intersection Over Union on Validation Set every 2 epoch
and modify the learning rate every batch using poly learning rate formula
"""
class CallbackmIoU(Callback):
def __init__(self, pathTest, lr, pathRGB, pathSEG, pathSoft, pathGraphs, lr_base_on_epochs=True, max_iter=0,
poly_lr=1):
super().__init__()
self.path = pathTest
self.dir_img = pathRGB
self.dir_seg = pathSEG
self.dir_softmax = pathSoft
self.init_lr = lr
self.lr_base_on_epochs = lr_base_on_epochs
self.max_iter = max_iter
self.poly_lr = poly_lr
# parts list names
self.listParts = listPartsNames()
# dict image / class to same at the end
self.img_dict = dictImages()
"""
Poly learning rate formula
"""
def on_train_batch_begin(self, batch, logs=None):
if self.poly_lr:
current_iter = tf.keras.backend.eval(self.model.optimizer.iterations)
if current_iter != 0:
step = self.params['steps']
epoch_sz = self.params['epochs']
if self.lr_base_on_epochs:
max_iter = epoch_sz * step
else:
max_iter = self.max_iter
up = (1 - (current_iter / max_iter)) ** 0.9
new = self.init_lr * up
tf.keras.backend.set_value(self.model.optimizer.lr, new)
if current_iter % step == 0:
print('\nIteration %05d: reducing learning '
'rate to %s.' % (current_iter, new))
else:
tf.keras.backend.set_value(self.model.optimizer.lr, self.init_lr)
print('\nIteration %05d: reducing learning '
'rate to %s.' % (current_iter, self.init_lr))
def on_epoch_begin(self, epoch, logs=None):
if True :
# if epoch % 2 == 0 and epoch != 0:
print("Begin-Callback")
images = glob.glob(self.dir_img + "*.png")
images.sort()
softmaxL = glob.glob(self.dir_softmax + "*.npy")
softmaxL.sort()
segs = glob.glob(self.dir_seg + "*.png")
segs.sort()
mat = np.zeros(shape=(108, 108), dtype=np.int32)
for k in tqdm(range(len(images))):
img = cv2.imread(images[k])
soft = np.load(softmaxL[k])
seg = cv2.imread(segs[k], cv2.IMREAD_GRAYSCALE)
mask = seg != 255
seg = seg[mask]
scale = img / 127.5 - 1
res = self.model.predict([np.expand_dims(scale, 0), np.expand_dims(soft, 0)])
labels = np.argmax(res.squeeze(), -1)
labels = labels.astype(np.uint8)
labels = labels[mask]
# if (images[k])[-15:] in self.img_dict:
#
# h_z, w_z = labels.shape
# imgNew = np.zeros((h_z, w_z, 3), np.uint8)
# for i in range(0, 21):
# mask = cv2.inRange(labels, i, i)
# v = self.mapLabel[i]
# imgNew[mask > 0] = v
#
# image = make_image(imgNew)
# summary = tf.Summary(
# value=[tf.Summary.Value(tag=self.img_dict.get((images[k])[-15:]), image=image)])
# writer = tf.summary.FileWriter(self.path + 'logs')
# writer.add_summary(summary, epoch)
# writer.close()
tmp = confusion_matrix(seg, labels, range(108))
mat = mat + tmp
iou_108, IoU_per_class_108 = compute_and_print_IoU_per_class(confusion_matrix=mat, num_classes=108,
namePart=self.listParts)
print('The mIoU for epoch {} is {:7.2f}.'.format(epoch, iou_108))
def on_train_end(self, logs=None):
print("Begin-Callback-end-train")
pathCMap = 'Y:/tesisti/rossi/cmap255.mat'
fileMat = loadmat(pathCMap)
cmap = fileMat['cmap']
images = glob.glob(self.dir_img + "*.png")
images.sort()
softmaxL = glob.glob(self.dir_softmax + "*.npy")
softmaxL.sort()
segs = glob.glob(self.dir_seg + "*.png")
segs.sort()
mat = np.zeros(shape=(108, 108), dtype=np.int32)
for k in tqdm(range(len(images))):
img = cv2.imread(images[k])
soft = np.load(softmaxL[k])
seg = cv2.imread(segs[k], cv2.IMREAD_GRAYSCALE)
mask = seg != 255
seg = seg[mask]
scale = img / 127.5 - 1
res = self.model.predict([np.expand_dims(scale, 0), np.expand_dims(soft, 0)])
labels = np.argmax(res.squeeze(), -1)
labels = labels.astype(np.uint8)
labels_mask = labels[mask]
if (images[k])[-15:] in self.img_dict:
h_z, w_z = labels.shape
imgNew = np.zeros((h_z, w_z, 3), np.uint8)
for i in range(0, 108):
mask = cv2.inRange(labels, i, i)
v = cmap[i]
imgNew[mask > 0] = v
pathTmp = self.path + (images[k])[-15:]
cv2.imwrite(pathTmp, imgNew)
# colorSeg = np.zeros((h_z, w_z, 3), np.uint8)
# for i in range(0, 108):
# mask = cv2.inRange(seg, i, i)
# v = cmap[i]
# colorSeg[mask > 0] = v
# pathTmp = self.path + "GT_" + (images[k])[-15:]
# cv2.imwrite(pathTmp, colorSeg)
tmp = confusion_matrix(seg, labels_mask, range(108))
mat = mat + tmp
iou_108, IoU_per_class_108 = compute_and_print_IoU_per_class(confusion_matrix=mat, num_classes=108,
namePart=self.listParts)
print('The mIoU on train end is {:7.2f}.'.format(iou_108))
create_excel_file(results21=None, results108=IoU_per_class_108, path=self.path)
print("End at" + datetime.now().strftime("%Y%m%d-%H%M%S"))
class CallbackmIoU_baseline(Callback):
def __init__(self, pathTest, lr, pathRGB, pathSEG, lr_base_on_epochs=True, max_iter=50000):
super().__init__()
self.path = pathTest
self.dir_img = pathRGB
self.dir_seg = pathSEG
self.init_lr = lr
self.lr_base_on_epochs = lr_base_on_epochs
self.max_iter = max_iter
# parts list names
self.listParts = listPartsNames()
# dict image / class to same at the end
self.img_dict = dictImages()
"""
Poly learning rate formula
"""
def on_train_batch_begin(self, batch, logs=None):
current_iter = tf.keras.backend.eval(self.model.optimizer.iterations)
if current_iter != 0:
step = self.params['steps']
epoch_sz = self.params['epochs']
if self.lr_base_on_epochs:
max_iter = epoch_sz * step
else:
max_iter = self.max_iter
up = (1 - (current_iter / max_iter)) ** 0.9
new = self.init_lr * up
tf.keras.backend.set_value(self.model.optimizer.lr, new)
if current_iter % step == 0:
print('\nIteration %05d: reducing learning '
'rate to %s.' % (current_iter, new))
else:
tf.keras.backend.set_value(self.model.optimizer.lr, self.init_lr)
print('\nIteration %05d: reducing learning '
'rate to %s.' % (current_iter, self.init_lr))
def on_epoch_begin(self, epoch, logs=None):
# if True:
if epoch % 2 == 0 and epoch != 0:
print("Begin-Callback")
images = glob.glob(self.dir_img + "*.png")
images.sort()
segs = glob.glob(self.dir_seg + "*.png")
segs.sort()
mat = np.zeros(shape=(108, 108), dtype=np.int32)
for k in tqdm(range(len(images))):
img = cv2.imread(images[k])
seg = cv2.imread(segs[k], cv2.IMREAD_GRAYSCALE)
mask = seg != 255
seg = seg[mask]
scale = img / 127.5 - 1
res = self.model.predict(np.expand_dims(scale, 0))
labels = np.argmax(res.squeeze(), -1)
labels = labels.astype(np.uint8)
labels = labels[mask]
tmp = confusion_matrix(seg, labels, range(108))
mat = mat + tmp
iou_108, IoU_per_class_108 = compute_and_print_IoU_per_class(confusion_matrix=mat, num_classes=108,
namePart=self.listParts)
print('The mIoU for epoch {} is {:7.2f}.'.format(epoch, iou_108))
def on_train_end(self, logs=None):
print("Begin-Callback-end-train")
pathCMap = 'Y:/tesisti/rossi/cmap255.mat'
fileMat = loadmat(pathCMap)
cmap = fileMat['cmap']
images = glob.glob(self.dir_img + "*.png")
images.sort()
segs = glob.glob(self.dir_seg + "*.png")
segs.sort()
mat = np.zeros(shape=(108, 108), dtype=np.int32)
for k in tqdm(range(len(images))):
img = cv2.imread(images[k])
seg = cv2.imread(segs[k], cv2.IMREAD_GRAYSCALE)
mask = seg != 255
seg = seg[mask]
scale = img / 127.5 - 1
res = self.model.predict(np.expand_dims(scale, 0))
res = softmax(res)
labels = np.argmax(res.squeeze(), -1)
labels = labels.astype(np.uint8)
labels_mask = labels[mask]
if (images[k])[-15:] in self.img_dict:
h_z, w_z = labels.shape
imgNew = np.zeros((h_z, w_z, 3), np.uint8)
for i in range(0, 108):
mask = cv2.inRange(labels, i, i)
v = cmap[i]
imgNew[mask > 0] = v
pathTmp = self.path + (images[k])[-15:]
cv2.imwrite(pathTmp, imgNew)
tmp = confusion_matrix(seg, labels_mask, range(108))
mat = mat + tmp
iou_108, IoU_per_class_108 = compute_and_print_IoU_per_class(confusion_matrix=mat, num_classes=108,
namePart=self.listParts)
print('The mIoU on train end is {:7.2f}.'.format(iou_108))
create_excel_file(results21=None, results108=IoU_per_class_108, path=self.path)