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

Ändern der Reihenfolge recall_0 und recall_1 zu recall_not_oos und recall_oos...

Ändern der Reihenfolge recall_0 und recall_1 zu recall_not_oos und recall_oos / Logik für augmentierte Daten angepasst
parent 33862d94
......@@ -90,8 +90,8 @@ def run_model(config, config_knn, worksheet, r):
training_duration_model_str = format(training_duration_model)
print('Die Dauer für das Erstellen des Modells beträgt: ' + training_duration_model_str)
# learning curves
print("Erstelle den Plot Graphen")
plot_values(history, config)
# print("Erstelle den Plot Graphen")
# plot_values(history, config)
print("Inhalte in die Excel-Tabelle schreiben")
worksheet, r = create_excel_result(worksheet, r, callback, config, config_knn, evaluate_metrics,
training_duration_model)
......
bilder:
# original_path: "assets/Bilder/Datengrundlage-Reduziert-Test/"
original_path: "assets/Bilder/Datengrundlage/"
# original_path: "assets/Bilder/Datengrundlage/"
original_path: "assets/Bilder/Datengrundlage-Augmentiert"
knn_path: "assets/Bilder/AktuelleTrainingsUndTestdaten/"
knn:
epochs: 10
epochs: 3
exception_activation_funktion_1:
['ReLU']
result:
......
......@@ -44,8 +44,8 @@ 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, '0_OOS'))
os.makedirs(os.path.join(dest_dir, '1_!OOS'))
os.makedirs(os.path.join(dest_dir, '!OOS'))
os.makedirs(os.path.join(dest_dir, 'OOS'))
# Durchlaufe die Unterordner im Quellverzeichnis
......@@ -58,7 +58,7 @@ def copy_images_with_exclusion(src_dir, dest_dir, exclude_dir=None):
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, '0_OOS'))
shutil.copy(os.path.join(src_dir, subdir, folder, img), os.path.join(dest_dir, '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, '1_!OOS'))
shutil.copy(os.path.join(src_dir, subdir, folder, img), os.path.join(dest_dir, '!OOS'))
import os
import shutil
import tensorflow as tf
from src.enum.marktEnum import Markt
def createPathWithPictures():
......@@ -26,27 +27,28 @@ def createPathWithPictures():
def generateAugmentedNotOOSPictures():
# Pfad zum Ordner "!OOS"
folder_path = "/assets/Bilder/Modelldaten/only!OOS"
for markt in Markt:
# Pfad zum Ordner "!OOS"
folder_path = "assets/Bilder/Datengrundlage_Test/" + markt.name + "/"
# Erstellen Sie eine Instanz des ImageDataGenerator
datagen = get_images()
# Erstellen Sie eine Instanz des ImageDataGenerator
datagen = get_images()
# Pfad für den neuen Ordner, in dem die augmentierten Bilder gespeichert werden sollen
save_to_dir = "/assets/Bilder/Modelldaten/alleMaerkteAugmented/!OOS"
# Pfad für den neuen Ordner, in dem die augmentierten Bilder gespeichert werden sollen
save_to_dir = "assets/Bilder/Datengrundlage-Augmentiert/" + markt.name + "/!OOS"
for i in range(9):
# Trainingsdaten konfigurieren
train_it = datagen.flow_from_directory(
folder_path,
class_mode='binary',
batch_size=1425,
target_size=(224, 224),
save_to_dir=save_to_dir,
save_prefix="augmented_" + str(i) + "_",
save_format='jpg',
)
train_it.next()
for i in range(9):
# Trainingsdaten konfigurieren
train_it = datagen.flow_from_directory(
folder_path,
class_mode='binary',
batch_size=1425,
target_size=(224, 224),
save_to_dir=save_to_dir,
save_prefix="augmented_" + str(i) + "_",
save_format='jpg',
)
train_it.next()
def get_images():
......@@ -65,4 +67,4 @@ def get_images():
return augmentedImageDefinition
generateAugmentedNotOOSPictures()
# generateAugmentedNotOOSPictures()
......@@ -19,7 +19,7 @@ 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)
loss, accuracy, recall_0, recall_1 = all_metrics
loss, accuracy, recall_not_oos, recall_oos = all_metrics
print('loss: %.3f' % loss + ' - accuracy: %.3f' % accuracy +
' - recall_0: %.3f' % recall_0 + ' - recall_1: %.3f' % recall_1)
' - recall_not_oos: %.3f' % recall_not_oos + ' - recall_oos: %.3f' % recall_oos)
return all_metrics
......@@ -23,11 +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()
recall_0 = tf.keras.metrics.Recall(name='recall_0', class_id=0)
recall_1 = tf.keras.metrics.Recall(name='recall_1', class_id=1)
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)
# Füge die Metriken dem Modell hinzu
model.compile(optimizer=opt, loss=loss_fn, metrics=['accuracy', recall_0, recall_1])
model.compile(optimizer=opt, loss=loss_fn, metrics=['accuracy', recall_not_oos, recall_oos])
return model
......
......@@ -35,10 +35,10 @@ class CustomCallback(tf.keras.callbacks.Callback):
logs = {}
self.epoch_training_losses.append(logs.get('loss'))
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_training_recall_oos.append(logs.get('recall_oos'))
self.epoch_training_recall_not_oos.append(logs.get('recall_not_oos'))
self.epoch_test_losses.append(logs.get('val_loss'))
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_test_recall_oos.append(logs.get('val_recall_oos'))
self.epoch_test_recall_not_oos.append(logs.get('val_recall_not_oos'))
self.epoch_durations.append(time.time() - self.epoch_start_time)
......@@ -15,11 +15,11 @@ def plot_values(history, config):
pyplot.subplot(212)
pyplot.title('Classification 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['recall_oos'], color='blue', label='Recall OOS')
pyplot.plot(history.history['recall_not_oos'], color='yellow', label='Recall !OOS')
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')
pyplot.plot(history.history['val_recall_oos'], color='red', label='Test Recall OOS')
pyplot.plot(history.history['val_recall_not_oos'], 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