Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
MMCV
Commits
ab77af82
"vscode:/vscode.git/clone" did not exist on "8c550d4c85ed15058ab7546ce1307ed875c60862"
Unverified
Commit
ab77af82
authored
Jun 29, 2020
by
Rui Xu
Committed by
GitHub
Jun 29, 2020
Browse files
add set_random_seed and use rank shift feature (#373)
parent
e43fe0e2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
0 deletions
+31
-0
mmcv/runner/utils.py
mmcv/runner/utils.py
+31
-0
No files found.
mmcv/runner/utils.py
View file @
ab77af82
# Copyright (c) Open-MMLab. All rights reserved.
import
os
import
random
import
sys
import
time
from
getpass
import
getuser
from
socket
import
gethostname
import
numpy
as
np
import
torch
import
mmcv
...
...
@@ -48,3 +53,29 @@ def obj_from_dict(info, parent=None, default_args=None):
for
name
,
value
in
default_args
.
items
():
args
.
setdefault
(
name
,
value
)
return
obj_type
(
**
args
)
def
set_random_seed
(
seed
,
deterministic
=
False
,
use_rank_shift
=
False
):
"""Set random seed.
Args:
seed (int): Seed to be used.
deterministic (bool): Whether to set the deterministic option for
CUDNN backend, i.e., set `torch.backends.cudnn.deterministic`
to True and `torch.backends.cudnn.benchmark` to False.
Default: False.
rank_shift (bool): Whether to add rank number to the random seed to
have different random seed in different threads. Default: False.
"""
if
use_rank_shift
:
rank
,
_
=
mmcv
.
runner
.
get_dist_info
()
seed
+=
rank
random
.
seed
(
seed
)
np
.
random
.
seed
(
seed
)
torch
.
manual_seed
(
seed
)
torch
.
cuda
.
manual_seed
(
seed
)
torch
.
cuda
.
manual_seed_all
(
seed
)
os
.
environ
[
'PYTHONHASHSEED'
]
=
str
(
seed
)
if
deterministic
:
torch
.
backends
.
cudnn
.
deterministic
=
True
torch
.
backends
.
cudnn
.
benchmark
=
False
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment