Example #1
0
    def _prepare_sample(self, data: Tuple[np.ndarray, np.ndarray]) -> Dict[str, Any]:
        image_array, label_array = data

        return dict(
            image=Image(image_array.transpose((2, 0, 1))),
            label=Label(int(label_array) % 10, categories=self._categories),
        )
Example #2
0
 def _prepare_sample(self, data: Tuple[torch.Tensor, torch.Tensor], *,
                     config: DatasetConfig) -> Dict[str, Any]:
     image, label = data
     return dict(
         image=Image(image),
         label=Label(label, dtype=torch.int64, categories=self.categories),
     )
Example #3
0
    def _prepare_sample(self, data: Dict[str, Any]) -> Dict[str, Any]:
        label_id = data.get("emotion")

        return dict(
            image=Image(torch.tensor([int(idx) for idx in data["pixels"].split()], dtype=torch.uint8).reshape(48, 48)),
            label=Label(int(label_id), categories=self.categories) if label_id is not None else None,
        )
Example #4
0
    def _prepare_sample(self, data: Tuple[str, ...]) -> Dict[str, Any]:
        image_data, label_data = data[:256], data[256:-1]

        return dict(
            image=Image(torch.tensor([float(pixel) for pixel in image_data], dtype=torch.float).reshape(16, 16)),
            label=OneHotLabel([int(label) for label in label_data], categories=self._categories),
        )
Example #5
0
 def _prepare_sample(self, line: str) -> Dict[str, Any]:
     label, *values = line.strip().split(" ")
     values = [float(value.split(":")[1]) for value in values]
     pixels = torch.tensor(values).add_(1).div_(2)
     return dict(
         image=Image(pixels.reshape(16, 16)),
         label=Label(int(label) - 1, categories=self._categories),
     )
Example #6
0
    def _collate_and_decode(
        self,
        data: Tuple[torch.Tensor, torch.Tensor],
        *,
        config: DatasetConfig,
        decoder: Optional[Callable[[io.IOBase], torch.Tensor]],
    ) -> Dict[str, Any]:
        image, label = data

        if decoder is raw:
            image = Image(image)
        else:
            image_buffer = image_buffer_from_array(image.numpy())
            image = decoder(
                image_buffer
            ) if decoder else image_buffer  # type: ignore[assignment]

        label = Label(label,
                      dtype=torch.int64,
                      category=self.info.categories[int(label)])

        return dict(image=image, label=label)
Example #7
0
    def _collate_and_decode(
        self,
        data: Tuple[np.ndarray, int],
        *,
        decoder: Optional[Callable[[io.IOBase], torch.Tensor]],
    ) -> Dict[str, Any]:
        image_array, category_idx = data

        image: Union[Image, io.BytesIO]
        if decoder is raw:
            image = Image(image_array)
        else:
            image_buffer = image_buffer_from_array(image_array.transpose((1, 2, 0)))
            image = decoder(image_buffer) if decoder else image_buffer  # type: ignore[assignment]

        label = Label(category_idx, category=self.categories[category_idx])

        return dict(image=image, label=label)
Example #8
0
    def _collate_and_decode_sample(
        self,
        data: Tuple[np.ndarray, np.ndarray],
        *,
        decoder: Optional[Callable[[io.IOBase], torch.Tensor]],
    ) -> Dict[str, Any]:
        image_array, label_array = data

        if decoder is raw:
            image = Image(image_array.transpose((2, 0, 1)))
        else:
            image_buffer = image_buffer_from_array(image_array)
            image = decoder(
                image_buffer
            ) if decoder else image_buffer  # type: ignore[assignment]

        return dict(
            image=image,
            label=Label(int(label_array) % 10),
        )
Example #9
0
    def _collate_and_decode_sample(
        self,
        data: Dict[str, Any],
        *,
        decoder: Optional[Callable[[io.IOBase], torch.Tensor]],
    ) -> Dict[str, Any]:
        raw_image = torch.tensor([int(idx) for idx in data["pixels"].split()], dtype=torch.uint8).reshape(48, 48)
        label_id = data.get("emotion")
        label_idx = int(label_id) if label_id is not None else None

        image: Union[Image, io.BytesIO]
        if decoder is raw:
            image = Image(raw_image)
        else:
            image_buffer = image_buffer_from_array(raw_image.numpy())
            image = decoder(image_buffer) if decoder else image_buffer  # type: ignore[assignment]

        return dict(
            image=image,
            label=Label(label_idx, category=self.info.categories[label_idx]) if label_idx is not None else None,
        )
Example #10
0
    def _collate_and_decode_sample(
        self,
        data: Tuple[str, ...],
        *,
        decoder: Optional[Callable[[io.IOBase], torch.Tensor]],
    ) -> Dict[str, Any]:
        image_data = torch.tensor([float(pixel) for pixel in data[:256]],
                                  dtype=torch.uint8).reshape(16, 16)
        label_data = [int(label) for label in data[256:] if label]

        if decoder is raw:
            image = Image(image_data.unsqueeze(0))
        else:
            image_buffer = image_buffer_from_array(image_data.numpy())
            image = decoder(
                image_buffer
            ) if decoder else image_buffer  # type: ignore[assignment]

        label_idx = next((idx for idx, one_hot_label in enumerate(label_data)
                          if one_hot_label))
        return dict(image=image,
                    label=Label(label_idx,
                                category=self.info.categories[label_idx]))
Example #11
0
 def _prepare_sample(self, data: Tuple[np.ndarray, int]) -> Dict[str, Any]:
     image_array, category_idx = data
     return dict(
         image=Image(image_array),
         label=Label(category_idx, categories=self._categories),
     )
Example #12
0
 def image(input: Image, *, mean: Sequence[float],
           std: Sequence[float]) -> Image:
     mean_t = Normalize._channel_stats_to_tensor(mean, like=input)
     std_t = Normalize._channel_stats_to_tensor(std, like=input)
     return Image((input - mean_t) / std_t, like=input)
Example #13
0
 def image(input: Image, *, crop_box: BoundingBox) -> Image:
     # FIXME: pad input in case it is smaller than crop_box
     x1, y1, x2, y2 = crop_box.convert("xyxy").to_parts()
     return Image(input[..., y1 : y2 + 1, x1 : x2 + 1], like=input)  # type: ignore[misc]
Example #14
0
 def image(input: Image, *, size: Tuple[int, int], interpolation_mode: str = "nearest") -> Image:
     return Image(interpolate(input.unsqueeze(0), size, mode=interpolation_mode).squeeze(0), like=input)
Example #15
0
 def image(input: Image) -> Image:
     return Image(input.flip((-1,)), like=input)