File size: 22,804 Bytes
ed96dd1 d729bee |
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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
class ImageMulticlassClassification:
def __init__(self, imgWidth=300, imgHeight=300, batchSize=32):
from time import time
import tensorflow as tf
import matplotlib.pyplot as plt
import pathlib
import datetime
from sklearn.metrics import roc_curve, auc, roc_auc_score
import os
import keras
import numpy as np
import pandas as pd
import tarfile
import sklearn
self.time = time
self.sklearn = sklearn
self.tf = tf
self.plt = plt
self.pathlib = pathlib
self.datetime = datetime
self.roc_curve = roc_curve
self.roc_auc_score = roc_auc_score
self.auc = auc
self.os = os
self.keras = keras
self.np = np
self.AUTOTUNE = tf.data.AUTOTUNE
self.pd = pd
self.tarfile = tarfile
self.imgWidth = imgWidth
self.imgHeight = imgHeight
self.numGPU = len(self.tf.config.list_physical_devices('GPU'))
if self.numGPU > 0:
self.batchSize = batchSize * self.numGPU
else:
self.batchSize = batchSize
self.Model = None
self.time_callback = None
self.history = None
self.confusionMatrix = None
self.validation_label = None
self.trainDataset = None
self.validationDataset = None
self.accuracy = None
self.recall = None
self.precision = None
self.f1Score = None
self.modelName = ""
def data_MakeDataset(self, datasetUrl=None, datasetPath=None, datasetDirectoryName="Dataset Covid19 Training", ratioValidation=0.2):
"""
Purpose:
- Make dataset from parameter
Parameter:
- datasetUrl: url of dataset
- type: string
- example: "https://storage.googleapis.com/fdataset/Dataset%20Covid19%20Training.tgz"
- datasetPath: path of dataset
- type: string
- example: "C:/Users/User/Desktop/Dataset Covid19 Training.tgz"
- datasetDirectoryName: name of dataset directory
- type: string
- example: "Dataset Covid19 Training"
- ratioValidation: ratio of validation data
- type: float
- example: 0.2
Return:
- {"success":True, "code":200, "detail":"success"}
"""
try:
if datasetUrl is not None:
dataset_url = datasetUrl
data_dir = self.tf.keras.utils.get_file(datasetDirectoryName, origin=dataset_url, untar=True)
data_dir = self.pathlib.Path(data_dir)
elif datasetPath is not None:
currentPath = self.os.getcwd()
if self.os.path.exists(currentPath + "/" + datasetDirectoryName):
# remove dataset directory with all file inside
self.os.system("rm -rf " + currentPath + "/" + datasetDirectoryName)
# extract dataset
my_tar = self.tarfile.open(datasetPath)
# check if dataset directory exist then delete it
my_tar.extractall(currentPath) # specify which folder to extract to
my_tar.close()
data_dir = self.pathlib.Path(f'{currentPath}/{datasetDirectoryName}/')
image_count = len(list(data_dir.glob('*/*.jpg')))
train_ds = self.tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
seed=123,
subset="training",
validation_split=ratioValidation,
image_size=(self.imgWidth, self.imgHeight),
batch_size=self.batchSize)
val_ds = self.tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
seed=123,
subset="validation",
validation_split=ratioValidation,
image_size=(self.imgWidth, self.imgHeight),
batch_size=self.batchSize)
self.trainDataset = train_ds.cache().shuffle(1000).prefetch(buffer_size=self.AUTOTUNE)
self.validationDataset = val_ds.cache().prefetch(buffer_size=self.AUTOTUNE)
return {"success":True, "code":200, "detail":"success"}
except Exception as e:
return {"success":False, "code":500, "detail":str(e)}
def data_PreprocessingDataset(self, typeRandomFlip="horizontal_and_vertical", RandomRotation=0.3, RandomZoom=0.2, shuffleTrainDataset=True, augmentTrainDataset=True):
"""
Purpose:
- Preprocessing dataset
Parameter:
- typeRandomFlip: type of random flip
- type: string
- example: "horizontal_and_vertical"
- options: "horizontal", "vertical", "horizontal_and_vertical"
- RandomRotation: random rotation
- type: float
- example: 0.3
- RandomZoom: random zoom
- type: float
- example: 0.2
- shuffleTrainDataset: shuffle train dataset
- type: bool
- example: True
- augmentTrainDataset: augment train dataset
- type: bool
- example: True
Return:
- {"success":True, "code":200, "detail":"success"}
"""
try:
rescale = self.tf.keras.layers.Rescaling(1.0 / 255, input_shape=(self.imgWidth, self.imgHeight, 3))
data_augmentation = self.tf.keras.Sequential(
[
self.tf.keras.layers.RandomFlip(typeRandomFlip, input_shape=(self.imgWidth,self.imgHeight,3)),
self.tf.keras.layers.RandomRotation(RandomRotation),
self.tf.keras.layers.RandomZoom(RandomZoom),
]
)
def prepare(ds, shuffle=False, augment=False):
# Rescale dataset
ds = ds.map(lambda x, y: (rescale(x), y), num_parallel_calls=self.AUTOTUNE)
if shuffle:
ds = ds.shuffle(1024)
# Use data augmentation only on the training set
if augment:
ds = ds.map(lambda x, y: (data_augmentation(x), y), num_parallel_calls=self.AUTOTUNE,)
# Use buffered prefecting
return ds.prefetch(buffer_size=self.AUTOTUNE)
self.trainDataset = prepare(self.trainDataset, shuffle=shuffleTrainDataset, augment=augmentTrainDataset)
self.validationDataset = prepare(self.validationDataset)
return {"success":True, "code":200, "detail":"success"}
except Exception as e:
return {"success":False, "code":500, "detail":str(e)}
def data_GetLabelFromDataset(self, dataset):
"""
Purpose:
- Get label from dataset
Parameter:
- dataset: dataset
- type: tf.data.Dataset
- example: trainDataset
Return:
- {"success":True, "code":200, "detail":"success", "label":array([0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1,
1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0], dtype=int32)}
"""
try:
label = self.np.concatenate([y for x, y in dataset], axis=0)
return {"success":True, "code":200, "detail":"success", "label":label}
except Exception as e:
return {"success":False, "code":500, "detail":str(e)}
def model_make(self, model=None):
"""
Purpose:
- Make default model
Parameter:
- model: model
- type: tf.keras.Model
- example: model
- default: None
Return:
- {"success":True, "code":200, "detail":"success", "model":model}
"""
try:
if model is None:
model = self.tf.keras.Sequential()
base_model = self.tf.keras.applications.DenseNet121(include_top=False, input_shape=(self.imgWidth, self.imgHeight, 3))
base_model.trainable=True
model.add(base_model)
model.add(self.tf.keras.layers.Dropout(0.4))
model.add(self.tf.keras.layers.Flatten())
model.add(self.tf.keras.layers.Dense(128,activation='relu'))
model.add(self.tf.keras.layers.Dropout(0.5))
model.add(self.tf.keras.layers.Dense(32,activation='relu'))
model.add(self.tf.keras.layers.Dense(1, activation="sigmoid"))
self.Model = model
else:
self.Model = model
return {"success":True, "code":200, "detail":"success", "model":self.Model}
except Exception as e:
return {"success":False, "code":500, "detail":str(e)}
def training_model(self, epochs=10, lossFunction="binary_crossentropy", optimizer="adam", metrics=["accuracy"], device='/GPU:0', modelName=None):
"""
Purpose:
- Training model
Parameter:
- model: model
- type: tf.keras.Model
- example: model
- default: True
- epochs: epochs
- type: int
- example: 10
- lossFunction: loss function
- type: string
- example: "binary_crossentropy"
- options: "binary_crossentropy", "categorical_crossentropy", "sparse_categorical_crossentropy"
- optimizer: optimizer
- type: string
- example: "adam"
- options: "adam", "adamax", "nadam", "rmsprop", "sgd", tf.keras.optimizers.RMSprop(learning_rate=1e-4)
- metrics: metrics
- type: list
- example: ["accuracy"]
- device: device
- type: string
- example: "/GPU:0"
- options: "/CPU:0", "/GPU:0"
- modelName: model name
- type: string
- example: "model"
Return:
- {"success":True, "code":200, "detail":"success"}
"""
try:
if modelName is not None:
self.modelName = modelName
self.time_callback = TimeHistory()
self.Model.compile(
loss=lossFunction,
optimizer=optimizer,
metrics=metrics,
)
print(self.Model.summary())
with self.tf.device(device):
self.history = self.Model.fit(
self.trainDataset, validation_data=self.validationDataset, epochs=epochs, verbose=1, callbacks=[self.time_callback]
)
# make excel file report.xlsx and save data in column 1 is number of training loss, column 2 is training accuracy, column 3 is validation loss, column 4 is validation accuracy, column 5 is training time
dataFrameHistory = self.pd.DataFrame({"training_loss":self.history.history["loss"], "training_accuracy":self.history.history["accuracy"], "validation_loss":self.history.history["val_loss"], "validation_accuracy":self.history.history["val_accuracy"], "training_time":self.time_callback.times})
dataFrameHistory.to_excel(f"report_{self.modelName}.xlsx")
return {"success":True, "code":200, "detail":"success"}
except Exception as e:
return {"success":False, "code":500, "detail":str(e)}
def training_model_multiGPU(self, epochs=10, lossFunction="binary_crossentropy", optimizer="adam", metrics=["accuracy"], device='/GPU:0', modelName=None):
"""
Purpose:
- Training model with multi GPU support, with mirrored strategy
Parameter:
- model: model
- type: tf.keras.Model
- example: model
- default: True
- epochs: epochs
- type: int
- example: 10
- lossFunction: loss function
- type: string
- example: "binary_crossentropy"
- options: "binary_crossentropy", "categorical_crossentropy", "sparse_categorical_crossentropy"
- optimizer: optimizer
- type: string
- example: "adam"
- options: "adam", "adamax", "nadam", "rmsprop", "sgd", tf.keras.optimizers.RMSprop(learning_rate=1e-4)
- metrics: metrics
- type: list
- example: ["accuracy"]
- device: device
- type: string
- example: "/GPU:0"
- options: "/CPU:0", "/GPU:0"
Return:
- {"success":True, "code":200, "detail":"success"}
"""
try:
if modelName is not None:
self.modelName = modelName
self.time_callback = TimeHistory()
print(self.Model.summary())
strategy = self.tf.distribute.MirroredStrategy()
with strategy.scope():
model = self.Model
model.compile(loss=lossFunction, optimizer=optimizer, metrics=metrics)
self.history = model.fit(self.trainDataset, validation_data=self.validationDataset, epochs=epochs, verbose=1, callbacks=[self.time_callback])
# make excel file report.xlsx and save data in column 1 is number of training loss, column 2 is training accuracy, column 3 is validation loss, column 4 is validation accuracy, column 5 is training time
dataFrameHistory = self.pd.DataFrame({"training_loss":self.history.history["loss"], "training_accuracy":self.history.history["accuracy"], "validation_loss":self.history.history["val_loss"], "validation_accuracy":self.history.history["val_accuracy"], "training_time":self.time_callback.times})
dataFrameHistory.to_excel(f"report_{self.modelName}.xlsx")
return {"success":True, "code":200, "detail":"success"}
except Exception as e:
return {"success":False, "code":500, "detail":str(e)}
def evaluation(self, labelName=["COVID19", "NORMAL"]):
"""
Purpose:
- Evaluation model with confusionMatrix, precision, recall, f1Score, accuracy
Parameter:
- labelName: label name
- type: list
- example: ["COVID19", "NORMAL"]
Return:
- {"success":True, "code":200, "detail":"success", "confusionMatrix":confusionMatrix, "precision":precision, "recall":recall, "f1Score":f1Score, "accuracy":accuracy}
"""
try:
self.Model.evaluate(self.validationDataset)
prediction_result = self.Model.predict(self.validationDataset)
prediction_result = self.np.argmax(prediction_result, axis=1)
self.validation_label = self.np.concatenate([y for x, y in self.validationDataset], axis=0)
self.confusionMatrix = self.tf.math.confusion_matrix(labels=self.validation_label, predictions=prediction_result).numpy()
self.accuracy = self.sklearn.metrics.accuracy_score(self.validation_label, prediction_result)
self.precision = self.sklearn.metrics.precision_score(self.validation_label, prediction_result, average="macro", zero_division=0)
self.recall = self.sklearn.metrics.recall_score(self.validation_label, prediction_result, average="macro")
self.f1Score = self.sklearn.metrics.f1_score(self.validation_label, prediction_result, average="macro")
self.__drawConfusionMatrix(labelName)
self.__drawROC()
dataFrameScore = self.pd.DataFrame({"accuracy":[self.accuracy], "recall":[self.recall], "precision":[self.precision], "f1Score":[self.f1Score]})
dataFrameScore.to_excel(f"reportScore_{self.modelName}.xlsx")
self.__drawHistoryAccuracy()
self.__drawHistoryLoss()
return {"success":True, "code":200, "detail":"success"}
except Exception as e:
return {"success":False, "code":500, "detail":str(e)}
def __drawConfusionMatrix(self, labelName=["COVID19", "NORMAL"]):
# draw confusion matrix with numeric value on the center and library matplotlib with label of validation dataset like this sample https://scikit-learn.org/stable/_images/sklearn-metrics-plot_confusion_matrix-1.png
labelName.sort()
fig, ax = self.plt.subplots()
im = ax.imshow(self.confusionMatrix)
ax.figure.colorbar(im, ax=ax)
ax.set(xticks=self.np.arange(self.confusionMatrix.shape[1]), yticks=self.np.arange(self.confusionMatrix.shape[0]), xticklabels=labelName, yticklabels=labelName, title="Confusion Matrix", ylabel="True label", xlabel="Predicted label")
ax.set_xlabel("Predicted")
ax.set_ylabel("True")
self.plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
for i in range(self.confusionMatrix.shape[0]):
for j in range(self.confusionMatrix.shape[1]):
ax.text(j, i, self.confusionMatrix[i, j], ha="center", va="center", color="w")
self.plt.tight_layout()
self.plt.savefig(f"confusionMatrix_{self.modelName}.png")
self.plt.show()
self.plt.close()
# save confusion matrix to excel file
dataFrameConfusionMatrix = self.pd.DataFrame(self.confusionMatrix)
dataFrameConfusionMatrix.to_excel(f"confusionMatrix_{self.modelName}.xlsx")
def __drawROC(self):
"""
Purpose:
- Draw ROC curve like this sample https://scikit-learn.org/stable/_images/sphx_glr_plot_roc_001.png for multi class
"""
predictResult = self.Model.predict(self.validationDataset)
fpr, tpr, thresholds = self.sklearn.metrics.roc_curve(self.validation_label, predictResult[:, 1], pos_label=1)
self.auc = self.sklearn.metrics.auc(fpr, tpr)
fig, ax = self.plt.subplots()
ax.plot(fpr, tpr, label="ROC curve (area = %0.2f)" % self.auc)
ax.plot([0, 1], [0, 1], "k--")
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel("False Positive Rate")
ax.set_ylabel("True Positive Rate")
ax.set_title("Receiver operating characteristic")
ax.legend(loc="best")
self.plt.savefig(f"ROC_{self.modelName}.png")
self.plt.show()
self.plt.close()
# save ROC curve to excel file
dataFrameROC = self.pd.DataFrame({"fpr":fpr, "tpr":tpr, "thresholds":thresholds, "auc":self.auc})
dataFrameROC.to_excel(f"ROC_{self.modelName}.xlsx")
def __drawHistoryAccuracy(self):
"""
Purpose:
- Draw history accuracy with training and validation dataset
"""
fig, ax = self.plt.subplots()
ax.plot(self.history.history["accuracy"], label="training dataset")
ax.plot(self.history.history["val_accuracy"], label="validation dataset")
ax.set_xlabel("Epoch")
ax.set_ylabel("Accuracy")
ax.set_title("Accuracy")
ax.legend(loc="best")
self.plt.savefig(f"historyAccuracy_{self.modelName}.png")
self.plt.show()
self.plt.close()
def __drawHistoryLoss(self):
"""
Purpose:
- Draw history loss with training and validation dataset
"""
fig, ax = self.plt.subplots()
ax.plot(self.history.history["loss"], label="training dataset")
ax.plot(self.history.history["val_loss"], label="validation dataset")
ax.set_xlabel("Epoch")
ax.set_ylabel("Loss")
ax.set_title("Loss")
ax.legend(loc="best")
self.plt.savefig(f"historyLoss_{self.modelName}.png")
self.plt.show()
self.plt.close()
def import_data_Dataset(self, trainDataset, validationDataset):
"""
Purpose:
- Import dataset
Parameter:
- trainDataset: dataset
- type: tf.data.Dataset
- example: trainDataset
- validationDataset: dataset
- type: tf.data.Dataset
- example: validationDataset
Return:
- {"success":True, "code":200, "detail":"success"}
"""
try:
self.trainDataset = trainDataset
self.validationDataset = validationDataset
return {"success":True, "code":200, "detail":"success"}
except Exception as e:
return {"success":False, "code":500, "detail":str(e)}
def saveModelWithWeight(self, fileName):
"""
Purpose:
- Save model with weight
Parameter:
- fileName: file name
- type: string
- example: "my_model"
- options: "my_model", "gs://bucket/my_model"
Return:
- {"success":True, "code":200, "detail":"success"}
"""
try:
self.Model.save(fileName)
return {"success":True, "code":200, "detail":"success"}
except Exception as e:
return {"success":False, "code":500, "detail":str(e)}
def loadModelWithWeightAndCustomObject(self, fileName, customObject):
"""
Purpose:
- Load model with weight and custom object
Parameter:
- fileName: file name
- type: string
- example: "my_model"
- options: "my_model", "gs://bucket/my_model"
- customObject: custom object
- type: dict
- example: {"MyCustomObject":MyCustomObject}
Return:
- {"success":True, "code":200, "detail":"success"}
"""
try:
self.Model = self.tf.keras.models.load_model(fileName, custom_objects=customObject)
return {"success":True, "code":200, "detail":"success"}
except Exception as e:
return {"success":False, "code":500, "detail":str(e)}
import tensorflow as tf
from time import time
class TimeHistory(tf.keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.times = []
def on_epoch_begin(self, batch, logs={}):
self.epoch_time_start = time()
def on_epoch_end(self, batch, logs={}):
self.times.append(time() - self.epoch_time_start) |