echarlaix HF staff commited on
Commit
90e803b
1 Parent(s): 46e8818

Add normalization to the positions of the detected objects

Browse files
Files changed (1) hide show
  1. vqa-lxmert.py +17 -15
vqa-lxmert.py CHANGED
@@ -77,7 +77,7 @@ class VqaV2Lxmert(datasets.GeneratorBasedBuilder):
77
  "question_id": datasets.Value("int32"),
78
  "image_id": datasets.Value("string"),
79
  "features": datasets.Array2D(_SHAPE_FEATURES, dtype="float32"),
80
- "boxes": datasets.Array2D(_SHAPE_BOXES, dtype="float32"),
81
  "answer_type": datasets.Value("string"),
82
  "label": datasets.Sequence(
83
  {
@@ -117,22 +117,24 @@ class VqaV2Lxmert(datasets.GeneratorBasedBuilder):
117
  reader = csv.DictReader(f, FIELDNAMES, delimiter="\t")
118
  for i, item in enumerate(reader):
119
  features = {}
120
- for key in ["img_h", "img_w", "num_boxes"]:
121
- features[key] = int(item[key])
122
- num_boxes = features["num_boxes"]
123
- decode_config = [
124
- ("objects_id", (num_boxes,), np.int64),
125
- ("objects_conf", (num_boxes,), np.float32),
126
- ("attrs_id", (num_boxes,), np.int64),
127
- ("attrs_conf", (num_boxes,), np.float32),
128
- ("boxes", (num_boxes, 4), np.float32),
129
- ("features", (num_boxes, -1), np.float32),
130
- ]
131
- for key, shape, dtype in decode_config:
132
- features[key] = np.frombuffer(base64.b64decode(item[key]), dtype=dtype).reshape(shape)
133
  id2features[item["img_id"]] = features
134
  return id2features
135
 
 
 
 
 
 
 
 
136
  def _generate_examples(self, filepath, imgfeat):
137
  """ Yields examples as (key, example) tuples."""
138
  id2features = self._load_features(imgfeat)
@@ -148,7 +150,7 @@ class VqaV2Lxmert(datasets.GeneratorBasedBuilder):
148
  "question_id": d["question_id"],
149
  "image_id": d["img_id"],
150
  "features": img_features["features"],
151
- "boxes": img_features["boxes"],
152
  "answer_type": d["answer_type"],
153
  "label": {
154
  "ids": ids,
 
77
  "question_id": datasets.Value("int32"),
78
  "image_id": datasets.Value("string"),
79
  "features": datasets.Array2D(_SHAPE_FEATURES, dtype="float32"),
80
+ "normalized_boxes": datasets.Array2D(_SHAPE_BOXES, dtype="float32"),
81
  "answer_type": datasets.Value("string"),
82
  "label": datasets.Sequence(
83
  {
 
117
  reader = csv.DictReader(f, FIELDNAMES, delimiter="\t")
118
  for i, item in enumerate(reader):
119
  features = {}
120
+ img_h = int(item["img_h"])
121
+ img_w = int(item["img_w"])
122
+ num_boxes = int(item["num_boxes"])
123
+ features["features"] = np.frombuffer(base64.b64decode(item["features"]), dtype=np.float32).reshape(
124
+ (num_boxes, -1)
125
+ )
126
+ boxes = np.frombuffer(base64.b64decode(item["boxes"]), dtype=np.float32).reshape((num_boxes, 4))
127
+ features["normalized_boxes"] = self._normalize_boxes(boxes, img_h, img_w)
 
 
 
 
 
128
  id2features[item["img_id"]] = features
129
  return id2features
130
 
131
+ def _normalize_boxes(self, boxes, img_h, img_w):
132
+ """ Normalize the input boxes given the original image size."""
133
+ normalized_boxes = boxes.copy()
134
+ normalized_boxes[:, (0, 2)] /= img_w
135
+ normalized_boxes[:, (1, 3)] /= img_h
136
+ return normalized_boxes
137
+
138
  def _generate_examples(self, filepath, imgfeat):
139
  """ Yields examples as (key, example) tuples."""
140
  id2features = self._load_features(imgfeat)
 
150
  "question_id": d["question_id"],
151
  "image_id": d["img_id"],
152
  "features": img_features["features"],
153
+ "normalized_boxes": img_features["normalized_boxes"],
154
  "answer_type": d["answer_type"],
155
  "label": {
156
  "ids": ids,