-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModelTrainAndEval.py
402 lines (289 loc) · 15.7 KB
/
ModelTrainAndEval.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# -*- coding: utf-8 -*-
"""
Created on Thurs Oct 13 2022
@author: Simon Bilik
This class is used for training and evaluation of the selected model
"""
import os
import random
import logging
import traceback
import keras.models
import cv2 as cv
import numpy as np
import tensorflow as tf
random.seed(42)
np.random.seed(42)
tf.random.set_seed(42)
import matplotlib.pyplot as plt
from scipy import stats
from keras import callbacks
from skimage.util import view_as_blocks
from skimage.metrics import structural_similarity as SSIM
from ModelSaved import ModelSaved
AUTOTUNE = tf.data.experimental.AUTOTUNE
class ModelTrainAndEval():
## Set the constants and paths
def __init__(self, modelPath, model, layer, dataGenerator, labelInfo, imageDim, imIndxList, numEpoch, evalFlag, npzSave):
# Set model and training parameters
self.labelInfo = labelInfo
self.numEpoch = numEpoch
# Set image dimensions and plot indices
self.imageDim = imageDim
self.imIndxList = imIndxList
# Set model name and save path
self.layerName = layer
self.modelName = model
self.modelPath = modelPath
self.npzSave = npzSave
# Set data generator
self.dataGenerator = dataGenerator
# Get the model and its type
modelObj = ModelSaved(self.modelName, self.layerName, self.imageDim, dataVariance = 0.5, intermediateDim = 64, latentDim = 32, num_embeddings = 32)
self.model = modelObj.model
self.typeAE = modelObj.typeAE
# Print the separator
logging.info('------------------------------------------------------------------------------------------------')
logging.info("Autoencoder architecture name: " + self.layerName + '-' + self.modelName + '_' + self.labelInfo)
logging.info('')
# Set callbacks, train model and visualise the results
self.setCallbacks()
self.modelTrain()
if evalFlag:
# Encode, decode and visualise the training data
self.dataEncodeDecode()
## Set callbacks
def setCallbacks(self):
try:
# Configure the early stopping callback
if self.typeAE == 'VAE1' or self.typeAE == 'VAE2' or self.typeAE == 'VQVAE1':
self.esCallBack = callbacks.EarlyStopping(
monitor = 'total_loss',
patience = 10)
else:
self.esCallBack = callbacks.EarlyStopping(
monitor = 'loss',
patience = 10)
except:
logging.error('Callback initialization of the ' + self.layerName + '-' + self.modelName + ' model failed...')
traceback.print_exc()
return
else:
logging.info('Callback of the ' + self.layerName + '-' + self.modelName + ' model initialized...')
## Compile and train the model
def modelTrain(self):
try:
# Set the validation DS
if self.typeAE == 'VAE1' or self.typeAE == 'VAE2' or self.typeAE == 'VQVAE1':
valDS = None
else:
valDS = self.dataGenerator.dsValid
# Train the model
self.trainHistory = self.model.fit(
x = self.dataGenerator.dsTrain,
epochs = self.numEpoch,
validation_data = valDS,
verbose = 1,
callbacks = [self.esCallBack])
#self.model.save(self.modelPath, save_format="hdf5", save_traces = True)
#self.model.save(os.path.join(self.modelPath, 'model.keras'))
self.model.save_weights(os.path.join(self.modelPath, 'model.weights.h5'))
except:
logging.error('Training of the ' + self.layerName + '-' + self.modelName + ' model failed...')
traceback.print_exc()
return
else:
logging.info('Training of the ' + self.layerName + '-' + self.modelName + ' model was finished...')
self.visualiseTrainResults()
## Get the encoded and decoded data from selected model and dataset
def dataEncodeDecode(self):
actStrs = ['Train', 'Test', 'Valid']
dataGens = [self.dataGenerator.dsTrain, self.dataGenerator.dsTest, self.dataGenerator.dsValid]
for actStr, dataGen in zip(actStrs, dataGens):
try:
# Get the encoded data
encoder = keras.models.Model(inputs = self.model.input, outputs = self.model.get_layer('enc').output)
if self.typeAE == 'VAE1' or self.typeAE == 'VAE2':
z_mean, z_log_var, _ = encoder.predict(dataGen)
enc_out = np.dstack((z_mean, z_log_var))
elif self.typeAE == 'VQVAE1':
quantizer = self.model.get_layer("vector_quantizer")
encoded_outputs = encoder.predict(dataGen)
flat_enc_outputs = encoded_outputs.reshape(-1, encoded_outputs.shape[-1])
codebook_indices = quantizer.get_code_indices(flat_enc_outputs)
enc_out = codebook_indices.numpy().reshape(encoded_outputs.shape[:-1])
else:
enc_out = encoder.predict(dataGen)
# Get the decoded data
dec_out = self.model.predict(dataGen)
# Save the data for visualisation
self.dataGenerator.processedData[actStr]['Enc'] = enc_out
self.dataGenerator.processedData[actStr]['Dec'] = dec_out
# Save the obtained data to NPZ
if self.npzSave:
outputPath = os.path.join(self.modelPath, 'modelData', 'Eval_' + actStr)
np.savez_compressed(outputPath, encData = enc_out, decData = dec_out)
# Visualise the obtained data
if actStr == 'Test':
self.visualiseEncDecResults(actStr)
# Get the Pearson correlation coeff.
if actStr == 'Test':
self.getSimilarityCoeff(dec_out)
except:
logging.error('Data encode and decode for the model ' + self.layerName + '-' + self.modelName + ' failed...')
traceback.print_exc()
return
else:
logging.info('Data encode and decode for the model ' + self.layerName + '-' + self.modelName + ' was succesful...')
## Get Pearson correlation coefficient and SSIM metric
def getSimilarityCoeff(self, decData):
classIDs = [-1, 1]
classLab = ['NOK', 'OK']
pAvg = []
ssimAvg = []
# Get the original data
tempData = self.dataGenerator.processedData.get('Test')
orgData = tempData.get('Org')
labels = tempData.get('Lab')
for classID, classLb in zip(classIDs, classLab):
# Get IDs and data
idx = np.where(labels == classID)
orgDataSel = orgData[idx[0], :, :, :]
decDataSel = decData[idx[0], :, :, :]
pVal = []
ssimVal = []
# Loop through the test images
for imgOrg, imgDec in zip(orgDataSel, decDataSel):
# Compute the SSIM metric
ssimVal.append(SSIM(imgOrg, imgDec, data_range = 1, channel_axis = 2))
# Convert images to gray
if(imgOrg.shape[2] == 3):
imgOrg = cv.cvtColor(imgOrg, cv.COLOR_BGR2GRAY)
imgDec = cv.cvtColor(imgDec, cv.COLOR_BGR2GRAY)
else:
imgOrg = np.squeeze(imgOrg)
imgDec = np.squeeze(imgDec)
# Split the image into 32x32 images
batchOrg = view_as_blocks(imgOrg, (32, 32))
batchOrg = batchOrg.reshape(batchOrg.shape[0]*batchOrg.shape[1], 32, 32)
batchDec = view_as_blocks(imgDec, (32, 32))
batchDec = batchDec.reshape(batchDec.shape[0]*batchDec.shape[1], 32, 32)
temppVal = []
for subImgOrg, subImgDec in zip(batchOrg, batchDec):
# Compute Pearson Coefficient
pears = stats.pearsonr(subImgOrg.flatten(), subImgDec.flatten())
if not np.isnan(pears.statistic):
temppVal.append(np.abs(pears.statistic))
pVal.append(np.median(np.array(temppVal)))
# Compute median SSIM
ssimVal = np.median(np.array(ssimVal))
ssimAvg.append(ssimVal)
# Compute median p-value
pVal = np.median(np.array(pVal))
pAvg.append(pVal)
logging.info('Median Pearson Coefficient: ' + f'{float(pVal):.2f}' + ' for class ' + classLb)
logging.info('Median SSIM value: ' + f'{float(ssimVal):.2f}' + ' for class ' + classLb)
# Compute the ratio between the Pearson coeffs and SSIM by the OK and NOK data
if pAvg[1] == 0:
pRatio = 0
else:
pRatio = pAvg[0]/pAvg[1]
if ssimAvg[1] == 0:
ssimRatio = 0
else:
ssimRatio = ssimAvg[0]/ssimAvg[1]
logging.info('Pearson Coefficient ratio: ' + f'{float(pRatio):.2f}' + ' for model ' + self.layerName + '-' + self.modelName)
logging.info('SSIM ratio: ' + f'{float(ssimRatio):.2f}' + ' for model ' + self.layerName + '-' + self.modelName)
## Return original, encoded and decoded data with labels
def returnProcessedData(self):
return self.dataGenerator.processedData
## Visualise the results
def visualiseTrainResults(self):
try:
# Plot the history and save the curves
if self.typeAE == 'VAE1' or self.typeAE == 'VAE2':
train_loss = self.trainHistory.history['total_loss']
val_loss = self.trainHistory.history['kl_loss']
plotLabel = 'KL loss [-]'
tempTitle = 'Training and KL Loss of ' + self.layerName + '-' + self.modelName + '_' + self.labelInfo + ' model'
elif self.typeAE == 'VQVAE1':
train_loss = self.trainHistory.history['total_loss']
val_loss = self.trainHistory.history['vqvae_loss']
plotLabel = 'VQ-VAE loss [-]'
tempTitle = 'Training and VQ-VAE Loss of ' + self.layerName + '-' + self.modelName + '_' + self.labelInfo + ' model'
else:
val_loss = self.trainHistory.history['val_loss']
train_loss = self.trainHistory.history['loss']
plotLabel = 'Validation loss [-]'
tempTitle = 'Training and Validation Loss of ' + self.layerName + '-' + self.modelName + '_' + self.labelInfo + ' model'
fig, axarr = plt.subplots(2)
fig.suptitle(tempTitle, fontsize=14)
axarr[0].plot(train_loss)
axarr[0].set(xlabel = 'Number of Epochs', ylabel = 'Training Loss [-]')
axarr[1].plot(val_loss)
axarr[1].set(xlabel = 'Number of Epochs', ylabel = plotLabel)
fig.tight_layout()
fig.subplots_adjust(top=0.88)
fig.savefig(os.path.join(self.modelPath, 'modelData', self.layerName + '-' + self.modelName + '_' + self.labelInfo + '_TrainLosses.png'))
except:
logging.error('Visualisation of the ' + self.modelName + ' model training results failed...')
traceback.print_exc()
else:
logging.info('Visualisation of the ' + self.modelName + ' model training results was finished...')
## Visualise the results
def visualiseEncDecResults(self, actStr):
# TODO: pridat parametr, ktery urci vykreslovane vzorky
try:
# Set the train or test data
if actStr == 'Train':
label = 'during training'
else:
label = 'during testing'
# Get the original, encoded and decoded data
tempData = self.dataGenerator.processedData.get(actStr)
orgData = tempData.get('Org')
encData = tempData.get('Enc')
decData = tempData.get('Dec')
# Compute the difference images (org - dec)
diffData = np.subtract(orgData, decData)
# Define the image source and title lists
imgSourceList = [orgData, encData, decData, diffData]
imgTitleList = ['Original', 'Encoded', 'Decoded', 'Difference']
# Plot the encoded samples from all classes
fig, axarr = plt.subplots(len(self.imIndxList), 4)
tempTitle = 'Visualisations of the ' + self.layerName + '-' + self.modelName + '_' + self.labelInfo + ' model'
fig.suptitle(tempTitle, fontsize=18)
fig.set_size_inches(4 * len(self.imIndxList), 16)
vIdx = 0
# Loop through selected images for plot
for imgIndx in self.imIndxList:
hIdx = 0
# Loop through org, enc, dec and diff data
for imgTitle, imgSource in zip(imgTitleList, imgSourceList):
axarr[vIdx, hIdx].set_title(imgTitle)
# Plot the encoded images
if imgTitle == 'Encoded':
if self.typeAE == 'VAE1' or self.typeAE == 'VAE2':
axarr[vIdx, hIdx].scatter(imgSource[imgIndx, :, 0], imgSource[imgIndx, :, 1], s = 4)
axarr[vIdx, hIdx].set(xlabel = "Mean", ylabel = "Variance", xlim = (-10, 10), ylim = (-10, 10))
elif self.typeAE == 'BAE1' or self.typeAE == 'BAE2':
axarr[vIdx, hIdx].imshow(cv.normalize(imgSource[imgIndx].mean(axis=2), None, 0, 255, cv.NORM_MINMAX, cv.CV_8U))
axarr[vIdx, hIdx].axis('off')
elif self.typeAE == 'VQVAE1':
axarr[vIdx, hIdx].imshow(cv.normalize(imgSource[imgIndx], None, 0, 255, cv.NORM_MINMAX, cv.CV_8U))
axarr[vIdx, hIdx].axis('off')
# Plot the original, decoded and diff images
else:
axarr[vIdx, hIdx].imshow(cv.normalize(imgSource[imgIndx], None, 0, 255, cv.NORM_MINMAX, cv.CV_8U))
axarr[vIdx, hIdx].axis('off')
hIdx += 1
vIdx += 1
# Save the illustration figure
fig.tight_layout()
fig.subplots_adjust(top=0.88)
fig.savefig(os.path.join(self.modelPath, 'modelData', self.layerName + '-' + self.modelName + '_' + self.labelInfo + '_' + actStr + '_AEResults.png'))
except:
logging.error('Data visualisation of the model ' + self.layerName + '-' + self.modelName + '_' + self.labelInfo + ' and its ' + actStr + ' dataset failed...')
traceback.print_exc()
else:
logging.info('Data visualisation of the model ' + self.layerName + '-' + self.modelName + ' and its ' + actStr + ' dataset was succesful...')