def load_synthetic_captions(generation_file, vocab):
  """Loads weakly supervised examples.

  Args:
    generation_file: Path to output of conditional generation (text planner).
    vocab: Instance of text_utils.Vocab.

  Returns:
    synthetic_captions: Dictionary of image_id --> list of synthetic captions.
  """
  tf.logging.info("Loading generations.")
  synthetic_captions = collections.defaultdict(list)
  total = 0
  with tf.io.gfile.GFile(generation_file, "r") as f:
    for line in f:
      entry = json.loads(line)
      image_id = entry["image_id"]

      # Take captions that pass the reward filter.
      consistent_captions = []
      for idx in np.argsort(-np.array(entry["span_scores"])):
        caption = vocab.clean([vocab.i2t(i) for i in entry["token_ids"][idx]])
        if entry[FLAGS.reward][idx] >= FLAGS.threshold:
          consistent_captions.append(" ".join(caption))

      # Add (up to the limit) to the synthetic dataset.
      if consistent_captions:
        total += 1
        synthetic_captions[image_id].extend(consistent_captions[:FLAGS.limit])

        if total % 1000 == 0:
          for caption in consistent_captions:
            tf.logging.info(caption)
          tf.logging.info("Loaded %d generations.", total)

  return synthetic_captions
Loads weakly supervised examples.

  Args:
    generation_file: Path to output of conditional generation (text planner).
    vocab: Instance of text_utils.Vocab.

  Returns:
    synthetic_captions: Dictionary of image_id --> list of synthetic captions.
  
language/capwap/preprocessing/weakly_supervised.py