Skip to content
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

add typehints for .datasets.samplers #2667

Merged
merged 1 commit into from Sep 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 23 additions & 16 deletions torchvision/datasets/samplers/clip_sampler.py
Expand Up @@ -3,6 +3,7 @@
from torch.utils.data import Sampler
import torch.distributed as dist
from torchvision.datasets.video_utils import VideoClips
from typing import Optional, List, Iterator, Sized, Union, cast


class DistributedSampler(Sampler):
Expand Down Expand Up @@ -34,7 +35,14 @@ class DistributedSampler(Sampler):

"""

def __init__(self, dataset, num_replicas=None, rank=None, shuffle=False, group_size=1):
def __init__(
self,
dataset: Sized,
num_replicas: Optional[int] = None,
rank: Optional[int] = None,
shuffle: bool = False,
group_size: int = 1,
) -> None:
if num_replicas is None:
if not dist.is_available():
raise RuntimeError("Requires distributed package to be available")
Expand All @@ -60,10 +68,11 @@ def __init__(self, dataset, num_replicas=None, rank=None, shuffle=False, group_s
self.total_size = self.num_samples * self.num_replicas
self.shuffle = shuffle

def __iter__(self):
def __iter__(self) -> Iterator[int]:
# deterministically shuffle based on epoch
g = torch.Generator()
g.manual_seed(self.epoch)
indices: Union[torch.Tensor, List[int]]
if self.shuffle:
indices = torch.randperm(len(self.dataset), generator=g).tolist()
else:
Expand All @@ -89,10 +98,10 @@ def __iter__(self):

return iter(indices)

def __len__(self):
def __len__(self) -> int:
return self.num_samples

def set_epoch(self, epoch):
def set_epoch(self, epoch: int) -> None:
self.epoch = epoch


Expand All @@ -106,14 +115,14 @@ class UniformClipSampler(Sampler):
video_clips (VideoClips): video clips to sample from
num_clips_per_video (int): number of clips to be sampled per video
"""
def __init__(self, video_clips, num_clips_per_video):
def __init__(self, video_clips: VideoClips, num_clips_per_video: int) -> None:
if not isinstance(video_clips, VideoClips):
raise TypeError("Expected video_clips to be an instance of VideoClips, "
"got {}".format(type(video_clips)))
self.video_clips = video_clips
self.num_clips_per_video = num_clips_per_video

def __iter__(self):
def __iter__(self) -> Iterator[int]:
idxs = []
s = 0
# select num_clips_per_video for each video, uniformly spaced
Expand All @@ -130,10 +139,9 @@ def __iter__(self):
)
s += length
idxs.append(sampled)
idxs = torch.cat(idxs).tolist()
return iter(idxs)
return iter(cast(List[int], torch.cat(idxs).tolist()))

def __len__(self):
def __len__(self) -> int:
return sum(
self.num_clips_per_video for c in self.video_clips.clips if len(c) > 0
)
Expand All @@ -147,14 +155,14 @@ class RandomClipSampler(Sampler):
video_clips (VideoClips): video clips to sample from
max_clips_per_video (int): maximum number of clips to be sampled per video
"""
def __init__(self, video_clips, max_clips_per_video):
def __init__(self, video_clips: VideoClips, max_clips_per_video: int) -> None:
if not isinstance(video_clips, VideoClips):
raise TypeError("Expected video_clips to be an instance of VideoClips, "
"got {}".format(type(video_clips)))
self.video_clips = video_clips
self.max_clips_per_video = max_clips_per_video

def __iter__(self):
def __iter__(self) -> Iterator[int]:
idxs = []
s = 0
# select at most max_clips_per_video for each video, randomly
Expand All @@ -164,11 +172,10 @@ def __iter__(self):
sampled = torch.randperm(length)[:size] + s
s += length
idxs.append(sampled)
idxs = torch.cat(idxs)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This scheme of overriding the variable type was quite common here. In this example idxs was List[torch.Tensor] before, torch.Tensor here, and List[int] later. Thus, I need to rename some variables or merge statements to avoid assignments all together. Otherwise, this will be an even more complicated mess with a lot of casting.

idxs_ = torch.cat(idxs)
# shuffle all clips randomly
perm = torch.randperm(len(idxs))
idxs = idxs[perm].tolist()
return iter(idxs)
perm = torch.randperm(len(idxs_))
return iter(idxs_[perm].tolist())

def __len__(self):
def __len__(self) -> int:
return sum(min(len(c), self.max_clips_per_video) for c in self.video_clips.clips)