Skip to content

Commit 98a353b

Browse files
authored
[Fix] Set random seed for generating palette if not given. (open-mmlab#1152)
* Fix colors * fix comments * Add comments * Add comments. Add random seed in datasets
1 parent 9975c67 commit 98a353b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

mmseg/datasets/custom.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,16 @@ def get_palette_for_custom_classes(self, class_names, palette=None):
349349

350350
elif palette is None:
351351
if self.PALETTE is None:
352+
# Get random state before set seed, and restore
353+
# random state later.
354+
# It will prevent loss of randomness, as the palette
355+
# may be different in each iteration if not specified.
356+
# See: https://github.com/open-mmlab/mmdetection/issues/5844
357+
state = np.random.get_state()
358+
np.random.seed(42)
359+
# random palette
352360
palette = np.random.randint(0, 255, size=(len(class_names), 3))
361+
np.random.set_state(state)
353362
else:
354363
palette = self.PALETTE
355364

mmseg/models/segmentors/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,17 @@ def show_result(self,
245245
seg = result[0]
246246
if palette is None:
247247
if self.PALETTE is None:
248+
# Get random state before set seed,
249+
# and restore random state later.
250+
# It will prevent loss of randomness, as the palette
251+
# may be different in each iteration if not specified.
252+
# See: https://github.com/open-mmlab/mmdetection/issues/5844
253+
state = np.random.get_state()
254+
np.random.seed(42)
255+
# random palette
248256
palette = np.random.randint(
249257
0, 255, size=(len(self.CLASSES), 3))
258+
np.random.set_state(state)
250259
else:
251260
palette = self.PALETTE
252261
palette = np.array(palette)

0 commit comments

Comments
 (0)