Skip to content

Commit

Permalink
[Fix] Set random seed for generating palette if not given. (#1152)
Browse files Browse the repository at this point in the history
* Fix colors

* fix comments

* Add comments

* Add comments. Add random seed in datasets
  • Loading branch information
jbwang1997 authored Dec 23, 2021
1 parent 9975c67 commit 98a353b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mmseg/datasets/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,16 @@ def get_palette_for_custom_classes(self, class_names, palette=None):

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

Expand Down
9 changes: 9 additions & 0 deletions mmseg/models/segmentors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,17 @@ def show_result(self,
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)
Expand Down

0 comments on commit 98a353b

Please sign in to comment.