MCPcopy Create free account
hub / github.com/apple/axlearn / _create_annotation

Function _create_annotation

axlearn/vision/coco_utils.py:284–325  ·  view source on GitHub ↗
(*, batch_id: int, image_id: int, box_id: int, class_label: int)

Source from the content-addressed store, hash-verified

282 ]
283
284 def _create_annotation(*, batch_id: int, image_id: int, box_id: int, class_label: int) -> dict:
285 ann = {}
286 ann["image_id"] = int(groundtruths["source_id"][batch_id][image_id])
287 if "is_crowds" in groundtruths:
288 ann["iscrowd"] = int(groundtruths["is_crowds"][batch_id][image_id, box_id])
289 else:
290 ann["iscrowd"] = 0
291 ann["category_id"] = class_label
292 boxes = groundtruths["boxes"][batch_id]
293 ann["bbox"] = [
294 float(boxes[image_id, box_id, 1]),
295 float(boxes[image_id, box_id, 0]),
296 float(boxes[image_id, box_id, 3] - boxes[image_id, box_id, 1]),
297 float(boxes[image_id, box_id, 2] - boxes[image_id, box_id, 0]),
298 ]
299 if "areas" in groundtruths:
300 ann["area"] = float(groundtruths["areas"][batch_id][image_id, box_id])
301 else:
302 ann["area"] = float(
303 (boxes[image_id, box_id, 3] - boxes[image_id, box_id, 1])
304 * (boxes[image_id, box_id, 2] - boxes[image_id, box_id, 0])
305 )
306 if "masks" in groundtruths:
307 if isinstance(groundtruths["masks"][batch_id][image_id, box_id], tf.Tensor):
308 mask = Image.open(
309 six.BytesIO(groundtruths["masks"][batch_id][image_id, box_id].numpy())
310 )
311 width, height = mask.size
312 np_mask = np.array(mask.getdata()).reshape(height, width).astype(np.uint8)
313 else:
314 mask = Image.open(six.BytesIO(groundtruths["masks"][batch_id][image_id, box_id]))
315 width, height = mask.size
316 np_mask = np.array(mask.getdata()).reshape(height, width).astype(np.uint8)
317 np_mask[np_mask > 0] = 255
318 encoded_mask = mask_api.encode(np.asfortranarray(np_mask))
319 ann["segmentation"] = encoded_mask
320 # Ensure the content of `counts` is JSON serializable string.
321 if "counts" in ann["segmentation"]:
322 ann["segmentation"]["counts"] = six.ensure_str(ann["segmentation"]["counts"])
323 if "areas" not in groundtruths:
324 ann["area"] = mask_api.area(encoded_mask)
325 return ann
326
327 gt_annotations = []
328 num_batches = len(groundtruths["source_id"])

Callers 1

Calls 2

astypeMethod · 0.80
encodeMethod · 0.45

Tested by

no test coverage detected