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

eurosat.py 2.7 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
68
69
70
71
  1. import os
  2. from pathlib import Path
  3. from typing import Any, Callable, Optional, Union
  4. from .folder import default_loader, ImageFolder
  5. from .utils import download_and_extract_archive
  6. class EuroSAT(ImageFolder):
  7. """RGB version of the `EuroSAT <https://github.com/phelber/eurosat>`_ Dataset.
  8. For the MS version of the dataset, see
  9. `TorchGeo <https://torchgeo.readthedocs.io/en/stable/api/datasets.html#eurosat>`__.
  10. Args:
  11. root (str or ``pathlib.Path``): Root directory of dataset where ``root/eurosat`` exists.
  12. transform (callable, optional): A function/transform that takes in a PIL image or torch.Tensor, depends on the given loader,
  13. and returns a transformed version. E.g, ``transforms.RandomCrop``
  14. target_transform (callable, optional): A function/transform that takes in the
  15. target and transforms it.
  16. download (bool, optional): If True, downloads the dataset from the internet and
  17. puts it in root directory. If dataset is already downloaded, it is not
  18. downloaded again. Default is False.
  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. def __init__(
  24. self,
  25. root: Union[str, Path],
  26. transform: Optional[Callable] = None,
  27. target_transform: Optional[Callable] = None,
  28. download: bool = False,
  29. loader: Callable[[str], Any] = default_loader,
  30. ) -> None:
  31. self.root = os.path.expanduser(root)
  32. self._base_folder = os.path.join(self.root, "eurosat")
  33. self._data_folder = os.path.join(self._base_folder, "2750")
  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__(
  39. self._data_folder,
  40. transform=transform,
  41. target_transform=target_transform,
  42. loader=loader,
  43. )
  44. self.root = os.path.expanduser(root)
  45. def __len__(self) -> int:
  46. return len(self.samples)
  47. def _check_exists(self) -> bool:
  48. return os.path.exists(self._data_folder)
  49. def download(self) -> None:
  50. if self._check_exists():
  51. return
  52. os.makedirs(self._base_folder, exist_ok=True)
  53. download_and_extract_archive(
  54. "https://huggingface.co/datasets/torchgeo/eurosat/resolve/c877bcd43f099cd0196738f714544e355477f3fd/EuroSAT.zip",
  55. download_root=self._base_folder,
  56. md5="c8fa014336c82ac7804f0398fcb19387",
  57. )
Tip!

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

Comments

Loading...