Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
mednet
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
medai
software
mednet
Commits
1099d715
Commit
1099d715
authored
1 year ago
by
Daniel CARRON
Browse files
Options
Downloads
Patches
Plain Diff
Moved alexnet model to lightning
parent
4f3105a7
No related branches found
No related tags found
1 merge request
!4
Moved code to lightning
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/ptbench/configs/models/alexnet.py
+9
-9
9 additions, 9 deletions
src/ptbench/configs/models/alexnet.py
src/ptbench/models/alexnet.py
+71
-16
71 additions, 16 deletions
src/ptbench/models/alexnet.py
with
80 additions
and
25 deletions
src/ptbench/configs/models/alexnet.py
+
9
−
9
View file @
1099d715
...
...
@@ -4,19 +4,19 @@
"""
AlexNet.
"""
from
torch
import
empty
from
torch.nn
import
BCEWithLogitsLoss
from
torch.optim
import
SGD
from
...models.alexnet
import
build_a
lexnet
from
...models.alexnet
import
A
lexnet
# config
lr
=
0.01
# model
model
=
build_alexnet
(
pretrained
=
False
)
optimizer_configs
=
{
"
lr
"
:
0.01
,
"
momentum
"
:
0.1
}
# optimizer
optimizer
=
SGD
(
model
.
parameters
(),
lr
=
lr
,
momentum
=
0.1
)
optimizer
=
"
SGD
"
# criterion
criterion
=
BCEWithLogitsLoss
()
criterion
=
BCEWithLogitsLoss
(
pos_weight
=
empty
(
1
))
criterion_valid
=
BCEWithLogitsLoss
(
pos_weight
=
empty
(
1
))
# model
model
=
Alexnet
(
criterion
,
criterion_valid
,
optimizer
,
optimizer_configs
)
This diff is collapsed.
Click to expand it.
src/ptbench/models/alexnet.py
+
71
−
16
View file @
1099d715
...
...
@@ -2,29 +2,45 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later
from
collections
import
OrderedDict
import
pytorch_lightning
as
pl
import
torch
import
torch.nn
as
nn
import
torchvision.models
as
models
from
.normalizer
import
TorchVisionNormalizer
class
Alexnet
(
nn
.
Module
):
class
Alexnet
(
pl
.
Lightning
Module
):
"""
Alexnet module.
Note: only usable with a normalized dataset
"""
def
__init__
(
self
,
pretrained
=
False
):
def
__init__
(
self
,
criterion
,
criterion_valid
,
optimizer
,
optimizer_configs
,
pretrained
=
False
,
):
super
().
__init__
()
self
.
save_hyperparameters
()
self
.
criterion
=
criterion
self
.
criterion_valid
=
criterion_valid
self
.
name
=
"
AlexNet
"
# Load pretrained model
weights
=
(
None
if
pretrained
is
False
else
models
.
AlexNet_Weights
.
DEFAULT
)
self
.
model_ft
=
models
.
alexnet
(
weights
=
weights
)
self
.
normalizer
=
TorchVisionNormalizer
(
nb_channels
=
1
)
# Adapt output features
self
.
model_ft
.
classifier
[
4
]
=
nn
.
Linear
(
4096
,
512
)
self
.
model_ft
.
classifier
[
6
]
=
nn
.
Linear
(
512
,
1
)
...
...
@@ -44,20 +60,59 @@ class Alexnet(nn.Module):
tensor : :py:class:`torch.Tensor`
"""
return
self
.
model_ft
(
x
)
x
=
self
.
normalizer
(
x
)
x
=
self
.
model_ft
(
x
)
return
x
def
build_alexnet
(
pretrained
=
False
):
"""
Build Alexnet CNN.
def
training_step
(
self
,
batch
,
batch_idx
):
images
=
batch
[
1
]
labels
=
batch
[
2
]
Returns
-------
# Increase label dimension if too low
# Allows single and multiclass usage
if
labels
.
ndim
==
1
:
labels
=
torch
.
reshape
(
labels
,
(
labels
.
shape
[
0
],
1
))
module : :py:class:`torch.nn.Module`
"""
model
=
Alexnet
(
pretrained
=
pretrained
)
model
=
[(
"
normalizer
"
,
TorchVisionNormalizer
()),
(
"
model
"
,
model
)]
model
=
nn
.
Sequential
(
OrderedDict
(
model
))
# Forward pass on the network
outputs
=
self
(
images
)
training_loss
=
self
.
criterion
(
outputs
,
labels
.
double
())
return
{
"
loss
"
:
training_loss
}
def
validation_step
(
self
,
batch
,
batch_idx
):
images
=
batch
[
1
]
labels
=
batch
[
2
]
# Increase label dimension if too low
# Allows single and multiclass usage
if
labels
.
ndim
==
1
:
labels
=
torch
.
reshape
(
labels
,
(
labels
.
shape
[
0
],
1
))
# data forwarding on the existing network
outputs
=
self
(
images
)
validation_loss
=
self
.
criterion_valid
(
outputs
,
labels
.
double
())
return
{
"
validation_loss
"
:
validation_loss
}
def
predict_step
(
self
,
batch
,
batch_idx
,
grad_cams
=
False
):
names
=
batch
[
0
]
images
=
batch
[
1
]
outputs
=
self
(
images
)
probabilities
=
torch
.
sigmoid
(
outputs
)
# necessary check for HED architecture that uses several outputs
# for loss calculation instead of just the last concatfuse block
if
isinstance
(
outputs
,
list
):
outputs
=
outputs
[
-
1
]
return
names
[
0
],
torch
.
flatten
(
probabilities
),
torch
.
flatten
(
batch
[
2
])
def
configure_optimizers
(
self
):
optimizer
=
getattr
(
torch
.
optim
,
self
.
hparams
.
optimizer
)(
self
.
parameters
(),
**
self
.
hparams
.
optimizer_configs
)
model
.
name
=
"
AlexNet
"
return
model
return
optimizer
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