import numpy as np PALETTE = np.array([ [0, 0, 0], [100, 0, 100], [255, 255, 255], [0, 255, 0], [255, 255, 0], [0, 0, 255], [255, 0, 0], [255, 0, 255], [0, 255, 255], ], dtype=np.int32) def decode_mask(mask_rgb): h, w = mask_rgb.shape[:2] flat = mask_rgb.reshape(-1, 3).astype(np.int32) d2 = ( (flat[:, None, :] - PALETTE[None, :, :]) ** 2 ).sum(-1) classes = d2.argmin(axis=1).astype(np.uint8) return classes.reshape(h, w)