Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pseudo_image
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Léo Calmettes
pseudo_image
Commits
38ca8ef3
Commit
38ca8ef3
authored
2 months ago
by
Schneider Leo
Browse files
Options
Downloads
Patches
Plain Diff
model cuda loading
parent
4efef23b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.py
+42
-3
42 additions, 3 deletions
main.py
requirements.txt
+2
-1
2 additions, 1 deletion
requirements.txt
with
44 additions
and
4 deletions
main.py
+
42
−
3
View file @
38ca8ef3
import
matplotlib.pyplot
as
plt
import
numpy
as
np
from
config.config
import
load_args
from
dataset.dataset
import
load_data
...
...
@@ -6,7 +7,9 @@ import torch
import
torch.nn
as
nn
from
models.model
import
Classification_model
import
torch.optim
as
optim
from
sklearn.metrics
import
confusion_matrix
import
seaborn
as
sn
import
pandas
as
pd
...
...
@@ -31,7 +34,7 @@ def train(model, data_train, optimizer, loss_function, epoch):
optimizer
.
step
()
losses
=
losses
/
len
(
data_train
.
dataset
)
acc
=
acc
/
len
(
data_train
.
dataset
)
print
(
'
T
rain
epoch
'
,
epoch
,
'
loss :
'
,
losses
,
'
acc :
'
,
acc
)
print
(
'
T
est
epoch
{}
, loss :
{:.3f} acc : {:.3f}
'
.
format
(
epoch
,
losses
,
acc
)
)
return
losses
,
acc
def
test
(
model
,
data_test
,
loss_function
,
epoch
):
...
...
@@ -52,7 +55,7 @@ def test(model, data_test, loss_function, epoch):
losses
+=
loss
.
item
()
losses
=
losses
/
len
(
data_test
.
dataset
)
acc
=
acc
/
len
(
data_test
.
dataset
)
print
(
'
Test epoch
'
,
epoch
,
'
loss :
'
,
losses
,
'
acc :
'
,
acc
)
print
(
'
Test epoch
{}, loss : {:.3f} acc : {:.3f}
'
.
format
(
epoch
,
losses
,
acc
)
)
return
losses
,
acc
def
run
(
args
):
...
...
@@ -85,8 +88,44 @@ def run(args):
plt
.
plot
(
val_acc
)
plt
.
plot
(
train_acc
)
plt
.
plot
(
train_acc
)
plt
.
show
()
plt
.
savefig
(
'
output/training_plot.png
'
)
load_model
(
model
,
args
.
save_path
)
make_prediction
(
model
,
data_test
)
def
make_prediction
(
model
,
data
):
y_pred
=
[]
y_true
=
[]
# iterate over test data
for
im
,
label
in
data
:
label
=
label
.
long
()
if
torch
.
cuda
.
is_available
():
im
=
im
.
cuda
()
output
=
model
(
im
)
output
=
(
torch
.
max
(
torch
.
exp
(
output
),
1
)[
1
]).
data
.
cpu
().
numpy
()
y_pred
.
extend
(
output
)
label
=
label
.
data
.
cpu
().
numpy
()
y_true
.
extend
(
label
)
# Save Truth
# constant for classes
classes
=
data
.
dataset
.
dataset
.
classes
# Build confusion matrix
cf_matrix
=
confusion_matrix
(
y_true
,
y_pred
)
df_cm
=
pd
.
DataFrame
(
cf_matrix
/
np
.
sum
(
cf_matrix
,
axis
=
1
)[:,
None
],
index
=
[
i
for
i
in
classes
],
columns
=
[
i
for
i
in
classes
])
plt
.
figure
(
figsize
=
(
12
,
7
))
sn
.
heatmap
(
df_cm
,
annot
=
True
)
plt
.
savefig
(
'
output.png
'
)
def
save_model
(
model
,
path
):
print
(
'
Model saved
'
)
torch
.
save
(
model
.
state_dict
(),
path
)
def
load_model
(
model
,
path
):
...
...
This diff is collapsed.
Click to expand it.
requirements.txt
+
2
−
1
View file @
38ca8ef3
...
...
@@ -5,4 +5,5 @@ pyopenms~=3.3.0
openpyxl
torch
~=2.6.0
torchvision
~=0.21.0
pillow
~=11.1.0
\ No newline at end of file
pillow
~=11.1.0
seaborn
~=0.13.2
\ No newline at end of file
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