build.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 .yolox import YOLOX
  7. # build object detector
  8. def build_yolox(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 = YOLOX(
  15. cfg=cfg,
  16. device=device,
  17. num_classes=num_classes,
  18. trainable=trainable,
  19. conf_thresh=args.conf_thresh,
  20. nms_thresh=args.nms_thresh,
  21. topk=args.topk,
  22. deploy=deploy
  23. )
  24. # -------------- Initialize YOLO --------------
  25. for m in model.modules():
  26. if isinstance(m, nn.BatchNorm2d):
  27. m.eps = 1e-3
  28. m.momentum = 0.03
  29. # -------------- Build criterion --------------
  30. criterion = None
  31. if trainable:
  32. # build criterion for training
  33. criterion = build_criterion(cfg, device, num_classes)
  34. return model, criterion