mmdet/datasets/pipelines/transforms.py
def _train_aug(self, results):
        """Random crop and around padding the original image.

        Args:
            results (dict): Image infomations in the augment pipeline.
        Returns:
            results (dict): The updated dict.
        """
        img = results['img']
        h, w, c = img.shape
        boxes = results['gt_bboxes']
        while True:
            scale = random.choice(self.ratios)
            new_h = int(self.crop_size[0] * scale)
            new_w = int(self.crop_size[1] * scale)
            h_border = self._get_border(self.border, h)
            w_border = self._get_border(self.border, w)

            for i in range(50):
                center_x = random.randint(low=w_border, high=w - w_border)
                center_y = random.randint(low=h_border, high=h - h_border)

                cropped_img, border, patch = self._crop_image_and_paste(
                    img, [center_y, center_x], [new_h, new_w])

                mask = self._filter_boxes(patch, boxes)
                # if image do not have valid bbox, any crop patch is valid.
                if not mask.any() and len(boxes) > 0:
                    continue

                results['img'] = cropped_img
                results['img_shape'] = cropped_img.shape
                results['pad_shape'] = cropped_img.shape

                x0, y0, x1, y1 = patch

                left_w, top_h = center_x - x0, center_y - y0
                cropped_center_x, cropped_center_y = new_w // 2, new_h // 2

                # crop bboxes accordingly and clip to the image boundary
                for key in results.get('bbox_fields', []):
                    mask = self._filter_boxes(patch, results[key])
                    bboxes = results[key][mask]
                    bboxes[:, 0:4:2] += cropped_center_x - left_w - x0
                    bboxes[:, 1:4:2] += cropped_center_y - top_h - y0
                    bboxes[:, 0:4:2] = np.clip(bboxes[:, 0:4:2], 0, new_w)
                    bboxes[:, 1:4:2] = np.clip(bboxes[:, 1:4:2], 0, new_h)
                    keep = (bboxes[:, 2] > bboxes[:, 0]) & (
                            bboxes[:, 3] > bboxes[:, 1])
                    bboxes = bboxes[keep]
                    results[key] = bboxes
                    if key in ['gt_bboxes']:
                        if 'gt_labels' in results:
                            labels = results['gt_labels'][mask]
                            labels = labels[keep]
                            results['gt_labels'] = labels
                        if 'gt_masks' in results:
                            raise NotImplementedError(
                                'RandomCenterCropPad only supports bbox.')

                # crop semantic seg
                for key in results.get('seg_fields', []):
                    raise NotImplementedError(
                        'RandomCenterCropPad only supports bbox.')
                return results
Random crop and around padding the original image.

        Args:
            results (dict): Image infomations in the augment pipeline.
        Returns:
            results (dict): The updated dict.
        
Random crop and around padding the original image.
Random crop and around padding the original image .
def _train_aug(self, results):
        img = results['img']
        h, w, c = img.shape
        boxes = results['gt_bboxes']
        while True:
            scale = random.choice(self.ratios)
            new_h = int(self.crop_size[0] * scale)
            new_w = int(self.crop_size[1] * scale)
            h_border = self._get_border(self.border, h)
            w_border = self._get_border(self.border, w)
            for i in range(50):
                center_x = random.randint(low=w_border, high=w - w_border)
                center_y = random.randint(low=h_border, high=h - h_border)
                cropped_img, border, patch = self._crop_image_and_paste(
                    img, [center_y, center_x], [new_h, new_w])
                mask = self._filter_boxes(patch, boxes)
                if not mask.any() and len(boxes) > 0:
                    continue
                results['img'] = cropped_img
                results['img_shape'] = cropped_img.shape
                results['pad_shape'] = cropped_img.shape
                x0, y0, x1, y1 = patch
                left_w, top_h = center_x - x0, center_y - y0
                cropped_center_x, cropped_center_y = new_w // 2, new_h // 2
                for key in results.get('bbox_fields', []):
                    mask = self._filter_boxes(patch, results[key])
                    bboxes = results[key][mask]
                    bboxes[:, 0:4:2] += cropped_center_x - left_w - x0
                    bboxes[:, 1:4:2] += cropped_center_y - top_h - y0
                    bboxes[:, 0:4:2] = np.clip(bboxes[:, 0:4:2], 0, new_w)
                    bboxes[:, 1:4:2] = np.clip(bboxes[:, 1:4:2], 0, new_h)
                    keep = (bboxes[:, 2] > bboxes[:, 0]) & (
                            bboxes[:, 3] > bboxes[:, 1])
                    bboxes = bboxes[keep]
                    results[key] = bboxes
                    if key in ['gt_bboxes']:
                        if 'gt_labels' in results:
                            labels = results['gt_labels'][mask]
                            labels = labels[keep]
                            results['gt_labels'] = labels
                        if 'gt_masks' in results:
                            raise NotImplementedError(
                                'RandomCenterCropPad only supports bbox.')
                for key in results.get('seg_fields', []):
                    raise NotImplementedError(
                        'RandomCenterCropPad only supports bbox.')
                return results
def _train_aug ( self , results ) : img = results [ 'img' ] h , w , c = img . shape boxes = results [ 'gt_bboxes' ] while True : scale = random . choice ( self . ratios ) new_h = int ( self . crop_size [ 0 ] * scale ) new_w = int ( self . crop_size [ 1 ] * scale ) h_border = self . _get_border ( self . border , h ) w_border = self . _get_border ( self . border , w ) for i in range ( 50 ) : center_x = random . randint ( low = w_border , high = w - w_border ) center_y = random . randint ( low = h_border , high = h - h_border ) cropped_img , border , patch = self . _crop_image_and_paste ( img , [ center_y , center_x ] , [ new_h , new_w ] ) mask = self . _filter_boxes ( patch , boxes ) if not mask . any ( ) and len ( boxes ) > 0 : continue results [ 'img' ] = cropped_img results [ 'img_shape' ] = cropped_img . shape results [ 'pad_shape' ] = cropped_img . shape x0 , y0 , x1 , y1 = patch left_w , top_h = center_x - x0 , center_y - y0 cropped_center_x , cropped_center_y = new_w // 2 , new_h // 2 for key in results . get ( 'bbox_fields' , [ ] ) : mask = self . _filter_boxes ( patch , results [ key ] ) bboxes = results [ key ] [ mask ] bboxes [ : , 0 : 4 : 2 ] += cropped_center_x - left_w - x0 bboxes [ : , 1 : 4 : 2 ] += cropped_center_y - top_h - y0 bboxes [ : , 0 : 4 : 2 ] = np . clip ( bboxes [ : , 0 : 4 : 2 ] , 0 , new_w ) bboxes [ : , 1 : 4 : 2 ] = np . clip ( bboxes [ : , 1 : 4 : 2 ] , 0 , new_h ) keep = ( bboxes [ : , 2 ] > bboxes [ : , 0 ] ) & ( bboxes [ : , 3 ] > bboxes [ : , 1 ] ) bboxes = bboxes [ keep ] results [ key ] = bboxes if key in [ 'gt_bboxes' ] : if 'gt_labels' in results : labels = results [ 'gt_labels' ] [ mask ] labels = labels [ keep ] results [ 'gt_labels' ] = labels if 'gt_masks' in results : raise NotImplementedError ( 'RandomCenterCropPad only supports bbox.' ) for key in results . get ( 'seg_fields' , [ ] ) : raise NotImplementedError ( 'RandomCenterCropPad only supports bbox.' ) return results
Random crop and around padding the original image.
Random crop and around padding the original image .
"""Random crop and around padding the original image.

        Args:
            results (dict): Image infomations in the augment pipeline.
        Returns:
            results (dict): The updated dict.
        """ # if image do not have valid bbox, any crop patch is valid. # crop bboxes accordingly and clip to the image boundary # crop semantic seg
results (dict): The updated dict.
results ( dict ) : The updated dict .
self
results
Image infomations in the augment pipeline.
Image infomations in the augment pipeline .