title: Train a custom Mask RCNN with different image sizes in Google Colab. Unfortunately i can't get it running
body: <p>currently I'm trying to train a Matterport Mask R-CNN with custom classes and a custom dataset on Colab. I followed this tutorial:
  <a href="https://github.com/TannerGilbert/MaskRCNN-Object-Detection-and-Segmentation" rel="nofollow noreferrer">https://github.com/TannerGilbert/MaskRCNN-Object-Detection-and-Segmentation</a></p>
  <p>Instead of using images with the same size, my images do have different sizes. I spend hours to match the masks(.json) to the image size. But finally it is working:</p>
  <p>The following lines are loading two sample images and displaying the segmentation mask:</p>
  <pre><code># Load and display random samples
  image_ids = np.random.choice(dataset_train.image_ids, 2)
  for image_id in image_ids:
      image, img_height, img_width = dataset_train.load_image(image_id)
      mask, class_ids = dataset_train.load_mask(image_id, img_height, img_width)
      visualize.display_top_masks(image, mask, class_ids, dataset_train.class_names)
  </code></pre>
  <p>load_image() in utils.py is looking like this:</p>
  <pre><code>def load_image(self, image_id):
          &quot;&quot;&quot;Load the specified image and return a [H,W,3] Numpy array.
          &quot;&quot;&quot;
          # Load image
          image = skimage.io.imread(self.image_info[image_id]['path'])
          img_height, img_width, num_channels = image.shape
          # If grayscale. Convert to RGB for consistency.
          if image.ndim != 3:
              image = skimage.color.gray2rgb(image)
          # If has an alpha channel, remove it for consistency
          if num_channels == 4:
              image = image[..., :3]
          return image, img_height, img_width
  </code></pre>
  <p>load_mask() is looking like this:</p>
  <pre><code>def load_mask(self, image_id, img_height, img_width):
          # get details of image
          info = self.image_info[image_id]
          # define box file location
          path = info['annotation']
          # load XML
          masks, classes = self.extract_masks(path, img_height, img_width)
          return masks, np.asarray(classes, dtype='int32')
  </code></pre>
  <p>And extract_mask() is looking like this:</p>
  <pre><code>def extract_masks(self, filename, img_height, img_width):
          json_file = os.path.join(filename)
          with open(json_file) as f:
              img_anns = json.load(f)
              
          masks = np.zeros([img_height, img_width, len(img_anns['shapes'])], dtype='uint8')
          classes = []
          for i, anno in enumerate(img_anns['shapes']):
              mask = np.zeros([img_height, img_width], dtype=np.uint8)
              cv2.fillPoly(mask, np.array([anno['points']], dtype=np.int32), 1)
              masks[:, :, i] = mask
              classes.append(self.class_names.index(anno['label']))
          return masks, classes
  </code></pre>
  <p>Now we are getting to the curious part...</p>
  <p>After going on with my code creating the model</p>
  <pre><code># Create model in training mode
  model = modellib.MaskRCNN(mode=&quot;training&quot;, config=config,
                            model_dir=MODEL_DIR)
  </code></pre>
  <p>...and choosing the weights to start with</p>
  <pre><code># Which weights to start with?
  init_with = &quot;coco&quot;  # imagenet, coco, or last
  
  if init_with == &quot;imagenet&quot;:
      model.load_weights(model.get_imagenet_weights(), by_name=True)
  elif init_with == &quot;coco&quot;:
      # Load weights trained on MS COCO, but skip layers that
      # are different due to the different number of classes
      # See README for instructions to download the COCO weights
      model.load_weights(COCO_MODEL_PATH, by_name=True,
                         exclude=[&quot;mrcnn_class_logits&quot;, &quot;mrcnn_bbox_fc&quot;, 
                                  &quot;mrcnn_bbox&quot;, &quot;mrcnn_mask&quot;])
  elif init_with == &quot;last&quot;:
      # Load the last model you trained and continue training
      model.load_weights(model.find_last(), by_name=True)
  </code></pre>
  <p>...I get to the point where training should start:</p>
  <pre><code>model.train(dataset_train, dataset_val, 
              learning_rate=config.LEARNING_RATE, 
              epochs=5, 
              layers='heads')
  </code></pre>
  <p>I'm receiving following error:</p>
  <pre><code>/content/drive/My Drive/Colab/Mask_RCNN/mrcnn/model.py in load_image_gt()
     1210     # Load image and mask
     1211     image, img_height, img_width = dataset.load_image(image_id)
  -&gt; 1212     mask, class_ids = dataset.load_mask(image_id, img_height, img_width)
     1213     original_shape = image.shape
     1214     image, window, scale, padding, crop = utils.resize_image(
  
  TypeError: load_mask() missing 2 required positional arguments: 'img_height' and 'img_width'
  </code></pre>
  <p>I noticed, that load_image_gt() is different to the above mentioned load_image(). I've already adjusted load_image_gt() as followed (I added  <strong>#&lt;-------</strong> to the relevant lines):</p>
  <pre><code>def load_image_gt(dataset, config, image_id, augment=False, augmentation=None,
                    use_mini_mask=False):
      &quot;&quot;&quot;Load and return ground truth data for an image (image, mask, bounding boxes).
  
      augment: (deprecated. Use augmentation instead). If true, apply random
          image augmentation. Currently, only horizontal flipping is offered.
      augmentation: Optional. An imgaug (https://github.com/aleju/imgaug) augmentation.
          For example, passing imgaug.augmenters.Fliplr(0.5) flips images
          right/left 50% of the time.
      use_mini_mask: If False, returns full-size masks that are the same height
          and width as the original image. These can be big, for example
          1024x1024x100 (for 100 instances). Mini masks are smaller, typically,
          224x224 and are generated by extracting the bounding box of the
          object and resizing it to MINI_MASK_SHAPE.
  
      Returns:
      image: [height, width, 3]
      shape: the original shape of the image before resizing and cropping.
      class_ids: [instance_count] Integer class IDs
      bbox: [instance_count, (y1, x1, y2, x2)]
      mask: [height, width, instance_count]. The height and width are those
          of the image unless use_mini_mask is True, in which case they are
          defined in MINI_MASK_SHAPE.
      &quot;&quot;&quot;
      # Load image and mask
      image, img_height, img_width = dataset.load_image(image_id)                    **#&lt;-------**
      mask, class_ids = dataset.load_mask(image_id, img_height, img_width)           **#&lt;-------** 
      original_shape = image.shape
      image, window, scale, padding, crop = utils.resize_image(
          image,
          min_dim=config.IMAGE_MIN_DIM,
          min_scale=config.IMAGE_MIN_SCALE,
          max_dim=config.IMAGE_MAX_DIM,
          mode=config.IMAGE_RESIZE_MODE)
      mask = utils.resize_mask(mask, scale, padding, crop)
  
      # Random horizontal flips.
      # TODO: will be removed in a future update in favor of augmentation
      if augment:
          logging.warning(&quot;'augment' is deprecated. Use 'augmentation' instead.&quot;)
          if random.randint(0, 1):
              image = np.fliplr(image)
              mask = np.fliplr(mask)
  
      # Augmentation
      # This requires the imgaug lib (https://github.com/aleju/imgaug)
      if augmentation:
          import imgaug
  
          # Augmenters that are safe to apply to masks
          # Some, such as Affine, have settings that make them unsafe, so always
          # test your augmentation on masks
          MASK_AUGMENTERS = [&quot;Sequential&quot;, &quot;SomeOf&quot;, &quot;OneOf&quot;, &quot;Sometimes&quot;,
                             &quot;Fliplr&quot;, &quot;Flipud&quot;, &quot;CropAndPad&quot;,
                             &quot;Affine&quot;, &quot;PiecewiseAffine&quot;]
  
          def hook(images, augmenter, parents, default):
              &quot;&quot;&quot;Determines which augmenters to apply to masks.&quot;&quot;&quot;
              return augmenter.__class__.__name__ in MASK_AUGMENTERS
  
          # Store shapes before augmentation to compare
          image_shape = image.shape
          mask_shape = mask.shape
          # Make augmenters deterministic to apply similarly to images and masks
          det = augmentation.to_deterministic()
          image = det.augment_image(image)
          # Change mask to np.uint8 because imgaug doesn't support np.bool
          mask = det.augment_image(mask.astype(np.uint8),
                                   hooks=imgaug.HooksImages(activator=hook))
          # Verify that shapes didn't change
          assert image.shape == image_shape, &quot;Augmentation shouldn't change image size&quot;
          assert mask.shape == mask_shape, &quot;Augmentation shouldn't change mask size&quot;
          # Change mask back to bool
          mask = mask.astype(np.bool)
  
      # Note that some boxes might be all zeros if the corresponding mask got cropped out.
      # and here is to filter them out
      _idx = np.sum(mask, axis=(0, 1)) &gt; 0
      mask = mask[:, :, _idx]
      class_ids = class_ids[_idx]
      # Bounding boxes. Note that some boxes might be all zeros
      # if the corresponding mask got cropped out.
      # bbox: [num_instances, (y1, x1, y2, x2)]
      bbox = utils.extract_bboxes(mask)
  
      # Active classes
      # Different datasets have different classes, so track the
      # classes supported in the dataset of this image.
      active_class_ids = np.zeros([dataset.num_classes], dtype=np.int32)
      source_class_ids = dataset.source_class_ids[dataset.image_info[image_id][&quot;source&quot;]]
      active_class_ids[source_class_ids] = 1
  
      # Resize masks to smaller size to reduce memory usage
      if use_mini_mask:
          mask = utils.minimize_mask(bbox, mask, config.MINI_MASK_SHAPE)
  
      # Image meta data
      image_meta = compose_image_meta(image_id, original_shape, image.shape,
                                      window, scale, active_class_ids)
  
      return image, img_height, img_width, image_meta, class_ids, bbox, mask          **#&lt;-------**
  </code></pre>
  <p>I don't know why the two requied arguments are missing, because img_height and img_with are defined, arent't they?
  In my oppion the code is exactly the same like the &quot;# Load and display random samples&quot;-code mentioned above.</p>
  <p>I would be very gratefull, if someone could help.
  Many thanks in advance!</p>