Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Evaluation künstlicher neuronaler Netze für eine OOS-Erkennung
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dennis Willers
Evaluation künstlicher neuronaler Netze für eine OOS-Erkennung
Commits
af35f578
Commit
af35f578
authored
Jan 20, 2023
by
Dennis Willers
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Excel Dokumentationsausgabe implementiert
parent
2f264f7c
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
111 additions
and
72 deletions
+111
-72
.gitignore
.gitignore
+2
-0
main.py
main.py
+5
-4
config.yaml
ressources/config/config.yaml
+6
-6
copyData.py
src/data/copyData.py
+5
-6
modelData.py
src/data/modelData.py
+10
-1
createKNN.py
src/knn/createKNN.py
+11
-8
defineKNN.py
src/knn/defineKNN.py
+1
-2
createExcelFile.py
src/result/createExcelFile.py
+67
-41
customCallback.py
src/result/customCallback.py
+2
-2
plotResult.py
src/result/plotResult.py
+2
-2
No files found.
.gitignore
View file @
af35f578
...
...
@@ -55,3 +55,5 @@ assets/Bilder/*
assets/Bilder/AktuelleTrainingsUndTestdaten/*
assets/Bilder/Datengrundlage/*
assets/Bilder/Datengrundlage/
/ressources/results/
/ressources/results/*
main.py
View file @
af35f578
...
...
@@ -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_m
odel
(
model
,
test_it
)
evaluate_m
etrics
=
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
...
...
ressources/config/config.yaml
View file @
af35f578
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/"
src/data/copyData.py
View file @
af35f578
...
...
@@ -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
...
...
src/data/modelData.py
View file @
af35f578
...
...
@@ -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
src/knn/createKNN.py
View file @
af35f578
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
(
'>
%.3
f'
%
(
metrics
*
100.0
))
loss
,
accuracy
,
recall_0
,
recall_1
=
all_metrics
print
(
'loss:
%.3
f'
%
loss
+
' - accuracy:
%.3
f'
%
accuracy
+
' - recall_0:
%.3
f'
%
recall_0
+
' - recall_1:
%.3
f'
%
recall_1
)
return
all_metrics
src/knn/defineKNN.py
View file @
af35f578
...
...
@@ -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
...
...
src/result/createExcelFile.py
View file @
af35f578
...
...
@@ -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
# S
etzen 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
# S
paltenbreite 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
src/result/customCallback.py
View file @
af35f578
...
...
@@ -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
)
src/result/plotResult.py
View file @
af35f578
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment