country211.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from pathlib import Path
  2. from typing import Callable, Optional
  3. from .folder import 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 (string): 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 an PIL image and returns a transformed
  15. 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. """
  20. _URL = "https://openaipublic.azureedge.net/clip/data/country211.tgz"
  21. _MD5 = "84988d7644798601126c29e9877aab6a"
  22. def __init__(
  23. self,
  24. root: str,
  25. split: str = "train",
  26. transform: Optional[Callable] = None,
  27. target_transform: Optional[Callable] = None,
  28. download: bool = False,
  29. ) -> None:
  30. self._split = verify_str_arg(split, "split", ("train", "valid", "test"))
  31. root = Path(root).expanduser()
  32. self.root = str(root)
  33. self._base_folder = root / "country211"
  34. if download:
  35. self._download()
  36. if not self._check_exists():
  37. raise RuntimeError("Dataset not found. You can use download=True to download it")
  38. super().__init__(str(self._base_folder / self._split), transform=transform, target_transform=target_transform)
  39. self.root = str(root)
  40. def _check_exists(self) -> bool:
  41. return self._base_folder.exists() and self._base_folder.is_dir()
  42. def _download(self) -> None:
  43. if self._check_exists():
  44. return
  45. download_and_extract_archive(self._URL, download_root=self.root, md5=self._MD5)