__init__.py 3.2 KB

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