echarlaix HF staff commited on
Commit
d053c32
1 Parent(s): f748c12

Add normalization to the positions of the detected objects

Browse files
Files changed (1) hide show
  1. gqa-lxmert.py +16 -14
gqa-lxmert.py CHANGED
@@ -109,22 +109,24 @@ class GqaLxmert(datasets.GeneratorBasedBuilder):
109
  reader = csv.DictReader(f, FIELDNAMES, delimiter="\t")
110
  for i, item in enumerate(reader):
111
  features = {}
112
- for key in ["img_h", "img_w", "num_boxes"]:
113
- features[key] = int(item[key])
114
- num_boxes = features["num_boxes"]
115
- decode_config = [
116
- ("objects_id", (num_boxes,), np.int64),
117
- ("objects_conf", (num_boxes,), np.float32),
118
- ("attrs_id", (num_boxes,), np.int64),
119
- ("attrs_conf", (num_boxes,), np.float32),
120
- ("boxes", (num_boxes, 4), np.float32),
121
- ("features", (num_boxes, -1), np.float32),
122
- ]
123
- for key, shape, dtype in decode_config:
124
- features[key] = np.frombuffer(base64.b64decode(item[key]), dtype=dtype).reshape(shape)
125
  id2features[item["img_id"]] = features
126
  return id2features
127
 
 
 
 
 
 
 
 
128
  def _generate_examples(self, filepath):
129
  """ Yields examples as (key, example) tuples."""
130
  with open(filepath, encoding="utf-8") as f:
@@ -137,6 +139,6 @@ class GqaLxmert(datasets.GeneratorBasedBuilder):
137
  "question_id": d["question_id"],
138
  "image_id": d["img_id"],
139
  "features": img_features["features"],
140
- "boxes": img_features["boxes"],
141
  "label": label,
142
  }
 
109
  reader = csv.DictReader(f, FIELDNAMES, delimiter="\t")
110
  for i, item in enumerate(reader):
111
  features = {}
112
+ img_h = int(item["img_h"])
113
+ img_w = int(item["img_w"])
114
+ num_boxes = int(item["num_boxes"])
115
+ features["features"] = np.frombuffer(base64.b64decode(item["features"]), dtype=np.float32).reshape(
116
+ (num_boxes, -1)
117
+ )
118
+ boxes = np.frombuffer(base64.b64decode(item["boxes"]), dtype=np.float32).reshape((num_boxes, 4))
119
+ features["normalized_boxes"] = self._normalize_boxes(boxes, img_h, img_w)
 
 
 
 
 
120
  id2features[item["img_id"]] = features
121
  return id2features
122
 
123
+ def _normalize_boxes(self, boxes, img_h, img_w):
124
+ """ Normalizes the input boxes given the original image size."""
125
+ normalized_boxes = boxes.copy()
126
+ normalized_boxes[:, (0, 2)] /= img_w
127
+ normalized_boxes[:, (1, 3)] /= img_h
128
+ return normalized_boxes
129
+
130
  def _generate_examples(self, filepath):
131
  """ Yields examples as (key, example) tuples."""
132
  with open(filepath, encoding="utf-8") as f:
 
139
  "question_id": d["question_id"],
140
  "image_id": d["img_id"],
141
  "features": img_features["features"],
142
+ "normalized_boxes": img_features["normalized_boxes"],
143
  "label": label,
144
  }