ptrblck:

Maybe you could load the 10-channel mask and slice it into 10 binary images?


This is my generate mask function. I was concatenating it and returning it to the dataset.
    def _generate_mask(self, sample_shapes, height, width, sample_grid):
        """
        Generate ground truth.
        :param sample_shapes:
        :param height:
        :param width:
        :param sample_grid:
        :return:
        """
        layers = []
        # generate individual mask layers
        for class_id in range(1, self.classes, 1):
            layer = self._generate_class_truth_layer(shapes=sample_shapes[class_id],
                                                     height=height,
                                                     width=width,
                                                     sample_grid=sample_grid)
            layers.append(np.expand_dims(layer, 2))

        # concatenate individual mask layers
        mask = np.concatenate(layers, axis=2)

        # generate background mask
        if self.background_mask:
            background = np.ones((height, width, 1)) - np.expand_dims(np.logical_or.reduce((mask[:, :, :10] == 1), axis=2), axis=2)
            # first channel is the background mask
            mask = np.concatenate((background, mask), axis=2).astype(np.uint8)

        return mask

This is the portion of my dataset that returns the image and mask towards the end.
        """
        Mask generation
        """
        mask = self.mask_generator.mask(id=id_, height=h, width=w)
        if mask is None:
            raise ValueError('Could not generate concatenated mask!')

        """
        Sample of our dataset will be dict {'image': image, 'mask': mask}.
        This dataset will take an optional argument transform so that any
        required processing can be applied on the sample.

        We will convert the scale and convert the sample to uint8 so that
        it can be viewed as a normal file. This will be useful for offline
        batch processing to generate a cached dataset.
        """

        image = (image * 255).astype(np.uint8)
        mask  = (mask  * 255).astype(np.uint8)

        image = torch.from_numpy(image)
        image = tvf.to_pil_image(image)

        mask = torch.from_numpy(mask)
        mask = tvf.to_pil_image(mask)

        """
        Apply user-specified transforms to image and mask.
        """
        if self.transform:
            image, mask = self._transform(image, mask, self.transform)

        """
        Sample of our dataset will be dict {'image': image, 'mask': mask}.
        This dataset will take an optional argument transform so that any
        required processing can be applied on the sample.
        """
        sample = {'image': image,
                  'mask' : mask}

        return sample

So. we’re doing all this just because PIL can’t handle transformation of a 10-ch mask. If I do not use Torch transformers and write my own Numpy based transformers, it should work?