Strange error while using torchvision.datasets.UCF101

costa98: <p>I am trying to import the ucf101 dataset using</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>But if I perform any kind of operation with ‘dataset’, like dataLoader or len() i get the error</p>
<pre><code class="lang-auto">IndexError: list index out of range
</code></pre>
<p>Full error message is:</p>
<pre><code class="lang-auto">
    ---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-16-dc3631481cb3&gt; in &lt;module&gt;
----&gt; 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:
--&gt; 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 
---&gt; 92         if not isinstance(self.num_samples, int) or self.num_samples &lt;= 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:
--&gt; 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):
---&gt; 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         """
--&gt; 243         return self.cumulative_sizes[-1]
    244 
    245     def get_clip_location(self, idx):

IndexError: list index out of range
</code></pre>

ptrblck: <p>Could you check, what</p>
<pre><code class="lang-python">print(dataset.video_clips, dataset.video_clips_metadata, dataset.indices)
</code></pre>
<p>returns?<br>
It seems that no video clips were found using the specified directories.</p>

costa98: <p>Thanks for your reply, this is the output:</p>
<pre><code class="lang-auto">&lt;torchvision.datasets.video_utils.VideoClips object at 0x0000028F45B39208&gt; 
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.

Current values:
NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)
NotebookApp.rate_limit_window=3.0 (secs)
</code></pre>

costa98: <p>So I fixed the rate_limit problem and I get:</p>
<pre><code class="lang-auto">&lt;torchvision.datasets.video_utils.VideoClips object at 0x000001B8B4381EC8&gt; {'video_paths': ['ucf101\\ApplyEyeMakeup\\v_ApplyEyeMakeup_g01_c01.avi', 'ucf101\\ApplyEyeMakeup\\v_ApplyEyeMakeup_g01_c02.avi', 'ucf101\\ApplyEyeMakeup\\v_ApplyEyeMakeup_g01_c03.avi',
</code></pre>
<p>till it arrives at the last videos</p>
<pre><code class="lang-auto">.... ucf101\\YoYo\\v_YoYo_g25_c04.avi', 'ucf101\\YoYo\\v_YoYo_g25_c05.avi'],
</code></pre>
<p>For the metadata I get all the video-to-tensor like this one:</p>
<pre><code class="lang-auto">'video_pts': [tensor([  1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
         15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,
         29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,
         43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,
         57,  58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,
         71,  72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
         85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,
         99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
        113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
        127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
        141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
        155, 156, 157, 158, 159, 160, 161, 162, 163, 164]),
</code></pre>
<p>and finally</p>
<pre><code class="lang-auto">'video_fps': [25.0, 25.0, 25.0, 25.0, 25.0, 25.0, 25.0, 25.0.....
</code></pre>
<p>The output of dataset.indices is: [ ]</p>

jeanrobin: <p>Hi costa98, I have the same problem as you but couldn’t find any solution… did you find the reason for this bug or did you implement a customized Dataset?</p>
<p>I just found the cause, incompatibility in the paths strings between the text files of train_test_splits/ and the file name of the dataset.samples. The txt files have ‘root\action/video_name’ whereas the samples are (‘root\action\video_name’, label)</p>

xploiter-projects: <p><strong>Problem:</strong></p>
<p>This problem occurs when you run your code on windows because windows paths use backslash ("\") instead of forward slash ("/").</p>
<p>As you see in the code:</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/vision/blob/7b9d30eb7c4d92490d9ac038a140398e0a690db6/torchvision/datasets/ucf101.py#L94" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/7b9d30eb7c4d92490d9ac038a140398e0a690db6/torchvision/datasets/ucf101.py#L94" target="_blank" rel="noopener nofollow ugc">pytorch/vision/blob/7b9d30eb7c4d92490d9ac038a140398e0a690db6/torchvision/datasets/ucf101.py#L94</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="84" style="counter-reset: li-counter 83 ;">
<li>    return self.full_video_clips.metadata</li><li></li><li>def _select_fold(self, video_list, annotation_path, fold, train):</li><li>    name = "train" if train else "test"</li><li>    name = "{}list{:02d}.txt".format(name, fold)</li><li>    f = os.path.join(annotation_path, name)</li><li>    selected_files = []</li><li>    with open(f, "r") as fid:</li><li>        data = fid.readlines()</li><li>        data = [x.strip().split(" ") for x in data]</li><li class="selected">        data = [os.path.join(self.root, x[0]) for x in data]</li><li>        selected_files.extend(data)</li><li>    selected_files = set(selected_files)</li><li>    indices = [i for i in range(len(video_list)) if video_list[i] in selected_files]</li><li>    return indices</li><li></li><li>def __len__(self):</li><li>    return self.video_clips.num_clips()</li><li></li><li>def __getitem__(self, idx):</li><li>    video, audio, info, video_idx = self.video_clips.get_clip(idx)</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>So, this line of code reads the file path from label file as “action\video_name” and merge it with “root” path using backslash therefore full path becomes like “root\action/video_name”. Such paths doesn’t match with the video lists at line#97 and returns empty list for <em>indices</em> variable.</p>
<p><strong>Solution:</strong></p>
<p>Two of possible solutions can be:</p>
<ol>
<li>Replace the forwardslashes “/” in the label files with backslashes “\”.</li>
<li>Override the <strong>_select_fold(…)</strong> function of class <strong>UCF101</strong> and fix the backslashes inside the function.</li>
</ol>