def __call__(self, sample):
        instances_info = {
            obj_id: obj_info
            for obj_id, obj_info in sample['instances_info'].items()
            if not obj_info['ignore']
        }

        obj_id, obj_info = random.choice(list(instances_info.items()))
        sample['instances_info'] = {obj_id: obj_info}
        obj_mask = sample['instances_mask'] == obj_id

        crop_height, crop_width = self.crop_size

        obj_bbox = get_bbox_from_mask(obj_mask)
        obj_bbox = fit_bbox_ratio(obj_bbox, crop_height / crop_width)

        expand_k = np.random.uniform(self.min_expand, self.max_expand)
        obj_bbox = expand_bbox(obj_bbox, expand_ratio=expand_k)
        obj_bbox = clamp_bbox(obj_bbox, 0, sample['image'].shape[0] - 1, 0,
                              sample['image'].shape[1] - 1)

        sample['image'] = sample['image'][obj_bbox[0]:obj_bbox[1] + 1,
                                          obj_bbox[2]:obj_bbox[3] + 1, :]
        sample['instances_mask'] = sample['instances_mask'][
            obj_bbox[0]:obj_bbox[1] + 1, obj_bbox[2]:obj_bbox[3] + 1]

        sample['image'] = cv2.resize(sample['image'],
                                     (crop_width, crop_height))
        sample['instances_mask'] = cv2.resize(sample['instances_mask'],
                                              (crop_width, crop_height),
                                              interpolation=cv2.INTER_NEAREST)

        return sample
Exemplo n.º 2
0
def get_object_roi(pred_mask, clicks_list, expansion_ratio, min_crop_size):
    pred_mask = pred_mask.copy()

    for click in clicks_list:
        if click.is_positive:
            pred_mask[int(click.coords[0]), int(click.coords[1])] = 1

    bbox = get_bbox_from_mask(pred_mask)
    bbox = expand_bbox(bbox, expansion_ratio, min_crop_size)
    h, w = pred_mask.shape[0], pred_mask.shape[1]
    bbox = clamp_bbox(bbox, 0, h - 1, 0, w - 1)

    return bbox
Exemplo n.º 3
0
    def get_params_dependent_on_targets(self, params):
        instances = params['mask']

        is_mask_layer = len(instances.shape) > 2
        candidates = []
        if is_mask_layer:
            for layer_indx in range(instances.shape[2]):
                labels, areas = get_labels_with_sizes(instances[:, :,
                                                                layer_indx])
                candidates.extend([(layer_indx, obj_id)
                                   for obj_id, area in zip(labels, areas)
                                   if area > self.min_area])
        else:
            labels, areas = get_labels_with_sizes(instances)
            candidates = [
                obj_id for obj_id, area in zip(labels, areas)
                if area > self.min_area
            ]

        selected_object = None
        bbox = None
        if candidates:
            selected_object = random.choice(candidates)
            if is_mask_layer:
                layer_indx, mask_id = selected_object
                obj_mask = instances[:, :, layer_indx] == mask_id
            else:
                obj_mask = instances == selected_object

            bbox = get_bbox_from_mask(obj_mask)

            if isinstance(self.expansion_ratio, tuple):
                expansion_ratio = random.uniform(*self.expansion_ratio)
            else:
                expansion_ratio = self.expansion_ratio

            bbox = expand_bbox(bbox, expansion_ratio, self.min_crop_size)
            bbox = self._jitter_bbox(bbox)
            bbox = clamp_bbox(bbox, 0, obj_mask.shape[0] - 1, 0,
                              obj_mask.shape[1] - 1)

        return {'selected_object': selected_object, 'bbox': bbox}