Error when importing torchvision

ErA: <p>Hi there,</p>
<p>I have installed pytorch (conda install pytorch torchvision cudatoolkit=10.1 -c pytorch) on an Ubuntu platform with cuda 10.2 with no error.<br>
When I try to import torchvision, I encounter the following error:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/5/45100c0e8ac4a35aa39bc234e26c9cd3ba246424.png" data-download-href="https://discuss.pytorch.org/uploads/default/45100c0e8ac4a35aa39bc234e26c9cd3ba246424" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/5/45100c0e8ac4a35aa39bc234e26c9cd3ba246424_2_690x311.png" alt="image" data-base62-sha1="9QXkdWnTzXXq2py4xXdtIcvgQwA" width="690" height="311" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/5/45100c0e8ac4a35aa39bc234e26c9cd3ba246424_2_690x311.png, https://discuss.pytorch.org/uploads/default/original/3X/4/5/45100c0e8ac4a35aa39bc234e26c9cd3ba246424.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/5/45100c0e8ac4a35aa39bc234e26c9cd3ba246424.png 2x" data-dominant-color="F5F7F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">986×445 53.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I was wondering if anybody could help?<br>
Thank you!</p>

ptrblck: <p>That’s a weird error.<br>
Could you check, what classes are defined in <code>.../site-packages/torchvision/datasets/mnist.py</code>?<br>
<code>QMNIST</code> was added 8 months ago, so I’m wondering why it cannot be imported.</p>

ErA: <p>They are:<br>
MNIST, FashionMNIST, KMNIST, EMNIST, QMNIST.<br>
I copied QMNIST :</p>
<p>class QMNIST(MNIST):<br>
“”"<code>QMNIST &lt;https://github.com/facebookresearch/qmnist&gt;</code>_ Dataset.</p>
<pre><code>Args:
    root (string): Root directory of dataset whose ``processed''
        subdir contains torch binary files with the datasets.
    what (string,optional): Can be 'train', 'test', 'test10k',
        'test50k', or 'nist' for respectively the mnist compatible
        training set, the 60k qmnist testing set, the 10k qmnist
        examples that match the mnist testing set, the 50k
        remaining qmnist testing examples, or all the nist
        digits. The default is to select 'train' or 'test'
        according to the compatibility argument 'train'.
    compat (bool,optional): A boolean that says whether the target
        for each example is class number (for compatibility with
        the MNIST dataloader) or a torch vector containing the
        full qmnist information. Default=True.
    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.
    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.
    train (bool,optional,compatibility): When argument 'what' is
        not specified, this boolean decides whether to load the
        training set ot the testing set.  Default: True.

"""

subsets = {
    'train': 'train',
    'test': 'test',
    'test10k': 'test',
    'test50k': 'test',
    'nist': 'nist'
}
resources = {
    'train': [('https://raw.githubusercontent.com/facebookresearch/qmnist/master/qmnist-train-images-idx3-ubyte.gz',
               'ed72d4157d28c017586c42bc6afe6370'),
              ('https://raw.githubusercontent.com/facebookresearch/qmnist/master/qmnist-train-labels-idx2-int.gz',
               '0058f8dd561b90ffdd0f734c6a30e5e4')],
    'test': [('https://raw.githubusercontent.com/facebookresearch/qmnist/master/qmnist-test-images-idx3-ubyte.gz',
              '1394631089c404de565df7b7aeaf9412'),
             ('https://raw.githubusercontent.com/facebookresearch/qmnist/master/qmnist-test-labels-idx2-int.gz',
              '5b5b05890a5e13444e108efe57b788aa')],
    'nist': [('https://raw.githubusercontent.com/facebookresearch/qmnist/master/xnist-images-idx3-ubyte.xz',
              '7f124b3b8ab81486c9d8c2749c17f834'),
             ('https://raw.githubusercontent.com/facebookresearch/qmnist/master/xnist-labels-idx2-int.xz',
              '5ed0e788978e45d4a8bd4b7caec3d79d')]
}
classes = ['0 - zero', '1 - one', '2 - two', '3 - three', '4 - four',
           '5 - five', '6 - six', '7 - seven', '8 - eight', '9 - nine']

def __init__(self, root, what=None, compat=True, train=True, **kwargs):
    if what is None:
        what = 'train' if train else 'test'
    self.what = verify_str_arg(what, "what", tuple(self.subsets.keys()))
    self.compat = compat
    self.data_file = what + '.pt'
    self.training_file = self.data_file
    self.test_file = self.data_file
    super(QMNIST, self).__init__(root, train, **kwargs)

def download(self):
    """Download the QMNIST data if it doesn't exist in processed_folder already.
       Note that we only download what has been asked for (argument 'what').
    """
    if self._check_exists():
        return
    makedir_exist_ok(self.raw_folder)
    makedir_exist_ok(self.processed_folder)
    split = self.resources[self.subsets[self.what]]
    files = []

    # download data files if not already there
    for url, md5 in split:
        filename = url.rpartition('/')[2]
        file_path = os.path.join(self.raw_folder, filename)
        if not os.path.isfile(file_path):
            download_url(url, root=self.raw_folder, filename=filename, md5=md5)
        files.append(file_path)

    # process and save as torch files
    print('Processing...')
    data = read_sn3_pascalvincent_tensor(files[0])
    assert(data.dtype == torch.uint8)
    assert(data.ndimension() == 3)
    targets = read_sn3_pascalvincent_tensor(files[1]).long()
    assert(targets.ndimension() == 2)
    if self.what == 'test10k':
        data = data[0:10000, :, :].clone()
        targets = targets[0:10000, :].clone()
    if self.what == 'test50k':
        data = data[10000:, :, :].clone()
        targets = targets[10000:, :].clone()
    with open(os.path.join(self.processed_folder, self.data_file), 'wb') as f:
        torch.save((data, targets), f)

def __getitem__(self, index):
    # redefined to handle the compat flag
    img, target = self.data[index], self.targets[index]
    img = Image.fromarray(img.numpy(), mode='L')
    if self.transform is not None:
        img = self.transform(img)
    if self.compat:
        target = int(target[0])
    if self.target_transform is not None:
        target = self.target_transform(target)
    return img, target

def extra_repr(self):
    return "Split: {}".format(self.what)</code></pre>

ptrblck: <p>So you see the class definition in the file and still get the error?<br>
Could you just for the sake of debugging create a new (conda) environment and reinstall the latest PyTorch and torchvision versions, please?</p>

ErA: <p>Thank you Ptrblck!<br>
It works now.<br>
Solution: I created a new conda environment and reinstalled pytorch.</p>

Wei_Xiao: <p>Thank you very much, I have a same problem, and now it’s solved.</p>