(args)
| 26 | |
| 27 | |
| 28 | def main(args): |
| 29 | annotations = {} |
| 30 | for line in open(args.annotations, "r"): |
| 31 | line = line.strip().split(args.separator) |
| 32 | if len(line) < 2 or not line[1].isnumeric(): |
| 33 | print("Could not parse the annotations file correctly, make sure the correct separator is used") |
| 34 | sys.exit(1) |
| 35 | annotations[os.path.basename(line[0])] = int(line[1]) |
| 36 | |
| 37 | trt_infer = TensorRTInfer(args.engine) |
| 38 | batcher = ImageBatcher(args.input, *trt_infer.input_spec(), preprocessor=args.preprocessor) |
| 39 | top1 = 0 |
| 40 | top5 = 0 |
| 41 | total = 0 |
| 42 | for batch, images in batcher.get_batch(): |
| 43 | classes, scores, top = trt_infer.infer(batch, top=5) |
| 44 | for i in range(len(images)): |
| 45 | image = os.path.basename(images[i]) |
| 46 | if image not in annotations.keys(): |
| 47 | print( |
| 48 | "Image '{}' does not appear in the annotations file, please make sure all evaluated " |
| 49 | "images have a corresponding ground truth label".format(image) |
| 50 | ) |
| 51 | sys.exit(1) |
| 52 | if annotations[image] == classes[i]: |
| 53 | top1 += 1 |
| 54 | if annotations[image] in top[0][i]: |
| 55 | top5 += 1 |
| 56 | total += 1 |
| 57 | top1_acc = 100 * (top1 / total) |
| 58 | top5_acc = 100 * (top5 / total) |
| 59 | print( |
| 60 | "Processing {} / {} : Top-1 {:0.1f}% , Top-5: {:0.1f}% ".format( |
| 61 | total, batcher.num_images, top1_acc, top5_acc |
| 62 | ), |
| 63 | end="\r", |
| 64 | ) |
| 65 | print() |
| 66 | print("Top-1 Accuracy: {:0.3f}%".format(top1_acc)) |
| 67 | print("Top-5 Accuracy: {:0.3f}%".format(top5_acc)) |
| 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
no test coverage detected