class_docstr: Implementation of a video dataset.
  
  The VideoDataset allows random reads from a video file without extracting
  all frames beforehand. This is more storage efficient but is slower.
  
  Attributes:
      root:
          Root directory path.
      extensions:
          Tuple of allowed extensions.
      transform:
          Function that takes a PIL image and returns transformed version
      target_transform:
          As transform but for targets
      is_valid_file:
          Used to check corrupt files
      exception_on_non_increasing_timestamp:
          If True, a NonIncreasingTimestampError is raised when trying to load
          a frame that has a timestamp lower or equal to the timestamps of
          previous frames in the same video.
class_docstr_tok: ["Implementation","of","a","video","dataset",".","The","VideoDataset","allows","random","reads","from","a","video","file","without","extracting","all","frames","beforehand",".","This","is","more","storage","efficient","but","is","slower",".","Attributes",":","root",":","Root","directory","path",".","extensions",":","Tuple","of","allowed","extensions",".","transform",":","Function","that","takes","a","PIL","image","and","returns","transformed","version","target_transform",":","As","transform","but","for","targets","is_valid_file",":","Used","to","check","corrupt","files","exception_on_non_increasing_timestamp",":","If","True",",","a","NonIncreasingTimestampError","is","raised","when","trying","to","load","a","frame","that","has","a","timestamp","lower","or","equal","to","the","timestamps","of","previous","frames","in","the","same","video","."]
docstr: Returns item at index.
  
  Finds the video of the frame at index with the help of the frame
  offsets. Then, loads the frame from the video, applies the transforms,
  and returns the frame along with the index of the video (as target).
  
  For example, if there are two videos with 10 and 20 frames respectively
  in the input directory:
  
  Requesting the 5th sample returns the 5th frame from the first video and
  the target indicates the index of the source video which is 0.
  >>> dataset[5]
  >>> > <PIL Image>, 0
  
  Requesting the 20th sample returns the 10th frame from the second video
  and the target indicates the index of the source video which is 1.
  >>> dataset[20]
  >>> > <PIL Image>, 1
  
  Args:
      index:
          Index of the sample to retrieve.
  
  Returns:
      A tuple (sample, target) where target indicates the video index.
  
  Raises:
      IndexError:
          If index is out of bounds.
      VideoError:
          If the frame at the given index could not be loaded.
docstr_tok: ["Returns","item","at","index",".","Finds","the","video","of","the","frame","at","index","with","the","help","of","the","frame","offsets",".","Then",",","loads","the","frame","from","the","video",",","applies","the","transforms",",","and","returns","the","frame","along","with","the","index","of","the","video","(","as","target",")",".","For","example",",","if","there","are","two","videos","with","10","and","20","frames","respectively","in","the","input","directory",":","Requesting","the","5th","sample","returns","the","5th","frame","from","the","first","video","and","the","target","indicates","the","index","of","the","source","video","which","is","0",".",">",">",">","dataset","[","5","]",">",">",">",">","<","PIL","Image",">",",","0","Requesting","the","20th","sample","returns","the","10th","frame","from","the","second","video","and","the","target","indicates","the","index","of","the","source","video","which","is","1",".",">",">",">","dataset","[","20","]",">",">",">",">","<","PIL","Image",">",",","1","Args",":","index",":","Index","of","the","sample","to","retrieve",".","Returns",":","A","tuple","(","sample",",","target",")","where","target","indicates","the","video","index",".","Raises",":","IndexError",":","If","index","is","out","of","bounds",".","VideoError",":","If","the","frame","at","the","given","index","could","not","be","loaded","."]
code: def __getitem__(self, index):
      """Returns item at index.
  
      Finds the video of the frame at index with the help of the frame
      offsets. Then, loads the frame from the video, applies the transforms,
      and returns the frame along with the index of the video (as target).
  
      For example, if there are two videos with 10 and 20 frames respectively
      in the input directory:
  
      Requesting the 5th sample returns the 5th frame from the first video and
      the target indicates the index of the source video which is 0.
      >>> dataset[5]
      >>> > <PIL Image>, 0
  
      Requesting the 20th sample returns the 10th frame from the second video
      and the target indicates the index of the source video which is 1.
      >>> dataset[20]
      >>> > <PIL Image>, 1
  
      Args:
          index:
              Index of the sample to retrieve.
  
      Returns:
          A tuple (sample, target) where target indicates the video index.
  
      Raises:
          IndexError:
              If index is out of bounds.
          VideoError:
              If the frame at the given index could not be loaded.
  
      """
      if index < 0 or index >= self.__len__():
          raise IndexError(
              f"Index {index} is out of bounds for VideoDataset"
              f" of size {self.__len__()}."
          )
  
      # each sample belongs to a video, to load the sample at index, we need
      # to find the video to which the sample belongs and then read the frame
      # from this video on the disk.
      i = len(self.offsets) - 1
      while self.offsets[i] > index:
          i = i - 1
  
      timestamp_idx = index - self.offsets[i]
  
      if (
          self.exception_on_non_increasing_timestamp
          and self.video_timestamps_is_non_increasing[i][timestamp_idx]
      ):
          raise NonIncreasingTimestampError(
              f"Frame {timestamp_idx} of video {self.videos[i]} has "
              f"a timestamp that is equal or lower than timestamps of previous "
              f"frames in the video. Trying to load this frame might result "
              f"in the wrong frame being returned. Set the VideoDataset.exception_on_non_increasing_timestamp"
              f"attribute to False to allow unsafe frame loading."
          )
  
      # find and return the frame as PIL image
      frame_timestamp = self.video_timestamps[i][timestamp_idx]
      video_loader = self._get_video_loader(i)
      sample = video_loader.read_frame(frame_timestamp)
  
      target = i
      if self.transform is not None:
          sample = self.transform(sample)
      if self.target_transform is not None:
          target = self.target_transform(target)
  
      return sample, target
  
imports: import os
  import threading
  import warnings
  import weakref
  from fractions import Fraction
  from typing import Any, Dict, List, Tuple
  import numpy
  import torch
  import torchvision
  from PIL import Image
  from torch.utils.data import DataLoader, Dataset
  from torchvision import datasets, io
  from tqdm import tqdm
label_desc: Use image node_id 2 for calling the VideoDataset obj's underlying member method code with example usage: obj.__getitem__(index) and returns: sample,  target