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

Recall eingebunden.

parent 9e9d1038
......@@ -53,3 +53,5 @@ Thumbs.db
assets/Bilder/*
assets/Bilder/AktuelleTrainingsUndTestdaten/*
assets/Bilder/Datengrundlage/*
assets/Bilder/Datengrundlage/
......@@ -21,10 +21,12 @@ def run_model():
with open("ressources/config/config.yaml", "r") as file:
config = yaml.load(file, Loader=yaml.FullLoader)
# Initialisieren der KNN-Modellbau Eigenschaften
config_knn = ConfigKNN(excluded_folder=Markt.MARKT_B,
activation_function_1_units=Aktivierungsfunktion.Sigmoid,
activation_function_128_units=Aktivierungsfunktion.ReLU,
optimization_method=Optimierungsverfahren.Adam)
config_knn = ConfigKNN(
excluded_folder=Markt.Kein_Markt,
activation_function_1_units=Aktivierungsfunktion.sigmoid,
activation_function_128_units=Aktivierungsfunktion.ReLU,
optimization_method=Optimierungsverfahren.SGD
)
print("Initialisiere Callback")
# Erstellen Sie eine Instanz des benutzerdefinierten Callbacks
......
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/"
knn:
epochs: 2
epochs: 6
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/"
......@@ -45,20 +45,21 @@ def copy_images_with_exclusion(src_dir, dest_dir, exclude_dir=None):
os.remove(file_or_dir_path)
# Erstelle die gewünschte Ordnerstruktur im Zielverzeichnis
os.makedirs(os.path.join(dest_dir, 'OOS'))
os.makedirs(os.path.join(dest_dir, '!OOS'))
os.makedirs(os.path.join(dest_dir, '0_OOS'))
os.makedirs(os.path.join(dest_dir, '1_!OOS'))
# Durchlaufe die Unterordner im Quellverzeichnis
for subdir in os.listdir(src_dir):
# if subdir == exclude_dir:
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:
continue
for folder in os.listdir(os.path.join(src_dir, subdir)):
if folder == 'OOS':
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':
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():
def get_train_data(datagen, directory):
train_it = datagen.flow_from_directory(
directory=directory,
class_mode='binary',
class_mode='categorical',
batch_size=64,
target_size=(224, 224),
subset='training')
......@@ -29,7 +29,7 @@ def get_train_data(datagen, directory):
def get_test_data(datagen, directory):
test_it = datagen.flow_from_directory(
directory=directory,
class_mode='binary',
class_mode='categorical',
batch_size=64,
target_size=(224, 224),
subset='validation')
......
......@@ -3,4 +3,4 @@ from enum import Enum
class Aktivierungsfunktion(Enum):
ReLU = 0,
Sigmoid = 1,
sigmoid = 1,
......@@ -2,9 +2,10 @@ from enum import Enum
class Markt(Enum):
MARKT_A = 'Markt_A',
MARKT_B = 'Markt_B',
MARKT_C = 'Markt_C',
MARKT_D = 'Markt_D',
MARKT_E = 'Markt_E',
MARKT_F = 'Markt_F'
Markt_A = 0,
Markt_B = 1,
Markt_C = 2,
Markt_D = 3,
Markt_E = 4,
Markt_F = 5
Kein_Markt = -1
......@@ -12,5 +12,11 @@ def fit_model(config, model, train_it, test_it, callback):
def evaluate_model(model, test_it):
_, acc = model.evaluate(test_it, steps=len(test_it), verbose=1)
print('> %.3f' % (acc * 100.0))
\ No newline at end of file
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))
......@@ -14,26 +14,31 @@ def define_model(config_knn):
# Die Fully-Connected-Schichten werden definiert
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_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
model = tf.keras.models.Model(inputs=model.inputs, outputs=output_layer_2)
# Das Optimierungsverfahren wird definiert
opt = get_optimization_method(config_knn.optimization_method)
# Die Klassifizierungsmetriken und die Loss-Funktion werden definiert
recall_not_oos = tf.keras.metrics.Recall(name='recall_not_oos', class_id=0)
recall_oos = tf.keras.metrics.Recall(name='recall_oos', class_id=1)
model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy', recall_not_oos, recall_oos])
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])
return model
def get_next_layer(units, activation_function, flat_or_dense):
if activation_function == Aktivierungsfunktion.ReLU:
return tf.keras.layers.Dense(units,
activation=activation_function,
activation=activation_function.name,
kernel_initializer='he_uniform')(flat_or_dense)
if activation_function == Aktivierungsfunktion.Sigmoid:
return tf.keras.layers.Dense(units, activation='sigmoid')(flat_or_dense)
if activation_function == Aktivierungsfunktion.sigmoid:
return tf.keras.layers.Dense(units, activation=activation_function.name)(flat_or_dense)
return None
......@@ -43,3 +48,4 @@ def get_optimization_method(optimization_method):
if optimization_method == Optimierungsverfahren.Adam:
return tf.keras.optimizers.Adam(learning_rate=0.001)
return None
......@@ -7,7 +7,19 @@ def create_excel_result(callback, config):
workbook = openpyxl.Workbook()
# 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
# TODO: Implementieren der Excluded Folder, Aktivierungsfunktion (128 Einheiten, 1 Einheit), Optimierungsverfahren
......@@ -15,22 +27,33 @@ def create_excel_result(callback, config):
# 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 = 'loss'
worksheet.cell(row=1, column=3).value = 'accuracy'
worksheet.cell(row=1, column=4).value = 'val_loss'
worksheet.cell(row=1, column=5).value = 'val_accuracy'
worksheet.cell(row=1, column=6).value = 'duration'
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'
# Schreiben Sie die Ergebnisse der Epochen in die Arbeitsmappe
for i, (loss, accuracy, val_loss, val_accuracy, duration) in enumerate(
zip(callback.epoch_losses, callback.epoch_accuracies, callback.epoch_val_losses,
callback.epoch_val_accuracies, callback.epoch_durations)):
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 = val_loss
worksheet.cell(row=i + 2, column=5).value = val_accuracy
worksheet.cell(row=i + 2, column=6).value = duration
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
now = datetime.now()
date_time = now.strftime("%d_%m_%y__%H_%M")
......
......@@ -3,19 +3,42 @@ import time
class CustomCallback(tf.keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.epoch_losses = []
self.epoch_accuracies = []
self.epoch_val_losses = []
self.epoch_val_accuracies = []
def __init__(self):
super().__init__()
self.epoch_start_time = None
self.epoch_training_losses = None
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 = []
def on_epoch_begin(self, epoch, logs={}):
def on_epoch_begin(self, epoch, logs=None):
self.epoch_start_time = time.time()
def on_epoch_end(self, epoch, logs={}):
self.epoch_losses.append(logs.get('loss'))
self.epoch_accuracies.append(logs.get('accuracy'))
self.epoch_val_losses.append(logs.get('val_loss'))
self.epoch_val_accuracies.append(logs.get('val_accuracy'))
self.epoch_durations.append(time.time() - self.epoch_start_time)
\ No newline at end of file
def on_epoch_end(self, epoch, logs=None):
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_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)
......@@ -14,8 +14,12 @@ def plot_values(history, config):
# plot accuracy
pyplot.subplot(212)
pyplot.title('Classification Accuracy')
pyplot.plot(history.history['accuracy'], color='blue', label='train')
pyplot.plot(history.history['val_accuracy'], color='orange', label='test')
pyplot.plot(history.history['binary_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_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
filename = sys.argv[0].split('/')[-1]
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