Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COM3015 Group Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package 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
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Trewern, James R (PG/R - Comp Sci & Elec Eng)
COM3015 Group Project
Commits
974a21bf
Commit
974a21bf
authored
2 years ago
by
Graves, Conor S (UG - Comp Sci & Elec Eng)
Browse files
Options
Downloads
Patches
Plain Diff
Added confusion matrix evaluation
parent
ca3dcb8e
Branches
master
No related tags found
1 merge request
!9
Evaluation merge
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
evaluation.py
+51
-3
51 additions, 3 deletions
evaluation.py
main.py
+6
-3
6 additions, 3 deletions
main.py
with
57 additions
and
6 deletions
evaluation.py
+
51
−
3
View file @
974a21bf
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
import
torch
import
torch
import
torch.nn
as
nn
import
torch.nn
as
nn
from
sklearn.metrics
import
accuracy_score
,
classification_report
from
sklearn.metrics
import
accuracy_score
,
classification_report
,
confusion_matrix
import
torch.optim
as
optim
import
torch.optim
as
optim
import
numpy
as
np
device
=
torch
.
device
(
'
cuda
'
if
torch
.
cuda
.
is_available
()
else
'
cpu
'
)
device
=
torch
.
device
(
'
cuda
'
if
torch
.
cuda
.
is_available
()
else
'
cpu
'
)
def
setupEval
(
model_run
,
model
):
def
setupEval
(
model_run
,
model
):
...
@@ -10,7 +11,7 @@ def setupEval(model_run, model):
...
@@ -10,7 +11,7 @@ def setupEval(model_run, model):
model
.
load_state_dict
(
torch
.
load
(
path
,
map_location
=
torch
.
device
(
device
)))
model
.
load_state_dict
(
torch
.
load
(
path
,
map_location
=
torch
.
device
(
device
)))
model
.
eval
()
model
.
eval
()
def
printM
etrics
(
model_run
,
model
,
val_loader
):
def
m
etrics
(
model_run
,
model
,
val_loader
):
setupEval
(
model_run
,
model
)
setupEval
(
model_run
,
model
)
# Set loss function and optimizer
# Set loss function and optimizer
...
@@ -53,4 +54,51 @@ def printMetrics(model_run, model, val_loader):
...
@@ -53,4 +54,51 @@ def printMetrics(model_run, model, val_loader):
plt
.
plot
(
val_accuracy
,
label
=
"
Accuracy
"
)
plt
.
plot
(
val_accuracy
,
label
=
"
Accuracy
"
)
plt
.
xlabel
(
"
Epoch
"
)
plt
.
xlabel
(
"
Epoch
"
)
plt
.
legend
()
plt
.
legend
()
plt
.
show
()
plt
.
show
()
\ No newline at end of file
def
confMtrx
(
model_run
,
model
,
train_loader
,
val_loader
):
setupEval
(
model_run
,
model
)
# initialize empty lists for true labels and predicted labels
train_true_labels
=
[]
train_pred_labels
=
[]
val_true_labels
=
[]
val_pred_labels
=
[]
# get the true and predicted labels for the train_loader
with
torch
.
no_grad
():
for
data
in
train_loader
:
#for i, data in enumerate(train_loader):
#if i == 20:
#break
inputs
,
labels
=
data
inputs
,
labels
=
inputs
.
to
(
device
),
labels
.
to
(
device
)
outputs
=
model
(
inputs
)
_
,
predicted
=
torch
.
max
(
outputs
.
data
,
1
)
train_true_labels
+=
labels
.
tolist
()
train_pred_labels
+=
predicted
.
tolist
()
# get the true and predicted labels for the val_loader
with
torch
.
no_grad
():
#for data in val_loader:
for
i
,
data
in
enumerate
(
val_loader
):
if
i
==
20
:
break
inputs
,
labels
=
data
inputs
,
labels
=
inputs
.
to
(
device
),
labels
.
to
(
device
)
outputs
=
model
(
inputs
)
_
,
predicted
=
torch
.
max
(
outputs
.
data
,
1
)
val_true_labels
+=
labels
.
tolist
()
val_pred_labels
+=
predicted
.
tolist
()
# define the labels for the confusion matrix
labels
=
[
'
akiec
'
,
'
bcc
'
,
'
bkl
'
,
'
df
'
,
'
mel
'
,
'
nv
'
,
'
vasc
'
]
# create the confusion matrices for train and val sets
train_conf_matrix
=
confusion_matrix
(
train_true_labels
,
train_pred_labels
,
labels
=
labels
)
val_conf_matrix
=
confusion_matrix
(
val_true_labels
,
val_pred_labels
,
labels
=
labels
)
print
(
"
Train Confusion Matrix:
"
)
print
(
train_conf_matrix
)
print
(
"
Val Confusion Matrix:
"
)
print
(
val_conf_matrix
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
main.py
+
6
−
3
View file @
974a21bf
...
@@ -9,15 +9,18 @@ import seg_model
...
@@ -9,15 +9,18 @@ import seg_model
from
constants
import
EPOCHS
,
MODEL_NAME
from
constants
import
EPOCHS
,
MODEL_NAME
from
train
import
train
from
train
import
train
from
model
import
get_model
from
model
import
get_model
from
evaluation
import
printMetrics
from
evaluation
import
metrics
,
confMtrx
def
main
():
def
main
():
model
=
get_model
(
MODEL_NAME
,
7
,
False
)
model
=
get_model
(
MODEL_NAME
,
7
,
False
)
train_loader
,
val_loader
=
datahandler
.
getHamDataLoaders
()
train_loader
,
val_loader
=
datahandler
.
getHamDataLoaders
()
#train(train_loader, val_loader, model, EPOCHS)
#train(train_loader, val_loader, model, EPOCHS)
model_run
=
"
densenet121_run4
"
#Evaluation: Uncomment below
printMetrics
(
model_run
,
model
,
val_loader
)
model_run
=
"
densenet121_run20
"
#metrics(model_run, model, val_loader)
#confMtrx(model_run, model, train_loader, val_loader)
print
(
"
\n\n
-------------Done----------------
"
)
print
(
"
\n\n
-------------Done----------------
"
)
...
...
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