pytorch JupyterLab上的运行时错误

v1uwarro  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(258)

我尝试在JupyterLab上运行以下示例代码(通过GCP顶点AI):

import torch
from torchvision import transforms
from torchvision import datasets

train_data = datasets.MNIST(root='data', train=True, download=True, transform=None)
print(train_data)

版本: Torch -1.12.1+铜-113 Torch 视觉-0.13.1+铜-113

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_10081/229378695.py in <module>
     11 from torchvision import datasets
     12 
---> 13 train_data = datasets.MNIST(root='data', train=True, download=True, transform=None)
     14 print(train_data)

/opt/conda/lib/python3.7/site-packages/torchvision/datasets/mnist.py in __init__(self, root, train, transform, target_transform, download)
    102             raise RuntimeError("Dataset not found. You can use download=True to download it")
    103 
--> 104         self.data, self.targets = self._load_data()
    105 
    106     def _check_legacy_exist(self):

/opt/conda/lib/python3.7/site-packages/torchvision/datasets/mnist.py in _load_data(self)
    121     def _load_data(self):
    122         image_file = f"{'train' if self.train else 't10k'}-images-idx3-ubyte"
--> 123         data = read_image_file(os.path.join(self.raw_folder, image_file))
    124 
    125         label_file = f"{'train' if self.train else 't10k'}-labels-idx1-ubyte"

/opt/conda/lib/python3.7/site-packages/torchvision/datasets/mnist.py in read_image_file(path)
    542 
    543 def read_image_file(path: str) -> torch.Tensor:
--> 544     x = read_sn3_pascalvincent_tensor(path, strict=False)
    545     if x.dtype != torch.uint8:
    546         raise TypeError(f"x should be of dtype torch.uint8 instead of {x.dtype}")

/opt/conda/lib/python3.7/site-packages/torchvision/datasets/mnist.py in read_sn3_pascalvincent_tensor(path, strict)
    529 
    530     assert parsed.shape[0] == np.prod(s) or not strict
--> 531     return parsed.view(*s)
    532 
    533 

RuntimeError: shape '[60000, 28, 28]' is invalid for input of size 9437168
____________________

我在尝试加载MNIST时收到这个奇怪的错误

  • 我试着在其他环境中复制它,但不行--它在本地和斗篷上都很好用
  • 我试过很多其他版本的torch和torchvision,但都不起作用
sdnqo3pr

sdnqo3pr1#

此错误通常是由下载到系统上的MNIST数据集文件的问题引起的。请尝试删除data目录中的MNIST数据集文件,然后再次运行代码以下载数据集文件的新副本。请按照以下代码操作:

import os
import shutil

mnist_folder = 'data/MNIST'
if os.path.exists(mnist_folder):
    shutil.rmtree(mnist_folder)

train_data = datasets.MNIST(root='data', train=True, download=True, transform=None)

如果此方法不起作用,请访问this website并将它们放在data/MNIST文件夹中。

相关问题