| |
| |
| |
| |
|
|
| from typing import Any, Tuple |
|
|
| from torchvision.datasets import VisionDataset |
|
|
| from .extended import ExtendedVisionDataset |
| from .decoders import TargetDecoder, ImageDataDecoder |
|
|
| from pathlib import Path |
| from typing import Callable, List, Optional, Tuple, Union |
|
|
| from PIL import Image |
|
|
| class TestVisionDataset(ExtendedVisionDataset): |
| def __init__(self, root, *args, **kwargs) -> None: |
| super().__init__(*args, **kwargs) |
| |
| folder_path = Path(root) |
|
|
| |
| image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'} |
|
|
| |
| self.image_files = [p for p in folder_path.rglob("*") if p.suffix.lower() in image_extensions] |
| print("Found this many files", len(self.image_files)) |
| |
|
|
| def __getitem__(self, index: int) -> Tuple[Any, Any]: |
| try: |
| path = self.image_files[index] |
| image = Image.open(path).convert("RGB") |
| except Exception as e: |
| raise RuntimeError(f"can not read image for sample {index}") from e |
| |
| |
| |
| if self.transforms is not None: |
| print(image.size, path) |
| return self.transforms(image, None) |
|
|
| |
| |
| |
|
|
| |
| |
|
|
| return image, None |
|
|
| def __len__(self) -> int: |
| return len(self.image_files) |
|
|