Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Troubled Cell Detection
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Laura Christine Kühle
Troubled Cell Detection
Commits
cad015d0
Commit
cad015d0
authored
3 years ago
by
Laura Christine Kühle
Browse files
Options
Downloads
Patches
Plain Diff
Added ANN testing (based on Soraya's implementation).
parent
87920cbb
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
ANN_Training.py
+63
-3
63 additions, 3 deletions
ANN_Training.py
Plotting.py
+24
-0
24 additions, 0 deletions
Plotting.py
with
87 additions
and
3 deletions
ANN_Training.py
+
63
−
3
View file @
cad015d0
...
...
@@ -3,20 +3,23 @@
@author: Laura C. Kühle, Soraya Terrab (sorayaterrab)
TODO: Improve
'
epoch_training()
'
TODO: Add ANN testing from Soraya
TODO: Add ANN testing from Soraya
-> Done
TODO: Add ANN classification from Soraya
TODO: Improve naming of training data/model (maybe different folders?)
TODO: Adjust input file naming to fit training data -> Done
TODO: Change code to add model directory if not existing -> Done
TODO: Remove unnecessary comments -> Done
TODO: Add option to set plot directory
"""
import
numpy
as
np
import
os
import
torch
from
torch.utils.data
import
TensorDataset
,
DataLoader
# from sklearn.metrics import accuracy_score, precision_recall_fscore_support
import
ANN_Model
from
Plotting
import
plot_classification_accuracy
class
ModelTrainer
(
object
):
...
...
@@ -104,6 +107,62 @@ class ModelTrainer(object):
if
valid_loss
/
len
(
valid_dl
)
<
self
.
_threshold
:
break
def
test_model
(
self
):
self
.
epoch_training
()
self
.
_model
.
eval
()
x_test
,
y_test
=
self
.
_training_data
[
'
test
'
]
model_output
=
torch
.
round
(
self
.
_model
(
x_test
.
float
()))
# acc = np.sum(model_output.numpy() == y_test.numpy())
test_accuracy
=
(
model_output
==
y_test
).
float
().
mean
()
print
(
test_accuracy
)
# print(model_output.nelement())
# accuracy1 = torch.sum(torch.eq(model_output, y_test)).item() # /model_output.nelement()
# print(test_accuracy, accuracy1/model_output.nelement())
# print(accuracy1)
tp
,
fp
,
tn
,
fn
=
self
.
_evaluate_classification
(
model_output
,
y_test
)
precision
,
recall
,
accuracy
=
self
.
_evaluate_stats
(
tp
,
fp
,
tn
,
fn
)
# print(precision, recall)
# print(accuracy)
plot_classification_accuracy
(
precision
,
recall
,
accuracy
,
[
'
real
'
])
@staticmethod
def
_evaluate_classification
(
model_output
,
true_output
):
# Positive being Discontinuous/Troubled Cells, Negative being Smooth/Good Cells
true_positive
=
0
true_negative
=
0
false_positive
=
0
false_negative
=
0
for
i
in
range
(
true_output
.
size
()[
0
]):
if
model_output
[
i
,
1
]
==
model_output
[
i
,
0
]:
print
(
i
,
model_output
[
i
])
if
true_output
[
i
,
0
]
==
torch
.
tensor
([
1
]):
if
model_output
[
i
,
0
]
==
true_output
[
i
,
0
]:
true_positive
+=
1
else
:
false_negative
+=
1
if
true_output
[
i
,
1
]
==
torch
.
tensor
([
1
]):
if
model_output
[
i
,
1
]
==
true_output
[
i
,
1
]:
true_negative
+=
1
else
:
false_positive
+=
1
return
true_positive
,
true_negative
,
false_positive
,
false_negative
@staticmethod
def
_evaluate_stats
(
true_positive
,
true_negative
,
false_positive
,
false_negative
):
if
true_positive
+
false_positive
==
0
:
precision
=
0
recall
=
0
else
:
precision
=
true_positive
/
(
true_positive
+
false_positive
)
recall
=
true_positive
/
(
true_positive
+
false_negative
)
accuracy
=
(
true_positive
+
true_negative
)
/
(
true_positive
+
true_negative
+
false_positive
+
false_negative
)
# print(true_positive+true_negative+false_positive+false_negative)
return
precision
,
recall
,
accuracy
def
save_model
(
self
):
# Saving Model
train_name
=
self
.
_training_file
.
split
(
'
.npy
'
)[
0
]
...
...
@@ -124,6 +183,7 @@ class ModelTrainer(object):
# Loss Functions: BCELoss, BCEWithLogitsLoss, CrossEntropyLoss (not working), MSELoss (with reduction='sum')
# Optimizer: Adam, SGD
trainer
=
ModelTrainer
({
'
loss_function
'
:
'
MSELoss
'
,
'
loss_config
'
:
{
'
reduction
'
:
'
sum
'
}})
trainer
.
epoch_training
()
trainer
=
ModelTrainer
({
'
num_epochs
'
:
100
})
# trainer.epoch_training()
trainer
.
test_model
()
trainer
.
save_model
()
This diff is collapsed.
Click to expand it.
Plotting.py
+
24
−
0
View file @
cad015d0
...
...
@@ -2,6 +2,8 @@
"""
@author: Laura C. Kühle
TODO: Give option to select plotting color
"""
import
numpy
as
np
import
matplotlib.pyplot
as
plt
...
...
@@ -112,3 +114,25 @@ def calculate_exact_solution(mesh, cell_len, wave_speed, final_time, interval_le
grid
=
np
.
reshape
(
np
.
array
(
grid
),
(
1
,
len
(
grid
)
*
len
(
grid
[
0
])))
return
grid
,
exact
def
plot_classification_accuracy
(
precision
,
recall
,
accuracy
,
xlabels
):
precision
=
[
precision
]
recall
=
[
recall
]
accuracy
=
[
accuracy
]
pos
=
np
.
arange
(
len
(
xlabels
))
width
=
0.3
fig
=
plt
.
figure
(
'
classification_accuracy
'
)
ax
=
fig
.
add_axes
([
0.15
,
0.1
,
0.75
,
0.8
])
ax
.
bar
(
pos
-
width
,
precision
,
width
,
label
=
'
Precision
'
)
ax
.
bar
(
pos
,
recall
,
width
,
label
=
'
Recall
'
)
ax
.
bar
(
pos
+
width
,
accuracy
,
width
,
label
=
'
Accuracy
'
)
ax
.
set_xticks
(
x
)
ax
.
set_xticklabels
(
xlabels
)
ax
.
set_ylabel
(
'
Classification (%)
'
)
ax
.
set_ylim
(
bottom
=
0.6
)
ax
.
set_ylim
(
top
=
1.02
)
ax
.
set_title
(
'
Non-Normalized Test Data
'
)
ax
.
legend
(
loc
=
'
upper right
'
)
# fig.tight_layout()
fig
.
savefig
(
'
TestAdamPrecisionRecallAccuracy.pdf
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment