実現したいこと
RuntimeErrorを解決したい。
前提
https://qiita.com/Kentea/items/e4263a90f94738213483
画像分類を行うために上記のサイトを参考にしてコードを組んでいるのですが、私の該当のソースコードの1番最後の行で発生するRuntimeErrorの解決策が、手を尽くしたのですが分かりません。どなたか解決法をご教授くださると嬉しいです。
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) Cell In[8], line 31 29 class_names = image_datasets['train'].classes 30 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") ---> 31 inputs, classes = next(iter(dataloaders['train'])) File ~\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\utils\data\dataloader.py:633, in _BaseDataLoaderIter.__next__(self) 630 if self._sampler_iter is None: 631 # TODO(https://github.com/pytorch/pytorch/issues/76750) 632 self._reset() # type: ignore[call-arg] --> 633 data = self._next_data() 634 self._num_yielded += 1 635 if self._dataset_kind == _DatasetKind.Iterable and \ 636 self._IterableDataset_len_called is not None and \ 637 self._num_yielded > self._IterableDataset_len_called: File ~\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\utils\data\dataloader.py:1345, in _MultiProcessingDataLoaderIter._next_data(self) 1343 else: 1344 del self._task_info[idx] -> 1345 return self._process_data(data) File ~\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\utils\data\dataloader.py:1371, in _MultiProcessingDataLoaderIter._process_data(self, data) 1369 self._try_put_index() 1370 if isinstance(data, ExceptionWrapper): -> 1371 data.reraise() 1372 return data File ~\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\_utils.py:644, in ExceptionWrapper.reraise(self) 640 except TypeError: 641 # If the exception takes multiple arguments, don't try to 642 # instantiate since we don't know how to 643 raise RuntimeError(msg) from None --> 644 raise exception RuntimeError: Caught RuntimeError in DataLoader worker process 0. Original Traceback (most recent call last): File "C:\Users\pkmn9\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\utils\data\_utils\worker.py", line 308, in _worker_loop data = fetcher.fetch(index) ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pkmn9\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\utils\data\_utils\fetch.py", line 54, in fetch return self.collate_fn(data) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pkmn9\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\utils\data\_utils\collate.py", line 265, in default_collate return collate(batch, collate_fn_map=default_collate_fn_map) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pkmn9\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\utils\data\_utils\collate.py", line 142, in collate return [collate(samples, collate_fn_map=collate_fn_map) for samples in transposed] # Backwards compatibility. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pkmn9\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\utils\data\_utils\collate.py", line 142, in <listcomp> return [collate(samples, collate_fn_map=collate_fn_map) for samples in transposed] # Backwards compatibility. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pkmn9\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\utils\data\_utils\collate.py", line 119, in collate return collate_fn_map[elem_type](batch, collate_fn_map=collate_fn_map) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pkmn9\Programming\JupyterNotebook\Pytorch_study\PySt\Lib\site-packages\torch\utils\data\_utils\collate.py", line 161, in collate_tensor_fn out = elem.new(storage).resize_(len(batch), *list(elem.size())) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: Trying to resize storage that is not resizable
該当のソースコード
Python
1import torch 2from torchvision import datasets, transforms 3from torch.utils.data import DataLoader 4import numpy as np 5import os 6 7data_transforms = {8 'train': transforms.Compose([9 transforms.Resize(224),10 transforms.ToTensor(),11 ]),12 'validation': transforms.Compose([13 transforms.Resize(224),14 transforms.ToTensor(),15 ]),16}17 18data_dir = 'vuis_data'19image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),20 data_transforms[x])21 for x in ['train', 'validation']} 22 23batch_size = 824dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size,25 shuffle=True, num_workers=2)26 for x in ['train', 'validation']}27 28dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'validation']}29class_names = image_datasets['train'].classes 30device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")31inputs, classes = next(iter(dataloaders['train']))
【データセットの構造】
試したこと
エラーの文章を見るに画像のサイズの問題のように思えますが、data_transformsで224に設定しているはずなので何故かが分からないです。
https://discuss.pytorch.org/t/runtime-error-on-using-next-iter-dataloader/126979
上記のサイトを参考に、
def main(dataloaders): for i, data in enumerate(dataloaders): images, labels = next(iter(data)) if __name__ == '__main__': main(dataloaders)
としてみましたが、
ValueError Traceback (most recent call last)
Cell In[11], line 8
5 images, labels = next(iter(data))
7 if name == 'main':
----> 8 main(dataloaders)
Cell In[11], line 5, in main(dataloaders)
3 def main(dataloaders):
4 for i, data in enumerate(dataloaders):
----> 5 images, labels = next(iter(data))
ValueError: not enough values to unpack (expected 2, got 1)
というエラーが出て解決しませんでした。
補足情報(FW/ツールのバージョンなど)
Windows==11
Python==3.11.5
torch==2.0.1
torchvision==0.15.2
tornado==6.3.3
numpy==1.25.2
0 コメント