__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 .yolox.build import build_yolox
  10. from .yolov6.build import build_yolov6
  11. from .yolov7.build import build_yolov7
  12. from .yolov8.build import build_yolov8
  13. from .yolov9.build import build_gelan
  14. from .yolov10.build import build_yolov10
  15. from .yolo11.build import build_yolo11
  16. from .yolof.build import build_yolof
  17. from .fcos.build import build_fcos
  18. from .rtdetr.build import build_rtdetr
  19. # build object detector
  20. def build_model(args, cfg, is_val=False):
  21. # ------------ build object detector ------------
  22. ## Modified YOLOv1
  23. if 'yolov10' in args.model:
  24. model, criterion = build_yolov10(cfg, is_val)
  25. elif 'yolov1' in args.model:
  26. model, criterion = build_yolov1(cfg, is_val)
  27. ## Modified YOLOv2
  28. elif 'yolov2' in args.model:
  29. model, criterion = build_yolov2(cfg, is_val)
  30. ## Modified YOLOv3
  31. elif 'yolov3' in args.model:
  32. model, criterion = build_yolov3(cfg, is_val)
  33. ## Modified YOLOv4
  34. elif 'yolov4' in args.model:
  35. model, criterion = build_yolov4(cfg, is_val)
  36. ## Anchor-free YOLOv5
  37. elif 'yolox' in args.model:
  38. model, criterion = build_yolox(cfg, is_val)
  39. ## Modified YOLOv5
  40. elif 'yolov5' in args.model:
  41. model, criterion = build_yolov5(cfg, is_val)
  42. ## YOLOv6
  43. elif 'yolov6' in args.model:
  44. model, criterion = build_yolov6(cfg, is_val)
  45. ## YOLOv7
  46. elif 'yolov7' in args.model:
  47. model, criterion = build_yolov7(cfg, is_val)
  48. ## YOLOv8
  49. elif 'yolov8' in args.model:
  50. model, criterion = build_yolov8(cfg, is_val)
  51. ## GElan
  52. elif 'yolov9' in args.model:
  53. model, criterion = build_gelan(cfg, is_val)
  54. ## YOLO11
  55. elif 'yolo11' in args.model:
  56. model, criterion = build_yolo11(cfg, is_val)
  57. ## Yolof
  58. elif 'yolof' in args.model:
  59. model, criterion = build_yolof(cfg, is_val)
  60. ## Fcos
  61. elif 'fcos' in args.model:
  62. model, criterion = build_fcos(cfg, is_val)
  63. ## RT-DETR
  64. elif 'rtdetr' in args.model:
  65. model, criterion = build_rtdetr(cfg, is_val)
  66. if is_val:
  67. # ------------ Load pretrained weight ------------
  68. if hasattr(args, "pretrained") and 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 from the given checkpoint ------------
  88. if hasattr(args, "resume") and args.resume and args.resume != "None":
  89. checkpoint = torch.load(args.resume, map_location='cpu')
  90. # checkpoint state dict
  91. try:
  92. checkpoint_state_dict = checkpoint.pop("model")
  93. print('Load model from the checkpoint: ', args.resume)
  94. model.load_state_dict(checkpoint_state_dict)
  95. del checkpoint, checkpoint_state_dict
  96. except:
  97. print("No model in the given checkpoint.")
  98. return model, criterion
  99. else:
  100. return model