MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / main

Function main

samples/python/tensorflow_object_detection_api/eval_coco.py:27–90  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

25from image_batcher import ImageBatcher
26
27def main(args):
28 try:
29 import object_detection.metrics.coco_tools as coco_tools
30 except ImportError:
31 print("Could not import the 'object_detection.metrics.coco_tools' module from TFOD. Maybe you did not install TFOD API")
32 print("Please install TensorFlow 2 Object Detection API, check https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html")
33 sys.exit(1)
34
35 trt_infer = TensorRTInfer(args.engine, args.preprocessor, args.detection_type, args.iou_threshold)
36 batcher = ImageBatcher(args.input, *trt_infer.input_spec(), preprocessor=args.preprocessor)
37 # Read annotations json as dictionary.
38 with open(args.annotations) as f:
39 data = json.load(f)
40 groundtruth = coco_tools.COCOWrapper(data, detection_type=args.detection_type)
41 detections_list = []
42 for batch, images, scales in batcher.get_batch():
43 print("Processing Image {} / {}".format(batcher.image_index, batcher.num_images), end="\r")
44 detections = trt_infer.infer(batch, scales, args.nms_threshold)
45 for i in range(len(images)):
46 # Get inference image resolution.
47 infer_im = Image.open(images[i])
48 im_width, im_height = infer_im.size
49 for n in range(len(detections[i])):
50 source_id = int(os.path.splitext(os.path.basename(images[i]))[0])
51 det = detections[i][n]
52 if args.detection_type == 'bbox':
53 coco_det = {
54 'image_id': source_id,
55 'category_id': det['class']+1, # adjust class num
56 'bbox': [det['xmin'], det['ymin'], det['xmax'] - det['xmin'], det['ymax'] - det['ymin']],
57 'score': det['score']
58 }
59 detections_list.append(coco_det)
60 elif args.detection_type == 'segmentation':
61 # Get detection bbox resolution.
62 det_width = round(det['xmax'] - det['xmin'])
63 det_height = round(det['ymax'] - det['ymin'])
64 # Create an image out of predicted mask array.
65 small_mask = Image.fromarray(det['mask'])
66 # Upsample mask to detection bbox's size.
67 mask = small_mask.resize((det_width, det_height), resample=Image.BILINEAR)
68 # Create an original image sized template for correct mask placement.
69 pad = Image.new("L", (im_width, im_height))
70 # Place your mask according to detection bbox placement.
71 pad.paste(mask, (round(det['xmin']), (round(det['ymin']))))
72 # Reconvert mask into numpy array for evaluation.
73 padded_mask = np.array(pad)
74 # Add one more dimension of 1, this is required by ExportSingleImageDetectionMasksToCoco.
75 final_mask = padded_mask[np.newaxis, :, :]
76 # Export detection mask to COCO format
77 coco_mask = coco_tools.ExportSingleImageDetectionMasksToCoco(image_id=source_id,
78 category_id_set=set(list(range(1,91))),
79 detection_classes=np.array([det['class']+1]),
80 detection_scores=np.array([det['score']]),
81 detection_masks=final_mask)
82 detections_list.append(coco_mask[0])
83
84 # Finish evalutions.

Callers 1

eval_coco.pyFile · 0.70

Calls 10

input_specMethod · 0.95
get_batchMethod · 0.95
inferMethod · 0.95
TensorRTInferClass · 0.90
ImageBatcherClass · 0.90
printFunction · 0.85
roundFunction · 0.50
loadMethod · 0.45
appendMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected