class_docstr: Dataset for supervised fine-tuning.
class_docstr_tok: ["Dataset","for","supervised","fine-tuning","."]
code: def __getitem__(self, i) -> Dict[str, torch.Tensor]:
      attempt, max_attempt = 0, 10
      while attempt < max_attempt:
          try:
              sources = self.list_data_dict[i]
              suffix = None
              if isinstance(i, int):
                  sources = [sources]
              assert (
                  len(sources) == 1
              ), "Don't know why it is wrapped to a list"  # FIXME
              if "image" in sources[0]:
                  image_file = self.list_data_dict[i]["image"]
                  image_folder = self.data_args.image_folder
                  processor = self.data_args.image_processor
  
                  # convert image type for OCR VQA dataset
                  if image_file is not None:
                      if "ocr" in image_file:
                          if not os.path.exists(
                              os.path.join(image_folder, image_file)
                          ):
                              image_file = image_file.replace(
                                  ".jpg", ".png"
                              )
  
                      # convert image for VG dataset
                      elif "VG_100K" in image_file:
                          image_file = image_file.replace(
                              "VG_100K_2", "images"
                          )
                          image_file = image_file.replace(
                              "VG_100K", "images"
                          )
  
                  image = Image.open(
                      os.path.join(image_folder, image_file)
                  ).convert("RGB")
                  if self.data_args.image_aspect_ratio == "pad":
  
                      def expand2square(pil_img, background_color):
                          width, height = pil_img.size
                          if width == height:
                              return pil_img
                          elif width > height:
                              result = Image.new(
                                  pil_img.mode,
                                  (width, width),
                                  background_color,
                              )
                              result.paste(
                                  pil_img, (0, (width - height) // 2)
                              )
                              return result
                          else:
                              result = Image.new(
                                  pil_img.mode,
                                  (height, height),
                                  background_color,
                              )
                              result.paste(
                                  pil_img, ((height - width) // 2, 0)
                              )
                              return result
  
                      image = expand2square(
                          image,
                          tuple(
                              int(x * 255) for x in processor.image_mean
                          ),
                      )
                      image = processor.preprocess(
                          image, return_tensors="pt"
                      )["pixel_values"][0]
                  else:
                      image = processor.preprocess(
                          image, return_tensors="pt"
                      )["pixel_values"][0]
                  sources = preprocess_multimodal(
                      copy.deepcopy(
                          [e["conversations"] for e in sources]
                      ),
                      self.data_args,
                  )
              elif "video" in sources[0]:
                  video_file = self.list_data_dict[i]["video"]
                  video_folder = self.data_args.video_folder
                  video_file = os.path.join(video_folder, video_file)
                  suffix = video_file.split(".")[-1]
                  if not os.path.exists(video_file):
                      print("File {} not exist!".format(video_file))
  
                  if suffix == "pkl":
                      video_info = pickle.load(open(video_file, "rb"))
                      image = torch.from_numpy(
                          video_info["feats"][:, 1:]
                      )
                      input_prompt = video_info["inputs"].replace(
                          "...", ""
                      )
                      # replace the default image token with multiple tokens
                      input_prompt = input_prompt.replace(
                          DEFAULT_IMAGE_TOKEN,
                          DEFAULT_IMAGE_TOKEN
                          * self.data_args.video_token,
                      )
                      (
                          sources,
                          query_prompt,
                      ) = preprocess_multimodal_movie(
                          copy.deepcopy(
                              [e["conversations"] for e in sources]
                          ),
                          self.data_args,
                          input_prompt,
                      )
                  else:
                      vr = VideoReader(video_file, ctx=cpu(0))
                      sample_fps = round(
                          vr.get_avg_fps() / self.data_args.video_fps
                      )
                      frame_idx = [
                          i for i in range(0, len(vr), sample_fps)
                      ]
                      video = vr.get_batch(frame_idx).asnumpy()
                      processor = self.data_args.image_processor
                      image = processor.preprocess(
                          video, return_tensors="pt"
                      )["pixel_values"]
                      sources = preprocess_multimodal(
                          copy.deepcopy(
                              [e["conversations"] for e in sources]
                          ),
                          self.data_args,
                      )
              else:
                  sources = copy.deepcopy(
                      [e["conversations"] for e in sources]
                  )
  
              break
          except:
              attempt += 1
              print(f"Error in loading {i}, retrying...")
              i = random.randint(0, len(self.list_data_dict) - 1)
  
      has_image = ("image" in self.list_data_dict[i]) or (
          "video" in self.list_data_dict[i]
      )
      data_dict = preprocess(
          sources,
          self.tokenizer,
          has_image=has_image,
          prompt=self.data_args.input_prompt,
          refine_prompt=self.data_args.refine_prompt,
      )
  
      if "prompt" in data_dict:
          prompt = data_dict["prompt"]
      else:
          prompt = None
  
      if suffix == "pkl":
          prompt = [query_prompt]
  
      if isinstance(i, int):
          data_dict = dict(
              input_ids=data_dict["input_ids"][0],
              labels=data_dict["labels"][0],
          )
  
      # image exist in the data
      if "image" in self.list_data_dict[i]:
          data_dict["image"] = image
      elif "video" in self.list_data_dict[i]:
          data_dict["image"] = image
      elif self.data_args.is_multimodal:
          # image does not exist in the data, but the model is multimodal
          crop_size = self.data_args.image_processor.crop_size
          data_dict["image"] = torch.zeros(
              3, crop_size["height"], crop_size["width"]
          )
  
      # prompt exist in the data
      if prompt is not None:
          data_dict["prompt"] = prompt
  
      return data_dict
  
imports: import os
  import copy
  import random
  from dataclasses import dataclass, field
  import json
  import logging
  import pathlib
  from typing import Dict, Optional, Sequence, List
  import pickle
  import math
  import torch
  import transformers
  from llamavid.constants import IGNORE_INDEX, IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
  from torch.utils.data import Dataset
  from llamavid.train.llava_trainer import LLaVATrainer
  from llamavid import conversation
  from llamavid.model import *
  from llava.mm_utils import tokenizer_image_token
  from PIL import Image
  from decord import VideoReader, cpu
label_desc: Use image node_id 5 for calling the LazySupervisedDataset obj's underlying member method code with example usage: obj.__getitem__(i) and returns: data_dict, pil_img, result, result