在google colab中使用GPU时获得无法设置的属性,但在使用CPU时却没有

数据挖掘 火炬 显卡 谷歌 合作实验室
2022-02-13 02:26:57

嗨,我正在学习使用我在 Udacity 中学习的 google colab 中的 pytorch 创建分类器。链接在这里

所以我在数据加载器中加载数据,当我使用 cpu 时,它加载并显示得很好。但我想到了使用 gpu,因为训练猫狗数据集需要时间。但奇怪的是,一个错误是属性错误:无法设置属性。

AttributeError                            Traceback (most recent call 

last)
<ipython-input-41-c1a77981bf85> in <module>()
----> 1 images, labels = next(iter(dataloader))
      2 
      3 
      4 imshow(images[0], normalize=False)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    613         if self.num_workers == 0:  # same-process loading
    614             indices = next(self.sample_iter)  # may raise StopIteration
--> 615             batch = self.collate_fn([self.dataset[i] for i in indices])
    616             if self.pin_memory:
    617                 batch = pin_memory_batch(batch)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in <listcomp>(.0)
    613         if self.num_workers == 0:  # same-process loading
    614             indices = next(self.sample_iter)  # may raise StopIteration
--> 615             batch = self.collate_fn([self.dataset[i] for i in indices])
    616             if self.pin_memory:
    617                 batch = pin_memory_batch(batch)

/usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py in __getitem__(self, index)
     99         """
    100         path, target = self.samples[index]
--> 101         sample = self.loader(path)
    102         if self.transform is not None:
    103             sample = self.transform(sample)

/usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py in default_loader(path)
    145         return accimage_loader(path)
    146     else:
--> 147         return pil_loader(path)
    148 
    149 

/usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py in pil_loader(path)
    127     # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    128     with open(path, 'rb') as f:
--> 129         img = Image.open(f)
    130         return img.convert('RGB')
    131 

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
   2670 
   2671 def effect_mandelbrot(size, extent, quality):
-> 2672     """
   2673     Generate a Mandelbrot set covering the given extent.
   2674 

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in _open_core(fp, filename, prefix)
   2656 # Simple display support.  User code may override this.
   2657 
-> 2658 def _show(image, **options):
   2659     # override me, as necessary
   2660     _showxv(image, **options)

/usr/local/lib/python3.6/dist-packages/PIL/JpegImagePlugin.py in jpeg_factory(fp, filename)
    762 # Factory for making JPEG and MPO instances
    763 def jpeg_factory(fp=None, filename=None):
--> 764     im = JpegImageFile(fp, filename)
    765     try:
    766         mpheader = im._getmp()

/usr/local/lib/python3.6/dist-packages/PIL/ImageFile.py in __init__(self, fp, filename)
     98 
     99         try:
--> 100             self._open()
    101         except (IndexError,  # end of data
    102                 TypeError,  # end of data (ord)

/usr/local/lib/python3.6/dist-packages/PIL/JpegImagePlugin.py in _open(self)
    333                 # print(hex(i), name, description)
    334                 if handler is not None:
--> 335                     handler(self, i)
    336                 if i == 0xFFDA:  # start of scan
    337                     rawmode = self.mode

/usr/local/lib/python3.6/dist-packages/PIL/JpegImagePlugin.py in SOF(self, marker)
    156     n = i16(self.fp.read(2))-2
    157     s = ImageFile._safe_read(self.fp, n)
--> 158     self.size = i16(s[3:]), i16(s[1:])
    159 
    160     self.bits = i8(s[0])

AttributeError: can't set attribute

这是我正在工作的代码:

data_dir = 'Cat_Dog_data/train'

transform = transforms.Compose([transforms.Resize(255),#to 255 square pic
                               transforms.CenterCrop(224),
                               transforms.ToTensor()])# TODO: compose transforms here
dataset = datasets.ImageFolder(data_dir,transform = transform)# TODO: create the ImageFolder
dataloader = torch.utils.data.DataLoader(dataset, batch_size = 32, shuffle = True)# TODO: use the ImageFolder dataset to create the DataLoader

# Run this to test your data loader
images, labels = next(iter(dataloader))


imshow(images[0], normalize=False)

因此,当我不使用 GPU 时程序运行良好,但使用 GPU 时出现问题。我不知道如何解决这个问题!

0个回答
没有发现任何回复~