__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 .yolovx.build import build_yolovx
  12. # My custom YOLO
  13. from .yolox.build import build_yolox
  14. # Lightweight Detector
  15. from .lowdet.build import build_lowdet
  16. # Real-time DETR
  17. from .rtdetr.build import build_rtdetr
  18. # build object detector
  19. def build_model(args,
  20. model_cfg,
  21. device,
  22. num_classes=80,
  23. trainable=False,
  24. deploy=False):
  25. # YOLOv1
  26. if args.model == 'yolov1':
  27. model, criterion = build_yolov1(
  28. args, model_cfg, device, num_classes, trainable, deploy)
  29. # YOLOv2
  30. elif args.model == 'yolov2':
  31. model, criterion = build_yolov2(
  32. args, model_cfg, device, num_classes, trainable, deploy)
  33. # YOLOv3
  34. elif args.model in ['yolov3', 'yolov3_tiny']:
  35. model, criterion = build_yolov3(
  36. args, model_cfg, device, num_classes, trainable, deploy)
  37. # YOLOv4
  38. elif args.model in ['yolov4', 'yolov4_tiny']:
  39. model, criterion = build_yolov4(
  40. args, model_cfg, device, num_classes, trainable, deploy)
  41. # YOLOv5
  42. elif args.model in ['yolov5_n', 'yolov5_s', 'yolov5_m', 'yolov5_l', 'yolov5_x']:
  43. model, criterion = build_yolov5(
  44. args, model_cfg, device, num_classes, trainable, deploy)
  45. # YOLOv7
  46. elif args.model in ['yolov7_tiny', 'yolov7', 'yolov7_x']:
  47. model, criterion = build_yolov7(
  48. args, model_cfg, device, num_classes, trainable, deploy)
  49. # YOLOvx
  50. elif args.model in ['yolovx_n', 'yolovx_t', 'yolovx_s', 'yolovx_m', 'yolovx_l', 'yolovx_x']:
  51. model, criterion = build_yolovx(
  52. args, model_cfg, device, num_classes, trainable, deploy)
  53. # YOLOX
  54. elif args.model in ['yolox_n', 'yolox_s', 'yolox_m', 'yolox_l', 'yolox_x']:
  55. model, criterion = build_yolox(
  56. args, model_cfg, device, num_classes, trainable, deploy)
  57. # LOWDet
  58. elif args.model == 'lowdet':
  59. model, criterion = build_lowdet(
  60. args, model_cfg, device, num_classes, trainable, deploy)
  61. # RT-DETR
  62. elif args.model in ['rtdetr_n', 'rtdetr_s', 'rtdetr_m', 'rtdetr_l', 'rtdetr_x']:
  63. model, criterion = build_rtdetr(
  64. args, model_cfg, device, num_classes, trainable, deploy)
  65. if trainable:
  66. # Load pretrained weight
  67. if args.pretrained is not None:
  68. print('Loading COCO pretrained weight ...')
  69. checkpoint = torch.load(args.pretrained, map_location='cpu')
  70. # checkpoint state dict
  71. checkpoint_state_dict = checkpoint.pop("model")
  72. # model state dict
  73. model_state_dict = model.state_dict()
  74. # check
  75. for k in list(checkpoint_state_dict.keys()):
  76. if k in model_state_dict:
  77. shape_model = tuple(model_state_dict[k].shape)
  78. shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
  79. if shape_model != shape_checkpoint:
  80. checkpoint_state_dict.pop(k)
  81. print(k)
  82. else:
  83. checkpoint_state_dict.pop(k)
  84. print(k)
  85. model.load_state_dict(checkpoint_state_dict, strict=False)
  86. # keep training
  87. if args.resume is not None:
  88. print('keep training: ', args.resume)
  89. checkpoint = torch.load(args.resume, map_location='cpu')
  90. # checkpoint state dict
  91. checkpoint_state_dict = checkpoint.pop("model")
  92. model.load_state_dict(checkpoint_state_dict)
  93. return model, criterion
  94. else:
  95. return model