Exemple #1
0
def get_val_dicts():
    data = json.load(open("stars_carla_val.json"))
    for im in data:
        anno = im['annotations']
        for an in anno:
            an['bbox_mode'] = BoxMode(an['bbox_mode'])
    return data
Exemple #2
0
    def get_data_dicts(self, json_file):
        data = json.load(open(json_file))

        for im in data:
            anno = im["annotations"]
            for an in anno:
                an["bbox_mode"] = BoxMode(an["bbox_mode"])
        return data
    def load_mosaic(self, image, data_dict):
        """
            from https://github.com/ultralytics/yolov3
        """
        # loads images in a mosaic
        data_dicts = [data_dict]
        data_dicts.extend([
            copy.deepcopy(self._dataset[random.randint(0,
                                                       len(self._dataset) -
                                                       1)]) for _ in range(3)
        ])
        images = [image]
        labels = []
        w, h = 1024, 1024
        for i, data_dict in enumerate(data_dicts):
            if i > 0:
                # load image and boxes
                image = self.load_image(data_dict)
                # if random.random() < self.mixup_prob:
                #     image, data_dict = self.load_mixup(image, data_dict)
                #     data_dicts[i] = data_dict
                images.append(image)
            if "annotations" in data_dict:
                labels.append(
                    np.array([[obj['category_id']] + obj['bbox']
                              for obj in data_dict["annotations"]],
                             dtype=np.float32))
            else:
                labels.append(np.array([]))
            w, h = min(w, data_dict["width"]), min(h, data_dict["height"])

        xc, yc = int(random.uniform(w * 0.25, w * 0.75)), int(
            random.uniform(h * 0.25, h * 0.75))  # mosaic center x, y

        if self.img_format == "BGR":
            fill_val = (103, 116, 123)
        else:  # rgb
            fill_val = (123, 116, 103)

        mosaic_img = np.full(
            (h, w, 3), fill_val,
            dtype=np.uint8)  # base image with 4 tiles, BGR order
        mosaic_labels = []
        for i, (image, data_dict) in enumerate(zip(images, data_dicts)):
            wi, hi = data_dict['width'], data_dict['height']

            # place img in mosaic_img
            if i == 0:  # top left
                x1a, y1a, x2a, y2a = max(xc - wi, 0), max(
                    yc - hi, 0), xc, yc  # xmin, ymin, xmax, ymax (large image)
                x1b, y1b, x2b, y2b = wi - (x2a - x1a), hi - (
                    y2a - y1a), wi, hi  # xmin, ymin, xmax, ymax (small image)
            elif i == 1:  # top right
                x1a, y1a, x2a, y2a = xc, max(yc - hi, 0), min(xc + wi, w), yc
                x1b, y1b, x2b, y2b = 0, hi - (y2a - y1a), min(wi,
                                                              x2a - x1a), hi
            elif i == 2:  # bottom left
                x1a, y1a, x2a, y2a = max(xc - wi, 0), yc, xc, min(h, yc + hi)
                x1b, y1b, x2b, y2b = wi - (x2a - x1a), 0, max(xc, wi), min(
                    y2a - y1a, hi)
            elif i == 3:  # bottom right
                x1a, y1a, x2a, y2a = xc, yc, min(xc + wi, w), min(h, yc + hi)
                x1b, y1b, x2b, y2b = 0, 0, min(wi,
                                               x2a - x1a), min(y2a - y1a, hi)

            mosaic_img[y1a:y2a,
                       x1a:x2a] = image[y1b:y2b,
                                        x1b:x2b]  # img4[ymin:ymax, xmin:xmax]
            padw = x1a - x1b
            padh = y1a - y1b

            # Labels
            if len(labels[i]) > 0:
                labels_i = labels[i].copy()
                # xywh to xyxy
                labels_i[:, 1] += padw
                labels_i[:, 2] += padh
                labels_i[:, 3] = labels_i[:, 1] + labels_i[:, 3]
                labels_i[:, 4] = labels_i[:, 2] + labels_i[:, 4]
                mosaic_labels.append(labels_i)

        # Concat/clip labels
        if len(mosaic_labels):
            mosaic_labels = np.concatenate(mosaic_labels, 0)
            np.clip(mosaic_labels[:, 1], 0, w, out=mosaic_labels[:, 1])
            np.clip(mosaic_labels[:, 3], 0, w, out=mosaic_labels[:, 3])
            np.clip(mosaic_labels[:, 2], 0, h, out=mosaic_labels[:, 2])
            np.clip(mosaic_labels[:, 4], 0, h, out=mosaic_labels[:, 4])
            mosaic_labels.astype(np.int32)
            mosaic_labels = mosaic_labels[np.where(
                (mosaic_labels[:, 3] - mosaic_labels[:, 1]) *
                (mosaic_labels[:, 4] - mosaic_labels[:, 2]) > 0)]

        # Augment
        # mosaic_img, mosaic_labels = random_affine(mosaic_img,
        #                                           mosaic_labels,
        #                                           degrees=0.0,
        #                                           translate=0.0,
        #                                           scale=0.5,
        #                                           shear=0.0,
        #                                           border=-w // 2)  # border to remove

        # translate back to detectron2 format
        annos = []
        for i in range(mosaic_labels.shape[0]):
            bbox = [
                mosaic_labels[i, 1], mosaic_labels[i, 2],
                max(mosaic_labels[i, 3] - mosaic_labels[i, 1], 0),
                max(mosaic_labels[i, 4] - mosaic_labels[i, 2], 0)
            ]
            anno = {
                'iscrowd': 0,
                # xyxy to xywh
                'bbox': bbox,
                'category_id': int(mosaic_labels[i, 0]),
                'bbox_mode': BoxMode(1),  # xywh
                'area': bbox[2] * bbox[3],
            }
            annos.append(anno)

        data_dict = {
            "file_name": data_dicts[0]['file_name'],
            "height": h,
            "width": w,
            "image_id": data_dicts[0]['image_id'],
            "annotations": annos
        }
        return mosaic_img, data_dict