mmdet/datasets/pipelines/custom_transforms.py
def _crop_data(self, results, crop_size, allow_negative_crop):
        """Function to randomly crop images, bounding boxes, masks, semantic
        segmentation maps.
        Args:
            results (dict): Result dict from loading pipeline.
            crop_size (tuple): Expected absolute size after cropping, (h, w).
            allow_negative_crop (bool): Whether to allow a crop that does not
                contain any bbox area. Default to False.
        Returns:
            dict: Randomly cropped results, 'img_shape' key in result dict is
                updated according to crop size.
        """
        assert crop_size[0] > 0 and crop_size[1] > 0
        for key in results.get('img_fields', ['img']):
            img = results[key]
            margin_h = max(img.shape[0] - crop_size[0], 0)
            margin_w = max(img.shape[1] - crop_size[1], 0)
            offset_h = np.random.randint(0, margin_h + 1)
            offset_w = np.random.randint(0, margin_w + 1)
            crop_y1, crop_y2 = offset_h, offset_h + crop_size[0]
            crop_x1, crop_x2 = offset_w, offset_w + crop_size[1]

            # crop the image
            img = img[crop_y1:crop_y2, crop_x1:crop_x2, ...]
            img_shape = img.shape
            results[key] = img
        results['img_shape'] = img_shape

        # crop bboxes accordingly and clip to the image boundary
        for key in results.get('bbox_fields', []):
            # e.g. gt_bboxes and gt_bboxes_ignore
            bbox_offset = np.array([offset_w, offset_h, offset_w, offset_h],
                                   dtype=np.float32)
            bboxes = results[key] - bbox_offset
            if self.bbox_clip_border:
                bboxes[:, 0::2] = np.clip(bboxes[:, 0::2], 0, img_shape[1])
                bboxes[:, 1::2] = np.clip(bboxes[:, 1::2], 0, img_shape[0])
            valid_inds = (bboxes[:, 2] > bboxes[:, 0]) & (
                bboxes[:, 3] > bboxes[:, 1])
            # If the crop does not contain any gt-bbox area and
            # allow_negative_crop is False, skip this image.
            if (key == 'gt_bboxes' and not valid_inds.any()
                    and not allow_negative_crop):
                return None
            results[key] = bboxes[valid_inds, :]
            # label fields. e.g. gt_labels and gt_labels_ignore
            label_key = self.bbox2label.get(key)
            if label_key in results:
                results[label_key] = results[label_key][valid_inds]

            # mask fields, e.g. gt_masks and gt_masks_ignore
            mask_key = self.bbox2mask.get(key)
            if mask_key in results:
                results[mask_key] = results[mask_key][
                    valid_inds.nonzero()[0]].crop(
                        np.asarray([crop_x1, crop_y1, crop_x2, crop_y2]))

        # crop semantic seg
        for key in results.get('seg_fields', []):
            results[key] = results[key][crop_y1:crop_y2, crop_x1:crop_x2]

        return results
Function to randomly crop images, bounding boxes, masks, semantic
        segmentation maps.
        Args:
            results (dict): Result dict from loading pipeline.
            crop_size (tuple): Expected absolute size after cropping, (h, w).
            allow_negative_crop (bool): Whether to allow a crop that does not
                contain any bbox area. Default to False.
        Returns:
            dict: Randomly cropped results, 'img_shape' key in result dict is
                updated according to crop size.
        
Function to randomly crop images, bounding boxes, masks, semantic
segmentation maps.
Function to randomly crop images bounding boxes masks semantic segmentation maps .
def _crop_data(self, results, crop_size, allow_negative_crop):
        assert crop_size[0] > 0 and crop_size[1] > 0
        for key in results.get('img_fields', ['img']):
            img = results[key]
            margin_h = max(img.shape[0] - crop_size[0], 0)
            margin_w = max(img.shape[1] - crop_size[1], 0)
            offset_h = np.random.randint(0, margin_h + 1)
            offset_w = np.random.randint(0, margin_w + 1)
            crop_y1, crop_y2 = offset_h, offset_h + crop_size[0]
            crop_x1, crop_x2 = offset_w, offset_w + crop_size[1]
            img = img[crop_y1:crop_y2, crop_x1:crop_x2, ...]
            img_shape = img.shape
            results[key] = img
        results['img_shape'] = img_shape
        for key in results.get('bbox_fields', []):
            bbox_offset = np.array([offset_w, offset_h, offset_w, offset_h],
                                   dtype=np.float32)
            bboxes = results[key] - bbox_offset
            if self.bbox_clip_border:
                bboxes[:, 0::2] = np.clip(bboxes[:, 0::2], 0, img_shape[1])
                bboxes[:, 1::2] = np.clip(bboxes[:, 1::2], 0, img_shape[0])
            valid_inds = (bboxes[:, 2] > bboxes[:, 0]) & (
                bboxes[:, 3] > bboxes[:, 1])
            if (key == 'gt_bboxes' and not valid_inds.any()
                    and not allow_negative_crop):
                return None
            results[key] = bboxes[valid_inds, :]
            label_key = self.bbox2label.get(key)
            if label_key in results:
                results[label_key] = results[label_key][valid_inds]
            mask_key = self.bbox2mask.get(key)
            if mask_key in results:
                results[mask_key] = results[mask_key][
                    valid_inds.nonzero()[0]].crop(
                        np.asarray([crop_x1, crop_y1, crop_x2, crop_y2]))
        for key in results.get('seg_fields', []):
            results[key] = results[key][crop_y1:crop_y2, crop_x1:crop_x2]
        return results
def _crop_data ( self , results , crop_size , allow_negative_crop ) : assert crop_size [ 0 ] > 0 and crop_size [ 1 ] > 0 for key in results . get ( 'img_fields' , [ 'img' ] ) : img = results [ key ] margin_h = max ( img . shape [ 0 ] - crop_size [ 0 ] , 0 ) margin_w = max ( img . shape [ 1 ] - crop_size [ 1 ] , 0 ) offset_h = np . random . randint ( 0 , margin_h + 1 ) offset_w = np . random . randint ( 0 , margin_w + 1 ) crop_y1 , crop_y2 = offset_h , offset_h + crop_size [ 0 ] crop_x1 , crop_x2 = offset_w , offset_w + crop_size [ 1 ] img = img [ crop_y1 : crop_y2 , crop_x1 : crop_x2 , ... ] img_shape = img . shape results [ key ] = img results [ 'img_shape' ] = img_shape for key in results . get ( 'bbox_fields' , [ ] ) : bbox_offset = np . array ( [ offset_w , offset_h , offset_w , offset_h ] , dtype = np . float32 ) bboxes = results [ key ] - bbox_offset if self . bbox_clip_border : bboxes [ : , 0 : : 2 ] = np . clip ( bboxes [ : , 0 : : 2 ] , 0 , img_shape [ 1 ] ) bboxes [ : , 1 : : 2 ] = np . clip ( bboxes [ : , 1 : : 2 ] , 0 , img_shape [ 0 ] ) valid_inds = ( bboxes [ : , 2 ] > bboxes [ : , 0 ] ) & ( bboxes [ : , 3 ] > bboxes [ : , 1 ] ) if ( key == 'gt_bboxes' and not valid_inds . any ( ) and not allow_negative_crop ) : return None results [ key ] = bboxes [ valid_inds , : ] label_key = self . bbox2label . get ( key ) if label_key in results : results [ label_key ] = results [ label_key ] [ valid_inds ] mask_key = self . bbox2mask . get ( key ) if mask_key in results : results [ mask_key ] = results [ mask_key ] [ valid_inds . nonzero ( ) [ 0 ] ] . crop ( np . asarray ( [ crop_x1 , crop_y1 , crop_x2 , crop_y2 ] ) ) for key in results . get ( 'seg_fields' , [ ] ) : results [ key ] = results [ key ] [ crop_y1 : crop_y2 , crop_x1 : crop_x2 ] return results
Function to randomly crop images, bounding boxes, masks, semantic
segmentation maps.
Function to randomly crop images bounding boxes masks semantic segmentation maps .
"""Function to randomly crop images, bounding boxes, masks, semantic
        segmentation maps.
        Args:
            results (dict): Result dict from loading pipeline.
            crop_size (tuple): Expected absolute size after cropping, (h, w).
            allow_negative_crop (bool): Whether to allow a crop that does not
                contain any bbox area. Default to False.
        Returns:
            dict: Randomly cropped results, 'img_shape' key in result dict is
                updated according to crop size.
        """ # crop the image
 # crop bboxes accordingly and clip to the image boundary
 # e.g. gt_bboxes and gt_bboxes_ignore
 # If the crop does not contain any gt-bbox area and
 # allow_negative_crop is False, skip this image.
 # label fields. e.g. gt_labels and gt_labels_ignore
 # mask fields, e.g. gt_masks and gt_masks_ignore
 # crop semantic seg

Randomly cropped results, 'img_shape' key in result dict is
updated according to crop size.
Randomly cropped results ' img_shape ' key in result dict is updated according to crop size .
dict
self
results
Result dict from loading pipeline.
Result dict from loading pipeline .
crop_size
Expected absolute size after cropping, (h, w).
Expected absolute size after cropping ( h w ) .
allow_negative_crop
Whether to allow a crop that does not
contain any bbox area. Default to False.
Whether to allow a crop that does not contain any bbox area . Default to False .