"tests/pipelines/test_pipelines.py" did not exist on "29b2c93c9005c87f8f04b1f0835babbcea736204"
svhn.py 5.09 KB
Newer Older
1
2
3
4
5
6
from __future__ import print_function
import torch.utils.data as data
from PIL import Image
import os
import os.path
import numpy as np
soumith's avatar
soumith committed
7
from .utils import download_url, check_integrity
8

soumith's avatar
soumith committed
9

10
class SVHN(data.Dataset):
11
    """`SVHN <http://ufldl.stanford.edu/housenumbers/>`_ Dataset.
12
13
14
    Note: The SVHN dataset assigns the label `10` to the digit `0`. However, in this Dataset,
    we assign the label `0` to the digit `0` to be compatible with PyTorch loss functions which
    expect the class labels to be in the range `[0, C-1]`
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

    Args:
        root (string): Root directory of dataset where directory
            ``SVHN`` exists.
        split (string): One of {'train', 'test', 'extra'}.
            Accordingly dataset is selected. 'extra' is Extra training set.
        transform (callable, optional): A function/transform that  takes in an PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        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.

    """
30
31
32
33
34
35
36
37
38
39
40
41
    url = ""
    filename = ""
    file_md5 = ""

    split_list = {
        'train': ["http://ufldl.stanford.edu/housenumbers/train_32x32.mat",
                  "train_32x32.mat", "e26dedcc434d2e4c54c9b2d4a06d8373"],
        'test': ["http://ufldl.stanford.edu/housenumbers/test_32x32.mat",
                 "test_32x32.mat", "eb5a983be6a315427106f1b164d9cef3"],
        'extra': ["http://ufldl.stanford.edu/housenumbers/extra_32x32.mat",
                  "extra_32x32.mat", "a93ce644f1a588dc4d68dda5feec44a7"]}

soumith's avatar
soumith committed
42
43
    def __init__(self, root, split='train',
                 transform=None, target_transform=None, download=False):
44
        self.root = os.path.expanduser(root)
45
46
47
48
49
        self.transform = transform
        self.target_transform = target_transform
        self.split = split  # training set or test set or extra set

        if self.split not in self.split_list:
soumith's avatar
soumith committed
50
51
            raise ValueError('Wrong split entered! Please use split="train" '
                             'or split="extra" or split="test"')
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

        self.url = self.split_list[split][0]
        self.filename = self.split_list[split][1]
        self.file_md5 = self.split_list[split][2]

        if download:
            self.download()

        if not self._check_integrity():
            raise RuntimeError('Dataset not found or corrupted.' +
                               ' You can use download=True to download it')

        # import here rather than at top of file because this is
        # an optional dependency for torchvision
        import scipy.io as sio

        # reading(loading) mat file as array
moskomule's avatar
moskomule committed
69
        loaded_mat = sio.loadmat(os.path.join(self.root, self.filename))
70
71

        self.data = loaded_mat['X']
72
        # loading from the .mat file gives an np array of type np.uint8
vabh's avatar
vabh committed
73
        # converting to np.int64, so that we have a LongTensor after
74
75
76
        # the conversion from the numpy array
        # the squeeze is needed to obtain a 1D tensor
        self.labels = loaded_mat['y'].astype(np.int64).squeeze()
vabh's avatar
vabh committed
77

78
        # the svhn dataset assigns the class label "10" to the digit 0
vabh's avatar
vabh committed
79
        # this makes it inconsistent with several loss functions
80
        # which expect the class labels to be in the range [0, C-1]
vabh's avatar
vabh committed
81
        np.place(self.labels, self.labels == 10, 0)
82
83
84
        self.data = np.transpose(self.data, (3, 2, 0, 1))

    def __getitem__(self, index):
85
86
87
88
89
90
91
        """
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        """
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        img, target = self.data[index], self.labels[index]

        # doing this so that it is consistent with all other datasets
        # to return a PIL Image
        img = Image.fromarray(np.transpose(img, (1, 2, 0)))

        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = self.target_transform(target)

        return img, target

    def __len__(self):
        return len(self.data)

    def _check_integrity(self):
        root = self.root
        md5 = self.split_list[self.split][2]
        fpath = os.path.join(root, self.filename)
soumith's avatar
soumith committed
113
        return check_integrity(fpath, md5)
114
115

    def download(self):
soumith's avatar
soumith committed
116
117
        md5 = self.split_list[self.split][2]
        download_url(self.url, self.root, self.filename, md5)
118
119
120
121
122
123
124
125
126
127
128

    def __repr__(self):
        fmt_str = 'Dataset ' + self.__class__.__name__ + '\n'
        fmt_str += '    Number of datapoints: {}\n'.format(self.__len__())
        fmt_str += '    Split: {}\n'.format(self.split)
        fmt_str += '    Root Location: {}\n'.format(self.root)
        tmp = '    Transforms (if any): '
        fmt_str += '{0}{1}\n'.format(tmp, self.transform.__repr__().replace('\n', '\n' + ' ' * len(tmp)))
        tmp = '    Target Transforms (if any): '
        fmt_str += '{0}{1}'.format(tmp, self.target_transform.__repr__().replace('\n', '\n' + ' ' * len(tmp)))
        return fmt_str