__init__.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. # Real-time DETR series
  14. from .rtdetr.build import build_rtdetr
  15. # build object detector
  16. def build_model(args,
  17. model_cfg,
  18. device,
  19. num_classes=80,
  20. trainable=False,
  21. deploy=False):
  22. # YOLOv1
  23. if args.model == 'yolov1':
  24. model, criterion = build_yolov1(
  25. args, model_cfg, device, num_classes, trainable, deploy)
  26. # YOLOv2
  27. elif args.model == 'yolov2':
  28. model, criterion = build_yolov2(
  29. args, model_cfg, device, num_classes, trainable, deploy)
  30. # YOLOv3
  31. elif args.model in ['yolov3', 'yolov3_tiny']:
  32. model, criterion = build_yolov3(
  33. args, model_cfg, device, num_classes, trainable, deploy)
  34. # YOLOv4
  35. elif args.model in ['yolov4', 'yolov4_tiny']:
  36. model, criterion = build_yolov4(
  37. args, model_cfg, device, num_classes, trainable, deploy)
  38. # YOLOv5
  39. elif args.model in ['yolov5_n', 'yolov5_s', 'yolov5_m', 'yolov5_l', 'yolov5_x']:
  40. model, criterion = build_yolov5(
  41. args, model_cfg, device, num_classes, trainable, deploy)
  42. # YOLOv5-AdamW
  43. elif args.model in ['yolov5_n_adamw', 'yolov5_s_adamw', 'yolov5_m_adamw', 'yolov5_l_adamw', 'yolov5_x_adamw']:
  44. model, criterion = build_yolov5(
  45. args, model_cfg, device, num_classes, trainable, deploy)
  46. # YOLOv7
  47. elif args.model in ['yolov7_tiny', 'yolov7', 'yolov7_x']:
  48. model, criterion = build_yolov7(
  49. args, model_cfg, device, num_classes, trainable, deploy)
  50. # YOLOv8
  51. elif args.model in ['yolov8_n', 'yolov8_s', 'yolov8_m', 'yolov8_l', 'yolov8_x']:
  52. model, criterion = build_yolov8(
  53. args, model_cfg, device, num_classes, trainable, deploy)
  54. # YOLOX
  55. elif args.model in ['yolox_n', 'yolox_s', 'yolox_m', 'yolox_l', 'yolox_x']:
  56. model, criterion = build_yolox(
  57. args, model_cfg, device, num_classes, trainable, deploy)
  58. # YOLOX-AdamW
  59. elif args.model in ['yolox_n_adamw', 'yolox_s_adamw', 'yolox_m_adamw', 'yolox_l_adamw', 'yolox_x_adamw']:
  60. model, criterion = build_yolox(
  61. args, model_cfg, device, num_classes, trainable, deploy)
  62. # RT-DETR
  63. elif args.model in ['rtdetr_r18', 'rtdetr_r34', 'rtdetr_r50', 'rtdetr_r101']:
  64. model, criterion = build_rtdetr(
  65. args, model_cfg, num_classes, trainable, deploy)
  66. if trainable:
  67. # Load pretrained weight
  68. if args.pretrained is not None:
  69. print('Loading COCO pretrained weight ...')
  70. checkpoint = torch.load(args.pretrained, map_location='cpu')
  71. # checkpoint state dict
  72. checkpoint_state_dict = checkpoint.pop("model")
  73. # model state dict
  74. model_state_dict = model.state_dict()
  75. # check
  76. for k in list(checkpoint_state_dict.keys()):
  77. if k in model_state_dict:
  78. shape_model = tuple(model_state_dict[k].shape)
  79. shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
  80. if shape_model != shape_checkpoint:
  81. checkpoint_state_dict.pop(k)
  82. print(k)
  83. else:
  84. checkpoint_state_dict.pop(k)
  85. print(k)
  86. model.load_state_dict(checkpoint_state_dict, strict=False)
  87. # keep training
  88. if args.resume and args.resume != "None":
  89. print('keep training: ', args.resume)
  90. checkpoint = torch.load(args.resume, map_location='cpu')
  91. # checkpoint state dict
  92. checkpoint_state_dict = checkpoint.pop("model")
  93. model.load_state_dict(checkpoint_state_dict)
  94. del checkpoint, checkpoint_state_dict
  95. return model, criterion
  96. else:
  97. return model