Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PySegCNN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
earth_observation_public
PySegCNN
Commits
bac6bc17
Commit
bac6bc17
authored
4 years ago
by
Frisinghelli Daniel
Browse files
Options
Downloads
Patches
Plain Diff
Improved saving method to rebuild model in case of checkpoint or transfer learning
parent
7c1732db
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pytorch/models.py
+31
-7
31 additions, 7 deletions
pytorch/models.py
with
31 additions
and
7 deletions
pytorch/models.py
+
31
−
7
View file @
bac6bc17
...
...
@@ -35,20 +35,37 @@ class Network(nn.Module):
for
param
in
self
.
parameters
():
param
.
requires_grad
=
True
def
save
(
self
,
optimizer
,
state_file
,
def
save
(
self
,
state_file
,
optimizer
,
bands
,
outpath
=
os
.
path
.
join
(
os
.
getcwd
(),
'
_models
'
)):
# check if the output path exists and if not, create it
if
not
os
.
path
.
isdir
(
outpath
):
os
.
makedirs
(
outpath
,
exist_ok
=
True
)
# create a dictionary that stores the model state
model_state
=
{
'
epoch
'
:
self
.
epoch
,
'
model_state_dict
'
:
self
.
state_dict
(),
'
optim_state_dict
'
:
optimizer
.
state_dict
()
# initialize dictionary to store network parameters
model_state
=
{}
# store input bands
model_state
[
'
bands
'
]
=
bands
# store construction parameters to instanciate the network
model_state
[
'
params
'
]
=
{
'
skip
'
:
self
.
skip
,
'
filters
'
:
self
.
nfilters
,
'
nclasses
'
:
self
.
nclasses
,
'
in_channels
'
:
self
.
in_channels
}
# store optional keyword arguments
model_state
[
'
kwargs
'
]
=
self
.
kwargs
# store model epoch
model_state
[
'
epoch
'
]
=
self
.
epoch
# store model and optimizer state
model_state
[
'
model_state_dict
'
]
=
self
.
state_dict
()
model_state
[
'
optim_state_dict
'
]
=
optimizer
.
state_dict
()
# model state dictionary stores the values of all trainable parameters
state
=
os
.
path
.
join
(
outpath
,
state_file
)
torch
.
save
(
model_state
,
state
)
...
...
@@ -87,9 +104,16 @@ class UNet(Network):
# number of classes
self
.
nclasses
=
nclasses
# get the configuration for the convolutional layers of the encoder
# configuration of the convolutional layers in the network
self
.
kwargs
=
kwargs
self
.
nfilters
=
filters
# convolutional layers of the encoder
self
.
filters
=
np
.
hstack
([
np
.
array
(
in_channels
),
np
.
array
(
filters
)])
# whether to apply skip connections
self
.
skip
=
skip
# number of epochs trained
self
.
epoch
=
0
...
...
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