Add normalization to the positions of the detected objects
Browse files- 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 |
-
|
113 |
-
|
114 |
-
num_boxes =
|
115 |
-
|
116 |
-
(
|
117 |
-
|
118 |
-
|
119 |
-
|
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 |
-
"
|
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 |
}
|