Training with Grey images dataset - MASKRCNN Matterport

Viewed 341

I'm trying to train a set of custom dataset that are grey images.

The matterport is designed for RGB dataset.

These are the steps that I have followed so far for my grey scale dataset.

Step 1

> class DetectorConfig(Config):
>     Configuration for training pneumonia detection on the RSNA pneumonia dataset.
>     Overrides values in the base Config class https://github.com/matterport/Mask_RCNN/blob/master/mrcnn/config.py.
>     IMAGE_CHANNEL_COUNT = 1
>     MEAN_PIXEL = [123.7] # this value is the one that I chose

Step 2

> def load_image(self, image_id):
>         # Load image
>         image = skimage.io.imread(self.image_info[image_id]['path'])
>         # Convert to grayscale for consistency.
>         if image.ndim != 1:
>             image = skimage.color.gray2rgb(image) #Instead of rgb2gray(image)
> 
>         # Extending the size of the image to be (h,w,1)
>         image = image[..., np.newaxis]
>         return image

Alternate Step 2

>     def load_image(self, image_id):
>         """Load the specified image and return a [H,W,3] Numpy array.
>         # Load image
>         image = skimage.io.imread(self.image_info[image_id]['path'])         
>         image = image[..., np.newaxis] # Extending the size of the image to be (h,w,1)
>         return image

Alternate Step 2a

>     def load_image(self, image_id):
>         """Load the specified image and return a [H,W,3] Numpy array.
>         # Load image
>         image = cv2.imread(self.image_info[image_id]['path'])         
>         image = image[..., np.newaxis] # Extending the size of the image to be (h,w,1)
>         return image

Step 3

> model.load_weights(COCO_MODEL_PATH, by_name=True,
>                         exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", 
>                                  "mrcnn_bbox", "mrcnn_mask", "conv1"])

Step 4

>  layer_regex = {
>             # all layers but the backbone
>             "heads": r"(conv1\_.*)|(mrcnn\_.*)|(rpn\_.*)|(fpn\_.*)",
> 

> def load_image(self, image_id):
>      image = image[..., np.newaxis]

Step 5

> def resize_image(image, min_dim=None, max_dim=None, min_scale=None, mode="square"):
> padding = [(top_pad, bottom_pad), (left_pad, right_pad)]
> image = np.pad(image, padding, mode='constant', constant_values=0)

Step 6

>  if len(image.shape) != 3 or image.shape[2] != 3:
>         image = np.squeeze(image, axis = -1)
>         image = np.stack((image,) * 3, -1)

When I run this code, I faced this issue from train(model) ValueError: len(output_shape) cannot be smaller than the image dimensions

The issue is different if I used "rgb2gray(image)" instead at Step 2

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,2) and requested shape (3,2)

Alternate step 2

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,2) and requested shape (3,2)

Alternate Step 2a

ValueError: len(output_shape) cannot be smaller than the image dimensions

Please provide some assistance. All dataset images are of same dimensions.

1 Answers

The issue you are facing is due to dimension mismatch.

  1. Reason for len(output_shape) cannot be smaller than the image dimensions: The reason for this is that your source image is 4D (H, W, 3, 1), i.e. 3 channels. Possible reason for this would be

    image = skimage.color.gray2rgb(image)
    image = image[..., np.newaxis]
    

    Here you are first loading the images with 3 channels as then adding a new axis. To avoid this either use image = skimage.color.gray2rgb(image) or image = image[..., np.newaxis], but never both together in load_image. For reference here is what I used:

    def load_image(self, image_id):
        """Load the specified image and return a [H,W,3] Numpy array.
        """
        # Load image
        image = skimage.io.imread(self.image_info[image_id]['path'])
        image = image[..., np.newaxis]
        return image
    
  2. To get to the pertinent question, the reason for ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,2) and requested shape (3,2) is because your image is 3D (i.e. (H, W, 1) because of image = image[..., np.newaxis]) and your padding padding = [(top_pad, bottom_pad), (left_pad, right_pad)] is 2D. Reverting back to the original padding

    padding = [(top_pad, bottom_pad), (left_pad, right_pad), (0, 0)]
    

    will resolve this issue.

Hope this solves your problem.

Related