Esempio n. 1
0
    def _prepare_sample(
        self, data: Tuple[Tuple[Dict[str, str], Tuple[str, BinaryIO]], Tuple[str, BinaryIO]]
    ) -> Dict[str, Any]:
        ann_data, image_data = data
        classification_data, segmentation_data = ann_data
        segmentation_path, segmentation_buffer = segmentation_data
        image_path, image_buffer = image_data

        return dict(
            label=Label(int(classification_data["label"]) - 1, categories=self._categories),
            species="cat" if classification_data["species"] == "1" else "dog",
            segmentation_path=segmentation_path,
            segmentation=EncodedImage.from_file(segmentation_buffer),
            image_path=image_path,
            image=EncodedImage.from_file(image_buffer),
        )
Esempio n. 2
0
 def _prepare_sample(self, data: Tuple[str, Any]) -> Dict[str, Any]:
     path, buffer = data
     category = pathlib.Path(path).parent.name
     return dict(
         label=Label.from_category(category, categories=self._categories),
         path=path,
         image=EncodedImage.from_file(buffer),
     )
Esempio n. 3
0
 def _prepare_sample(
         self, data: Tuple[str, Tuple[str, BinaryIO]]) -> Dict[str, Any]:
     id, (path, buffer) = data
     return dict(
         label=Label.from_category(id.split("/", 1)[0],
                                   categories=self._categories),
         path=path,
         image=EncodedImage.from_file(buffer),
     )
Esempio n. 4
0
    def _prepare_sample(self, data: Tuple[Tuple[str, BinaryIO], Optional[Dict[str, Any]]]) -> Dict[str, Any]:
        image_data, scenes_data = data
        path, buffer = image_data

        return dict(
            path=path,
            image=EncodedImage.from_file(buffer),
            label=Label(len(scenes_data["objects"])) if scenes_data else None,
        )
Esempio n. 5
0
    def _prepare_sample(self, data: Tuple[str, BinaryIO]) -> Dict[str, Any]:
        path, buffer = data

        return dict(
            path=path,
            image=EncodedImage.from_file(buffer),
            label=Label(int(pathlib.Path(path).parent.name.split(".", 1)[0]) -
                        1,
                        categories=self._categories),
        )
Esempio n. 6
0
    def _prepare_sample(
        self,
        data: Tuple[Optional[Tuple[Label, str]], Tuple[str, BinaryIO]],
    ) -> Dict[str, Any]:
        label_data, (path, buffer) = data

        return dict(
            dict(zip(("label", "wnid"), label_data if label_data else (None, None))),
            path=path,
            image=EncodedImage.from_file(buffer),
        )
Esempio n. 7
0
    def _prepare_sample(self, data: Tuple[Tuple[str, BinaryIO], Tuple[int, int, int, int, int, str]]) -> Dict[str, Any]:
        image, target = data
        path, buffer = image
        image = EncodedImage.from_file(buffer)

        return dict(
            path=path,
            image=image,
            label=Label(target[4] - 1, categories=self.categories),
            bounding_box=BoundingBox(target[:4], format="xyxy", image_size=image.image_size),
        )
Esempio n. 8
0
 def _2011_prepare_ann(self, data: Tuple[str, Tuple[List[str],
                                                    Tuple[str, BinaryIO]]],
                       image_size: Tuple[int, int]) -> Dict[str, Any]:
     _, (bounding_box_data, segmentation_data) = data
     segmentation_path, segmentation_buffer = segmentation_data
     return dict(
         bounding_box=BoundingBox(
             [float(part) for part in bounding_box_data[1:]],
             format="xywh",
             image_size=image_size),
         segmentation_path=segmentation_path,
         segmentation=EncodedImage.from_file(segmentation_buffer),
     )
Esempio n. 9
0
    def _prepare_sample(
        self,
        data: Tuple[Tuple[Tuple[str, str], Tuple[str, BinaryIO]], Tuple[str, BinaryIO]],
    ) -> Dict[str, Any]:
        split_and_image_data, ann_data = data
        _, image_data = split_and_image_data
        image_path, image_buffer = image_data
        ann_path, ann_buffer = ann_data

        return dict(
            (self._prepare_detection_ann if self._task == "detection" else self._prepare_segmentation_ann)(ann_buffer),
            image_path=image_path,
            image=EncodedImage.from_file(image_buffer),
            ann_path=ann_path,
        )
Esempio n. 10
0
    def _prepare_sample(self, data: Tuple[Tuple[str, Any], Dict[str, Any]]) -> Dict[str, Any]:
        (path, buffer), csv_info = data
        label = int(csv_info["ClassId"])

        bounding_box = BoundingBox(
            [int(csv_info[k]) for k in ("Roi.X1", "Roi.Y1", "Roi.X2", "Roi.Y2")],
            format="xyxy",
            image_size=(int(csv_info["Height"]), int(csv_info["Width"])),
        )

        return {
            "path": path,
            "image": EncodedImage.from_file(buffer),
            "label": Label(label, categories=self._categories),
            "bounding_box": bounding_box,
        }
Esempio n. 11
0
    def _prepare_sample(self, data: Tuple[Tuple[Any, Tuple[str, BinaryIO]], Tuple[str, BinaryIO]]) -> Dict[str, Any]:
        split_and_image_data, ann_data = data
        _, image_data = split_and_image_data
        image_path, image_buffer = image_data
        ann_path, ann_buffer = ann_data

        anns = read_mat(ann_buffer, squeeze_me=True)["GTcls"]

        return dict(
            image_path=image_path,
            image=EncodedImage.from_file(image_buffer),
            ann_path=ann_path,
            # the boundaries are stored in sparse CSC format, which is not supported by PyTorch
            boundaries=_Feature(np.stack([raw_boundary.toarray() for raw_boundary in anns["Boundaries"].item()])),
            segmentation=_Feature(anns["Segmentation"].item()),
        )
Esempio n. 12
0
    def _prepare_sample(
        self,
        data: Tuple[Tuple[Tuple[str, str], Tuple[str, BinaryIO]], Tuple[str, BinaryIO]],
        *,
        prepare_ann_fn: Callable[[BinaryIO], Dict[str, Any]],
    ) -> Dict[str, Any]:
        split_and_image_data, ann_data = data
        _, image_data = split_and_image_data
        image_path, image_buffer = image_data
        ann_path, ann_buffer = ann_data

        return dict(
            prepare_ann_fn(ann_buffer),
            image_path=image_path,
            image=EncodedImage.from_file(image_buffer),
            ann_path=ann_path,
        )
Esempio n. 13
0
File: dtd.py Progetto: nairbv/vision
    def _prepare_sample(
        self, data: Tuple[Tuple[str, List[str]], Tuple[str, BinaryIO]]
    ) -> Dict[str, Any]:
        (_, joint_categories_data), image_data = data
        _, *joint_categories = joint_categories_data
        path, buffer = image_data

        category = pathlib.Path(path).parent.name

        return dict(
            joint_categories={
                category
                for category in joint_categories if category
            },
            label=Label.from_category(category, categories=self.categories),
            path=path,
            image=EncodedImage.from_file(buffer),
        )
Esempio n. 14
0
    def _prepare_sample(
        self,
        data: Tuple[Tuple[str, Tuple[str, BinaryIO]], Any],
        *,
        prepare_ann_fn: Callable[[Any, Tuple[int, int]], Dict[str, Any]],
    ) -> Dict[str, Any]:
        data, anns_data = data
        _, image_data = data
        path, buffer = image_data

        image = EncodedImage.from_file(buffer)

        return dict(
            prepare_ann_fn(anns_data, image.image_size),
            image=image,
            label=Label(int(pathlib.Path(path).parent.name.rsplit(".", 1)[0]),
                        categories=self._categories),
        )
Esempio n. 15
0
    def _prepare_sample(
        self,
        data: Tuple[Tuple[str, Tuple[Tuple[str, List[str]], Tuple[str,
                                                                  BinaryIO]]],
                    Tuple[Tuple[str, Dict[str, str]],
                          Tuple[str, Dict[str, str]], Tuple[str, Dict[str,
                                                                      str]],
                          Tuple[str, Dict[str, str]], ], ],
    ) -> Dict[str, Any]:
        split_and_image_data, ann_data = data
        _, (_, image_data) = split_and_image_data
        path, buffer = image_data

        image = EncodedImage.from_file(buffer)
        (_, identity), (_, attributes), (_,
                                         bounding_box), (_,
                                                         landmarks) = ann_data

        return dict(
            path=path,
            image=image,
            identity=Label(int(identity["identity"])),
            attributes={
                attr: value == "1"
                for attr, value in attributes.items()
            },
            bounding_box=BoundingBox(
                [
                    int(bounding_box[key])
                    for key in ("x_1", "y_1", "width", "height")
                ],
                format="xywh",
                image_size=image.image_size,
            ),
            landmarks={
                landmark: _Feature((int(landmarks[f"{landmark}_x"]),
                                    int(landmarks[f"{landmark}_y"])))
                for landmark in {key[:-2]
                                 for key in landmarks.keys()}
            },
        )
Esempio n. 16
0
    def _prepare_sample(
        self, data: Tuple[Tuple[str, str], Tuple[Tuple[str, BinaryIO],
                                                 Tuple[str, BinaryIO]]]
    ) -> Dict[str, Any]:
        key, (image_data, ann_data) = data
        category, _ = key
        image_path, image_buffer = image_data
        ann_path, ann_buffer = ann_data

        image = EncodedImage.from_file(image_buffer)
        ann = read_mat(ann_buffer)

        return dict(
            label=Label.from_category(category, categories=self._categories),
            image_path=image_path,
            image=image,
            ann_path=ann_path,
            bounding_box=BoundingBox(ann["box_coord"].astype(
                np.int64).squeeze()[[2, 0, 3, 1]],
                                     format="xyxy",
                                     image_size=image.image_size),
            contour=_Feature(ann["obj_contour"].T),
        )
Esempio n. 17
0
def _data_to_image_key(sample: Dict[str, Any]) -> Dict[str, Any]:
    sample["image"] = EncodedImage(sample.pop("data").data)
    return sample
Esempio n. 18
0
 def _prepare_image(self, data: Tuple[str, BinaryIO]) -> Dict[str, Any]:
     path, buffer = data
     return dict(
         path=path,
         image=EncodedImage.from_file(buffer),
     )
Esempio n. 19
0
File: voc.py Progetto: nairbv/vision
 def _prepare_segmentation_ann(self, buffer: BinaryIO) -> Dict[str, Any]:
     return dict(segmentation=EncodedImage.from_file(buffer))