-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add MovingMNIST dataset #7042
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
Add MovingMNIST dataset #7042
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
930be46
add moving mnist dataset
tsugumi-sys e90901a
remove unused modules
tsugumi-sys cf01151
modify docstring
tsugumi-sys bebc473
modify docstring and docs
tsugumi-sys 5b51812
add split and split ratio kwargs
tsugumi-sys 0d9bb5e
fix checking split argument
tsugumi-sys ea54f67
xCXMerge branch 'main' into add-moving-mnist-dataset
tsugumi-sys c9c73ad
remove unused package
tsugumi-sys 22889d5
delete lines
tsugumi-sys c9417e6
fix filename property
tsugumi-sys f0e6265
Merge branch 'main' into add-moving-mnist-dataset
tsugumi-sys 10863c4
fix reviews
tsugumi-sys 26fc123
modify docstrings
tsugumi-sys eacd778
add split tests and etc
tsugumi-sys 276aa08
fix tests
tsugumi-sys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import os.path | ||
from typing import Callable, Optional | ||
|
||
import numpy as np | ||
import torch | ||
from torchvision.datasets.utils import download_url, verify_str_arg | ||
from torchvision.datasets.vision import VisionDataset | ||
|
||
|
||
class MovingMNIST(VisionDataset): | ||
"""`MovingMNIST <http://www.cs.toronto.edu/~nitish/unsupervised_video/>`_ Dataset. | ||
|
||
Args: | ||
root (string): 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 | ||
frames ``data[:, :split_ratio]`` is returned. If ``split="test"``, the last split frames ``data[:, split_ratio:]`` | ||
is returned. If ``split=None``, this parameter is ignored and the all frames data is returned. | ||
transform (callable, optional): A function/transform that takes in an torch Tensor | ||
and returns a transformed version. E.g, ``transforms.RandomCrop`` | ||
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. | ||
""" | ||
|
||
_URL = "http://www.cs.toronto.edu/~nitish/unsupervised_video/mnist_test_seq.npy" | ||
|
||
def __init__( | ||
self, | ||
root: str, | ||
split: Optional[str] = None, | ||
split_ratio: int = 10, | ||
download: bool = False, | ||
transform: Optional[Callable] = None, | ||
) -> None: | ||
super().__init__(root, transform=transform) | ||
|
||
self._base_folder = os.path.join(self.root, self.__class__.__name__) | ||
self._filename = self._URL.split("/")[-1] | ||
|
||
if split is not None: | ||
verify_str_arg(split, "split", ("train", "test")) | ||
self.split = split | ||
|
||
if not isinstance(split_ratio, int): | ||
raise TypeError(f"`split_ratio` should be an integer, but got {type(split_ratio)}") | ||
elif not (1 <= split_ratio <= 19): | ||
raise ValueError(f"`split_ratio` should be `1 <= split_ratio <= 19`, but got {split_ratio} instead.") | ||
self.split_ratio = split_ratio | ||
tsugumi-sys marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if download: | ||
self.download() | ||
|
||
if not self._check_exists(): | ||
raise RuntimeError("Dataset not found. You can use download=True to download it.") | ||
|
||
data = torch.from_numpy(np.load(os.path.join(self._base_folder, self._filename))) | ||
if self.split == "train": | ||
data = data[: self.split_ratio] | ||
else: | ||
data = data[self.split_ratio :] | ||
self.data = data.transpose(0, 1).unsqueeze(2).contiguous() | ||
|
||
def __getitem__(self, idx: int) -> torch.Tensor: | ||
""" | ||
Args: | ||
index (int): Index | ||
Returns: | ||
torch.Tensor: Video frames (torch Tensor[T, C, H, W]). The `T` is the number of frames. | ||
""" | ||
data = self.data[idx] | ||
if self.transform is not None: | ||
data = self.transform(data) | ||
|
||
return data | ||
|
||
def __len__(self) -> int: | ||
return len(self.data) | ||
|
||
def _check_exists(self) -> bool: | ||
return os.path.exists(os.path.join(self._base_folder, self._filename)) | ||
|
||
def download(self) -> None: | ||
if self._check_exists(): | ||
return | ||
|
||
download_url( | ||
url=self._URL, | ||
root=self._base_folder, | ||
filename=self._filename, | ||
md5="be083ec986bfe91a449d63653c411eb2", | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.