forked from open-mmlab/mmrazor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] Delete redundant
set_random_seed
function (open-mmlab#104)
* refactor set_random_seed * add unittests * fix unittests error * fix lint * avoid bc breaking
- Loading branch information
1 parent
077c870
commit 0cbe900
Showing
14 changed files
with
139 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
from .inference import init_mmcls_model | ||
from .train import set_random_seed, train_model | ||
from .train import set_random_seed, train_mmcls_model | ||
|
||
__all__ = ['set_random_seed', 'train_model', 'init_mmcls_model'] | ||
__all__ = ['train_mmcls_model', 'init_mmcls_model', 'set_random_seed'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import random | ||
|
||
import numpy as np | ||
import torch | ||
|
||
|
||
def set_random_seed(seed: int, deterministic: bool = False) -> None: | ||
"""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. | ||
""" | ||
random.seed(seed) | ||
np.random.seed(seed) | ||
torch.manual_seed(seed) | ||
torch.cuda.manual_seed_all(seed) | ||
if deterministic: | ||
torch.backends.cudnn.deterministic = True | ||
torch.backends.cudnn.benchmark = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import pytest | ||
|
||
|
||
def test_v0_5_deprecated_set_random_seed() -> None: | ||
warn_msg = 'Deprecated in v0.3 and will be removed in v0.5, ' \ | ||
'please import `set_random_seed` directly from `mmrazor.apis`' | ||
from mmrazor.apis.mmcls import set_random_seed | ||
with pytest.deprecated_call(match=warn_msg): | ||
set_random_seed(123) | ||
|
||
from mmrazor.apis.mmdet import set_random_seed | ||
with pytest.deprecated_call(match=warn_msg): | ||
set_random_seed(123) | ||
|
||
from mmrazor.apis.mmseg import set_random_seed | ||
with pytest.deprecated_call(match=warn_msg): | ||
set_random_seed(123) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import numpy as np | ||
import torch | ||
|
||
from mmrazor.apis import set_random_seed | ||
|
||
|
||
def test_set_random_seed() -> None: | ||
set_random_seed(123, False) | ||
x1 = torch.rand(3, 3) | ||
x2 = np.random.rand(3, 3) | ||
|
||
set_random_seed(123, True) | ||
assert torch.backends.cudnn.deterministic | ||
assert not torch.backends.cudnn.benchmark | ||
y1 = torch.rand(3, 3) | ||
y2 = np.random.rand(3, 3) | ||
|
||
assert torch.allclose(x1, y1, 1e-6) | ||
assert np.allclose(x2, y2, 1e-6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters