class_docstr: Dataset for supervised fine-tuning.
class_docstr_tok: ["Dataset","for","supervised","fine-tuning","."]
code: def __getitem__(self, i) -> Dict[str, torch.Tensor]:
      has_img = True
      txn = None
      if i < self.len1:  # vqa
          ii = random.randint(0, self.len1 - 1)
          tp = self.list_vqa_dict[ii].copy()
          image_folder = self.data_args.image_folder
      elif i < self.len1 + self.len2:  # inpainting, outpainting
          if random.random() < self.ratio_aes:
              tp, txn = self.get_dataset_aes()
          else:
              tp, image_folder = self.get_dataset_coco()
      elif (
          i < self.len1 + self.len2 + self.len3
      ):  # similar image generation
          if random.random() < self.ratio_aes:
              tp, txn = self.get_dataset_aes()
          else:
              tp, image_folder = self.get_dataset_coco()
          if random.random() < 0.5:  # generate image
              tp["conversations"][0]["value"] = (
                  DEFAULT_IMAGE_TOKEN
                  + "\n"
                  + random.choice(regen_prompt)
              )
              if self.data_args.mm_use_output_start_end:
                  tp["conversations"][1]["value"] = (
                      DEFAULT_OUTPUT_START_TOKEN
                      + " "
                      + tp["conversations"][1]["value"]
                      + " "
                      + DEFAULT_OUTPUT_END_TOKEN
                  )
          else:  # describe image
              tp["conversations"][0]["value"] = (
                  DEFAULT_IMAGE_TOKEN
                  + "\n"
                  + random.choice(ques_prompt)
              )
      else:  # prompt refinement
          if random.random() < self.ratio_aes:
              tp, txn = self.get_dataset_aes()
          else:
              tp, coco = self.get_dataset_cocov2()
  
          has_img = False
  
      if not has_img:
          if txn is not None:
              text_complete = self.prompt_dict_ori[tp["image"]]
          else:
              text_complete = coco["coco_caption"]
          _ = tp.pop("image")
  
          if random.random() < 0.5:  # description
              tp_prompt = textextend_prompt2
              is_generation = False
          else:  # generation
              tp_prompt = textextend_prompt
              is_generation = True
  
          if random.random() < 0.5:
              tp["conversations"][0]["value"] = (
                  random.choice(tp_prompt) + " " + text_complete
              )
          else:
              tp["conversations"][0]["value"] = (
                  text_complete + " " + random.choice(tp_prompt)
              )
  
          if self.data_args.mm_use_output_start_end and is_generation:
              tp["conversations"][1]["value"] = (
                  DEFAULT_OUTPUT_START_TOKEN
                  + " "
                  + tp["conversations"][1]["value"]
                  + " "
                  + DEFAULT_OUTPUT_END_TOKEN
              )
  
      if isinstance(i, int):
          sources = [tp]
      assert (
          len(sources) == 1
      ), "Don't know why it is wrapped to a list"  # FIXME
      if "image" in sources[0]:
          image_file = tp["image"]
          processor = self.data_args.image_processor
          if txn is not None:
              image = txn(image_file).convert("RGB")
          else:
              image = Image.open(
                  os.path.join(image_folder, image_file)
              ).convert("RGB")
  
          def resize2square(pil_img):
              result = pil_img.resize((512, 512), Image.ANTIALIAS)
              return result
  
          def cropresize(pil_img):
              width, height = pil_img.size
              if width > height:
                  result = pil_img.resize(
                      (int(width / height * 512), 512), Image.ANTIALIAS
                  )
              else:
                  result = pil_img.resize(
                      (512, int(height / width * 512)), Image.ANTIALIAS
                  )
              result = transforms.CenterCrop(512)(result)
              return result
  
          if txn is not None:
              image = cropresize(image)
          else:
              image = resize2square(image)
          if i >= self.len1 and i < self.len1 + self.len2:
              image = np.array(image)
              mask, is_outpaint = self.gen_mask(image, i)
              mask[mask < 0.5] = 0
              mask[mask >= 0.5] = 1
              image = (1 - mask) * image
              image = image.astype("uint8")
              image = Image.fromarray(image)
              if is_outpaint:
                  sources[0]["conversations"][0]["value"] = (
                      DEFAULT_IMAGE_TOKEN
                      + "\n"
                      + random.choice(outpaint_prompt)
                  )
                  if self.data_args.mm_use_output_start_end:
                      sources[0]["conversations"][1]["value"] = (
                          DEFAULT_OUTPUT_START_TOKEN
                          + " "
                          + sources[0]["conversations"][1]["value"]
                          + " "
                          + DEFAULT_OUTPUT_END_TOKEN
                      )
              else:
                  sources[0]["conversations"][0]["value"] = (
                      DEFAULT_IMAGE_TOKEN
                      + "\n"
                      + random.choice(inpaint_prompt)
                  )
                  if self.data_args.mm_use_output_start_end:
                      sources[0]["conversations"][1]["value"] = (
                          DEFAULT_OUTPUT_START_TOKEN
                          + " "
                          + sources[0]["conversations"][1]["value"]
                          + " "
                          + DEFAULT_OUTPUT_END_TOKEN
                      )
  
          image = processor.preprocess(image, return_tensors="pt")[
              "pixel_values"
          ][0]
  
          sources = preprocess_multimodal(
              copy.deepcopy([e["conversations"] for e in sources]),
              self.data_args,
          )
      else:
          sources = copy.deepcopy([e["conversations"] for e in sources])
  
      data_dict = preprocess(
          sources, self.tokenizer, has_image=("image" in tp)
      )
      if isinstance(i, int):
          data_dict = dict(
              input_ids=data_dict["input_ids"][0],
              labels=data_dict["labels"][0],
          )
  
      if "image" in tp:
          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"]
          )
      return data_dict
  
imports: import os
  import copy
  from dataclasses import dataclass, field
  import json
  import logging
  import pathlib
  from typing import Dict, Optional, Sequence, List
  import torch
  import transformers
  from llmga.llava.constants import IGNORE_INDEX, IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN, DEFAULT_OUTPUT_START_TOKEN, DEFAULT_OUTPUT_END_TOKEN
  from torch.utils.data import Dataset
  from llmga.llava.train.llava_trainer import LLaVATrainer
  from llmga.llava import conversation
  from llmga.llava.model import *
  from llmga.llava.mm_utils import tokenizer_image_token
  from PIL import Image
  from llmga.llava.masks.make_mask import get_mask_generator
  from llmga.llava.prompt_temp import outpaint_prompt, inpaint_prompt, textextend_prompt, regen_prompt, ques_prompt, textextend_prompt2
  import random
  import numpy
  import lmdb
  import io
  import pytextrank
  from torchvision import transforms
label_desc: Use image node_id 6 for calling the LazySupervisedDataset obj's underlying member method code with example usage: obj.__getitem__(i) and returns: data_dict, result, result