Skip to content

Commit d2ae51d

Browse files
Vincent MoenspmeierNicolasHug
authored andcommitted
[fbsync] Add Country dataset (#5138)
Summary: * Add Country211 dataset To addresses issue #5108. * Add Country211 dataset To addresses issue #5108. * Update country211.py * Update country211.py * Code review reflected Reflect code review * Update test_datasets.py * Update with review Update with review * inherit from ImageFolder * Update test/test_datasets.py * Docstring + minor test update Reviewed By: NicolasHug Differential Revision: D33618167 fbshipit-source-id: 04de3c5290b966ff97f21ea32b2f678079aa2a6c Co-authored-by: Philip Meier <[email protected]> Co-authored-by: Nicolas Hug <[email protected]>
1 parent d7e9d37 commit d2ae51d

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

docs/source/datasets.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ You can also create your own datasets using the provided :ref:`base classes <bas
3838
Cityscapes
3939
CocoCaptions
4040
CocoDetection
41+
Country211
4142
DTD
4243
EMNIST
4344
FakeData

test/test_datasets.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,5 +2463,32 @@ def _meta_to_split_and_classification_ann(self, meta, idx):
24632463
return (image_id, class_id, species, breed_id)
24642464

24652465

2466+
class Country211TestCase(datasets_utils.ImageDatasetTestCase):
2467+
DATASET_CLASS = datasets.Country211
2468+
2469+
ADDITIONAL_CONFIGS = datasets_utils.combinations_grid(split=("train", "valid", "test"))
2470+
2471+
def inject_fake_data(self, tmpdir: str, config):
2472+
split_folder = pathlib.Path(tmpdir) / "country211" / config["split"]
2473+
split_folder.mkdir(parents=True, exist_ok=True)
2474+
2475+
num_examples = {
2476+
"train": 3,
2477+
"valid": 4,
2478+
"test": 5,
2479+
}[config["split"]]
2480+
2481+
classes = ("AD", "BS", "GR")
2482+
for cls in classes:
2483+
datasets_utils.create_image_folder(
2484+
split_folder,
2485+
name=cls,
2486+
file_name_fn=lambda idx: f"{idx}.jpg",
2487+
num_examples=num_examples,
2488+
)
2489+
2490+
return num_examples * len(classes)
2491+
2492+
24662493
if __name__ == "__main__":
24672494
unittest.main()

torchvision/datasets/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .cityscapes import Cityscapes
66
from .clevr import CLEVRClassification
77
from .coco import CocoCaptions, CocoDetection
8+
from .country211 import Country211
89
from .dtd import DTD
910
from .fakedata import FakeData
1011
from .fer2013 import FER2013
@@ -91,4 +92,5 @@
9192
"GTSRB",
9293
"CLEVRClassification",
9394
"OxfordIIITPet",
95+
"Country211",
9496
)

torchvision/datasets/country211.py

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

0 commit comments

Comments
 (0)