__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. import torch
  4. from .yolov1.build import build_yolov1
  5. from .yolov2.build import build_yolov2
  6. from .yolov3.build import build_yolov3
  7. from .yolov4.build import build_yolov4
  8. from .yolov5.build import build_yolov5
  9. from .yolov7.build import build_yolov7
  10. from .yolox.build import build_yolox
  11. # build object detector
  12. def build_model(args,
  13. model_cfg,
  14. device,
  15. num_classes=80,
  16. trainable=False,
  17. deploy=False):
  18. # YOLOv1
  19. if args.model == 'yolov1':
  20. model, criterion = build_yolov1(
  21. args, model_cfg, device, num_classes, trainable, deploy)
  22. # YOLOv2
  23. elif args.model == 'yolov2':
  24. model, criterion = build_yolov2(
  25. args, model_cfg, device, num_classes, trainable, deploy)
  26. # YOLOv3
  27. elif args.model in ['yolov3', 'yolov3_t']:
  28. model, criterion = build_yolov3(
  29. args, model_cfg, device, num_classes, trainable, deploy)
  30. # YOLOv4
  31. elif args.model in ['yolov4', 'yolov4_t']:
  32. model, criterion = build_yolov4(
  33. args, model_cfg, device, num_classes, trainable, deploy)
  34. # YOLOv5
  35. elif args.model in ['yolov5_n', 'yolov5_s', 'yolov5_m', 'yolov5_l', 'yolov5_x']:
  36. model, criterion = build_yolov5(
  37. args, model_cfg, device, num_classes, trainable, deploy)
  38. # YOLOv7
  39. elif args.model in ['yolov7_t', 'yolov7_l', 'yolov7_x']:
  40. model, criterion = build_yolov7(
  41. args, model_cfg, device, num_classes, trainable, deploy)
  42. # YOLOX
  43. elif args.model in ['yolox_n', 'yolox_s', 'yolox_m', 'yolox_l', 'yolox_x']:
  44. model, criterion = build_yolox(
  45. args, model_cfg, device, num_classes, trainable, deploy)
  46. if trainable:
  47. # Load pretrained weight
  48. if args.pretrained is not None:
  49. print('Loading COCO pretrained weight ...')
  50. checkpoint = torch.load(args.pretrained, map_location='cpu')
  51. # checkpoint state dict
  52. checkpoint_state_dict = checkpoint.pop("model")
  53. # model state dict
  54. model_state_dict = model.state_dict()
  55. # check
  56. for k in list(checkpoint_state_dict.keys()):
  57. if k in model_state_dict:
  58. shape_model = tuple(model_state_dict[k].shape)
  59. shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
  60. if shape_model != shape_checkpoint:
  61. checkpoint_state_dict.pop(k)
  62. print(k)
  63. else:
  64. checkpoint_state_dict.pop(k)
  65. print(k)
  66. model.load_state_dict(checkpoint_state_dict, strict=False)
  67. # keep training
  68. if args.resume is not None:
  69. print('keep training: ', args.resume)
  70. checkpoint = torch.load(args.resume, map_location='cpu')
  71. # checkpoint state dict
  72. checkpoint_state_dict = checkpoint.pop("model")
  73. model.load_state_dict(checkpoint_state_dict)
  74. return model, criterion
  75. else:
  76. return model