__init__.py 3.6 KB

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