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

Hotfix for to_pytorch #457

Merged
merged 2 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions hub/api/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,14 @@ def to_pytorch(
num_samples: int, optional
The number of samples required of the dataset that needs to be converted
"""
if "torch" not in sys.modules:
try:
import torch
except ModuleNotFoundError:
raise ModuleNotInstalledException("torch")
import torch

global torch

self.flush() # FIXME Without this some tests in test_converters.py fails, not clear why
if "r" not in self.mode:
self.flush() # FIXME Without this some tests in test_converters.py fails, not clear why
return TorchDataset(
self,
transform,
Expand Down
6 changes: 6 additions & 0 deletions hub/api/tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ def test_to_from_pytorch():
assert (res_ds["label", "d", "e", i].numpy() == i * np.ones((5, 3))).all()


@pytest.mark.skipif(not pytorch_loaded(), reason="requires pytorch to be loaded")
def test_to_pytorch_bug():
ds = hub.Dataset("activeloop/mnist", mode="r")
data = ds.to_pytorch()


if __name__ == "__main__":
with Timer("Test Converters"):
with Timer("from MNIST"):
Expand Down