__init__.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. import torch
  4. # YOLO series
  5. from .yolov1.build import build_yolov1
  6. from .yolov2.build import build_yolov2
  7. from .yolov3.build import build_yolov3
  8. from .yolov4.build import build_yolov4
  9. from .yolov5.build import build_yolov5
  10. from .yolov7.build import build_yolov7
  11. from .yolov8.build import build_yolov8
  12. from .yolox.build import build_yolox
  13. # My RTCDet series
  14. from .rtcdet.build import build_rtcdet
  15. from .rtdetr.build import build_rtdetr
  16. from .rtpdetr.build import build_rtpdetr
  17. # build object detector
  18. def build_model(args,
  19. model_cfg,
  20. device,
  21. num_classes=80,
  22. trainable=False,
  23. deploy=False):
  24. # YOLOv1
  25. if args.model == 'yolov1':
  26. model, criterion = build_yolov1(
  27. args, model_cfg, device, num_classes, trainable, deploy)
  28. # YOLOv2
  29. elif args.model == 'yolov2':
  30. model, criterion = build_yolov2(
  31. args, model_cfg, device, num_classes, trainable, deploy)
  32. # YOLOv3
  33. elif args.model in ['yolov3', 'yolov3_tiny']:
  34. model, criterion = build_yolov3(
  35. args, model_cfg, device, num_classes, trainable, deploy)
  36. # YOLOv4
  37. elif args.model in ['yolov4', 'yolov4_tiny']:
  38. model, criterion = build_yolov4(
  39. args, model_cfg, device, num_classes, trainable, deploy)
  40. # YOLOv5
  41. elif args.model in ['yolov5_n', 'yolov5_s', 'yolov5_m', 'yolov5_l', 'yolov5_x']:
  42. model, criterion = build_yolov5(
  43. args, model_cfg, device, num_classes, trainable, deploy)
  44. # YOLOv5-AdamW
  45. elif args.model in ['yolov5_n_adamw', 'yolov5_s_adamw', 'yolov5_m_adamw', 'yolov5_l_adamw', 'yolov5_x_adamw']:
  46. model, criterion = build_yolov5(
  47. args, model_cfg, device, num_classes, trainable, deploy)
  48. # YOLOv7
  49. elif args.model in ['yolov7_tiny', 'yolov7', 'yolov7_x']:
  50. model, criterion = build_yolov7(
  51. args, model_cfg, device, num_classes, trainable, deploy)
  52. # YOLOv8
  53. elif args.model in ['yolov8_n', 'yolov8_s', 'yolov8_m', 'yolov8_l', 'yolov8_x']:
  54. model, criterion = build_yolov8(
  55. args, model_cfg, device, num_classes, trainable, deploy)
  56. # YOLOX
  57. elif args.model in ['yolox_n', 'yolox_s', 'yolox_m', 'yolox_l', 'yolox_x']:
  58. model, criterion = build_yolox(
  59. args, model_cfg, device, num_classes, trainable, deploy)
  60. # YOLOX-AdamW
  61. elif args.model in ['yolox_n_adamw', 'yolox_s_adamw', 'yolox_m_adamw', 'yolox_l_adamw', 'yolox_x_adamw']:
  62. model, criterion = build_yolox(
  63. args, model_cfg, device, num_classes, trainable, deploy)
  64. # RTCDet
  65. elif args.model in ['rtcdet_n', 'rtcdet_t', 'rtcdet_s', 'rtcdet_m', 'rtcdet_l', 'rtcdet_x']:
  66. model, criterion = build_rtcdet(
  67. args, model_cfg, device, num_classes, trainable, deploy)
  68. # RT-DETR
  69. elif args.model in ['rtdetr_r18', 'rtdetr_r34', 'rtdetr_r50', 'rtdetr_r101']:
  70. model, criterion = build_rtdetr(
  71. args, model_cfg, num_classes, trainable, deploy)
  72. # RT-PlainDETR
  73. elif args.model in ['rtpdetr_r18', 'rtpdetr_r34', 'rtpdetr_r50', 'rtpdetr_r101']:
  74. model, criterion = build_rtpdetr(
  75. args, model_cfg, num_classes, trainable, deploy)
  76. if trainable:
  77. # Load pretrained weight
  78. if args.pretrained is not None:
  79. print('Loading COCO pretrained weight ...')
  80. checkpoint = torch.load(args.pretrained, map_location='cpu')
  81. # checkpoint state dict
  82. checkpoint_state_dict = checkpoint.pop("model")
  83. # model state dict
  84. model_state_dict = model.state_dict()
  85. # check
  86. for k in list(checkpoint_state_dict.keys()):
  87. if k in model_state_dict:
  88. shape_model = tuple(model_state_dict[k].shape)
  89. shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
  90. if shape_model != shape_checkpoint:
  91. checkpoint_state_dict.pop(k)
  92. print(k)
  93. else:
  94. checkpoint_state_dict.pop(k)
  95. print(k)
  96. model.load_state_dict(checkpoint_state_dict, strict=False)
  97. # keep training
  98. if args.resume and args.resume != "None":
  99. print('keep training: ', args.resume)
  100. checkpoint = torch.load(args.resume, map_location='cpu')
  101. # checkpoint state dict
  102. checkpoint_state_dict = checkpoint.pop("model")
  103. model.load_state_dict(checkpoint_state_dict)
  104. del checkpoint, checkpoint_state_dict
  105. return model, criterion
  106. else:
  107. return model