build.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. import torch
  4. import torch.nn as nn
  5. from .loss import build_criterion
  6. from .yolov8 import YOLOv8
  7. # build object detector
  8. def build_yolov8(args, cfg, device, num_classes=80, trainable=False, deploy=False):
  9. print('==============================')
  10. print('Build {} ...'.format(args.model.upper()))
  11. print('==============================')
  12. print('Model Configuration: \n', cfg)
  13. # -------------- Build YOLO --------------
  14. model = YOLOv8(cfg = cfg,
  15. device = device,
  16. num_classes = num_classes,
  17. trainable = trainable,
  18. conf_thresh = args.conf_thresh,
  19. nms_thresh = args.nms_thresh,
  20. topk = args.topk,
  21. max_dets = args.max_dets,
  22. deploy = deploy,
  23. nms_class_agnostic = args.nms_class_agnostic
  24. )
  25. # -------------- Initialize YOLO --------------
  26. for m in model.modules():
  27. if isinstance(m, nn.BatchNorm2d):
  28. m.eps = 1e-3
  29. m.momentum = 0.03
  30. # -------------- Build criterion --------------
  31. criterion = None
  32. if trainable:
  33. # build criterion for training
  34. criterion = build_criterion(cfg, device, num_classes)
  35. return model, criterion