Commit af35f578 authored by Dennis Willers's avatar Dennis Willers 🏀

Excel Dokumentationsausgabe implementiert

parent 2f264f7c
......@@ -55,3 +55,5 @@ assets/Bilder/*
assets/Bilder/AktuelleTrainingsUndTestdaten/*
assets/Bilder/Datengrundlage/*
assets/Bilder/Datengrundlage/
/ressources/results/
/ressources/results/*
......@@ -22,7 +22,7 @@ def run_model():
config = yaml.load(file, Loader=yaml.FullLoader)
# Initialisieren der KNN-Modellbau Eigenschaften
config_knn = ConfigKNN(
excluded_folder=Markt.Kein_Markt,
excluded_folder=Markt.Markt_C,
activation_function_1_units=Aktivierungsfunktion.sigmoid,
activation_function_128_units=Aktivierungsfunktion.ReLU,
optimization_method=Optimierungsverfahren.SGD
......@@ -56,16 +56,17 @@ def run_model():
history = fit_model(config, model, train_it, test_it, callback)
# evaluate model
print("Evaluiere das Modell")
evaluate_model(model, test_it)
evaluate_metrics = evaluate_model(model, test_it, datagen, config, config_knn)
# print duration
print("Stoppe die Zeit")
end_time = datetime.now()
print('Die Dauer für das Erstellen des Modells beträgt: {}'.format(end_time - start_time))
training_duration_model = format(end_time - start_time)
print('Die Dauer für das Erstellen des Modells beträgt: ' + training_duration_model)
# learning curves
print("Erstelle den Plot Graphen")
plot_values(history, config)
print("Erstelle die Excel-Tabelle")
create_excel_result(callback, config)
create_excel_result(callback, config, config_knn, evaluate_metrics, training_duration_model)
# entry point, run the test harness
......
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/"
knn_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/assets/Bilder/AktuelleTrainingsUndTestdaten/"
original_path: "assets/Bilder/Datengrundlage-Reduziert-Test/"
# original_path: "assets/Bilder/Datengrundlage/"
knn_path: "assets/Bilder/AktuelleTrainingsUndTestdaten/"
knn:
epochs: 6
epochs: 2
result:
plot_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/ressources/results/plot/"
excel_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/ressources/results/excel/"
plot_path: "ressources/results/plot/"
excel_path: "ressources/results/excel/"
......@@ -2,15 +2,14 @@ import os
import shutil
import random
# Pfad des Quellordners
src_dir = '/assets/Bilder/Datengrundlage'
# Pfad des Zielordners
dst_dir = '/assets/Bilder/Datengrundlage-Reduziert-Test'
dst_dir_trainingsdaten = '/assets/Bilder/AktuelleTrainingsUndTestdaten'
def create_reduzierte_testdaten():
# Pfad des Quellordners
src_dir = '/assets/Bilder/Datengrundlage'
# Pfad des Zielordners
dst_dir = '/assets/Bilder/Datengrundlage-Reduziert-Test'
def create_reduzierte_testdaten():
# Prozentualer Anteil der Bilder, die kopiert werden sollen
sample_rate = 0.05
......
......@@ -33,4 +33,13 @@ def get_test_data(datagen, directory):
batch_size=64,
target_size=(224, 224),
subset='validation')
return test_it
\ No newline at end of file
return test_it
def get_markt_data(datagen, directory):
test_it = datagen.flow_from_directory(
directory=directory,
class_mode='categorical',
batch_size=64,
target_size=(224, 224))
return test_it
from src.data.modelData import get_markt_data
from src.enum.marktEnum import Markt
def fit_model(config, model, train_it, test_it, callback):
trained_model = model.fit(
train_it,
......@@ -11,12 +15,11 @@ def fit_model(config, model, train_it, test_it, callback):
return trained_model
def evaluate_model(model, test_it):
def evaluate_model(model, test_it, datagen, config, config_knn):
if config_knn.excluded_folder != Markt.Kein_Markt:
test_it = get_markt_data(datagen, config["bilder"]["original_path"] + config_knn.excluded_folder.name)
all_metrics = model.evaluate(test_it, steps=len(test_it), verbose=1)
_, metrics, recall_0, recall_1 = all_metrics
print(all_metrics)
print(_)
print(metrics)
print(recall_0)
print(recall_1)
print('> %.3f' % (metrics * 100.0))
loss, accuracy, recall_0, recall_1 = all_metrics
print('loss: %.3f' % loss + ' - accuracy: %.3f' % accuracy +
' - recall_0: %.3f' % recall_0 + ' - recall_1: %.3f' % recall_1)
return all_metrics
......@@ -23,12 +23,11 @@ def define_model(config_knn):
opt = get_optimization_method(config_knn.optimization_method)
# Die Klassifizierungsmetriken und die Loss-Funktion werden definiert
loss_fn = tf.keras.losses.BinaryCrossentropy()
binary_accuracy = tf.keras.metrics.BinaryAccuracy()
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])
model.compile(optimizer=opt, loss=loss_fn, metrics=['accuracy', recall_0, recall_1])
return model
......
......@@ -2,67 +2,93 @@ import openpyxl
from datetime import datetime
def create_excel_result(callback, config):
def create_excel_result(callback, config, config_knn, evaluate_metrics, training_duration_model):
# Öffnen Sie eine neue Excel-Arbeitsmappe
workbook = openpyxl.Workbook()
# Fügen Sie ein neues Arbeitsblatt hinzu
worksheet = workbook.active
r = 1
# 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
# Spaltenbreite setzen
worksheet.column_dimensions["A"].width = 10
worksheet.column_dimensions["B"].width = 20
worksheet.column_dimensions["C"].width = 20
worksheet.column_dimensions["D"].width = 20
worksheet.column_dimensions["E"].width = 20
worksheet.column_dimensions["F"].width = 20
worksheet.column_dimensions["G"].width = 20
worksheet.column_dimensions["H"].width = 20
worksheet.column_dimensions["I"].width = 20
worksheet.column_dimensions["J"].width = 12
# Basis Configurationen hier beschreiben
# TODO: Implementieren der Excluded Folder, Aktivierungsfunktion (128 Einheiten, 1 Einheit), Optimierungsverfahren
# Basis Configurationen beschreiben
worksheet.cell(row=r, column=2).value = 'Optimierungsverfahren'
worksheet.cell(row=r, column=3).value = 'Aktivierungsfunktion 128'
worksheet.cell(row=r, column=4).value = 'Aktivierungsfunktion 2'
worksheet.cell(row=r, column=5).value = 'Evaluationsmarkt'
r = r+1
worksheet.cell(row=r, column=2).value = config_knn.optimization_method.name
worksheet.cell(row=r, column=3).value = config_knn.activation_function_128_units.name
worksheet.cell(row=r, column=4).value = config_knn.activation_function_1_units.name
worksheet.cell(row=r, column=5).value = config_knn.excluded_folder.name
r = r+2
# Schreiben Sie die Überschriften in die erste Zeile
# TODO: Ergänzung Recall 1, Recall 2
worksheet.cell(row=1, column=1).value = 'epoch'
worksheet.cell(row=1, column=2).value = 'training_loss'
worksheet.cell(row=1, column=3).value = 'training_accuracy'
worksheet.cell(row=1, column=4).value = 'training_recall_not_oos'
worksheet.cell(row=1, column=5).value = 'training_recall_oos'
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'
worksheet.cell(row=r, column=1).value = 'epoch'
worksheet.cell(row=r, column=2).value = 'training_loss'
worksheet.cell(row=r, column=3).value = 'training_accuracy'
worksheet.cell(row=r, column=4).value = 'training_recall_oos'
worksheet.cell(row=r, column=5).value = 'training_recall_not_oos'
worksheet.cell(row=r, column=6).value = 'test_loss'
worksheet.cell(row=r, column=7).value = 'test_accuracy'
worksheet.cell(row=r, column=8).value = 'test_recall_oos'
worksheet.cell(row=r, column=9).value = 'test_recall_not_oos'
worksheet.cell(row=r, column=10).value = 'duration'
r = r+1
# Schreiben Sie die Ergebnisse der Epochen in die Arbeitsmappe
for i, (loss, accuracy, recall_not_oos, recall_oos,
val_loss, val_accuracy, val_recall_not_oos, val_recall_oos, duration) in enumerate(
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=2).value = loss
worksheet.cell(row=i + 2, column=3).value = accuracy
worksheet.cell(row=i + 2, column=4).value = recall_not_oos
worksheet.cell(row=i + 2, column=5).value = recall_oos
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
for i, (loss, accuracy, recall_oos, recall_not_oos,
val_loss, val_accuracy, val_recall_oos, val_recall_not_oos, duration) in enumerate(
zip(callback.epoch_training_losses, callback.epoch_training_accuracies,
callback.epoch_training_recall_oos, callback.epoch_training_recall_not_oos,
callback.epoch_test_losses, callback.epoch_test_accuracies, callback.epoch_test_recall_oos,
callback.epoch_test_recall_not_oos, callback.epoch_durations)):
worksheet.cell(row=r+i, column=1).value = i + 1
worksheet.cell(row=r+i, column=2).value = loss
worksheet.cell(row=r+i, column=3).value = accuracy
worksheet.cell(row=r+i, column=4).value = recall_oos
worksheet.cell(row=r+i, column=5).value = recall_not_oos
worksheet.cell(row=r+i, column=6).value = val_loss
worksheet.cell(row=r+i, column=7).value = val_accuracy
worksheet.cell(row=r+i, column=8).value = val_recall_oos
worksheet.cell(row=r+i, column=9).value = val_recall_not_oos
worksheet.cell(row=r+i, column=10).value = duration
r = r+config["knn"]["epochs"]+1
worksheet.cell(row=r, column=2).value = 'Trainingsdauer'
worksheet.cell(row=r, column=3).value = training_duration_model
r = r+3
worksheet.cell(row=r, column=1).value = 'Evaluation'
worksheet.cell(row=r, column=2).value = 'loss'
worksheet.cell(row=r, column=3).value = 'accuracy'
worksheet.cell(row=r, column=4).value = 'recall_oos'
worksheet.cell(row=r, column=5).value = 'recall_not_oos'
r = r+1
worksheet.cell(row=r, column=1).value = config_knn.excluded_folder.name
worksheet.cell(row=r, column=2).value = evaluate_metrics[0]
worksheet.cell(row=r, column=3).value = evaluate_metrics[1]
worksheet.cell(row=r, column=4).value = evaluate_metrics[2]
worksheet.cell(row=r, column=5).value = evaluate_metrics[3]
now = datetime.now()
date_time = now.strftime("%d_%m_%y__%H_%M")
file_name = "epoch_results_" + date_time + ".xlsx"
file_name = config_knn.optimization_method.name + "_" + config_knn.activation_function_1_units.name + "_" + \
config_knn.activation_function_128_units.name + "_" + date_time + ".xlsx"
# Speichern Sie die Arbeitsmappe
workbook.save(config["result"]["excel_path"] + file_name)
# Abschlussbericht definieren
# TODO: Implementierung Evaluation mit Excluded Folder, Accuracy, Recall 1, Recall 2
# TODO: Abschluss Statistik nach Cross-Validation von einer Konfiguration: Durchschnitt Accuracy, Recall 1, Recall 2, Loss, Duration für Evaluation
......@@ -34,11 +34,11 @@ class CustomCallback(tf.keras.callbacks.Callback):
if logs is None:
logs = {}
self.epoch_training_losses.append(logs.get('loss'))
self.epoch_training_accuracies.append(logs.get('binary_accuracy'))
self.epoch_training_accuracies.append(logs.get('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_accuracies.append(logs.get('val_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)
......@@ -14,10 +14,10 @@ def plot_values(history, config):
# plot accuracy
pyplot.subplot(212)
pyplot.title('Classification Accuracy')
pyplot.plot(history.history['binary_accuracy'], color='blue', label='Accuracy')
pyplot.plot(history.history['accuracy'], color='blue', label='Accuracy')
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_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
......
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