首页 > 解决方案 > 我无法导入 ucf 101 数据集 (torchvision),“列表索引超出范围”错误

问题描述

dataset = torchvision.datasets.UCF101(r'my_directory', annotation_path=r'my_directory2', frames_per_clip=16, step_between_clips=1, frame_rate=None, fold=1, train=True, transform=transforms.Compose([transforms.ToTensor()]), _precomputed_metadata=None, num_workers=1, _video_width=64, _video_height=64, _video_min_dimension=0, _audio_samples=0)

这条线有效,问题是当我尝试使用“数据集”进行任何类型的操作时,特别是这个:

data = torch.utils.data.DataLoader(dataset, batch_size=512, shuffle=True)

我收到错误,所以我无法处理视频数据,因为我无法使用 dataLoader,错误是:

IndexError: list index out of range

完整的错误信息:


    ---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-16-dc3631481cb3> in <module>
----> 1 data = torch.utils.data.DataLoader(dataset, batch_size=512, shuffle=True)

~\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in __init__(self, dataset, batch_size, shuffle, sampler, batch_sampler, num_workers, collate_fn, pin_memory, drop_last, timeout, worker_init_fn, multiprocessing_context)
    211             else:  # map-style
    212                 if shuffle:
--> 213                     sampler = RandomSampler(dataset)
    214                 else:
    215                     sampler = SequentialSampler(dataset)

~\anaconda3\lib\site-packages\torch\utils\data\sampler.py in __init__(self, data_source, replacement, num_samples)
     90                              "since a random permute will be performed.")
     91 
---> 92         if not isinstance(self.num_samples, int) or self.num_samples <= 0:
     93             raise ValueError("num_samples should be a positive integer "
     94                              "value, but got num_samples={}".format(self.num_samples))

~\anaconda3\lib\site-packages\torch\utils\data\sampler.py in num_samples(self)
     98         # dataset size might change at runtime
     99         if self._num_samples is None:
--> 100             return len(self.data_source)
    101         return self._num_samples
    102 

~\anaconda3\lib\site-packages\torchvision\datasets\ucf101.py in __len__(self)
     96 
     97     def __len__(self):
---> 98         return self.video_clips.num_clips()
     99 
    100     def __getitem__(self, idx):

~\anaconda3\lib\site-packages\torchvision\datasets\video_utils.py in num_clips(self)
    241         Number of subclips that are available in the video list.
    242         """
--> 243         return self.cumulative_sizes[-1]
    244 
    245     def get_clip_location(self, idx):

IndexError: list index out of range

标签: pythondatasetpytorchtorchvision

解决方案


问题:

当您在 Windows 上运行代码时会出现此问题,因为 Windows 路径使用反斜杠 ("\") 而不是正斜杠 ("/")。

正如您在代码中看到的:

https://github.com/pytorch/vision/blob/7b9d30eb7c4d92490d9ac038a140398e0a690db6/torchvision/datasets/ucf101.py#L94

因此,这行代码从标签文件中读取文件路径为“action\video_name”,并使用反斜杠将其与“root”路径合并,因此完整路径变为“root\action/video_name”。此类路径与第 97 行的视频列表不匹配,并返回索引变量的空列表。

解决方案:

两种可能的解决方案可以是:

  1. 将标签文件中的正斜杠“/”替换为反斜杠“\”。
  2. 覆盖 UCF101 类的 _select_fold(…) 函数并修复函数内部的反斜杠

推荐阅读