[Fix] Set random seed for generating palette if not given. (#1152)

* Fix colors

* fix comments

* Add comments

* Add comments. Add random seed in datasets
This commit is contained in:
jbwang1997 2021-12-23 09:51:50 +08:00 committed by GitHub
parent e0b4f556d4
commit 44d4c3877e
2 changed files with 18 additions and 0 deletions

View File

@ -349,7 +349,16 @@ class CustomDataset(Dataset):
elif palette is None:
if self.PALETTE is None:
# Get random state before set seed, and restore
# random state later.
# It will prevent loss of randomness, as the palette
# may be different in each iteration if not specified.
# See: https://github.com/open-mmlab/mmdetection/issues/5844
state = np.random.get_state()
np.random.seed(42)
# random palette
palette = np.random.randint(0, 255, size=(len(class_names), 3))
np.random.set_state(state)
else:
palette = self.PALETTE

View File

@ -245,8 +245,17 @@ class BaseSegmentor(BaseModule, metaclass=ABCMeta):
seg = result[0]
if palette is None:
if self.PALETTE is None:
# Get random state before set seed,
# and restore random state later.
# It will prevent loss of randomness, as the palette
# may be different in each iteration if not specified.
# See: https://github.com/open-mmlab/mmdetection/issues/5844
state = np.random.get_state()
np.random.seed(42)
# random palette
palette = np.random.randint(
0, 255, size=(len(self.CLASSES), 3))
np.random.set_state(state)
else:
palette = self.PALETTE
palette = np.array(palette)