Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MLAir
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
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
esde
machine-learning
MLAir
Commits
5843d2f6
Commit
5843d2f6
authored
5 years ago
by
lukas leufen
Browse files
Options
Downloads
Patches
Plain Diff
added docs
parent
0b171807
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!37
include new development
,
!29
Lukas issue030 feat continue training
Pipeline
#28880
passed
5 years ago
Stage: test
Stage: pages
Stage: deploy
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/model_modules/keras_extensions.py
+49
-7
49 additions, 7 deletions
src/model_modules/keras_extensions.py
with
49 additions
and
7 deletions
src/model_modules/keras_extensions.py
+
49
−
7
View file @
5843d2f6
...
@@ -12,10 +12,23 @@ from keras.callbacks import History, ModelCheckpoint
...
@@ -12,10 +12,23 @@ from keras.callbacks import History, ModelCheckpoint
class
HistoryAdvanced
(
History
):
class
HistoryAdvanced
(
History
):
"""
This is almost an identical clone of the original History class. The only difference is that attributes epoch and
history are instantiated during the init phase and not during on_train_begin. This is required to resume an already
started but disrupted training from an saved state. This HistoryAdvanced callback needs to be added separately as
additional callback. To get the full history use this object for further steps instead of the default return of
training methods like fit_generator().
hist = HistoryAdvanced()
history = model.fit_generator(generator=.... , callbacks=[hist])
history = hist
def
__init__
(
self
,
old_epoch
=
None
,
old_history
=
None
):
If training was started from beginning this class is identical to the returned history class object.
self
.
epoch
=
old_epoch
or
[]
"""
self
.
history
=
old_history
or
{}
def
__init__
(
self
):
self
.
epoch
=
[]
self
.
history
=
{}
super
().
__init__
()
super
().
__init__
()
def
on_train_begin
(
self
,
logs
=
None
):
def
on_train_begin
(
self
,
logs
=
None
):
...
@@ -78,20 +91,46 @@ class LearningRateDecay(History):
...
@@ -78,20 +91,46 @@ class LearningRateDecay(History):
class
ModelCheckpointAdvanced
(
ModelCheckpoint
):
class
ModelCheckpointAdvanced
(
ModelCheckpoint
):
"""
"""
IMPORTANT: Always add the model checkpoint advanced as last callback to properly update all tracked callbacks, e.g.
Enhance the standard ModelCheckpoint class by additional saves of given callbacks. Specify this callbacks as follow:
fit_generator(callbacks=[..., <last_here>])
lr = CustomLearningRate()
hist = CustomHistory()
callbacks_name =
"
your_custom_path_%s.pickle
"
callbacks = [{
"
callback
"
: lr,
"
path
"
: callbacks_name %
"
lr
"
},
{
"
callback
"
: hist,
"
path
"
: callbacks_name %
"
hist
"
}]
ckpt_callbacks = ModelCheckpointAdvanced(filepath=.... , callbacks=callbacks)
Add this ckpt_callbacks as all other additional callbacks to the callback list. IMPORTANT: Always add ckpt_callbacks
as last callback to properly update all tracked callbacks, e.g.
fit_generator(.... , callbacks=[lr, hist, ckpt_callbacks])
"""
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
callbacks
=
kwargs
.
pop
(
"
callbacks
"
)
self
.
callbacks
=
kwargs
.
pop
(
"
callbacks
"
)
super
().
__init__
(
*
args
,
**
kwargs
)
super
().
__init__
(
*
args
,
**
kwargs
)
def
update_best
(
self
,
hist
):
def
update_best
(
self
,
hist
):
"""
Update internal best on resuming a training process. Otherwise best is set to +/- inf depending on the
performance metric and the first trained model (first of the resuming training process) will always saved as
best model because its performance will be better than infinity. To prevent this behaviour and compare the
performance with the best model performance, call this method before resuming the training process.
:param hist: The History object from the previous (interrupted) training.
"""
self
.
best
=
hist
.
history
.
get
(
self
.
monitor
)[
-
1
]
self
.
best
=
hist
.
history
.
get
(
self
.
monitor
)[
-
1
]
def
update_callbacks
(
self
,
callbacks
):
def
update_callbacks
(
self
,
callbacks
):
"""
Update all stored callback objects. The argument callbacks needs to follow the same convention like described
in the class description (list of dictionaries). Must be run before resuming a training process.
"""
self
.
callbacks
=
callbacks
self
.
callbacks
=
callbacks
def
on_epoch_end
(
self
,
epoch
,
logs
=
None
):
def
on_epoch_end
(
self
,
epoch
,
logs
=
None
):
"""
Save model as usual (see ModelCheckpoint class), but also save additional callbacks.
"""
super
().
on_epoch_end
(
epoch
,
logs
)
super
().
on_epoch_end
(
epoch
,
logs
)
for
callback
in
self
.
callbacks
:
for
callback
in
self
.
callbacks
:
...
@@ -100,9 +139,12 @@ class ModelCheckpointAdvanced(ModelCheckpoint):
...
@@ -100,9 +139,12 @@ class ModelCheckpointAdvanced(ModelCheckpoint):
if
self
.
save_best_only
:
if
self
.
save_best_only
:
current
=
logs
.
get
(
self
.
monitor
)
current
=
logs
.
get
(
self
.
monitor
)
if
current
==
self
.
best
:
if
current
==
self
.
best
:
if
self
.
verbose
>
0
:
print
(
'
\n
Epoch %05d: save to %s
'
%
(
epoch
+
1
,
file_path
))
print
(
'
\n
Epoch %05d: save to %s
'
%
(
epoch
+
1
,
file_path
))
with
open
(
file_path
,
"
wb
"
)
as
f
:
with
open
(
file_path
,
"
wb
"
)
as
f
:
pickle
.
dump
(
callback
[
"
callback
"
],
f
)
pickle
.
dump
(
callback
[
"
callback
"
],
f
)
else
:
else
:
with
open
(
file_path
,
"
wb
"
)
as
f
:
with
open
(
file_path
,
"
wb
"
)
as
f
:
if
self
.
verbose
>
0
:
print
(
'
\n
Epoch %05d: save to %s
'
%
(
epoch
+
1
,
file_path
))
pickle
.
dump
(
callback
[
"
callback
"
],
f
)
pickle
.
dump
(
callback
[
"
callback
"
],
f
)
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