def visualize_panoptic(panoptic,
                       save_path,
                       label_divisor,
                       colormap,
                       image=None,
                       weight=0.5,
                       ignore_index=255):
    """
    Save panoptic segmentation results.

    Args:
        panoptic(np.ndarray): The panoptic segmentation results, shape is (h, w).
        save_path(str): The save path.
        label_divisor(int): Used to convert panoptic id = semantic id * label_divisor + instance_id.
        colormap(np.ndarray): A color map for visualization.
        image(np.ndarray, optional): Origin image to prediction, merge panoptic with
            image if provided. Default: None.
        weight(float, optional): The image weight when merge panoptic with image. Default: 0.5.
        ignore_index(int, optional): Specifies a target value that is ignored. Default: 255.
    """
    colored_panoptic = np.zeros(
        (panoptic.shape[0], panoptic.shape[1], 3), dtype=np.uint8)
    taken_colors = set((0, 0, 0))

    def _random_color(base, max_dist=30):
        color = base + np.random.randint(
            low=-max_dist, high=max_dist + 1, size=3)
        return tuple(np.maximum(0, np.minimum(255, color)))

    for lab in np.unique(panoptic):
        mask = panoptic == lab

        ignore_mask = panoptic == ignore_index
        ins_mask = panoptic > label_divisor
        if lab > label_divisor:
            base_color = colormap[lab // label_divisor]
        elif lab != ignore_index:
            base_color = colormap[lab]
        else:
            continue
        if tuple(base_color) not in taken_colors:
            taken_colors.add(tuple(base_color))
            color = base_color
        else:
            while True:
                color = _random_color(base_color)
                if color not in taken_colors:
                    taken_colors.add(color)
                    break
        colored_panoptic[mask] = color

    if image is not None:
        colored_panoptic = cv2.addWeighted(image, weight, colored_panoptic,
                                           1 - weight, 0)
    cv2.imwrite(save_path, colored_panoptic)

    Save panoptic segmentation results.

    Args:
        panoptic(np.ndarray): The panoptic segmentation results, shape is (h, w).
        save_path(str): The save path.
        label_divisor(int): Used to convert panoptic id = semantic id * label_divisor + instance_id.
        colormap(np.ndarray): A color map for visualization.
        image(np.ndarray, optional): Origin image to prediction, merge panoptic with
            image if provided. Default: None.
        weight(float, optional): The image weight when merge panoptic with image. Default: 0.5.
        ignore_index(int, optional): Specifies a target value that is ignored. Default: 255.
    
contrib/PanopticDeepLab/utils/visualize.py