__init__.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 .yolov5.build import build_yolov5
  8. from .yolov5_af.build import build_yolov5af
  9. from .yolov6.build import build_yolov6
  10. from .yolov8.build import build_yolov8
  11. from .yolov8_e2e.build import build_yolov8_e2e
  12. from .gelan.build import build_gelan
  13. from .rtcdet.build import build_rtcdet
  14. from .rtdetr.build import build_rtdetr
  15. # build object detector
  16. def build_model(args, cfg, is_val=False):
  17. # ------------ build object detector ------------
  18. ## Modified YOLOv1
  19. if 'yolov1' in args.model:
  20. model, criterion = build_yolov1(cfg, is_val)
  21. ## Modified YOLOv2
  22. elif 'yolov2' in args.model:
  23. model, criterion = build_yolov2(cfg, is_val)
  24. ## Modified YOLOv3
  25. elif 'yolov3' in args.model:
  26. model, criterion = build_yolov3(cfg, is_val)
  27. ## Anchor-free YOLOv5
  28. elif 'yolov5_af' in args.model:
  29. model, criterion = build_yolov5af(cfg, is_val)
  30. ## Modified YOLOv5
  31. elif 'yolov5' in args.model:
  32. model, criterion = build_yolov5(cfg, is_val)
  33. ## YOLOv6
  34. elif 'yolov6' in args.model:
  35. model, criterion = build_yolov6(cfg, is_val)
  36. ## YOLOv8
  37. elif 'yolov8_e2e' in args.model:
  38. model, criterion = build_yolov8_e2e(cfg, is_val)
  39. ## YOLOv8
  40. elif 'yolov8' in args.model:
  41. model, criterion = build_yolov8(cfg, is_val)
  42. ## GElan
  43. elif 'gelan' in args.model:
  44. model, criterion = build_gelan(cfg, is_val)
  45. ## RTCDet
  46. elif 'rtcdet' in args.model:
  47. model, criterion = build_rtcdet(cfg, is_val)
  48. ## RT-DETR
  49. elif 'rtdetr' in args.model:
  50. model, criterion = build_rtdetr(cfg, is_val)
  51. if is_val:
  52. # ------------ Load pretrained weight ------------
  53. if args.pretrained is not None:
  54. print('Loading COCO pretrained weight ...')
  55. checkpoint = torch.load(args.pretrained, map_location='cpu')
  56. # checkpoint state dict
  57. checkpoint_state_dict = checkpoint.pop("model")
  58. # model state dict
  59. model_state_dict = model.state_dict()
  60. # check
  61. for k in list(checkpoint_state_dict.keys()):
  62. if k in model_state_dict:
  63. shape_model = tuple(model_state_dict[k].shape)
  64. shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
  65. if shape_model != shape_checkpoint:
  66. checkpoint_state_dict.pop(k)
  67. print(k)
  68. else:
  69. checkpoint_state_dict.pop(k)
  70. print(k)
  71. model.load_state_dict(checkpoint_state_dict, strict=False)
  72. # ------------ Keep training from the given checkpoint ------------
  73. if args.resume and args.resume != "None":
  74. checkpoint = torch.load(args.resume, map_location='cpu')
  75. # checkpoint state dict
  76. try:
  77. checkpoint_state_dict = checkpoint.pop("model")
  78. print('Load model from the checkpoint: ', args.resume)
  79. model.load_state_dict(checkpoint_state_dict)
  80. del checkpoint, checkpoint_state_dict
  81. except:
  82. print("No model in the given checkpoint.")
  83. return model, criterion
  84. else:
  85. return model