Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

country211.py 2.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  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)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...