deep-learningcomputer-visionimage-segmentationdetectron

Detectron2 Mask-Rcnn keep same color segmentation for same object class


I am using detectron2 implementation of Mask-Rcnn on video, the problem is that on each frame, the segmentation color of a same object change.

Is there any parameter that can allow me to keep a single color for an object class. I already tried detectron2.utils.visualizer.ColorMode(1) and it doesn't work


Solution

  • ColorMode(1) only has an effect if the metadata passed to the Visualizer has thing_colors defined. From the documentation for ColorMode,

    SEGMENTATION= 1

    Let instances of the same category have similar colors (from metadata.thing_colors), and overlay them with high opacity. This provides more attention on the quality of segmentation.

    So, you need to add a list of pre-defined colors (one for each class) to your metadata. From here,

    thing_colors (list[tuple(r, g, b)]): Pre-defined color (in [0, 255]) for each thing category. Used for visualization. If not given, random colors will be used.

    Now, the actual colors used may not be the exact same ones as specified in thing_colors. In Visualizer.draw_instance_predictions(), each specified color is jittered by adding a random value to it, so the overlaid color is a slightly different one. This use of a random value means that you will still see class colors change between frames. Depending on the colors you specify, this change may or may not be visually obvious.

    A simple solution to this might be to subclass Visualizer and override the _jitter() method so that it returns the color as is.

    class MyVisualizer(Visualizer):
        def _jitter(self, color):
            return color
    

    However, _jitter() is intended to be an internal method, so this is a hacky solution, and might break sometime down the line.

    A better solution might be to override draw_instance_predictions() and customize the drawing to your needs.