country211.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from pathlib import Path
  2. from typing import Any, Callable, Optional, Union
  3. from .folder import default_loader, ImageFolder
  4. from .utils import download_and_extract_archive, verify_str_arg
  5. class Country211(ImageFolder):
  6. """`The Country211 Data Set <https://github.com/openai/CLIP/blob/main/data/country211.md>`_ from OpenAI.
  7. This dataset was built by filtering the images from the YFCC100m dataset
  8. that have GPS coordinate corresponding to a ISO-3166 country code. The
  9. dataset is balanced by sampling 150 train images, 50 validation images, and
  10. 100 test images for each country.
  11. Args:
  12. root (str or ``pathlib.Path``): Root directory of the dataset.
  13. split (string, optional): The dataset split, supports ``"train"`` (default), ``"valid"`` and ``"test"``.
  14. transform (callable, optional): A function/transform that takes in a PIL image or torch.Tensor, depends on the given loader,
  15. and returns a transformed version. E.g, ``transforms.RandomCrop``
  16. target_transform (callable, optional): A function/transform that takes in the target and transforms it.
  17. download (bool, optional): If True, downloads the dataset from the internet and puts it into
  18. ``root/country211/``. If dataset is already downloaded, it is not downloaded again.
  19. loader (callable, optional): A function to load an image given its path.
  20. By default, it uses PIL as its image loader, but users could also pass in
  21. ``torchvision.io.decode_image`` for decoding image data into tensors directly.
  22. """
  23. _URL = "https://openaipublic.azureedge.net/clip/data/country211.tgz"
  24. _MD5 = "84988d7644798601126c29e9877aab6a"
  25. def __init__(
  26. self,
  27. root: Union[str, Path],
  28. split: str = "train",
  29. transform: Optional[Callable] = None,
  30. target_transform: Optional[Callable] = None,
  31. download: bool = False,
  32. loader: Callable[[str], Any] = default_loader,
  33. ) -> None:
  34. self._split = verify_str_arg(split, "split", ("train", "valid", "test"))
  35. root = Path(root).expanduser()
  36. self.root = str(root)
  37. self._base_folder = root / "country211"
  38. if download:
  39. self._download()
  40. if not self._check_exists():
  41. raise RuntimeError("Dataset not found. You can use download=True to download it")
  42. super().__init__(
  43. str(self._base_folder / self._split),
  44. transform=transform,
  45. target_transform=target_transform,
  46. loader=loader,
  47. )
  48. self.root = str(root)
  49. def _check_exists(self) -> bool:
  50. return self._base_folder.exists() and self._base_folder.is_dir()
  51. def _download(self) -> None:
  52. if self._check_exists():
  53. return
  54. download_and_extract_archive(self._URL, download_root=self.root, md5=self._MD5)