Skip to content

Added typing annotations to datasets/lfw #6844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Changes from all 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
22 changes: 11 additions & 11 deletions torchvision/datasets/lfw.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Any, Callable, List, Optional, Tuple
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

from PIL import Image

Expand Down Expand Up @@ -38,7 +38,7 @@ def __init__(
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
):
) -> None:
super().__init__(os.path.join(root, self.base_folder), transform=transform, target_transform=target_transform)

self.image_set = verify_str_arg(image_set.lower(), "image_set", self.file_dict.keys())
Expand All @@ -62,7 +62,7 @@ def _loader(self, path: str) -> Image.Image:
img = Image.open(f)
return img.convert("RGB")

def _check_integrity(self):
def _check_integrity(self) -> bool:
st1 = check_integrity(os.path.join(self.root, self.filename), self.md5)
st2 = check_integrity(os.path.join(self.root, self.labels_file), self.checksums[self.labels_file])
if not st1 or not st2:
Expand All @@ -71,7 +71,7 @@ def _check_integrity(self):
return check_integrity(os.path.join(self.root, self.names), self.checksums[self.names])
return True

def download(self):
def download(self) -> None:
if self._check_integrity():
print("Files already downloaded and verified")
return
Expand All @@ -81,13 +81,13 @@ def download(self):
if self.view == "people":
download_url(f"{self.download_url_prefix}{self.names}", self.root)

def _get_path(self, identity, no):
def _get_path(self, identity: str, no: Union[int, str]) -> str:
return os.path.join(self.images_dir, identity, f"{identity}_{int(no):04d}.jpg")

def extra_repr(self) -> str:
return f"Alignment: {self.image_set}\nSplit: {self.split}"

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


Expand Down Expand Up @@ -119,13 +119,13 @@ def __init__(
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
):
) -> None:
super().__init__(root, split, image_set, "people", transform, target_transform, download)

self.class_to_idx = self._get_classes()
self.data, self.targets = self._get_people()

def _get_people(self):
def _get_people(self) -> Tuple[List[str], List[int]]:
data, targets = [], []
with open(os.path.join(self.root, self.labels_file)) as f:
lines = f.readlines()
Expand All @@ -143,7 +143,7 @@ def _get_people(self):

return data, targets

def _get_classes(self):
def _get_classes(self) -> Dict[str, int]:
with open(os.path.join(self.root, self.names)) as f:
lines = f.readlines()
names = [line.strip().split()[0] for line in lines]
Expand Down Expand Up @@ -201,12 +201,12 @@ def __init__(
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
):
) -> None:
super().__init__(root, split, image_set, "pairs", transform, target_transform, download)

self.pair_names, self.data, self.targets = self._get_pairs(self.images_dir)

def _get_pairs(self, images_dir):
def _get_pairs(self, images_dir: str) -> Tuple[List[Tuple[str, str]], List[Tuple[str, str]], List[int]]:
pair_names, data, targets = [], [], []
with open(os.path.join(self.root, self.labels_file)) as f:
lines = f.readlines()
Expand Down