code: def save_image_contexts(task_opt: Opt):
      """
      Save a JSON of images and associated contexts for the model image chat task.
  
      Note that each image will have BST-style context information saved with it, such as
      persona strings and a pair of lines of dialogue from another dataset.
      TODO: perhaps have the image chat task make use of this context information
      """
  
      print("Creating teacher to loop over images.")
      agent = RepeatLabelAgent(task_opt)
      world = create_task(task_opt, agent)
      num_examples = task_opt["num_examples"]
  
      print("Creating context generator.")
      context_generator = get_context_generator()
  
      print(
          f"Looping over {num_examples:d} images and pulling a context for each one."
      )
      image_contexts = []
      unique_image_srcs = set()
      while len(image_contexts) < num_examples:
          # Get the next teacher act
          world.parley()
          teacher_act = world.get_acts()[0]
  
          image_src = get_image_src(image=teacher_act["image"])
          if image_src in unique_image_srcs:
              # Skip over non-unique images, such as from the later turns of an episode
              print("\tSkipping non-unique image.")
          else:
              unique_image_srcs.add(image_src)
              image_context = {
                  "image_act": teacher_act,
                  "context_info": context_generator.get_context(),
              }
              image_contexts.append(image_context)
              if len(image_contexts) % 5 == 0:
                  print(f"Collected {len(image_contexts):d} images.")
  
      print(f"{len(image_contexts):d} image contexts created.")
  
      # Save
      with open(task_opt["image_context_path"], "wb") as f:
          pickle.dump(image_contexts, f)
  
file_path: ParlAI/parlai/crowdsourcing/tasks/model_chat/scripts/save_image_contexts.py
imports: import os
  import pickle
  from parlai.agents.repeat_label.repeat_label import RepeatLabelAgent
  from parlai.core.opt import Opt
  from parlai.core.worlds import create_task
  from parlai.crowdsourcing.tasks.model_chat import run
  from parlai.crowdsourcing.tasks.model_chat.utils import get_context_generator, get_image_src
  from parlai.scripts.display_data import setup_args
label_desc: Use image node_id 2 for calling a global function with example usage: save_image_contexts(task_opt) without return types