Commit 9e9d1038 authored by Dennis Willers's avatar Dennis Willers 🏀

Initial commit

parents
# See http://help.github.com/ignore-files/ for more about ignoring files.
# compiled output
/dist
/tmp
/out-tsc
# Only exists if Bazel was run
/bazel-out
# dependencies
/node_modules
# profiling files
chrome-profiler-events*.json
speed-measure-plugin*.json
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*
# misc
/.angular/cache
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
yarn-error.log
testem.log
/typings
# System Files
.DS_Store
Thumbs.db
/venv/
/assets/parquet/*
*/.DS_Store/*
assets/Bilder/*
assets/Bilder/AktuelleTrainingsUndTestdaten/*
import yaml
from datetime import datetime
from src.data.copyData import copy_images_with_exclusion
from src.data.modelData import get_test_data, get_train_data, define_augmentation_rules
from src.enum.activierungsfunktionEnum import Aktivierungsfunktion
from src.enum.marktEnum import Markt
from src.enum.optimierungsverfahren import Optimierungsverfahren
from src.knn.configKNN import ConfigKNN
from src.knn.createKNN import evaluate_model, fit_model
from src.knn.defineKNN import define_model
from src.result.createExcelFile import create_excel_result
from src.result.customCallback import CustomCallback
from src.result.plotResult import plot_values
# run the test harness for evaluating a model
def run_model():
print("Lese Configurationsdatei")
# Lesen der Configurationdatei
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)
print("Initialisiere Callback")
# Erstellen Sie eine Instanz des benutzerdefinierten Callbacks
callback = CustomCallback()
print("Generiere Ordner mit OOS und !OOS Daten")
# Erstellen der Ordner mit Inhalt für die Trainings-/Testdaten und Evaluationsdaten, die für das nächste Modell
# benötigt werden
copy_images_with_exclusion(config["bilder"]["original_path"],
config["bilder"]["knn_path"],
config_knn.excluded_folder)
print("Starte Zeit")
start_time = datetime.now()
# define model
print("Definiere KNN Modell")
model = define_model(config_knn)
# create data generator
print("Initialisiere ImageDataGenerator")
datagen = define_augmentation_rules()
# prepare iterator
print("Definiere Trainings- und Testdaten Set")
train_it = get_train_data(datagen, config["bilder"]["knn_path"])
test_it = get_test_data(datagen, config["bilder"]["knn_path"])
print("Trainiere das Modell")
# fit model
history = fit_model(config, model, train_it, test_it, callback)
# evaluate model
print("Evaluiere das Modell")
evaluate_model(model, test_it)
# 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))
# learning curves
print("Erstelle den Plot Graphen")
plot_values(history, config)
print("Erstelle die Excel-Tabelle")
create_excel_result(callback, config)
# entry point, run the test harness
run_model()
bilder:
original_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/assets/Bilder/Datengrundlage-Reduziert-Test/"
knn_path: "/Users/dwillers/Programmierung/Master/Evaluation_OOS-Erkennung/assets/Bilder/AktuelleTrainingsUndTestdaten/"
knn:
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/"
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():
# Prozentualer Anteil der Bilder, die kopiert werden sollen
sample_rate = 0.05
# Erstelle den Zielordner, falls er nicht existiert
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
# Durchlaufe alle Unterordner des Quellordners
for subdir, dirs, files in os.walk(src_dir):
# Erstelle einen entsprechenden Unterordner im Zielordner
dst_subdir = subdir.replace(src_dir, dst_dir)
if not os.path.exists(dst_subdir):
os.makedirs(dst_subdir)
# Wähle zufällig einen Teil der Bilder aus
selected_files = random.sample(files, int(len(files) * sample_rate))
# Kopiere die ausgewählten Bilder in den Zielordner
for file in selected_files:
src_file = os.path.join(subdir, file)
dst_file = os.path.join(dst_subdir, file)
shutil.copy(src_file, dst_file)
def copy_images_with_exclusion(src_dir, dest_dir, exclude_dir=None):
# Lösche vorhandene Dateien und Unterordner im Zielordner
for file_or_dir in os.listdir(dest_dir):
file_or_dir_path = os.path.join(dest_dir, file_or_dir)
if os.path.isdir(file_or_dir_path):
shutil.rmtree(file_or_dir_path)
else:
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'))
# 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')
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'))
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'))
import tensorflow as tf
def define_augmentation_rules():
augmentation_rules = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1. / 224,
horizontal_flip=True,
rotation_range=15,
fill_mode='nearest',
width_shift_range=0.2,
height_shift_range=0.2,
brightness_range=[0.5, 1.5],
zoom_range=0.5,
validation_split=0.3,
)
return augmentation_rules
def get_train_data(datagen, directory):
train_it = datagen.flow_from_directory(
directory=directory,
class_mode='binary',
batch_size=64,
target_size=(224, 224),
subset='training')
return train_it
def get_test_data(datagen, directory):
test_it = datagen.flow_from_directory(
directory=directory,
class_mode='binary',
batch_size=64,
target_size=(224, 224),
subset='validation')
return test_it
\ No newline at end of file
import os
import shutil
import tensorflow as tf
def createPathWithPictures():
# Pfad zu den 6 Ordnern
main_folder_path = "/assets/Bilder/Datengrundlage"
# Pfad zum neuen Ordner
new_folder_path = "/assets/Bilder/Modelldaten/alleMaerkte"
# Schleife durch die 6 Ordner
for folder in os.listdir(main_folder_path):
current_folder = os.path.join(main_folder_path, folder)
for subfolder in os.listdir(current_folder):
if subfolder in ["OOS", "!OOS"]:
subfolder_path = os.path.join(current_folder, subfolder)
# Erstellen Sie Unterordner im Zielordner mit dem Namen des Unterordners
new_subfolder_path = os.path.join(new_folder_path, subfolder)
os.makedirs(new_subfolder_path, exist_ok=True)
for image in os.listdir(subfolder_path):
image_path = os.path.join(subfolder_path, image)
# Kopieren Sie die Bilder in den neuen Unterordner im Zielordner
shutil.copy(image_path, new_subfolder_path)
def generateAugmentedNotOOSPictures():
# Pfad zum Ordner "!OOS"
folder_path = "/assets/Bilder/Modelldaten/only!OOS"
# 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"
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():
augmentedImageDefinition = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1. / 224,
horizontal_flip=True,
rotation_range=15,
fill_mode='nearest',
width_shift_range=0.2,
height_shift_range=0.2,
brightness_range=[0.5, 1.5],
zoom_range=0.5,
)
# specify imagenet mean values for centering
# datagen.mean = [124, 150, 130]
return augmentedImageDefinition
generateAugmentedNotOOSPictures()
from enum import Enum
class Aktivierungsfunktion(Enum):
ReLU = 0,
Sigmoid = 1,
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'
from enum import Enum
class Optimierungsverfahren(Enum):
SGD = 0,
Adam = 1
from src.enum.activierungsfunktionEnum import Aktivierungsfunktion
from src.enum.optimierungsverfahren import Optimierungsverfahren
class ConfigKNN:
def __init__(self,
excluded_folder=None,
activation_function_128_units=Aktivierungsfunktion.ReLU,
activation_function_1_units=Aktivierungsfunktion.ReLU,
optimization_method=Optimierungsverfahren.SGD):
self.excluded_folder = excluded_folder
self.activation_function_128_units = activation_function_128_units
self.activation_function_1_units = activation_function_1_units
self.optimization_method = optimization_method
def fit_model(config, model, train_it, test_it, callback):
trained_model = model.fit(
train_it,
steps_per_epoch=len(train_it),
validation_data=test_it,
validation_steps=len(test_it),
epochs=config["knn"]["epochs"],
verbose=1,
callbacks=[callback]
)
return trained_model
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
import tensorflow as tf
from src.enum.activierungsfunktionEnum import Aktivierungsfunktion
from src.enum.optimierungsverfahren import Optimierungsverfahren
# define cnn model
def define_model(config_knn):
# Vortrainierte VGG16-Modell wird geladen
model = tf.keras.applications.VGG16(include_top=False, input_shape=(224, 224, 3))
# Die geladenen Schichten müssen nicht neu trainiert werden
for layer in model.layers:
layer.trainable = False
# 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)
# 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])
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,
kernel_initializer='he_uniform')(flat_or_dense)
if activation_function == Aktivierungsfunktion.Sigmoid:
return tf.keras.layers.Dense(units, activation='sigmoid')(flat_or_dense)
return None
def get_optimization_method(optimization_method):
if optimization_method == Optimierungsverfahren.SGD:
return tf.keras.optimizers.SGD(learning_rate=0.001, momentum=0.9)
if optimization_method == Optimierungsverfahren.Adam:
return tf.keras.optimizers.Adam(learning_rate=0.001)
return None
import openpyxl
from datetime import datetime
def create_excel_result(callback, config):
# Öffnen Sie eine neue Excel-Arbeitsmappe
workbook = openpyxl.Workbook()
# Fügen Sie ein neues Arbeitsblatt hinzu
worksheet = workbook.create_sheet()
# Basis Configurationen hier beschreiben
# TODO: Implementieren der Excluded Folder, Aktivierungsfunktion (128 Einheiten, 1 Einheit), Optimierungsverfahren
# 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'
# 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)):
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
now = datetime.now()
date_time = now.strftime("%d_%m_%y__%H_%M")
file_name = "epoch_results_" + 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
import tensorflow as tf
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 = []
self.epoch_durations = []
def on_epoch_begin(self, epoch, logs={}):
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
import sys
from matplotlib import pyplot
# plot diagnostic learning curves
def plot_values(history, config):
# plot loss
"""
pyplot.subplot(211)
pyplot.title('Cross Entropy Loss')
pyplot.plot(history.history['loss'], color='blue', label='train')
pyplot.plot(history.history['val_loss'], color='orange', label='test')
"""
# 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')
# save plot to file
filename = sys.argv[0].split('/')[-1]
pyplot.savefig(config["result"]["plot_path"] + filename + '_plot.png')
pyplot.close()
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