Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/datasets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ You can also create your own datasets using the provided :ref:`base classes <bas
CocoCaptions
CocoDetection
EMNIST
EuroSAT
FakeData
FashionMNIST
Flickr8k
Expand Down
21 changes: 21 additions & 0 deletions test/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2168,5 +2168,26 @@ def inject_fake_data(self, tmpdir, config):
return num_sequences * (num_examples_per_sequence - 1)


class EuroSATTestCase(datasets_utils.ImageDatasetTestCase):
DATASET_CLASS = datasets.EuroSAT
FEATURE_TYPES = (PIL.Image.Image, int)

def inject_fake_data(self, tmpdir, config):
img_folder = os.path.join(tmpdir, "eurosat", "2750")
os.makedirs(img_folder)

num_examples_per_class = 3
classes = ("AnnualCrop", "Forest")
for cls in classes:
datasets_utils.create_image_folder(
root=img_folder,
name=cls,
file_name_fn=lambda idx: f"{cls}_{idx}.jpg",
num_examples=num_examples_per_class,
)

return len(classes) * num_examples_per_class


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions torchvision/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .cifar import CIFAR10, CIFAR100
from .cityscapes import Cityscapes
from .coco import CocoCaptions, CocoDetection
from .eurosat import EuroSAT
from .fakedata import FakeData
from .flickr import Flickr8k, Flickr30k
from .folder import ImageFolder, DatasetFolder
Expand Down Expand Up @@ -77,4 +78,5 @@
"FlyingChairs",
"FlyingThings3D",
"HD1K",
"EuroSAT",
)
74 changes: 74 additions & 0 deletions torchvision/datasets/eurosat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
from typing import Any

from .folder import ImageFolder
from .utils import download_and_extract_archive, check_integrity


class EuroSAT(ImageFolder):
"""RGB version of the `EuroSAT <https://arxiv.org/pdf/1709.00029.pdf>`_ Dataset.
Args:
root (string): Root directory of dataset where ``EuroSAT.zip`` exists.
download (bool, optional): If True, downloads the dataset from the internet and
puts it in root directory. If dataset is already downloaded, it is not
downloaded again.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
target_transform (callable, optional): A function/transform that takes in the
target and transforms it.
"""

url = "https://madm.dfki.de/files/sentinel/EuroSAT.zip"
md5 = "c8fa014336c82ac7804f0398fcb19387"
filename = "EuroSAT.zip"

_class_map = {
"AnnualCrop": "Annual Crop",
"Forest": "Forest",
"HerbaceousVegetation": "Herbaceous Vegetation",
"Highway": "Highway",
"Industrial": "Industrial Buildings",
"Pasture": "Pasture",
"PermanentCrop": "Permanent Crop",
"Residential": "Residential Buildings",
"River": "River",
"SeaLake": "Sea & Lake",
}

def __init__(
self,
root: str,
download: bool = False,
**kwargs: Any,
) -> None:
self.root = os.path.expanduser(root)

if download:
self.download()

if not self._check_exists():
raise RuntimeError("Dataset not found. You can use download=True to download it")

super().__init__(os.path.join(self.data_folder, "2750"), **kwargs)
self.classes = [self._class_map[cls] for cls in self.classes]
self.root = os.path.expanduser(root)

def __len__(self) -> int:
return len(self.samples)

@property
def data_folder(self) -> str:
return os.path.join(self.root, self.__class__.__name__.lower())

def _check_exists(self) -> bool:
return check_integrity(os.path.join(self.data_folder, self.filename))

def download(self) -> None:

if self._check_exists():
return

os.makedirs(self.data_folder, exist_ok=True)
print(f"Downloading {self.url}")
download_and_extract_archive(self.url, download_root=self.data_folder, filename=self.filename, md5=self.md5)