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 a generic test for the datasets #1015

Merged
merged 2 commits into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion test/fakedata_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _make_image_file(filename, num_images):
f.write(img.numpy().tobytes())

def _make_label_file(filename, num_images):
labels = torch.randint(0, 10, size=(num_images,), dtype=torch.uint8)
labels = torch.zeros((num_images,), dtype=torch.uint8)
with open(filename, "wb") as f:
f.write(_encode(2049)) # magic header
f.write(_encode(num_images))
Expand Down
62 changes: 20 additions & 42 deletions test/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
from fakedata_generation import mnist_root, cifar_root, imagenet_root


def generic_dataset_test(tester, dataset, num_images=1, cls='fakedata'):
Copy link
Member

Choose a reason for hiding this comment

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

Can you maybe rename this function to basic_classification_dataset_test or something that includes classification in it? Not all datasets have the class_to_idx attribute in it, so I'd not want (at least for now) to force people to add it just to make it compliant with this test. Maybe in the future we could have a ClassificationDataset that has this attribute, and then we could start enforcing the presence of this attribute.

Also, any particular reason why this is not a method in Tester? If it doesn't start with test, it won't be run by the Tester, so we can have helper functions inside Tester.

Copy link
Collaborator Author

@pmeier pmeier Jun 15, 2019

Choose a reason for hiding this comment

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

[...] many of our datasets do not return (Image, int), so I'd rather not give the impression that they should.

Shame on me, I keep forgetting that.

Also, any particular reason why this is not a method in Tester? If it doesn't start with test, it won't be run by the Tester [...]

I did not know that. Thanks for the clarification.

tester.assertEqual(len(dataset), num_images)
img, target = dataset[0]
tester.assertTrue(isinstance(img, PIL.Image.Image))
tester.assertTrue(isinstance(target, int))
tester.assertEqual(dataset.class_to_idx[cls], target)


class Tester(unittest.TestCase):
def test_imagefolder(self):
# TODO: create the fake data on-the-fly
Expand Down Expand Up @@ -64,47 +72,33 @@ def test_mnist(self, mock_download_extract):
num_examples = 30
with mnist_root(num_examples, "MNIST") as root:
dataset = torchvision.datasets.MNIST(root, download=True)
self.assertEqual(len(dataset), num_examples)
img, target = dataset[0]
self.assertTrue(isinstance(img, PIL.Image.Image))
self.assertTrue(isinstance(target, int))
generic_dataset_test(self, dataset, num_images=num_examples,
cls=dataset.classes[0])

@mock.patch('torchvision.datasets.mnist.download_and_extract_archive')
def test_kmnist(self, mock_download_extract):
num_examples = 30
with mnist_root(num_examples, "KMNIST") as root:
dataset = torchvision.datasets.KMNIST(root, download=True)
img, target = dataset[0]
self.assertEqual(len(dataset), num_examples)
self.assertTrue(isinstance(img, PIL.Image.Image))
self.assertTrue(isinstance(target, int))
generic_dataset_test(self, dataset, num_images=num_examples,
cls=dataset.classes[0])

@mock.patch('torchvision.datasets.mnist.download_and_extract_archive')
def test_fashionmnist(self, mock_download_extract):
num_examples = 30
with mnist_root(num_examples, "FashionMNIST") as root:
dataset = torchvision.datasets.FashionMNIST(root, download=True)
img, target = dataset[0]
self.assertEqual(len(dataset), num_examples)
self.assertTrue(isinstance(img, PIL.Image.Image))
self.assertTrue(isinstance(target, int))
generic_dataset_test(self, dataset, num_images=num_examples,
cls=dataset.classes[0])

@mock.patch('torchvision.datasets.utils.download_url')
def test_imagenet(self, mock_download):
with imagenet_root() as root:
dataset = torchvision.datasets.ImageNet(root, split='train', download=True)
self.assertEqual(len(dataset), 1)
img, target = dataset[0]
self.assertTrue(isinstance(img, PIL.Image.Image))
self.assertTrue(isinstance(target, int))
self.assertEqual(dataset.class_to_idx['fakedata'], target)
generic_dataset_test(self, dataset)

dataset = torchvision.datasets.ImageNet(root, split='val', download=True)
self.assertEqual(len(dataset), 1)
img, target = dataset[0]
self.assertTrue(isinstance(img, PIL.Image.Image))
self.assertTrue(isinstance(target, int))
self.assertEqual(dataset.class_to_idx['fakedata'], target)
generic_dataset_test(self, dataset)

@mock.patch('torchvision.datasets.cifar.check_integrity')
@mock.patch('torchvision.datasets.cifar.CIFAR10._check_integrity')
Expand All @@ -113,18 +107,10 @@ def test_cifar10(self, mock_ext_check, mock_int_check):
mock_int_check.return_value = True
with cifar_root('CIFAR10') as root:
dataset = torchvision.datasets.CIFAR10(root, train=True, download=True)
self.assertEqual(len(dataset), 5)
img, target = dataset[0]
self.assertTrue(isinstance(img, PIL.Image.Image))
self.assertTrue(isinstance(target, int))
self.assertEqual(dataset.class_to_idx['fakedata'], target)
generic_dataset_test(self, dataset, num_images=5)

dataset = torchvision.datasets.CIFAR10(root, train=False, download=True)
self.assertEqual(len(dataset), 1)
img, target = dataset[0]
self.assertTrue(isinstance(img, PIL.Image.Image))
self.assertTrue(isinstance(target, int))
self.assertEqual(dataset.class_to_idx['fakedata'], target)
generic_dataset_test(self, dataset)

@mock.patch('torchvision.datasets.cifar.check_integrity')
@mock.patch('torchvision.datasets.cifar.CIFAR10._check_integrity')
Expand All @@ -133,18 +119,10 @@ def test_cifar100(self, mock_ext_check, mock_int_check):
mock_int_check.return_value = True
with cifar_root('CIFAR100') as root:
dataset = torchvision.datasets.CIFAR100(root, train=True, download=True)
self.assertEqual(len(dataset), 1)
img, target = dataset[0]
self.assertTrue(isinstance(img, PIL.Image.Image))
self.assertTrue(isinstance(target, int))
self.assertEqual(dataset.class_to_idx['fakedata'], target)
generic_dataset_test(self, dataset)

dataset = torchvision.datasets.CIFAR100(root, train=False, download=True)
self.assertEqual(len(dataset), 1)
img, target = dataset[0]
self.assertTrue(isinstance(img, PIL.Image.Image))
self.assertTrue(isinstance(target, int))
self.assertEqual(dataset.class_to_idx['fakedata'], target)
generic_dataset_test(self, dataset)


if __name__ == '__main__':
Expand Down