()
| 605 | |
| 606 | |
| 607 | def benchmark(): |
| 608 | _outputs = {} |
| 609 | main_args = get_main_args_parser().parse_args() |
| 610 | main_args.commad_txt = "Command: " + " ".join(sys.argv) |
| 611 | |
| 612 | # load cfg file and update the args |
| 613 | print("Loading config file from {}".format(main_args.config_file)) |
| 614 | cfg = SLConfig.fromfile(main_args.config_file) |
| 615 | if main_args.options is not None: |
| 616 | cfg.merge_from_dict(main_args.options) |
| 617 | cfg_dict = cfg._cfg_dict.to_dict() |
| 618 | args_vars = vars(main_args) |
| 619 | for k, v in cfg_dict.items(): |
| 620 | if k not in args_vars: |
| 621 | setattr(main_args, k, v) |
| 622 | else: |
| 623 | raise ValueError("Key {} can used by args only".format(k)) |
| 624 | |
| 625 | dataset = build_dataset("val", main_args) |
| 626 | model, _, _ = build_model_main(main_args) |
| 627 | n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad) |
| 628 | _outputs.update({"nparam": n_parameters}) |
| 629 | |
| 630 | model.cuda() |
| 631 | model.eval() |
| 632 | |
| 633 | warmup_step = 5 |
| 634 | total_step = 20 |
| 635 | |
| 636 | images = [] |
| 637 | for idx in range(total_step): |
| 638 | img, t = dataset[idx] |
| 639 | images.append(img) |
| 640 | |
| 641 | with torch.no_grad(): |
| 642 | tmp = [] |
| 643 | tmp2 = [] |
| 644 | for imgid, img in enumerate(tqdm.tqdm(images)): |
| 645 | inputs = [img.to("cuda")] |
| 646 | res = flop_count(model, (inputs,)) |
| 647 | t = measure_time(model, inputs) |
| 648 | tmp.append(sum(res.values())) |
| 649 | if imgid >= warmup_step: |
| 650 | tmp2.append(t) |
| 651 | _outputs.update({"detailed_flops": res}) |
| 652 | _outputs.update({"flops": fmt_res(np.array(tmp)), "time": fmt_res(np.array(tmp2))}) |
| 653 | |
| 654 | mean_infer_time = float(fmt_res(np.array(tmp2))["mean"]) |
| 655 | _outputs.update({"fps": 1 / mean_infer_time}) |
| 656 | |
| 657 | res = {"flops": fmt_res(np.array(tmp)), "time": fmt_res(np.array(tmp2))} |
| 658 | # print(res) |
| 659 | |
| 660 | output_file = os.path.join(main_args.output_dir, "flops", "log.txt") |
| 661 | os.makedirs(os.path.dirname(output_file), exist_ok=True) |
| 662 | with open(output_file, "a") as f: |
| 663 | f.write(main_args.commad_txt + "\n") |
| 664 | f.write(json.dumps(_outputs, indent=2) + "\n") |
no test coverage detected