Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ICV-mmengine_basecode
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
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
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
Florian Schiffel
ICV-mmengine_basecode
Commits
193b7fdf
Unverified
Commit
193b7fdf
authored
1 year ago
by
Zaida Zhou
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
[Refactor] Let unit tests not affect each other (#1169)
parent
5d4e7214
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
mmengine/hooks/checkpoint_hook.py
+1
-1
1 addition, 1 deletion
mmengine/hooks/checkpoint_hook.py
tests/test_hooks/test_checkpoint_hook.py
+45
-21
45 additions, 21 deletions
tests/test_hooks/test_checkpoint_hook.py
with
46 additions
and
22 deletions
mmengine/hooks/checkpoint_hook.py
+
1
−
1
View file @
193b7fdf
...
@@ -85,7 +85,7 @@ class CheckpointHook(Hook):
...
@@ -85,7 +85,7 @@ class CheckpointHook(Hook):
accordingly.
accordingly.
backend_args (dict, optional): Arguments to instantiate the
backend_args (dict, optional): Arguments to instantiate the
prefix of uri corresponding backend. Defaults to None.
prefix of uri corresponding backend. Defaults to None.
New in v0.2.0.
`
New in v
ersion
0.2.0.
`
published_keys (str, List[str], optional): If ``save_last`` is ``True``
published_keys (str, List[str], optional): If ``save_last`` is ``True``
or ``save_best`` is not ``None``, it will automatically
or ``save_best`` is not ``None``, it will automatically
publish model with keys in the list after training.
publish model with keys in the list after training.
...
...
This diff is collapsed.
Click to expand it.
tests/test_hooks/test_checkpoint_hook.py
+
45
−
21
View file @
193b7fdf
...
@@ -429,34 +429,37 @@ class TestCheckpointHook(RunnerTestCase):
...
@@ -429,34 +429,37 @@ class TestCheckpointHook(RunnerTestCase):
@parameterized.expand
([[
'
iter
'
],
[
'
epoch
'
]])
@parameterized.expand
([[
'
iter
'
],
[
'
epoch
'
]])
def
test_with_runner
(
self
,
training_type
):
def
test_with_runner
(
self
,
training_type
):
# Test interval in epoch based training
common_cfg
=
getattr
(
self
,
f
'
{
training_type
}
_based_cfg
'
)
save_iterval
=
2
setattr
(
common_cfg
.
train_cfg
,
f
'
max_
{
training_type
}
s
'
,
11
)
cfg
=
copy
.
deepcopy
(
getattr
(
self
,
f
'
{
training_type
}
_based_cfg
'
))
setattr
(
cfg
.
train_cfg
,
f
'
max_
{
training_type
}
s
'
,
11
)
checkpoint_cfg
=
dict
(
checkpoint_cfg
=
dict
(
type
=
'
CheckpointHook
'
,
type
=
'
CheckpointHook
'
,
interval
=
save_iterval
,
interval
=
2
,
by_epoch
=
training_type
==
'
epoch
'
)
by_epoch
=
training_type
==
'
epoch
'
)
cfg
.
default_hooks
=
dict
(
checkpoint
=
checkpoint_cfg
)
common_cfg
.
default_hooks
=
dict
(
checkpoint
=
checkpoint_cfg
)
# Test interval in epoch based training
cfg
=
copy
.
deepcopy
(
common_cfg
)
runner
=
self
.
build_runner
(
cfg
)
runner
=
self
.
build_runner
(
cfg
)
runner
.
train
()
runner
.
train
()
for
i
in
range
(
1
,
11
):
for
i
in
range
(
1
,
11
):
if
i
==
0
:
self
.
assertEqual
(
self
.
assertFalse
(
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_
{
i
}
.pth
'
)),
osp
.
isfile
(
i
%
2
==
0
)
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_
{
i
}
.pth
'
)))
if
i
%
2
==
0
:
self
.
assertTrue
(
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_
{
i
}
.pth
'
)))
# save_last=True
self
.
assertTrue
(
self
.
assertTrue
(
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_11.pth
'
)))
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_11.pth
'
)))
self
.
clear_work_dir
()
# Test save_optimizer=False
# Test save_optimizer=False
cfg
=
copy
.
deepcopy
(
common_cfg
)
runner
=
self
.
build_runner
(
cfg
)
runner
.
train
()
ckpt
=
torch
.
load
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_11.pth
'
))
ckpt
=
torch
.
load
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_11.pth
'
))
self
.
assertIn
(
'
optimizer
'
,
ckpt
)
self
.
assertIn
(
'
optimizer
'
,
ckpt
)
cfg
.
default_hooks
.
checkpoint
.
save_optimizer
=
False
cfg
.
default_hooks
.
checkpoint
.
save_optimizer
=
False
runner
=
self
.
build_runner
(
cfg
)
runner
=
self
.
build_runner
(
cfg
)
runner
.
train
()
runner
.
train
()
...
@@ -464,6 +467,7 @@ class TestCheckpointHook(RunnerTestCase):
...
@@ -464,6 +467,7 @@ class TestCheckpointHook(RunnerTestCase):
self
.
assertNotIn
(
'
optimizer
'
,
ckpt
)
self
.
assertNotIn
(
'
optimizer
'
,
ckpt
)
# Test save_param_scheduler=False
# Test save_param_scheduler=False
cfg
=
copy
.
deepcopy
(
common_cfg
)
cfg
.
param_scheduler
=
[
cfg
.
param_scheduler
=
[
dict
(
dict
(
type
=
'
LinearLR
'
,
type
=
'
LinearLR
'
,
...
@@ -483,7 +487,10 @@ class TestCheckpointHook(RunnerTestCase):
...
@@ -483,7 +487,10 @@ class TestCheckpointHook(RunnerTestCase):
ckpt
=
torch
.
load
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_11.pth
'
))
ckpt
=
torch
.
load
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_11.pth
'
))
self
.
assertNotIn
(
'
param_schedulers
'
,
ckpt
)
self
.
assertNotIn
(
'
param_schedulers
'
,
ckpt
)
self
.
clear_work_dir
()
# Test out_dir
# Test out_dir
cfg
=
copy
.
deepcopy
(
common_cfg
)
out_dir
=
osp
.
join
(
self
.
temp_dir
.
name
,
'
out_dir
'
)
out_dir
=
osp
.
join
(
self
.
temp_dir
.
name
,
'
out_dir
'
)
cfg
.
default_hooks
.
checkpoint
.
out_dir
=
out_dir
cfg
.
default_hooks
.
checkpoint
.
out_dir
=
out_dir
runner
=
self
.
build_runner
(
cfg
)
runner
=
self
.
build_runner
(
cfg
)
...
@@ -493,37 +500,54 @@ class TestCheckpointHook(RunnerTestCase):
...
@@ -493,37 +500,54 @@ class TestCheckpointHook(RunnerTestCase):
osp
.
join
(
out_dir
,
osp
.
basename
(
cfg
.
work_dir
),
osp
.
join
(
out_dir
,
osp
.
basename
(
cfg
.
work_dir
),
f
'
{
training_type
}
_11.pth
'
)))
f
'
{
training_type
}
_11.pth
'
)))
# Test max_keep_ckpts.
self
.
clear_work_dir
()
del
cfg
.
default_hooks
.
checkpoint
.
out_dir
# Test max_keep_ckpts
cfg
=
copy
.
deepcopy
(
common_cfg
)
cfg
.
default_hooks
.
checkpoint
.
interval
=
1
cfg
.
default_hooks
.
checkpoint
.
max_keep_ckpts
=
1
cfg
.
default_hooks
.
checkpoint
.
max_keep_ckpts
=
1
runner
=
self
.
build_runner
(
cfg
)
runner
=
self
.
build_runner
(
cfg
)
runner
.
train
()
runner
.
train
()
print
(
os
.
listdir
(
cfg
.
work_dir
))
self
.
assertTrue
(
self
.
assertTrue
(
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_1
0
.pth
'
)))
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_1
1
.pth
'
)))
for
i
in
range
(
1
0
):
for
i
in
range
(
1
1
):
self
.
assertFalse
(
self
.
assertFalse
(
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_
{
i
}
.pth
'
)))
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
f
'
{
training_type
}
_
{
i
}
.pth
'
)))
self
.
clear_work_dir
()
# Test filename_tmpl
# Test filename_tmpl
cfg
=
copy
.
deepcopy
(
common_cfg
)
cfg
.
default_hooks
.
checkpoint
.
filename_tmpl
=
'
test_{}.pth
'
cfg
.
default_hooks
.
checkpoint
.
filename_tmpl
=
'
test_{}.pth
'
runner
=
self
.
build_runner
(
cfg
)
runner
=
self
.
build_runner
(
cfg
)
runner
.
train
()
runner
.
train
()
self
.
assertTrue
(
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
'
test_10.pth
'
)))
self
.
assertTrue
(
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
'
test_11.pth
'
)))
self
.
clear_work_dir
()
# Test save_best
# Test save_best
cfg
=
copy
.
deepcopy
(
common_cfg
)
cfg
.
default_hooks
.
checkpoint
.
interval
=
1
cfg
.
default_hooks
.
checkpoint
.
save_best
=
'
test/acc
'
cfg
.
default_hooks
.
checkpoint
.
save_best
=
'
test/acc
'
cfg
.
val_evaluator
=
dict
(
type
=
'
TriangleMetric
'
,
length
=
11
)
cfg
.
val_evaluator
=
dict
(
type
=
'
TriangleMetric
'
,
length
=
11
)
cfg
.
train_cfg
.
val_interval
=
1
cfg
.
train_cfg
.
val_interval
=
1
runner
=
self
.
build_runner
(
cfg
)
runner
=
self
.
build_runner
(
cfg
)
runner
.
train
()
runner
.
train
()
self
.
assertTrue
(
best_ckpt
=
osp
.
join
(
cfg
.
work_dir
,
osp
.
isfile
(
osp
.
join
(
cfg
.
work_dir
,
'
best_test_acc_test_5.pth
'
)))
f
'
best_test_acc_
{
training_type
}
_5.pth
'
)
self
.
assertTrue
(
osp
.
isfile
(
best_ckpt
))
self
.
clear_work_dir
()
# test save published keys
# test save published keys
cfg
=
copy
.
deepcopy
(
common_cfg
)
cfg
.
default_hooks
.
checkpoint
.
published_keys
=
[
'
meta
'
,
'
state_dict
'
]
cfg
.
default_hooks
.
checkpoint
.
published_keys
=
[
'
meta
'
,
'
state_dict
'
]
runner
=
self
.
build_runner
(
cfg
)
runner
=
self
.
build_runner
(
cfg
)
runner
.
train
()
runner
.
train
()
ckpt_files
=
os
.
listdir
(
runner
.
work_dir
)
ckpt_files
=
os
.
listdir
(
runner
.
work_dir
)
self
.
assertTrue
(
self
.
assertTrue
(
any
(
re
.
findall
(
r
'
-[\d\w]{8}\.pth
'
,
file
)
for
file
in
ckpt_files
))
any
(
re
.
findall
(
r
'
-[\d\w]{8}\.pth
'
,
file
)
for
file
in
ckpt_files
))
self
.
clear_work_dir
()
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