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
9e721cb3
Commit
9e721cb3
authored
1 year ago
by
André Anjos
Browse files
Options
Downloads
Patches
Plain Diff
[models] Remove runtime checks; Use torchvision normaliser instead of our own
parent
3da537d0
No related branches found
No related tags found
1 merge request
!7
Reviewed DataModule design+docs+types
Pipeline
#75473
failed
1 year ago
Stage: qa
Stage: test
Stage: doc
Stage: dist
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/ptbench/models/densenet.py
+5
-10
5 additions, 10 deletions
src/ptbench/models/densenet.py
src/ptbench/models/normalizer.py
+37
-43
37 additions, 43 deletions
src/ptbench/models/normalizer.py
src/ptbench/models/pasa.py
+4
-9
4 additions, 9 deletions
src/ptbench/models/pasa.py
with
46 additions
and
62 deletions
src/ptbench/models/densenet.py
+
5
−
10
View file @
9e721cb3
...
...
@@ -7,8 +7,6 @@ import torch
import
torch.nn
as
nn
import
torchvision.models
as
models
from
.normalizer
import
TorchVisionNormalizer
class
Densenet
(
pl
.
LightningModule
):
"""
Densenet module.
...
...
@@ -31,7 +29,7 @@ class Densenet(pl.LightningModule):
self
.
name
=
"
Densenet
"
self
.
normalizer
=
TorchVisionNormalizer
(
nb_channels
=
nb_channels
)
self
.
normalizer
=
None
# Load pretrained model
weights
=
None
if
not
pretrained
else
models
.
DenseNet121_Weights
.
DEFAULT
...
...
@@ -55,16 +53,13 @@ class Densenet(pl.LightningModule):
imagenet weights, during contruction).
"""
if
self
.
pretrained
:
from
.normalizer
import
TorchVisionN
ormalizer
from
.normalizer
import
make_imagenet_n
ormalizer
self
.
normalizer
=
TorchVisionNormalizer
(
torch
.
Tensor
([
0.485
,
0.456
,
0.406
]),
torch
.
Tensor
([
0.229
,
0.224
,
0.225
]),
)
self
.
normalizer
=
make_imagenet_normalizer
()
else
:
from
.normalizer
import
get_znorm
_normalizer
from
.normalizer
import
make_z
_normalizer
self
.
normalizer
=
get_znorm
_normalizer
(
dataloader
)
self
.
normalizer
=
make_z
_normalizer
(
dataloader
)
def
training_step
(
self
,
batch
,
batch_idx
):
images
=
batch
[
1
]
...
...
This diff is collapsed.
Click to expand it.
src/ptbench/models/normalizer.py
+
37
−
43
View file @
9e721cb3
...
...
@@ -2,52 +2,23 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""
A network model that prefixes a
z-normalization
step to any other module.
"""
"""
A network model that prefixes a
subtract/divide
step to any other module.
"""
import
torch
import
torch.nn
import
torch.utils.data
import
torchvision.transforms
class
TorchVisionNormalizer
(
torch
.
nn
.
Module
):
"""
A simple normalizer that applies the standard torchvision normalization.
def
make_z_normalizer
(
dataloader
:
torch
.
utils
.
data
.
DataLoader
,
)
->
torchvision
.
transforms
.
Normalize
:
"""
Computes mean and standard deviation from a dataloader.
This module does not learn.
This function will input a dataloader, and compute the mean and standard
deviation by image channel. It will work for both monochromatic, and color
inputs with 2, 3 or more color planes.
Parameters
----------
nb_channels : :py:class:`int`, Optional
Number of images channels fed to the model
"""
def
__init__
(
self
,
subtract
:
torch
.
Tensor
,
divide
:
torch
.
Tensor
):
super
().
__init__
()
if
len
(
subtract
)
!=
len
(
divide
):
raise
ValueError
(
"
Lengths of
'
subtract
'
and
'
divide
'
tensors should be the same.
"
)
if
len
(
subtract
)
not
in
(
1
,
3
):
raise
ValueError
(
"
Length of
'
subtract
'
tensor should be either 1 or 3, depending on the number of color channels.
"
)
subtract
=
torch
.
as_tensor
(
subtract
)[
None
,
:,
None
,
None
]
divide
=
torch
.
as_tensor
(
divide
)[
None
,
:,
None
,
None
]
self
.
register_buffer
(
"
subtract
"
,
subtract
)
self
.
register_buffer
(
"
divide
"
,
divide
)
self
.
name
=
"
torchvision-normalizer
"
def
forward
(
self
,
inputs
:
torch
.
Tensor
):
"""
inputs shape [batches, planes, height, width]
"""
return
inputs
.
sub
(
self
.
subtract
).
div
(
self
.
divide
)
def
get_znorm_normalizer
(
dataloader
:
torch
.
utils
.
data
.
DataLoader
,
)
->
TorchVisionNormalizer
:
"""
Returns a normalizer with the mean and std computed from a dataloader
'
s
unaugmented training set.
Parameters
----------
...
...
@@ -55,15 +26,22 @@ def get_znorm_normalizer(
dataloader:
A torch Dataloader from which to compute the mean and std
Returns
-------
An initialized
TorchVisionN
ormalizer
An initialized
n
ormalizer
"""
mean
=
0.0
var
=
0.0
# Peek the number of channels of batches in the data loader
batch
=
next
(
iter
(
dataloader
))
channels
=
batch
[
0
].
shape
[
1
]
# Initialises accumulators
mean
=
torch
.
zeros
(
channels
,
dtype
=
batch
[
0
].
dtype
)
var
=
torch
.
zeros
(
channels
,
dtype
=
batch
[
0
].
dtype
)
num_images
=
0
# Evaluates mean and standard deviation
for
batch
in
dataloader
:
data
=
batch
[
0
]
data
=
data
.
view
(
data
.
size
(
0
),
data
.
size
(
1
),
-
1
)
...
...
@@ -76,5 +54,21 @@ def get_znorm_normalizer(
var
/=
num_images
std
=
torch
.
sqrt
(
var
)
normalizer
=
TorchVisionNormalizer
(
mean
,
std
)
return
normalizer
return
torchvision
.
transforms
.
Normalize
(
mean
,
std
)
def
make_imagenet_normalizer
()
->
torchvision
.
transforms
.
Normalize
:
"""
Returns the stock ImageNet normalisation weights from torchvision.
The weights are wrapped in a torch module. This normalizer only works for
**RGB (color) images**.
Returns
-------
An initialized normalizer
"""
return
torchvision
.
transforms
.
Normalize
(
(
0.485
,
0.456
,
0.406
),
(
0.229
,
0.224
,
0.225
)
)
This diff is collapsed.
Click to expand it.
src/ptbench/models/pasa.py
+
4
−
9
View file @
9e721cb3
...
...
@@ -79,12 +79,7 @@ class PASA(pl.LightningModule):
self
.
dense
=
nn
.
Linear
(
80
,
1
)
# Fully connected layer
def
forward
(
self
,
x
):
if
self
.
normalizer
is
None
:
raise
TypeError
(
"
The normalizer has not been initialized. Make sure to call set_normalizer() after creation of the model.
"
)
x
=
self
.
normalizer
(
x
)
x
=
self
.
normalizer
(
x
)
# type: ignore
# First convolution block
_x
=
x
...
...
@@ -140,11 +135,11 @@ class PASA(pl.LightningModule):
dataloader:
A torch Dataloader from which to compute the mean and std
"""
from
.normalizer
import
get_znorm
_normalizer
from
.normalizer
import
make_z
_normalizer
self
.
normalizer
=
get_znorm
_normalizer
(
dataloader
)
self
.
normalizer
=
make_z
_normalizer
(
dataloader
)
def
training_step
(
self
,
batch
,
batch_idx
):
def
training_step
(
self
,
batch
,
_
):
images
=
batch
[
0
]
labels
=
batch
[
1
][
"
label
"
]
...
...
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