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
6290c1bd
Commit
6290c1bd
authored
1 year ago
by
Daniel CARRON
Browse files
Options
Downloads
Patches
Plain Diff
Moved densenet to lightning
parent
a61efdb4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
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/densenet.py
+11
-8
11 additions, 8 deletions
src/ptbench/configs/models/densenet.py
src/ptbench/models/densenet.py
+62
-20
62 additions, 20 deletions
src/ptbench/models/densenet.py
with
73 additions
and
28 deletions
src/ptbench/configs/models/densenet.py
+
11
−
8
View file @
6290c1bd
...
...
@@ -4,19 +4,22 @@
"""
DenseNet.
"""
from
torch
import
empty
from
torch.nn
import
BCEWithLogitsLoss
from
torch.optim
import
Adam
from
...models.densenet
import
build_d
ensenet
from
...models.densenet
import
D
ensenet
# config
lr
=
0.0001
# model
model
=
build_densenet
(
pretrained
=
False
)
optimizer_configs
=
{
"
lr
"
:
0.0001
}
# optimizer
optimizer
=
Adam
(
model
.
parameters
(),
lr
=
lr
)
optimizer
=
"
Adam
"
# criterion
criterion
=
BCEWithLogitsLoss
()
criterion
=
BCEWithLogitsLoss
(
pos_weight
=
empty
(
1
))
criterion_valid
=
BCEWithLogitsLoss
(
pos_weight
=
empty
(
1
))
# model
model
=
Densenet
(
criterion
,
criterion_valid
,
optimizer
,
optimizer_configs
,
pretrained
=
False
)
This diff is collapsed.
Click to expand it.
src/ptbench/models/densenet.py
+
62
−
20
View file @
6290c1bd
...
...
@@ -2,23 +2,40 @@
#
# 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
Densenet
(
nn
.
Module
):
class
Densenet
(
pl
.
Lightning
Module
):
"""
Densenet module.
Note: only usable with a normalized dataset
"""
def
__init__
(
self
,
pretrained
=
False
):
def
__init__
(
self
,
criterion
,
criterion_valid
,
optimizer
,
optimizer_params
,
pretrained
=
False
,
nb_channels
=
3
,
):
super
().
__init__
()
self
.
save_hyperparameters
()
self
.
name
=
"
Densenet
"
self
.
criterion
=
criterion
self
.
criterion_valid
=
criterion_valid
self
.
normalizer
=
TorchVisionNormalizer
(
nb_channels
=
nb_channels
)
# Load pretrained model
weights
=
None
if
not
pretrained
else
models
.
DenseNet121_Weights
.
DEFAULT
self
.
model_ft
=
models
.
densenet121
(
weights
=
weights
)
...
...
@@ -43,23 +60,48 @@ class Densenet(nn.Module):
tensor : :py:class:`torch.Tensor`
"""
return
self
.
model_ft
(
x
)
x
=
self
.
normalizer
(
x
)
def
build_densenet
(
pretrained
=
False
,
nb_channels
=
3
):
"""
Build Densenet CNN.
x
=
self
.
model_ft
(
x
)
Returns
-------
return
x
module : :py:class:`torch.nn.Module`
"""
model
=
Densenet
(
pretrained
=
pretrained
)
model
=
[
(
"
normalizer
"
,
TorchVisionNormalizer
(
nb_channels
=
nb_channels
)),
(
"
model
"
,
model
),
]
model
=
nn
.
Sequential
(
OrderedDict
(
model
))
model
.
name
=
"
Densenet
"
return
model
def
training_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
))
# 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
configure_optimizers
(
self
):
# Dynamically instantiates the optimizer given the configs
optimizer
=
getattr
(
torch
.
optim
,
self
.
hparams
.
optimizer
)(
self
.
parameters
(),
**
self
.
hparams
.
optimizer_params
)
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