benchmark.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import argparse
  2. import cv2
  3. import os
  4. import time
  5. import numpy as np
  6. from copy import deepcopy
  7. import torch
  8. # load transform
  9. from dataset.build import build_dataset, build_transform
  10. # load some utils
  11. from utils.misc import load_weight, compute_flops
  12. from utils.box_ops import rescale_bboxes
  13. from utils.vis_tools import visualize
  14. from config import build_config
  15. from models import build_model
  16. def parse_args():
  17. parser = argparse.ArgumentParser(description='Real-time Object Detection LAB')
  18. # Basic setting
  19. parser.add_argument('-size', '--img_size', default=640, type=int,
  20. help='the max size of input image')
  21. parser.add_argument('--cuda', action='store_true', default=False,
  22. help='use cuda.')
  23. # Model setting
  24. parser.add_argument('-m', '--model', default='yolov1_r18', type=str,
  25. help='build yolo')
  26. parser.add_argument('--weight', default=None,
  27. type=str, help='Trained state_dict file path to open')
  28. parser.add_argument('--fuse_conv_bn', action='store_true', default=False,
  29. help='fuse Conv & BN')
  30. # Data setting
  31. parser.add_argument('--root', default='D:/python_work/dataset/COCO/',
  32. help='data root')
  33. return parser.parse_args()
  34. @torch.no_grad()
  35. def test_det(model,
  36. device,
  37. dataset,
  38. transform=None
  39. ):
  40. # Step-1: Compute FLOPs and Params
  41. compute_flops(model, cfg.test_img_size, device)
  42. # Step-2: Compute FPS
  43. num_images = 2002
  44. total_time = 0
  45. count = 0
  46. with torch.no_grad():
  47. for index in range(num_images):
  48. if index % 500 == 0:
  49. print('Testing image {:d}/{:d}....'.format(index+1, num_images))
  50. # Load an image
  51. image, _ = dataset.pull_image(index)
  52. # Preprocess
  53. x, _, ratio = transform(image)
  54. x = x.unsqueeze(0).to(device)
  55. # Start
  56. torch.cuda.synchronize()
  57. start_time = time.perf_counter()
  58. # Inference
  59. outputs = model(x)
  60. # End
  61. torch.cuda.synchronize()
  62. elapsed = time.perf_counter() - start_time
  63. if index > 1:
  64. total_time += elapsed
  65. count += 1
  66. print('- FPS :', 1.0 / (total_time / count))
  67. if __name__ == '__main__':
  68. args = parse_args()
  69. # cuda
  70. if args.cuda:
  71. print('use cuda')
  72. device = torch.device("cuda")
  73. else:
  74. device = torch.device("cpu")
  75. # Model Config
  76. cfg = build_config(args)
  77. # Transform
  78. transform = build_transform(cfg, is_train=False)
  79. # Dataset
  80. args.dataset = 'coco'
  81. dataset = build_dataset(args, cfg, transform, is_train=False)
  82. # Build model
  83. model = build_model(args, cfg, is_val=False)
  84. # Load trained weight
  85. model = load_weight(model, args.weight, args.fuse_conv_bn)
  86. model.to(device).eval()
  87. # Run
  88. test_det(model = model,
  89. device = device,
  90. dataset = dataset,
  91. transform = transform,
  92. )