diff --git a/torchvision/datasets/_optical_flow.py b/torchvision/datasets/_optical_flow.py index c7663258899..40d25583942 100644 --- a/torchvision/datasets/_optical_flow.py +++ b/torchvision/datasets/_optical_flow.py @@ -13,7 +13,6 @@ from .utils import _read_pfm, verify_str_arg from .vision import VisionDataset - T1 = Tuple[Image.Image, Image.Image, Optional[np.ndarray], Optional[np.ndarray]] T2 = Tuple[Image.Image, Image.Image, Optional[np.ndarray]] @@ -33,7 +32,7 @@ class FlowDataset(ABC, VisionDataset): # and it's up to whatever consumes the dataset to decide what valid_flow_mask should be. _has_builtin_flow_mask = False - def __init__(self, root: str, transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], transforms: Optional[Callable] = None) -> None: super().__init__(root=root) self.transforms = transforms @@ -113,7 +112,7 @@ class Sintel(FlowDataset): ... Args: - root (string): Root directory of the Sintel Dataset. + root (str or ``pathlib.Path``): Root directory of the Sintel Dataset. split (string, optional): The dataset split, either "train" (default) or "test" pass_name (string, optional): The pass to use, either "clean" (default), "final", or "both". See link above for details on the different passes. @@ -125,7 +124,7 @@ class Sintel(FlowDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", pass_name: str = "clean", transforms: Optional[Callable] = None, @@ -183,7 +182,7 @@ class KittiFlow(FlowDataset): flow_occ Args: - root (string): Root directory of the KittiFlow Dataset. + root (str or ``pathlib.Path``): Root directory of the KittiFlow Dataset. split (string, optional): The dataset split, either "train" (default) or "test" transforms (callable, optional): A function/transform that takes in ``img1, img2, flow, valid_flow_mask`` and returns a transformed version. @@ -191,7 +190,7 @@ class KittiFlow(FlowDataset): _has_builtin_flow_mask = True - def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], split: str = "train", transforms: Optional[Callable] = None) -> None: super().__init__(root=root, transforms=transforms) verify_str_arg(split, "split", valid_values=("train", "test")) @@ -248,7 +247,7 @@ class FlyingChairs(FlowDataset): Args: - root (string): Root directory of the FlyingChairs Dataset. + root (str or ``pathlib.Path``): Root directory of the FlyingChairs Dataset. split (string, optional): The dataset split, either "train" (default) or "val" transforms (callable, optional): A function/transform that takes in ``img1, img2, flow, valid_flow_mask`` and returns a transformed version. @@ -256,7 +255,7 @@ class FlyingChairs(FlowDataset): return a built-in valid mask, such as :class:`~torchvision.datasets.KittiFlow`. """ - def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], split: str = "train", transforms: Optional[Callable] = None) -> None: super().__init__(root=root, transforms=transforms) verify_str_arg(split, "split", valid_values=("train", "val")) @@ -316,7 +315,7 @@ class FlyingThings3D(FlowDataset): TRAIN Args: - root (string): Root directory of the intel FlyingThings3D Dataset. + root (str or ``pathlib.Path``): Root directory of the intel FlyingThings3D Dataset. split (string, optional): The dataset split, either "train" (default) or "test" pass_name (string, optional): The pass to use, either "clean" (default) or "final" or "both". See link above for details on the different passes. @@ -329,7 +328,7 @@ class FlyingThings3D(FlowDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", pass_name: str = "clean", camera: str = "left", @@ -411,7 +410,7 @@ class HD1K(FlowDataset): image_2 Args: - root (string): Root directory of the HD1K Dataset. + root (str or ``pathlib.Path``): Root directory of the HD1K Dataset. split (string, optional): The dataset split, either "train" (default) or "test" transforms (callable, optional): A function/transform that takes in ``img1, img2, flow, valid_flow_mask`` and returns a transformed version. @@ -419,7 +418,7 @@ class HD1K(FlowDataset): _has_builtin_flow_mask = True - def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], split: str = "train", transforms: Optional[Callable] = None) -> None: super().__init__(root=root, transforms=transforms) verify_str_arg(split, "split", valid_values=("train", "test")) diff --git a/torchvision/datasets/_stereo_matching.py b/torchvision/datasets/_stereo_matching.py index c180e2e1eb8..6a3f563a2da 100644 --- a/torchvision/datasets/_stereo_matching.py +++ b/torchvision/datasets/_stereo_matching.py @@ -27,7 +27,7 @@ class StereoMatchingDataset(ABC, VisionDataset): _has_built_in_disparity_mask = False - def __init__(self, root: str, transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], transforms: Optional[Callable] = None) -> None: """ Args: root(str): Root directory of the dataset. @@ -159,11 +159,11 @@ class CarlaStereo(StereoMatchingDataset): ... Args: - root (string): Root directory where `carla-highres` is located. + root (str or ``pathlib.Path``): Root directory where `carla-highres` is located. transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version. """ - def __init__(self, root: str, transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], transforms: Optional[Callable] = None) -> None: super().__init__(root, transforms) root = Path(root) / "carla-highres" @@ -233,14 +233,14 @@ class Kitti2012Stereo(StereoMatchingDataset): calib Args: - root (string): Root directory where `Kitti2012` is located. + root (str or ``pathlib.Path``): Root directory where `Kitti2012` is located. split (string, optional): The dataset split of scenes, either "train" (default) or "test". transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version. """ _has_built_in_disparity_mask = True - def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], split: str = "train", transforms: Optional[Callable] = None) -> None: super().__init__(root, transforms) verify_str_arg(split, "split", valid_values=("train", "test")) @@ -321,14 +321,14 @@ class Kitti2015Stereo(StereoMatchingDataset): calib Args: - root (string): Root directory where `Kitti2015` is located. + root (str or ``pathlib.Path``): Root directory where `Kitti2015` is located. split (string, optional): The dataset split of scenes, either "train" (default) or "test". transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version. """ _has_built_in_disparity_mask = True - def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], split: str = "train", transforms: Optional[Callable] = None) -> None: super().__init__(root, transforms) verify_str_arg(split, "split", valid_values=("train", "test")) @@ -420,7 +420,7 @@ class Middlebury2014Stereo(StereoMatchingDataset): ... Args: - root (string): Root directory of the Middleburry 2014 Dataset. + root (str or ``pathlib.Path``): Root directory of the Middleburry 2014 Dataset. split (string, optional): The dataset split of scenes, either "train" (default), "test", or "additional" use_ambient_views (boolean, optional): Whether to use different expose or lightning views when possible. The dataset samples with equal probability between ``[im1.png, im1E.png, im1L.png]``. @@ -480,7 +480,7 @@ class Middlebury2014Stereo(StereoMatchingDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", calibration: Optional[str] = "perfect", use_ambient_views: bool = False, @@ -576,7 +576,7 @@ def _read_disparity(self, file_path: str) -> Union[Tuple[None, None], Tuple[np.n valid_mask = (disparity_map > 0).squeeze(0) # mask out invalid disparities return disparity_map, valid_mask - def _download_dataset(self, root: str) -> None: + def _download_dataset(self, root: Union[str, Path]) -> None: base_url = "https://vision.middlebury.edu/stereo/data/scenes2014/zip" # train and additional splits have 2 different calibration settings root = Path(root) / "Middlebury2014" @@ -675,7 +675,7 @@ class CREStereo(StereoMatchingDataset): def __init__( self, - root: str, + root: Union[str, Path], transforms: Optional[Callable] = None, ) -> None: super().__init__(root, transforms) @@ -757,12 +757,12 @@ class FallingThingsStereo(StereoMatchingDataset): ... Args: - root (string): Root directory where FallingThings is located. + root (str or ``pathlib.Path``): Root directory where FallingThings is located. variant (string): Which variant to use. Either "single", "mixed", or "both". transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version. """ - def __init__(self, root: str, variant: str = "single", transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], variant: str = "single", transforms: Optional[Callable] = None) -> None: super().__init__(root, transforms) root = Path(root) / "FallingThings" @@ -868,7 +868,7 @@ class SceneFlowStereo(StereoMatchingDataset): ... Args: - root (string): Root directory where SceneFlow is located. + root (str or ``pathlib.Path``): Root directory where SceneFlow is located. variant (string): Which dataset variant to user, "FlyingThings3D" (default), "Monkaa" or "Driving". pass_name (string): Which pass to use, "clean" (default), "final" or "both". transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version. @@ -877,7 +877,7 @@ class SceneFlowStereo(StereoMatchingDataset): def __init__( self, - root: str, + root: Union[str, Path], variant: str = "FlyingThings3D", pass_name: str = "clean", transforms: Optional[Callable] = None, @@ -973,14 +973,14 @@ class SintelStereo(StereoMatchingDataset): ... Args: - root (string): Root directory where Sintel Stereo is located. + root (str or ``pathlib.Path``): Root directory where Sintel Stereo is located. pass_name (string): The name of the pass to use, either "final", "clean" or "both". transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version. """ _has_built_in_disparity_mask = True - def __init__(self, root: str, pass_name: str = "final", transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], pass_name: str = "final", transforms: Optional[Callable] = None) -> None: super().__init__(root, transforms) verify_str_arg(pass_name, "pass_name", valid_values=("final", "clean", "both")) @@ -1082,12 +1082,12 @@ class InStereo2k(StereoMatchingDataset): ... Args: - root (string): Root directory where InStereo2k is located. + root (str or ``pathlib.Path``): Root directory where InStereo2k is located. split (string): Either "train" or "test". transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version. """ - def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], split: str = "train", transforms: Optional[Callable] = None) -> None: super().__init__(root, transforms) root = Path(root) / "InStereo2k" / split @@ -1169,14 +1169,14 @@ class ETH3DStereo(StereoMatchingDataset): ... Args: - root (string): Root directory of the ETH3D Dataset. + root (str or ``pathlib.Path``): Root directory of the ETH3D Dataset. split (string, optional): The dataset split of scenes, either "train" (default) or "test". transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version. """ _has_built_in_disparity_mask = True - def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None: + def __init__(self, root: Union[str, Path], split: str = "train", transforms: Optional[Callable] = None) -> None: super().__init__(root, transforms) verify_str_arg(split, "split", valid_values=("train", "test")) diff --git a/torchvision/datasets/caltech.py b/torchvision/datasets/caltech.py index a1aa7596238..fe4f0fad208 100644 --- a/torchvision/datasets/caltech.py +++ b/torchvision/datasets/caltech.py @@ -1,5 +1,6 @@ import os import os.path +from pathlib import Path from typing import Any, Callable, List, Optional, Tuple, Union from PIL import Image @@ -16,7 +17,7 @@ class Caltech101(VisionDataset): This class needs `scipy `_ to load target files from `.mat` format. Args: - root (string): Root directory of dataset where directory + root (str or ``pathlib.Path``): Root directory of dataset where directory ``caltech101`` exists or will be saved to if download is set to True. target_type (string or list, optional): Type of target to use, ``category`` or ``annotation``. Can also be a list to output a tuple with all specified @@ -38,7 +39,7 @@ class Caltech101(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], target_type: Union[List[str], str] = "category", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, @@ -153,7 +154,7 @@ class Caltech256(VisionDataset): """`Caltech 256 `_ Dataset. Args: - root (string): Root directory of dataset where directory + root (str or ``pathlib.Path``): Root directory of dataset where directory ``caltech256`` exists or will be saved to if download is set to True. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop`` diff --git a/torchvision/datasets/celeba.py b/torchvision/datasets/celeba.py index 5c4269664eb..147597d3ab3 100644 --- a/torchvision/datasets/celeba.py +++ b/torchvision/datasets/celeba.py @@ -1,6 +1,7 @@ import csv import os from collections import namedtuple +from pathlib import Path from typing import Any, Callable, List, Optional, Tuple, Union import PIL @@ -16,7 +17,7 @@ class CelebA(VisionDataset): """`Large-scale CelebFaces Attributes (CelebA) Dataset `_ Dataset. Args: - root (string): Root directory where images are downloaded to. + root (str or ``pathlib.Path``): Root directory where images are downloaded to. split (string): One of {'train', 'valid', 'test', 'all'}. Accordingly dataset is selected. target_type (string or list, optional): Type of target to use, ``attr``, ``identity``, ``bbox``, @@ -63,7 +64,7 @@ class CelebA(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", target_type: Union[List[str], str] = "attr", transform: Optional[Callable] = None, diff --git a/torchvision/datasets/cifar.py b/torchvision/datasets/cifar.py index 4df0306634e..1637670ab91 100644 --- a/torchvision/datasets/cifar.py +++ b/torchvision/datasets/cifar.py @@ -1,6 +1,7 @@ import os.path import pickle -from typing import Any, Callable, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Optional, Tuple, Union import numpy as np from PIL import Image @@ -13,7 +14,7 @@ class CIFAR10(VisionDataset): """`CIFAR10 `_ Dataset. Args: - root (string): Root directory of dataset where directory + root (str or ``pathlib.Path``): Root directory of dataset where directory ``cifar-10-batches-py`` exists or will be saved to if download is set to True. train (bool, optional): If True, creates dataset from training set, otherwise creates from test set. @@ -50,7 +51,7 @@ class CIFAR10(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/cityscapes.py b/torchvision/datasets/cityscapes.py index 85544598176..6f7281f2574 100644 --- a/torchvision/datasets/cityscapes.py +++ b/torchvision/datasets/cityscapes.py @@ -1,6 +1,7 @@ import json import os from collections import namedtuple +from pathlib import Path from typing import Any, Callable, Dict, List, Optional, Tuple, Union from PIL import Image @@ -13,7 +14,7 @@ class Cityscapes(VisionDataset): """`Cityscapes `_ Dataset. Args: - root (string): Root directory of dataset where directory ``leftImg8bit`` + root (str or ``pathlib.Path``): Root directory of dataset where directory ``leftImg8bit`` and ``gtFine`` or ``gtCoarse`` are located. split (string, optional): The image split to use, ``train``, ``test`` or ``val`` if mode="fine" otherwise ``train``, ``train_extra`` or ``val`` @@ -103,7 +104,7 @@ class Cityscapes(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", mode: str = "fine", target_type: Union[List[str], str] = "instance", diff --git a/torchvision/datasets/clevr.py b/torchvision/datasets/clevr.py index f9ee63558fd..328eb7d79da 100644 --- a/torchvision/datasets/clevr.py +++ b/torchvision/datasets/clevr.py @@ -1,6 +1,6 @@ import json import pathlib -from typing import Any, Callable, List, Optional, Tuple +from typing import Any, Callable, List, Optional, Tuple, Union from urllib.parse import urlparse from PIL import Image @@ -15,7 +15,7 @@ class CLEVRClassification(VisionDataset): The number of objects in a scene are used as label. Args: - root (string): Root directory of dataset where directory ``root/clevr`` exists or will be saved to if download is + root (str or ``pathlib.Path``): Root directory of dataset where directory ``root/clevr`` exists or will be saved to if download is set to True. split (string, optional): The dataset split, supports ``"train"`` (default), ``"val"``, or ``"test"``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed @@ -30,7 +30,7 @@ class CLEVRClassification(VisionDataset): def __init__( self, - root: str, + root: Union[str, pathlib.Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/coco.py b/torchvision/datasets/coco.py index fd374a8ec24..f3b7be798b2 100644 --- a/torchvision/datasets/coco.py +++ b/torchvision/datasets/coco.py @@ -1,5 +1,6 @@ import os.path -from typing import Any, Callable, List, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, List, Optional, Tuple, Union from PIL import Image @@ -12,7 +13,7 @@ class CocoDetection(VisionDataset): It requires the `COCO API to be installed `_. Args: - root (string): Root directory where images are downloaded to. + root (str or ``pathlib.Path``): Root directory where images are downloaded to. annFile (string): Path to json annotation file. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.PILToTensor`` @@ -24,7 +25,7 @@ class CocoDetection(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], annFile: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, @@ -67,7 +68,7 @@ class CocoCaptions(CocoDetection): It requires the `COCO API to be installed `_. Args: - root (string): Root directory where images are downloaded to. + root (str or ``pathlib.Path``): Root directory where images are downloaded to. annFile (string): Path to json annotation file. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.PILToTensor`` diff --git a/torchvision/datasets/country211.py b/torchvision/datasets/country211.py index 906fdfb2065..a0f82ee1226 100644 --- a/torchvision/datasets/country211.py +++ b/torchvision/datasets/country211.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Callable, Optional +from typing import Callable, Optional, Union from .folder import ImageFolder from .utils import download_and_extract_archive, verify_str_arg @@ -14,7 +14,7 @@ class Country211(ImageFolder): 100 test images for each country. Args: - root (string): Root directory of the dataset. + root (str or ``pathlib.Path``): Root directory of the dataset. split (string, optional): The dataset split, supports ``"train"`` (default), ``"valid"`` and ``"test"``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop``. @@ -28,7 +28,7 @@ class Country211(ImageFolder): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/dtd.py b/torchvision/datasets/dtd.py index b4e045e7c20..71c556bd201 100644 --- a/torchvision/datasets/dtd.py +++ b/torchvision/datasets/dtd.py @@ -1,6 +1,6 @@ import os import pathlib -from typing import Any, Callable, Optional, Tuple +from typing import Any, Callable, Optional, Tuple, Union import PIL.Image @@ -12,7 +12,7 @@ class DTD(VisionDataset): """`Describable Textures Dataset (DTD) `_. Args: - root (string): Root directory of the dataset. + root (str or ``pathlib.Path``): Root directory of the dataset. split (string, optional): The dataset split, supports ``"train"`` (default), ``"val"``, or ``"test"``. partition (int, optional): The dataset partition. Should be ``1 <= partition <= 10``. Defaults to ``1``. @@ -34,7 +34,7 @@ class DTD(VisionDataset): def __init__( self, - root: str, + root: Union[str, pathlib.Path], split: str = "train", partition: int = 1, transform: Optional[Callable] = None, diff --git a/torchvision/datasets/eurosat.py b/torchvision/datasets/eurosat.py index 2ae8c073202..3f490b11902 100644 --- a/torchvision/datasets/eurosat.py +++ b/torchvision/datasets/eurosat.py @@ -1,5 +1,6 @@ import os -from typing import Callable, Optional +from pathlib import Path +from typing import Callable, Optional, Union from .folder import ImageFolder from .utils import download_and_extract_archive @@ -9,7 +10,7 @@ class EuroSAT(ImageFolder): """RGB version of the `EuroSAT `_ Dataset. Args: - root (string): Root directory of dataset where ``root/eurosat`` exists. + root (str or ``pathlib.Path``): Root directory of dataset where ``root/eurosat`` exists. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop`` target_transform (callable, optional): A function/transform that takes in the @@ -21,7 +22,7 @@ class EuroSAT(ImageFolder): def __init__( self, - root: str, + root: Union[str, Path], transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False, diff --git a/torchvision/datasets/fer2013.py b/torchvision/datasets/fer2013.py index 90bf5f304cd..057fe695a13 100644 --- a/torchvision/datasets/fer2013.py +++ b/torchvision/datasets/fer2013.py @@ -1,6 +1,6 @@ import csv import pathlib -from typing import Any, Callable, Optional, Tuple +from typing import Any, Callable, Optional, Tuple, Union import torch from PIL import Image @@ -14,7 +14,7 @@ class FER2013(VisionDataset): `_ Dataset. Args: - root (string): Root directory of dataset where directory + root (str or ``pathlib.Path``): Root directory of dataset where directory ``root/fer2013`` exists. split (string, optional): The dataset split, supports ``"train"`` (default), or ``"test"``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed @@ -29,7 +29,7 @@ class FER2013(VisionDataset): def __init__( self, - root: str, + root: Union[str, pathlib.Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/fgvc_aircraft.py b/torchvision/datasets/fgvc_aircraft.py index e4b071712c3..bbf4e970a78 100644 --- a/torchvision/datasets/fgvc_aircraft.py +++ b/torchvision/datasets/fgvc_aircraft.py @@ -1,7 +1,8 @@ from __future__ import annotations import os -from typing import Any, Callable, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Optional, Tuple, Union import PIL.Image @@ -23,7 +24,7 @@ class FGVCAircraft(VisionDataset): - ``manufacturer``, e.g. Boeing. The dataset comprises 30 different manufacturers. Args: - root (string): Root directory of the FGVC Aircraft dataset. + root (str or ``pathlib.Path``): Root directory of the FGVC Aircraft dataset. split (string, optional): The dataset split, supports ``train``, ``val``, ``trainval`` and ``test``. annotation_level (str, optional): The annotation level, supports ``variant``, @@ -41,7 +42,7 @@ class FGVCAircraft(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "trainval", annotation_level: str = "variant", transform: Optional[Callable] = None, diff --git a/torchvision/datasets/flickr.py b/torchvision/datasets/flickr.py index 0047a12268b..1021309db05 100644 --- a/torchvision/datasets/flickr.py +++ b/torchvision/datasets/flickr.py @@ -2,7 +2,8 @@ import os from collections import defaultdict from html.parser import HTMLParser -from typing import Any, Callable, Dict, List, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Dict, List, Optional, Tuple, Union from PIL import Image @@ -12,7 +13,7 @@ class Flickr8kParser(HTMLParser): """Parser for extracting captions from the Flickr8k dataset web page.""" - def __init__(self, root: str) -> None: + def __init__(self, root: Union[str, Path]) -> None: super().__init__() self.root = root @@ -56,7 +57,7 @@ class Flickr8k(VisionDataset): """`Flickr8k Entities `_ Dataset. Args: - root (string): Root directory where images are downloaded to. + root (str or ``pathlib.Path``): Root directory where images are downloaded to. ann_file (string): Path to annotation file. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.PILToTensor`` @@ -66,7 +67,7 @@ class Flickr8k(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], ann_file: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, @@ -112,7 +113,7 @@ class Flickr30k(VisionDataset): """`Flickr30k Entities `_ Dataset. Args: - root (string): Root directory where images are downloaded to. + root (str or ``pathlib.Path``): Root directory where images are downloaded to. ann_file (string): Path to annotation file. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.PILToTensor`` diff --git a/torchvision/datasets/flowers102.py b/torchvision/datasets/flowers102.py index 41966f336d2..07f403702f5 100644 --- a/torchvision/datasets/flowers102.py +++ b/torchvision/datasets/flowers102.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Any, Callable, Optional, Tuple +from typing import Any, Callable, Optional, Tuple, Union import PIL.Image @@ -22,7 +22,7 @@ class Flowers102(VisionDataset): have large variations within the category, and several very similar categories. Args: - root (string): Root directory of the dataset. + root (str or ``pathlib.Path``): Root directory of the dataset. split (string, optional): The dataset split, supports ``"train"`` (default), ``"val"``, or ``"test"``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop``. @@ -42,7 +42,7 @@ class Flowers102(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/folder.py b/torchvision/datasets/folder.py index 3a38d556287..9ee06b6a650 100644 --- a/torchvision/datasets/folder.py +++ b/torchvision/datasets/folder.py @@ -1,5 +1,6 @@ import os import os.path +from pathlib import Path from typing import Any, Callable, cast, Dict, List, Optional, Tuple, Union from PIL import Image @@ -32,7 +33,7 @@ def is_image_file(filename: str) -> bool: return has_file_allowed_extension(filename, IMG_EXTENSIONS) -def find_classes(directory: str) -> Tuple[List[str], Dict[str, int]]: +def find_classes(directory: Union[str, Path]) -> Tuple[List[str], Dict[str, int]]: """Finds the class folders in a dataset. See :class:`DatasetFolder` for details. @@ -46,7 +47,7 @@ def find_classes(directory: str) -> Tuple[List[str], Dict[str, int]]: def make_dataset( - directory: str, + directory: Union[str, Path], class_to_idx: Optional[Dict[str, int]] = None, extensions: Optional[Union[str, Tuple[str, ...]]] = None, is_valid_file: Optional[Callable[[str], bool]] = None, @@ -112,7 +113,7 @@ class DatasetFolder(VisionDataset): :meth:`find_classes` method. Args: - root (string): Root directory path. + root (str or ``pathlib.Path``): Root directory path. loader (callable): A function to load a sample given its path. extensions (tuple[string]): A list of allowed extensions. both extensions and is_valid_file should not be passed. @@ -136,7 +137,7 @@ class DatasetFolder(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], loader: Callable[[str], Any], extensions: Optional[Tuple[str, ...]] = None, transform: Optional[Callable] = None, @@ -164,7 +165,7 @@ def __init__( @staticmethod def make_dataset( - directory: str, + directory: Union[str, Path], class_to_idx: Dict[str, int], extensions: Optional[Tuple[str, ...]] = None, is_valid_file: Optional[Callable[[str], bool]] = None, @@ -203,7 +204,7 @@ def make_dataset( directory, class_to_idx, extensions=extensions, is_valid_file=is_valid_file, allow_empty=allow_empty ) - def find_classes(self, directory: str) -> Tuple[List[str], Dict[str, int]]: + def find_classes(self, directory: Union[str, Path]) -> Tuple[List[str], Dict[str, int]]: """Find the class folders in a dataset structured as follows:: directory/ @@ -298,7 +299,7 @@ class ImageFolder(DatasetFolder): the same methods can be overridden to customize the dataset. Args: - root (string): Root directory path. + root (str or ``pathlib.Path``): Root directory path. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop`` target_transform (callable, optional): A function/transform that takes in the diff --git a/torchvision/datasets/food101.py b/torchvision/datasets/food101.py index fa38abd7238..f734787c1bf 100644 --- a/torchvision/datasets/food101.py +++ b/torchvision/datasets/food101.py @@ -1,6 +1,6 @@ import json from pathlib import Path -from typing import Any, Callable, Optional, Tuple +from typing import Any, Callable, Optional, Tuple, Union import PIL.Image @@ -19,7 +19,7 @@ class Food101(VisionDataset): Args: - root (string): Root directory of the dataset. + root (str or ``pathlib.Path``): Root directory of the dataset. split (string, optional): The dataset split, supports ``"train"`` (default) and ``"test"``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop``. @@ -34,7 +34,7 @@ class Food101(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/gtsrb.py b/torchvision/datasets/gtsrb.py index ec746fbf686..a3d012c70b2 100644 --- a/torchvision/datasets/gtsrb.py +++ b/torchvision/datasets/gtsrb.py @@ -1,6 +1,6 @@ import csv import pathlib -from typing import Any, Callable, Optional, Tuple +from typing import Any, Callable, Optional, Tuple, Union import PIL @@ -13,7 +13,7 @@ class GTSRB(VisionDataset): """`German Traffic Sign Recognition Benchmark (GTSRB) `_ Dataset. Args: - root (string): Root directory of the dataset. + root (str or ``pathlib.Path``): Root directory of the dataset. split (string, optional): The dataset split, supports ``"train"`` (default), or ``"test"``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop``. @@ -25,7 +25,7 @@ class GTSRB(VisionDataset): def __init__( self, - root: str, + root: Union[str, pathlib.Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/hmdb51.py b/torchvision/datasets/hmdb51.py index a58ddc293d9..8377e40d57c 100644 --- a/torchvision/datasets/hmdb51.py +++ b/torchvision/datasets/hmdb51.py @@ -1,6 +1,7 @@ import glob import os -from typing import Any, Callable, Dict, List, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Dict, List, Optional, Tuple, Union from torch import Tensor @@ -28,7 +29,7 @@ class HMDB51(VisionDataset): Internally, it uses a VideoClips object to handle clip creation. Args: - root (string): Root directory of the HMDB51 Dataset. + root (str or ``pathlib.Path``): Root directory of the HMDB51 Dataset. annotation_path (str): Path to the folder containing the split files. frames_per_clip (int): Number of frames in a clip. step_between_clips (int): Number of frames between each clip. @@ -59,7 +60,7 @@ class HMDB51(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], annotation_path: str, frames_per_clip: int, step_between_clips: int = 1, diff --git a/torchvision/datasets/imagenet.py b/torchvision/datasets/imagenet.py index 290ae8f157f..d7caf328d2b 100644 --- a/torchvision/datasets/imagenet.py +++ b/torchvision/datasets/imagenet.py @@ -2,7 +2,8 @@ import shutil import tempfile from contextlib import contextmanager -from typing import Any, Dict, Iterator, List, Optional, Tuple +from pathlib import Path +from typing import Any, Dict, Iterator, List, Optional, Tuple, Union import torch @@ -28,7 +29,7 @@ class ImageNet(ImageFolder): or ``ILSVRC2012_img_val.tar`` based on ``split`` in the root directory. Args: - root (string): Root directory of the ImageNet Dataset. + root (str or ``pathlib.Path``): Root directory of the ImageNet Dataset. split (string, optional): The dataset split, supports ``train``, or ``val``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop`` @@ -45,7 +46,7 @@ class ImageNet(ImageFolder): targets (list): The class_index value for each image in the dataset """ - def __init__(self, root: str, split: str = "train", **kwargs: Any) -> None: + def __init__(self, root: Union[str, Path], split: str = "train", **kwargs: Any) -> None: root = self.root = os.path.expanduser(root) self.split = verify_str_arg(split, "split", ("train", "val")) @@ -78,7 +79,7 @@ def extra_repr(self) -> str: return "Split: {split}".format(**self.__dict__) -def load_meta_file(root: str, file: Optional[str] = None) -> Tuple[Dict[str, str], List[str]]: +def load_meta_file(root: Union[str, Path], file: Optional[str] = None) -> Tuple[Dict[str, str], List[str]]: if file is None: file = META_FILE file = os.path.join(root, file) @@ -93,7 +94,7 @@ def load_meta_file(root: str, file: Optional[str] = None) -> Tuple[Dict[str, str raise RuntimeError(msg.format(file, root)) -def _verify_archive(root: str, file: str, md5: str) -> None: +def _verify_archive(root: Union[str, Path], file: str, md5: str) -> None: if not check_integrity(os.path.join(root, file), md5): msg = ( "The archive {} is not present in the root directory or is corrupted. " @@ -102,12 +103,12 @@ def _verify_archive(root: str, file: str, md5: str) -> None: raise RuntimeError(msg.format(file, root)) -def parse_devkit_archive(root: str, file: Optional[str] = None) -> None: +def parse_devkit_archive(root: Union[str, Path], file: Optional[str] = None) -> None: """Parse the devkit archive of the ImageNet2012 classification dataset and save the meta information in a binary file. Args: - root (str): Root directory containing the devkit archive + root (str or ``pathlib.Path``): Root directory containing the devkit archive file (str, optional): Name of devkit archive. Defaults to 'ILSVRC2012_devkit_t12.tar.gz' """ @@ -156,12 +157,12 @@ def get_tmp_dir() -> Iterator[str]: torch.save((wnid_to_classes, val_wnids), os.path.join(root, META_FILE)) -def parse_train_archive(root: str, file: Optional[str] = None, folder: str = "train") -> None: +def parse_train_archive(root: Union[str, Path], file: Optional[str] = None, folder: str = "train") -> None: """Parse the train images archive of the ImageNet2012 classification dataset and prepare it for usage with the ImageNet dataset. Args: - root (str): Root directory containing the train images archive + root (str or ``pathlib.Path``): Root directory containing the train images archive file (str, optional): Name of train images archive. Defaults to 'ILSVRC2012_img_train.tar' folder (str, optional): Optional name for train images folder. Defaults to @@ -183,13 +184,13 @@ def parse_train_archive(root: str, file: Optional[str] = None, folder: str = "tr def parse_val_archive( - root: str, file: Optional[str] = None, wnids: Optional[List[str]] = None, folder: str = "val" + root: Union[str, Path], file: Optional[str] = None, wnids: Optional[List[str]] = None, folder: str = "val" ) -> None: """Parse the validation images archive of the ImageNet2012 classification dataset and prepare it for usage with the ImageNet dataset. Args: - root (str): Root directory containing the validation images archive + root (str or ``pathlib.Path``): Root directory containing the validation images archive file (str, optional): Name of validation images archive. Defaults to 'ILSVRC2012_img_val.tar' wnids (list, optional): List of WordNet IDs of the validation images. If None diff --git a/torchvision/datasets/imagenette.py b/torchvision/datasets/imagenette.py index bf0835ce0ab..05da537891b 100644 --- a/torchvision/datasets/imagenette.py +++ b/torchvision/datasets/imagenette.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Any, Callable, Optional, Tuple +from typing import Any, Callable, Optional, Tuple, Union from PIL import Image @@ -12,7 +12,7 @@ class Imagenette(VisionDataset): """`Imagenette `_ image classification dataset. Args: - root (string): Root directory of the Imagenette dataset. + root (str or ``pathlib.Path``): Root directory of the Imagenette dataset. split (string, optional): The dataset split. Supports ``"train"`` (default), and ``"val"``. size (string, optional): The image size. Supports ``"full"`` (default), ``"320px"``, and ``"160px"``. download (bool, optional): If ``True``, downloads the dataset components and places them in ``root``. Already @@ -48,7 +48,7 @@ class Imagenette(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", size: str = "full", download=False, diff --git a/torchvision/datasets/inaturalist.py b/torchvision/datasets/inaturalist.py index 84d6907b485..68f9a77f56a 100644 --- a/torchvision/datasets/inaturalist.py +++ b/torchvision/datasets/inaturalist.py @@ -1,5 +1,6 @@ import os import os.path +from pathlib import Path from typing import Any, Callable, Dict, List, Optional, Tuple, Union from PIL import Image @@ -32,7 +33,7 @@ class INaturalist(VisionDataset): """`iNaturalist `_ Dataset. Args: - root (string): Root directory of dataset where the image files are stored. + root (str or ``pathlib.Path``): Root directory of dataset where the image files are stored. This class does not require/use annotation files. version (string, optional): Which version of the dataset to download/use. One of '2017', '2018', '2019', '2021_train', '2021_train_mini', '2021_valid'. @@ -65,7 +66,7 @@ class INaturalist(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], version: str = "2021_train", target_type: Union[List[str], str] = "full", transform: Optional[Callable] = None, diff --git a/torchvision/datasets/kinetics.py b/torchvision/datasets/kinetics.py index a5fda53aa90..42d32533953 100644 --- a/torchvision/datasets/kinetics.py +++ b/torchvision/datasets/kinetics.py @@ -5,7 +5,8 @@ from functools import partial from multiprocessing import Pool from os import path -from typing import Any, Callable, Dict, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Dict, Optional, Tuple, Union from torch import Tensor @@ -35,7 +36,7 @@ class Kinetics(VisionDataset): frames in a video might be present. Args: - root (string): Root directory of the Kinetics Dataset. + root (str or ``pathlib.Path``): Root directory of the Kinetics Dataset. Directory should be structured as follows: .. code:: @@ -90,7 +91,7 @@ class Kinetics(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], frames_per_clip: int, num_classes: str = "400", split: str = "train", diff --git a/torchvision/datasets/kitti.py b/torchvision/datasets/kitti.py index 37b99006c75..69e603c76f2 100644 --- a/torchvision/datasets/kitti.py +++ b/torchvision/datasets/kitti.py @@ -1,6 +1,7 @@ import csv import os -from typing import Any, Callable, List, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, List, Optional, Tuple, Union from PIL import Image @@ -14,7 +15,7 @@ class Kitti(VisionDataset): It corresponds to the "left color images of object" dataset, for object detection. Args: - root (string): Root directory where images are downloaded to. + root (str or ``pathlib.Path``): Root directory where images are downloaded to. Expects the following folder structure if download=False: .. code:: @@ -51,7 +52,7 @@ class Kitti(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/lfw.py b/torchvision/datasets/lfw.py index f46c4e4c872..69f1edaf72f 100644 --- a/torchvision/datasets/lfw.py +++ b/torchvision/datasets/lfw.py @@ -1,4 +1,5 @@ import os +from pathlib import Path from typing import Any, Callable, Dict, List, Optional, Tuple, Union from PIL import Image @@ -31,7 +32,7 @@ class _LFW(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str, image_set: str, view: str, @@ -95,7 +96,7 @@ class LFWPeople(_LFW): """`LFW `_ Dataset. Args: - root (string): Root directory of dataset where directory + root (str or ``pathlib.Path``): Root directory of dataset where directory ``lfw-py`` exists or will be saved to if download is set to True. split (string, optional): The image split to use. Can be one of ``train``, ``test``, ``10fold`` (default). @@ -177,7 +178,7 @@ class LFWPairs(_LFW): """`LFW `_ Dataset. Args: - root (string): Root directory of dataset where directory + root (str or ``pathlib.Path``): Root directory of dataset where directory ``lfw-py`` exists or will be saved to if download is set to True. split (string, optional): The image split to use. Can be one of ``train``, ``test``, ``10fold``. Defaults to ``10fold``. diff --git a/torchvision/datasets/lsun.py b/torchvision/datasets/lsun.py index 6fab66ff11e..a2f5e18b991 100644 --- a/torchvision/datasets/lsun.py +++ b/torchvision/datasets/lsun.py @@ -3,6 +3,7 @@ import pickle import string from collections.abc import Iterable +from pathlib import Path from typing import Any, Callable, cast, List, Optional, Tuple, Union from PIL import Image @@ -60,7 +61,7 @@ class LSUN(VisionDataset): ``pip install lmdb`` Args: - root (string): Root directory for the database files. + root (str or ``pathlib.Path``): Root directory for the database files. classes (string or list): One of {'train', 'val', 'test'} or a list of categories to load. e,g. ['bedroom_train', 'church_outdoor_train']. transform (callable, optional): A function/transform that takes in a PIL image @@ -71,7 +72,7 @@ class LSUN(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], classes: Union[str, List[str]] = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/mnist.py b/torchvision/datasets/mnist.py index fce8583985c..a2389d598e6 100644 --- a/torchvision/datasets/mnist.py +++ b/torchvision/datasets/mnist.py @@ -5,7 +5,8 @@ import string import sys import warnings -from typing import Any, Callable, Dict, List, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Dict, List, Optional, Tuple, Union from urllib.error import URLError import numpy as np @@ -20,7 +21,7 @@ class MNIST(VisionDataset): """`MNIST `_ Dataset. Args: - root (string): Root directory of dataset where ``MNIST/raw/train-images-idx3-ubyte`` + root (str or ``pathlib.Path``): Root directory of dataset where ``MNIST/raw/train-images-idx3-ubyte`` and ``MNIST/raw/t10k-images-idx3-ubyte`` exist. train (bool, optional): If True, creates dataset from ``train-images-idx3-ubyte``, otherwise from ``t10k-images-idx3-ubyte``. @@ -82,7 +83,7 @@ def test_data(self): def __init__( self, - root: str, + root: Union[str, Path], train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, @@ -203,7 +204,7 @@ class FashionMNIST(MNIST): """`Fashion-MNIST `_ Dataset. Args: - root (string): Root directory of dataset where ``FashionMNIST/raw/train-images-idx3-ubyte`` + root (str or ``pathlib.Path``): Root directory of dataset where ``FashionMNIST/raw/train-images-idx3-ubyte`` and ``FashionMNIST/raw/t10k-images-idx3-ubyte`` exist. train (bool, optional): If True, creates dataset from ``train-images-idx3-ubyte``, otherwise from ``t10k-images-idx3-ubyte``. @@ -231,7 +232,7 @@ class KMNIST(MNIST): """`Kuzushiji-MNIST `_ Dataset. Args: - root (string): Root directory of dataset where ``KMNIST/raw/train-images-idx3-ubyte`` + root (str or ``pathlib.Path``): Root directory of dataset where ``KMNIST/raw/train-images-idx3-ubyte`` and ``KMNIST/raw/t10k-images-idx3-ubyte`` exist. train (bool, optional): If True, creates dataset from ``train-images-idx3-ubyte``, otherwise from ``t10k-images-idx3-ubyte``. @@ -259,7 +260,7 @@ class EMNIST(MNIST): """`EMNIST `_ Dataset. Args: - root (string): Root directory of dataset where ``EMNIST/raw/train-images-idx3-ubyte`` + root (str or ``pathlib.Path``): Root directory of dataset where ``EMNIST/raw/train-images-idx3-ubyte`` and ``EMNIST/raw/t10k-images-idx3-ubyte`` exist. split (string): The dataset has 6 different splits: ``byclass``, ``bymerge``, ``balanced``, ``letters``, ``digits`` and ``mnist``. This argument specifies @@ -290,7 +291,7 @@ class EMNIST(MNIST): "mnist": list(string.digits), } - def __init__(self, root: str, split: str, **kwargs: Any) -> None: + def __init__(self, root: Union[str, Path], split: str, **kwargs: Any) -> None: self.split = verify_str_arg(split, "split", self.splits) self.training_file = self._training_file(split) self.test_file = self._test_file(split) @@ -343,7 +344,7 @@ class QMNIST(MNIST): """`QMNIST `_ Dataset. Args: - root (string): Root directory of dataset whose ``raw`` + root (str or ``pathlib.Path``): Root directory of dataset whose ``raw`` subdir contains binary files of the datasets. what (string,optional): Can be 'train', 'test', 'test10k', 'test50k', or 'nist' for respectively the mnist compatible @@ -416,7 +417,7 @@ class QMNIST(MNIST): ] def __init__( - self, root: str, what: Optional[str] = None, compat: bool = True, train: bool = True, **kwargs: Any + self, root: Union[str, Path], what: Optional[str] = None, compat: bool = True, train: bool = True, **kwargs: Any ) -> None: if what is None: what = "train" if train else "test" diff --git a/torchvision/datasets/moving_mnist.py b/torchvision/datasets/moving_mnist.py index 9dea36e4223..d02811762b8 100644 --- a/torchvision/datasets/moving_mnist.py +++ b/torchvision/datasets/moving_mnist.py @@ -1,5 +1,6 @@ import os.path -from typing import Callable, Optional +from pathlib import Path +from typing import Callable, Optional, Union import numpy as np import torch @@ -11,7 +12,7 @@ class MovingMNIST(VisionDataset): """`MovingMNIST `_ Dataset. Args: - root (string): Root directory of dataset where ``MovingMNIST/mnist_test_seq.npy`` exists. + root (str or ``pathlib.Path``): Root directory of dataset where ``MovingMNIST/mnist_test_seq.npy`` exists. split (string, optional): The dataset split, supports ``None`` (default), ``"train"`` and ``"test"``. If ``split=None``, the full data is returned. split_ratio (int, optional): The split ratio of number of frames. If ``split="train"``, the first split @@ -28,7 +29,7 @@ class MovingMNIST(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: Optional[str] = None, split_ratio: int = 10, download: bool = False, diff --git a/torchvision/datasets/omniglot.py b/torchvision/datasets/omniglot.py index 42717529a47..c02cf91234a 100644 --- a/torchvision/datasets/omniglot.py +++ b/torchvision/datasets/omniglot.py @@ -1,5 +1,6 @@ from os.path import join -from typing import Any, Callable, List, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, List, Optional, Tuple, Union from PIL import Image @@ -11,7 +12,7 @@ class Omniglot(VisionDataset): """`Omniglot `_ Dataset. Args: - root (string): Root directory of dataset where directory + root (str or ``pathlib.Path``): Root directory of dataset where directory ``omniglot-py`` exists. background (bool, optional): If True, creates dataset from the "background" set, otherwise creates from the "evaluation" set. This terminology is defined by the authors. @@ -33,7 +34,7 @@ class Omniglot(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], background: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/oxford_iiit_pet.py b/torchvision/datasets/oxford_iiit_pet.py index e4022251a8d..9fe78901626 100644 --- a/torchvision/datasets/oxford_iiit_pet.py +++ b/torchvision/datasets/oxford_iiit_pet.py @@ -13,7 +13,7 @@ class OxfordIIITPet(VisionDataset): """`Oxford-IIIT Pet Dataset `_. Args: - root (string): Root directory of the dataset. + root (str or ``pathlib.Path``): Root directory of the dataset. split (string, optional): The dataset split, supports ``"trainval"`` (default) or ``"test"``. target_types (string, sequence of strings, optional): Types of target to use. Can be ``category`` (default) or ``segmentation``. Can also be a list to output a tuple with all specified target types. The types represent: @@ -38,7 +38,7 @@ class OxfordIIITPet(VisionDataset): def __init__( self, - root: str, + root: Union[str, pathlib.Path], split: str = "trainval", target_types: Union[Sequence[str], str] = "category", transforms: Optional[Callable] = None, diff --git a/torchvision/datasets/pcam.py b/torchvision/datasets/pcam.py index 0268e0c9a10..8849e0ea39d 100644 --- a/torchvision/datasets/pcam.py +++ b/torchvision/datasets/pcam.py @@ -1,5 +1,5 @@ import pathlib -from typing import Any, Callable, Optional, Tuple +from typing import Any, Callable, Optional, Tuple, Union from PIL import Image @@ -18,7 +18,7 @@ class PCAM(VisionDataset): This dataset requires the ``h5py`` package which you can install with ``pip install h5py``. Args: - root (string): Root directory of the dataset. + root (str or ``pathlib.Path``): Root directory of the dataset. split (string, optional): The dataset split, supports ``"train"`` (default), ``"test"`` or ``"val"``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop``. @@ -72,7 +72,7 @@ class PCAM(VisionDataset): def __init__( self, - root: str, + root: Union[str, pathlib.Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/phototour.py b/torchvision/datasets/phototour.py index 5c7dd6b66da..fd2466a3d36 100644 --- a/torchvision/datasets/phototour.py +++ b/torchvision/datasets/phototour.py @@ -1,4 +1,5 @@ import os +from pathlib import Path from typing import Any, Callable, List, Optional, Tuple, Union import numpy as np @@ -24,7 +25,7 @@ class PhotoTour(VisionDataset): Args: - root (string): Root directory where images are. + root (str or ``pathlib.Path``): Root directory where images are. name (string): Name of the dataset to load. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. @@ -87,7 +88,12 @@ class PhotoTour(VisionDataset): matches_files = "m50_100000_100000_0.txt" def __init__( - self, root: str, name: str, train: bool = True, transform: Optional[Callable] = None, download: bool = False + self, + root: Union[str, Path], + name: str, + train: bool = True, + transform: Optional[Callable] = None, + download: bool = False, ) -> None: super().__init__(root, transform=transform) self.name = name diff --git a/torchvision/datasets/places365.py b/torchvision/datasets/places365.py index b98fc5e33b4..98966e1dc2f 100644 --- a/torchvision/datasets/places365.py +++ b/torchvision/datasets/places365.py @@ -1,6 +1,7 @@ import os from os import path -from typing import Any, Callable, Dict, List, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Dict, List, Optional, Tuple, Union from urllib.parse import urljoin from .folder import default_loader @@ -12,7 +13,7 @@ class Places365(VisionDataset): r"""`Places365 `_ classification dataset. Args: - root (string): Root directory of the Places365 dataset. + root (str or ``pathlib.Path``): Root directory of the Places365 dataset. split (string, optional): The dataset split. Can be one of ``train-standard`` (default), ``train-challenge``, ``val``. small (bool, optional): If ``True``, uses the small images, i.e. resized to 256 x 256 pixels, instead of the @@ -62,7 +63,7 @@ class Places365(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train-standard", small: bool = False, download: bool = False, diff --git a/torchvision/datasets/rendered_sst2.py b/torchvision/datasets/rendered_sst2.py index 6fa09b677bb..48b0ddfc4fb 100644 --- a/torchvision/datasets/rendered_sst2.py +++ b/torchvision/datasets/rendered_sst2.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Any, Callable, Optional, Tuple +from typing import Any, Callable, Optional, Tuple, Union import PIL.Image @@ -20,7 +20,7 @@ class RenderedSST2(VisionDataset): (444 positive and 428 negative), and a test split containing 1821 images (909 positive and 912 negative). Args: - root (string): Root directory of the dataset. + root (str or ``pathlib.Path``): Root directory of the dataset. split (string, optional): The dataset split, supports ``"train"`` (default), `"val"` and ``"test"``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop``. @@ -35,7 +35,7 @@ class RenderedSST2(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/sbd.py b/torchvision/datasets/sbd.py index 8399d025b1b..7f245675b2d 100644 --- a/torchvision/datasets/sbd.py +++ b/torchvision/datasets/sbd.py @@ -1,6 +1,7 @@ import os import shutil -from typing import Any, Callable, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Optional, Tuple, Union import numpy as np from PIL import Image @@ -27,7 +28,7 @@ class SBDataset(VisionDataset): This class needs `scipy `_ to load target files from `.mat` format. Args: - root (string): Root directory of the Semantic Boundaries Dataset + root (str or ``pathlib.Path``): Root directory of the Semantic Boundaries Dataset image_set (string, optional): Select the image_set to use, ``train``, ``val`` or ``train_noval``. Image set ``train_noval`` excludes VOC 2012 val images. mode (string, optional): Select target type. Possible values 'boundaries' or 'segmentation'. @@ -51,7 +52,7 @@ class SBDataset(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], image_set: str = "train", mode: str = "boundaries", download: bool = False, diff --git a/torchvision/datasets/sbu.py b/torchvision/datasets/sbu.py index ee90eeb64ae..3c349370a12 100644 --- a/torchvision/datasets/sbu.py +++ b/torchvision/datasets/sbu.py @@ -1,5 +1,6 @@ import os -from typing import Any, Callable, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Optional, Tuple, Union from PIL import Image @@ -11,7 +12,7 @@ class SBU(VisionDataset): """`SBU Captioned Photo `_ Dataset. Args: - root (string): Root directory of dataset where tarball + root (str or ``pathlib.Path``): Root directory of dataset where tarball ``SBUCaptionedPhotoDataset.tar.gz`` exists. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop`` @@ -28,7 +29,7 @@ class SBU(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = True, diff --git a/torchvision/datasets/semeion.py b/torchvision/datasets/semeion.py index 0703d267ad5..d0344c74241 100644 --- a/torchvision/datasets/semeion.py +++ b/torchvision/datasets/semeion.py @@ -1,5 +1,6 @@ import os.path -from typing import Any, Callable, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Optional, Tuple, Union import numpy as np from PIL import Image @@ -12,7 +13,7 @@ class SEMEION(VisionDataset): r"""`SEMEION `_ Dataset. Args: - root (string): Root directory of dataset where directory + root (str or ``pathlib.Path``): Root directory of dataset where directory ``semeion.py`` exists. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop`` @@ -29,7 +30,7 @@ class SEMEION(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = True, diff --git a/torchvision/datasets/stanford_cars.py b/torchvision/datasets/stanford_cars.py index 5818648d81d..c029ed0d358 100644 --- a/torchvision/datasets/stanford_cars.py +++ b/torchvision/datasets/stanford_cars.py @@ -1,5 +1,5 @@ import pathlib -from typing import Any, Callable, Optional, Tuple +from typing import Any, Callable, Optional, Tuple, Union from PIL import Image @@ -21,7 +21,7 @@ class StanfordCars(VisionDataset): This class needs `scipy `_ to load target files from `.mat` format. Args: - root (string): Root directory of dataset + root (str or ``pathlib.Path``): Root directory of dataset split (string, optional): The dataset split, supports ``"train"`` (default) or ``"test"``. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop`` @@ -35,7 +35,7 @@ class StanfordCars(VisionDataset): def __init__( self, - root: str, + root: Union[str, pathlib.Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/stl10.py b/torchvision/datasets/stl10.py index fbfd6650a97..90ff41738eb 100644 --- a/torchvision/datasets/stl10.py +++ b/torchvision/datasets/stl10.py @@ -1,5 +1,6 @@ import os.path -from typing import Any, Callable, cast, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, cast, Optional, Tuple, Union import numpy as np from PIL import Image @@ -12,7 +13,7 @@ class STL10(VisionDataset): """`STL10 `_ Dataset. Args: - root (string): Root directory of dataset where directory + root (str or ``pathlib.Path``): Root directory of dataset where directory ``stl10_binary`` exists. split (string): One of {'train', 'test', 'unlabeled', 'train+unlabeled'}. Accordingly, dataset is selected. @@ -45,7 +46,7 @@ class STL10(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", folds: Optional[int] = None, transform: Optional[Callable] = None, diff --git a/torchvision/datasets/sun397.py b/torchvision/datasets/sun397.py index a32644b940c..4db0a3cf237 100644 --- a/torchvision/datasets/sun397.py +++ b/torchvision/datasets/sun397.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Any, Callable, Optional, Tuple +from typing import Any, Callable, Optional, Tuple, Union import PIL.Image @@ -14,7 +14,7 @@ class SUN397(VisionDataset): 397 categories with 108'754 images. Args: - root (string): Root directory of the dataset. + root (str or ``pathlib.Path``): Root directory of the dataset. transform (callable, optional): A function/transform that takes in a 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. @@ -28,7 +28,7 @@ class SUN397(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False, diff --git a/torchvision/datasets/svhn.py b/torchvision/datasets/svhn.py index ece384e2daa..5d20d7db7e3 100644 --- a/torchvision/datasets/svhn.py +++ b/torchvision/datasets/svhn.py @@ -1,5 +1,6 @@ import os.path -from typing import Any, Callable, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Optional, Tuple, Union import numpy as np from PIL import Image @@ -19,7 +20,7 @@ class SVHN(VisionDataset): This class needs `scipy `_ to load data from `.mat` format. Args: - root (string): Root directory of the dataset where the data is stored. + root (str or ``pathlib.Path``): Root directory of the dataset where the data is stored. split (string): One of {'train', 'test', 'extra'}. Accordingly dataset is selected. 'extra' is Extra training set. transform (callable, optional): A function/transform that takes in a PIL image @@ -52,7 +53,7 @@ class SVHN(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/ucf101.py b/torchvision/datasets/ucf101.py index 7dece183060..935f8ad41c7 100644 --- a/torchvision/datasets/ucf101.py +++ b/torchvision/datasets/ucf101.py @@ -1,5 +1,6 @@ import os -from typing import Any, Callable, Dict, List, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Dict, List, Optional, Tuple, Union from torch import Tensor @@ -28,7 +29,7 @@ class UCF101(VisionDataset): Internally, it uses a VideoClips object to handle clip creation. Args: - root (string): Root directory of the UCF101 Dataset. + root (str or ``pathlib.Path``): Root directory of the UCF101 Dataset. annotation_path (str): path to the folder containing the split files; see docstring above for download instructions of these files frames_per_clip (int): number of frames in a clip. @@ -52,7 +53,7 @@ class UCF101(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], annotation_path: str, frames_per_clip: int, step_between_clips: int = 1, diff --git a/torchvision/datasets/usps.py b/torchvision/datasets/usps.py index 25f54d57cc9..9c681e79f6c 100644 --- a/torchvision/datasets/usps.py +++ b/torchvision/datasets/usps.py @@ -1,5 +1,6 @@ import os -from typing import Any, Callable, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, Optional, Tuple, Union import numpy as np from PIL import Image @@ -15,7 +16,7 @@ class USPS(VisionDataset): and make pixel values in ``[0, 255]``. Args: - root (string): Root directory of dataset to store``USPS`` data files. + root (str or ``pathlib.Path``): Root directory of dataset to store``USPS`` data files. train (bool, optional): If True, creates dataset from ``usps.bz2``, otherwise from ``usps.t.bz2``. transform (callable, optional): A function/transform that takes in a PIL image @@ -43,7 +44,7 @@ class USPS(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/vision.py b/torchvision/datasets/vision.py index 82debfc881f..e524c67e263 100644 --- a/torchvision/datasets/vision.py +++ b/torchvision/datasets/vision.py @@ -1,5 +1,6 @@ import os -from typing import Any, Callable, List, Optional, Tuple +from pathlib import Path +from typing import Any, Callable, List, Optional, Tuple, Union import torch.utils.data as data @@ -29,7 +30,7 @@ class VisionDataset(data.Dataset): def __init__( self, - root: str = None, # type: ignore[assignment] + root: Union[str, Path] = None, # type: ignore[assignment] transforms: Optional[Callable] = None, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, diff --git a/torchvision/datasets/voc.py b/torchvision/datasets/voc.py index cf76b8e5926..0f0e84c84fa 100644 --- a/torchvision/datasets/voc.py +++ b/torchvision/datasets/voc.py @@ -1,18 +1,18 @@ import collections import os +from pathlib import Path +from typing import Any, Callable, Dict, List, Optional, Tuple, Union from xml.etree.ElementTree import Element as ET_Element -from .vision import VisionDataset - try: from defusedxml.ElementTree import parse as ET_parse except ImportError: from xml.etree.ElementTree import parse as ET_parse -from typing import Any, Callable, Dict, List, Optional, Tuple from PIL import Image from .utils import download_and_extract_archive, verify_str_arg +from .vision import VisionDataset DATASET_YEAR_DICT = { "2012": { @@ -67,7 +67,7 @@ class _VOCBase(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], year: str = "2012", image_set: str = "train", download: bool = False, @@ -121,7 +121,7 @@ class VOCSegmentation(_VOCBase): """`Pascal VOC `_ Segmentation Dataset. Args: - root (string): Root directory of the VOC Dataset. + root (str or ``pathlib.Path``): Root directory of the VOC Dataset. year (string, optional): The dataset year, supports years ``"2007"`` to ``"2012"``. image_set (string, optional): Select the image_set to use, ``"train"``, ``"trainval"`` or ``"val"``. If ``year=="2007"``, can also be ``"test"``. @@ -165,7 +165,7 @@ class VOCDetection(_VOCBase): """`Pascal VOC `_ Detection Dataset. Args: - root (string): Root directory of the VOC Dataset. + root (str or ``pathlib.Path``): Root directory of the VOC Dataset. year (string, optional): The dataset year, supports years ``"2007"`` to ``"2012"``. image_set (string, optional): Select the image_set to use, ``"train"``, ``"trainval"`` or ``"val"``. If ``year=="2007"``, can also be ``"test"``. diff --git a/torchvision/datasets/widerface.py b/torchvision/datasets/widerface.py index 020a5e5415c..90f80b7175b 100644 --- a/torchvision/datasets/widerface.py +++ b/torchvision/datasets/widerface.py @@ -1,5 +1,7 @@ import os from os.path import abspath, expanduser +from pathlib import Path + from typing import Any, Callable, Dict, List, Optional, Tuple, Union import torch @@ -13,7 +15,7 @@ class WIDERFace(VisionDataset): """`WIDERFace `_ Dataset. Args: - root (string): Root directory where images and annotations are downloaded to. + root (str or ``pathlib.Path``): Root directory where images and annotations are downloaded to. Expects the following folder structure if download=False: .. code:: @@ -55,7 +57,7 @@ class WIDERFace(VisionDataset): def __init__( self, - root: str, + root: Union[str, Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None,