class_docstr: VQA V2, image feature, 10k question vocab, 3k answer label.
class_docstr_tok: ["VQA","V2",",","image","feature",",","10k","question","vocab",",","3k","answer","label","."]
docstr: VQA v2 generator using image features.
code: def vqa_v2_generator(self, data_dir, tmp_dir, datasets):
      """VQA v2 generator using image features."""
      _get_vqa_v2_annotations(tmp_dir, self._VQA_V2_ANNOTATION_URL)
      _get_vqa_v2_image_feature_dataset(
          tmp_dir, self._VQA_V2_FEATURE_URL
      )
      vocab_path = os.path.join(data_dir, self.vocab_filename)
      if not tf.gfile.Exists(vocab_path):
          vocab_tmp_path = os.path.join(tmp_dir, self.vocab_filename)
          tf.gfile.Copy(vocab_tmp_path, vocab_path)
          with tf.gfile.GFile(vocab_path, mode="r") as f:
              vocab_data = "<pad>\n<EOS>\n" + f.read() + "UNK\n"
          with tf.gfile.GFile(vocab_path, mode="w") as f:
              f.write(vocab_data)
      label_path = os.path.join(data_dir, self.label_filename)
      if not tf.gfile.Exists(label_path):
          label_tmp_path = os.path.join(tmp_dir, self.label_filename)
          tf.gfile.Copy(label_tmp_path, label_path)
  
      vocab_encoder = text_encoder.TokenTextEncoder(
          vocab_path, replace_oov="UNK"
      )
      label_encoder = text_encoder.ClassLabelEncoder(
          class_labels_fname=label_path
      )
  
      # merge annotations
      annotation_json = []
      for _, annotation_file in datasets:
          annotation_path = os.path.join(tmp_dir, annotation_file)
          with tf.gfile.Open(annotation_path) as f:
              annotation_json += json.loads(f.read())
      annotation_count = len(annotation_json)
      tf.logging.info(
          "Processing %d annotations for vqa v2" % (annotation_count)
      )
  
      imageid2annotation = {}
      for anno in annotation_json:
          if anno["image_id"] not in imageid2annotation:
              imageid2annotation[anno["image_id"]] = [anno]
          else:
              imageid2annotation[anno["image_id"]].append(anno)
  
      csv.field_size_limit(sys.maxsize)
      for feature_file, _ in datasets:
          feature_file_path = os.path.join(tmp_dir, feature_file)
          with open(feature_file_path, "r+b") as tsv_file:
              csv_reader = csv.DictReader(
                  tsv_file,
                  delimiter="\t",
                  fieldnames=self.feature_file_field_names,
              )
              for item in csv_reader:
                  item["num_boxes"] = int(item["num_boxes"])
                  image_id = int(item["image_id"])
                  image_w = float(item["image_w"])
                  image_h = float(item["image_h"])
                  bboxes = np.frombuffer(
                      base64.decodestring(item["boxes"]),
                      dtype=np.float32,
                  ).reshape((item["num_boxes"], -1))
  
                  box_width = bboxes[:, 2] - bboxes[:, 0]
                  box_height = bboxes[:, 3] - bboxes[:, 1]
                  scaled_width = box_width / image_w
                  scaled_height = box_height / image_h
                  scaled_x = bboxes[:, 0] / image_w
                  scaled_y = bboxes[:, 1] / image_h
  
                  box_width = box_width[..., np.newaxis]
                  box_height = box_height[..., np.newaxis]
                  scaled_width = scaled_width[..., np.newaxis]
                  scaled_height = scaled_height[..., np.newaxis]
                  scaled_x = scaled_x[..., np.newaxis]
                  scaled_y = scaled_y[..., np.newaxis]
  
                  spatial_features = np.concatenate(
                      (
                          scaled_x,
                          scaled_y,
                          scaled_x + scaled_width,
                          scaled_y + scaled_height,
                          scaled_width,
                          scaled_height,
                      ),
                      axis=1,
                  )
  
                  if image_id in imageid2annotation:
                      for anno in imageid2annotation[image_id]:
                          question = vocab_encoder.encode(
                              anno["question"]
                          )
                          answer = [
                              label_encoder.encode(ans)
                              for ans in anno["answer"]
                          ]
                          answer = (
                              answer if answer else [0]
                          )  # 0 indicates padding
                          yield {
                              "image/feature": np.frombuffer(
                                  base64.decodestring(item["features"]),
                                  dtype=np.float32,
                              ).tolist(),
                              "image/spatial_feature": spatial_features.flatten().tolist(),
                              "image/height": [image_h],
                              "image/width": [image_w],
                              "image/bboxes": bboxes.flatten().tolist(),
                              "image/image_id": [image_id],
                              "image/question_id": [
                                  anno["question_id"]
                              ],
                              "image/question": question,
                              "image/answer": answer,
                          }
  
                      del imageid2annotation[image_id]
  
      # assert all annotations are included
      assert not imageid2annotation
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import base64
  import csv
  import json
  import os
  import random
  import sys
  import tarfile
  import zipfile
  import numpy
  from tensor2tensor.data_generators import generator_utils
  from tensor2tensor.data_generators import image_utils
  from tensor2tensor.data_generators import problem
  from tensor2tensor.data_generators import text_encoder
  from tensor2tensor.data_generators import vqa_utils
  from tensor2tensor.layers import modalities
  from tensor2tensor.utils import contrib
  from tensor2tensor.utils import metrics
  from tensor2tensor.utils import registry
  import tensorflow.compat.v1
label_desc: Use image node_id 7 for calling the ImageVqav2RcnnFeatureTokens10kLabels3k obj's underlying member method code with example usage: obj.vqa_v2_generator(data_dir, tmp_dir, datasets) without return types