Commit 2f264f7c authored by Dennis Willers's avatar Dennis Willers 🏀

Recall eingebunden.

parent 9e9d1038
...@@ -53,3 +53,5 @@ Thumbs.db ...@@ -53,3 +53,5 @@ Thumbs.db
assets/Bilder/* assets/Bilder/*
assets/Bilder/AktuelleTrainingsUndTestdaten/* assets/Bilder/AktuelleTrainingsUndTestdaten/*
assets/Bilder/Datengrundlage/*
assets/Bilder/Datengrundlage/
...@@ -21,10 +21,12 @@ def run_model(): ...@@ -21,10 +21,12 @@ def run_model():
with open("ressources/config/config.yaml", "r") as file: with open("ressources/config/config.yaml", "r") as file:
config = yaml.load(file, Loader=yaml.FullLoader) config = yaml.load(file, Loader=yaml.FullLoader)
# Initialisieren der KNN-Modellbau Eigenschaften # Initialisieren der KNN-Modellbau Eigenschaften
config_knn = ConfigKNN(excluded_folder=Markt.MARKT_B, config_knn = ConfigKNN(
activation_function_1_units=Aktivierungsfunktion.Sigmoid, excluded_folder=Markt.Kein_Markt,
activation_function_1_units=Aktivierungsfunktion.sigmoid,
activation_function_128_units=Aktivierungsfunktion.ReLU, activation_function_128_units=Aktivierungsfunktion.ReLU,
optimization_method=Optimierungsverfahren.Adam) optimization_method=Optimierungsverfahren.SGD
)
print("Initialisiere Callback") print("Initialisiere Callback")
# Erstellen Sie eine Instanz des benutzerdefinierten Callbacks # Erstellen Sie eine Instanz des benutzerdefinierten Callbacks
......
bilder: bilder:
original_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/assets/Bilder/Datengrundlage-Reduziert-Test/" original_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/assets/Bilder/Datengrundlage-Reduziert-Test/"
# original_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/assets/Bilder/Datengrundlage/"
knn_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/assets/Bilder/AktuelleTrainingsUndTestdaten/" knn_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/assets/Bilder/AktuelleTrainingsUndTestdaten/"
knn: knn:
epochs: 2 epochs: 6
result: result:
plot_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/ressources/results/plot/" plot_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/ressources/results/plot/"
excel_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/ressources/results/excel/" excel_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/ressources/results/excel/"
...@@ -45,20 +45,21 @@ def copy_images_with_exclusion(src_dir, dest_dir, exclude_dir=None): ...@@ -45,20 +45,21 @@ def copy_images_with_exclusion(src_dir, dest_dir, exclude_dir=None):
os.remove(file_or_dir_path) os.remove(file_or_dir_path)
# Erstelle die gewünschte Ordnerstruktur im Zielverzeichnis # Erstelle die gewünschte Ordnerstruktur im Zielverzeichnis
os.makedirs(os.path.join(dest_dir, 'OOS')) os.makedirs(os.path.join(dest_dir, '0_OOS'))
os.makedirs(os.path.join(dest_dir, '!OOS')) os.makedirs(os.path.join(dest_dir, '1_!OOS'))
# Durchlaufe die Unterordner im Quellverzeichnis # Durchlaufe die Unterordner im Quellverzeichnis
for subdir in os.listdir(src_dir): for subdir in os.listdir(src_dir):
# if subdir == exclude_dir: # if subdir == exclude_dir:
subdirString = bytes(subdir, 'utf-8').decode('unicode_escape') subdirString = bytes(subdir, 'utf-8').decode('unicode_escape')
exclude_dirString = bytes(exclude_dir, 'utf-8').decode('unicode_escape') exclude_dirString = bytes(exclude_dir.name, 'utf-8').decode('unicode_escape')
if subdirString == exclude_dirString: if subdirString == exclude_dirString:
continue continue
for folder in os.listdir(os.path.join(src_dir, subdir)): for folder in os.listdir(os.path.join(src_dir, subdir)):
if folder == 'OOS': if folder == 'OOS':
for img in os.listdir(os.path.join(src_dir, subdir, folder)): for img in os.listdir(os.path.join(src_dir, subdir, folder)):
shutil.copy(os.path.join(src_dir, subdir, folder, img), os.path.join(dest_dir, 'OOS')) shutil.copy(os.path.join(src_dir, subdir, folder, img), os.path.join(dest_dir, '0_OOS'))
elif folder == '!OOS': elif folder == '!OOS':
for img in os.listdir(os.path.join(src_dir, subdir, folder)): for img in os.listdir(os.path.join(src_dir, subdir, folder)):
shutil.copy(os.path.join(src_dir, subdir, folder, img), os.path.join(dest_dir, '!OOS')) shutil.copy(os.path.join(src_dir, subdir, folder, img), os.path.join(dest_dir, '1_!OOS'))
...@@ -19,7 +19,7 @@ def define_augmentation_rules(): ...@@ -19,7 +19,7 @@ def define_augmentation_rules():
def get_train_data(datagen, directory): def get_train_data(datagen, directory):
train_it = datagen.flow_from_directory( train_it = datagen.flow_from_directory(
directory=directory, directory=directory,
class_mode='binary', class_mode='categorical',
batch_size=64, batch_size=64,
target_size=(224, 224), target_size=(224, 224),
subset='training') subset='training')
...@@ -29,7 +29,7 @@ def get_train_data(datagen, directory): ...@@ -29,7 +29,7 @@ def get_train_data(datagen, directory):
def get_test_data(datagen, directory): def get_test_data(datagen, directory):
test_it = datagen.flow_from_directory( test_it = datagen.flow_from_directory(
directory=directory, directory=directory,
class_mode='binary', class_mode='categorical',
batch_size=64, batch_size=64,
target_size=(224, 224), target_size=(224, 224),
subset='validation') subset='validation')
......
...@@ -3,4 +3,4 @@ from enum import Enum ...@@ -3,4 +3,4 @@ from enum import Enum
class Aktivierungsfunktion(Enum): class Aktivierungsfunktion(Enum):
ReLU = 0, ReLU = 0,
Sigmoid = 1, sigmoid = 1,
...@@ -2,9 +2,10 @@ from enum import Enum ...@@ -2,9 +2,10 @@ from enum import Enum
class Markt(Enum): class Markt(Enum):
MARKT_A = 'Markt_A', Markt_A = 0,
MARKT_B = 'Markt_B', Markt_B = 1,
MARKT_C = 'Markt_C', Markt_C = 2,
MARKT_D = 'Markt_D', Markt_D = 3,
MARKT_E = 'Markt_E', Markt_E = 4,
MARKT_F = 'Markt_F' Markt_F = 5
Kein_Markt = -1
...@@ -12,5 +12,11 @@ def fit_model(config, model, train_it, test_it, callback): ...@@ -12,5 +12,11 @@ def fit_model(config, model, train_it, test_it, callback):
def evaluate_model(model, test_it): def evaluate_model(model, test_it):
_, acc = model.evaluate(test_it, steps=len(test_it), verbose=1) all_metrics = model.evaluate(test_it, steps=len(test_it), verbose=1)
print('> %.3f' % (acc * 100.0)) _, metrics, recall_0, recall_1 = all_metrics
\ No newline at end of file print(all_metrics)
print(_)
print(metrics)
print(recall_0)
print(recall_1)
print('> %.3f' % (metrics * 100.0))
...@@ -14,26 +14,31 @@ def define_model(config_knn): ...@@ -14,26 +14,31 @@ def define_model(config_knn):
# Die Fully-Connected-Schichten werden definiert # Die Fully-Connected-Schichten werden definiert
flat = tf.keras.layers.Flatten()(model.layers[-1].output) flat = tf.keras.layers.Flatten()(model.layers[-1].output)
output_layer_1 = get_next_layer(128, config_knn.activation_function_128_units, flat) output_layer_1 = get_next_layer(128, config_knn.activation_function_128_units, flat)
output_layer_2 = get_next_layer(1, config_knn.activation_function_1_units, output_layer_1) output_layer_2 = get_next_layer(2, config_knn.activation_function_1_units, output_layer_1)
# Die Schichten des Modells werden definiert # Die Schichten des Modells werden definiert
model = tf.keras.models.Model(inputs=model.inputs, outputs=output_layer_2) model = tf.keras.models.Model(inputs=model.inputs, outputs=output_layer_2)
# Das Optimierungsverfahren wird definiert # Das Optimierungsverfahren wird definiert
opt = get_optimization_method(config_knn.optimization_method) opt = get_optimization_method(config_knn.optimization_method)
# Die Klassifizierungsmetriken und die Loss-Funktion werden definiert # Die Klassifizierungsmetriken und die Loss-Funktion werden definiert
recall_not_oos = tf.keras.metrics.Recall(name='recall_not_oos', class_id=0) loss_fn = tf.keras.losses.BinaryCrossentropy()
recall_oos = tf.keras.metrics.Recall(name='recall_oos', class_id=1) binary_accuracy = tf.keras.metrics.BinaryAccuracy()
model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy', recall_not_oos, recall_oos]) recall_0 = tf.keras.metrics.Recall(name='recall_0', class_id=0)
recall_1 = tf.keras.metrics.Recall(name='recall_1', class_id=1)
# Füge die Metriken dem Modell hinzu
model.compile(optimizer=opt, loss=loss_fn, metrics=[binary_accuracy, recall_0, recall_1])
return model return model
def get_next_layer(units, activation_function, flat_or_dense): def get_next_layer(units, activation_function, flat_or_dense):
if activation_function == Aktivierungsfunktion.ReLU: if activation_function == Aktivierungsfunktion.ReLU:
return tf.keras.layers.Dense(units, return tf.keras.layers.Dense(units,
activation=activation_function, activation=activation_function.name,
kernel_initializer='he_uniform')(flat_or_dense) kernel_initializer='he_uniform')(flat_or_dense)
if activation_function == Aktivierungsfunktion.Sigmoid: if activation_function == Aktivierungsfunktion.sigmoid:
return tf.keras.layers.Dense(units, activation='sigmoid')(flat_or_dense) return tf.keras.layers.Dense(units, activation=activation_function.name)(flat_or_dense)
return None return None
...@@ -43,3 +48,4 @@ def get_optimization_method(optimization_method): ...@@ -43,3 +48,4 @@ def get_optimization_method(optimization_method):
if optimization_method == Optimierungsverfahren.Adam: if optimization_method == Optimierungsverfahren.Adam:
return tf.keras.optimizers.Adam(learning_rate=0.001) return tf.keras.optimizers.Adam(learning_rate=0.001)
return None return None
...@@ -7,7 +7,19 @@ def create_excel_result(callback, config): ...@@ -7,7 +7,19 @@ def create_excel_result(callback, config):
workbook = openpyxl.Workbook() workbook = openpyxl.Workbook()
# Fügen Sie ein neues Arbeitsblatt hinzu # Fügen Sie ein neues Arbeitsblatt hinzu
worksheet = workbook.create_sheet() worksheet = workbook.active
# Setzen Sie die Spaltenbreite auf 19 Pixel
worksheet.column_dimensions["A"].width = 6
worksheet.column_dimensions["B"].width = 19
worksheet.column_dimensions["C"].width = 19
worksheet.column_dimensions["D"].width = 19
worksheet.column_dimensions["E"].width = 19
worksheet.column_dimensions["F"].width = 19
worksheet.column_dimensions["G"].width = 19
worksheet.column_dimensions["H"].width = 19
worksheet.column_dimensions["I"].width = 19
worksheet.column_dimensions["J"].width = 12
# Basis Configurationen hier beschreiben # Basis Configurationen hier beschreiben
# TODO: Implementieren der Excluded Folder, Aktivierungsfunktion (128 Einheiten, 1 Einheit), Optimierungsverfahren # TODO: Implementieren der Excluded Folder, Aktivierungsfunktion (128 Einheiten, 1 Einheit), Optimierungsverfahren
...@@ -15,22 +27,33 @@ def create_excel_result(callback, config): ...@@ -15,22 +27,33 @@ def create_excel_result(callback, config):
# Schreiben Sie die Überschriften in die erste Zeile # Schreiben Sie die Überschriften in die erste Zeile
# TODO: Ergänzung Recall 1, Recall 2 # TODO: Ergänzung Recall 1, Recall 2
worksheet.cell(row=1, column=1).value = 'epoch' worksheet.cell(row=1, column=1).value = 'epoch'
worksheet.cell(row=1, column=2).value = 'loss' worksheet.cell(row=1, column=2).value = 'training_loss'
worksheet.cell(row=1, column=3).value = 'accuracy' worksheet.cell(row=1, column=3).value = 'training_accuracy'
worksheet.cell(row=1, column=4).value = 'val_loss' worksheet.cell(row=1, column=4).value = 'training_recall_not_oos'
worksheet.cell(row=1, column=5).value = 'val_accuracy' worksheet.cell(row=1, column=5).value = 'training_recall_oos'
worksheet.cell(row=1, column=6).value = 'duration' worksheet.cell(row=1, column=6).value = 'test_loss'
worksheet.cell(row=1, column=7).value = 'test_accuracy'
worksheet.cell(row=1, column=8).value = 'test_recall_not_oos'
worksheet.cell(row=1, column=9).value = 'test_recall_oos'
worksheet.cell(row=1, column=10).value = 'duration'
# Schreiben Sie die Ergebnisse der Epochen in die Arbeitsmappe # Schreiben Sie die Ergebnisse der Epochen in die Arbeitsmappe
for i, (loss, accuracy, val_loss, val_accuracy, duration) in enumerate( for i, (loss, accuracy, recall_not_oos, recall_oos,
zip(callback.epoch_losses, callback.epoch_accuracies, callback.epoch_val_losses, val_loss, val_accuracy, val_recall_not_oos, val_recall_oos, duration) in enumerate(
callback.epoch_val_accuracies, callback.epoch_durations)): zip(callback.epoch_training_losses, callback.epoch_training_accuracies,
callback.epoch_training_recall_not_oos, callback.epoch_training_recall_oos,
callback.epoch_test_losses, callback.epoch_test_accuracies, callback.epoch_test_recall_not_oos,
callback.epoch_test_recall_oos, callback.epoch_durations)):
worksheet.cell(row=i + 2, column=1).value = i + 1 worksheet.cell(row=i + 2, column=1).value = i + 1
worksheet.cell(row=i + 2, column=2).value = loss worksheet.cell(row=i + 2, column=2).value = loss
worksheet.cell(row=i + 2, column=3).value = accuracy worksheet.cell(row=i + 2, column=3).value = accuracy
worksheet.cell(row=i + 2, column=4).value = val_loss worksheet.cell(row=i + 2, column=4).value = recall_not_oos
worksheet.cell(row=i + 2, column=5).value = val_accuracy worksheet.cell(row=i + 2, column=5).value = recall_oos
worksheet.cell(row=i + 2, column=6).value = duration worksheet.cell(row=i + 2, column=6).value = val_loss
worksheet.cell(row=i + 2, column=7).value = val_accuracy
worksheet.cell(row=i + 2, column=8).value = val_recall_not_oos
worksheet.cell(row=i + 2, column=9).value = val_recall_oos
worksheet.cell(row=i + 2, column=10).value = duration
now = datetime.now() now = datetime.now()
date_time = now.strftime("%d_%m_%y__%H_%M") date_time = now.strftime("%d_%m_%y__%H_%M")
......
...@@ -3,19 +3,42 @@ import time ...@@ -3,19 +3,42 @@ import time
class CustomCallback(tf.keras.callbacks.Callback): class CustomCallback(tf.keras.callbacks.Callback):
def on_train_begin(self, logs={}): def __init__(self):
self.epoch_losses = [] super().__init__()
self.epoch_accuracies = [] self.epoch_start_time = None
self.epoch_val_losses = [] self.epoch_training_losses = None
self.epoch_val_accuracies = [] self.epoch_training_accuracies = None
self.epoch_training_recall_oos = None
self.epoch_training_recall_not_oos = None
self.epoch_test_losses = None
self.epoch_test_accuracies = None
self.epoch_test_recall_oos = None
self.epoch_test_recall_not_oos = None
self.epoch_durations = None
def on_train_begin(self, logs=None):
self.epoch_training_losses = []
self.epoch_training_accuracies = []
self.epoch_training_recall_oos = []
self.epoch_training_recall_not_oos = []
self.epoch_test_losses = []
self.epoch_test_accuracies = []
self.epoch_test_recall_oos = []
self.epoch_test_recall_not_oos = []
self.epoch_durations = [] self.epoch_durations = []
def on_epoch_begin(self, epoch, logs={}): def on_epoch_begin(self, epoch, logs=None):
self.epoch_start_time = time.time() self.epoch_start_time = time.time()
def on_epoch_end(self, epoch, logs={}): def on_epoch_end(self, epoch, logs=None):
self.epoch_losses.append(logs.get('loss')) if logs is None:
self.epoch_accuracies.append(logs.get('accuracy')) logs = {}
self.epoch_val_losses.append(logs.get('val_loss')) self.epoch_training_losses.append(logs.get('loss'))
self.epoch_val_accuracies.append(logs.get('val_accuracy')) self.epoch_training_accuracies.append(logs.get('binary_accuracy'))
self.epoch_training_recall_oos.append(logs.get('recall_0'))
self.epoch_training_recall_not_oos.append(logs.get('recall_1'))
self.epoch_test_losses.append(logs.get('val_loss'))
self.epoch_test_accuracies.append(logs.get('val_binary_accuracy'))
self.epoch_test_recall_oos.append(logs.get('val_recall_0'))
self.epoch_test_recall_not_oos.append(logs.get('val_recall_1'))
self.epoch_durations.append(time.time() - self.epoch_start_time) self.epoch_durations.append(time.time() - self.epoch_start_time)
...@@ -14,8 +14,12 @@ def plot_values(history, config): ...@@ -14,8 +14,12 @@ def plot_values(history, config):
# plot accuracy # plot accuracy
pyplot.subplot(212) pyplot.subplot(212)
pyplot.title('Classification Accuracy') pyplot.title('Classification Accuracy')
pyplot.plot(history.history['accuracy'], color='blue', label='train') pyplot.plot(history.history['binary_accuracy'], color='blue', label='Accuracy')
pyplot.plot(history.history['val_accuracy'], color='orange', label='test') pyplot.plot(history.history['recall_0'], color='blue', label='Recall OOS')
pyplot.plot(history.history['recall_1'], color='yellow', label='Recall !OOS')
pyplot.plot(history.history['val_binary_accuracy'], color='orange', label='Test Accuracy')
pyplot.plot(history.history['val_recall_0'], color='red', label='Test Recall OOS')
pyplot.plot(history.history['val_recall_1'], color='green', label='Test Recall !OOS')
# save plot to file # save plot to file
filename = sys.argv[0].split('/')[-1] filename = sys.argv[0].split('/')[-1]
pyplot.savefig(config["result"]["plot_path"] + filename + '_plot.png') pyplot.savefig(config["result"]["plot_path"] + filename + '_plot.png')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment