__init__.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 .yolov8.build import build_yolov8
  12. from .yolox.build import build_yolox
  13. # build object detector
  14. def build_model(args,
  15. model_cfg,
  16. device,
  17. num_classes=80,
  18. trainable=False,
  19. deploy=False):
  20. # YOLOv1
  21. if args.model == 'yolov1':
  22. model, criterion = build_yolov1(
  23. args, model_cfg, device, num_classes, trainable, deploy)
  24. # YOLOv2
  25. elif args.model == 'yolov2':
  26. model, criterion = build_yolov2(
  27. args, model_cfg, device, num_classes, trainable, deploy)
  28. # YOLOv3
  29. elif args.model in ['yolov3', 'yolov3_tiny']:
  30. model, criterion = build_yolov3(
  31. args, model_cfg, device, num_classes, trainable, deploy)
  32. # YOLOv4
  33. elif args.model in ['yolov4', 'yolov4_tiny']:
  34. model, criterion = build_yolov4(
  35. args, model_cfg, device, num_classes, trainable, deploy)
  36. # YOLOv5
  37. elif args.model in ['yolov5_n', 'yolov5_s', 'yolov5_m', 'yolov5_l', 'yolov5_x']:
  38. model, criterion = build_yolov5(
  39. args, model_cfg, device, num_classes, trainable, deploy)
  40. # YOLOv5-AdamW
  41. elif args.model in ['yolov5_n_adamw', 'yolov5_s_adamw', 'yolov5_m_adamw', 'yolov5_l_adamw', 'yolov5_x_adamw']:
  42. model, criterion = build_yolov5(
  43. args, model_cfg, device, num_classes, trainable, deploy)
  44. # YOLOv7
  45. elif args.model in ['yolov7_tiny', 'yolov7', 'yolov7_x']:
  46. model, criterion = build_yolov7(
  47. args, model_cfg, device, num_classes, trainable, deploy)
  48. # YOLOv8
  49. elif args.model in ['yolov8_n', 'yolov8_s', 'yolov8_m', 'yolov8_l', 'yolov8_x']:
  50. model, criterion = build_yolov8(
  51. args, model_cfg, device, num_classes, trainable, deploy)
  52. # YOLOX
  53. elif args.model in ['yolox_n', 'yolox_s', 'yolox_m', 'yolox_l', 'yolox_x']:
  54. model, criterion = build_yolox(
  55. args, model_cfg, device, num_classes, trainable, deploy)
  56. # YOLOX-AdamW
  57. elif args.model in ['yolox_n_adamw', 'yolox_s_adamw', 'yolox_m_adamw', 'yolox_l_adamw', 'yolox_x_adamw']:
  58. model, criterion = build_yolox(
  59. args, model_cfg, device, num_classes, trainable, deploy)
  60. if trainable:
  61. # Load pretrained weight
  62. if args.pretrained is not None:
  63. print('Loading COCO pretrained weight ...')
  64. checkpoint = torch.load(args.pretrained, map_location='cpu')
  65. # checkpoint state dict
  66. checkpoint_state_dict = checkpoint.pop("model")
  67. # model state dict
  68. model_state_dict = model.state_dict()
  69. # check
  70. for k in list(checkpoint_state_dict.keys()):
  71. if k in model_state_dict:
  72. shape_model = tuple(model_state_dict[k].shape)
  73. shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
  74. if shape_model != shape_checkpoint:
  75. checkpoint_state_dict.pop(k)
  76. print(k)
  77. else:
  78. checkpoint_state_dict.pop(k)
  79. print(k)
  80. model.load_state_dict(checkpoint_state_dict, strict=False)
  81. # keep training
  82. if args.resume and args.resume != "None":
  83. checkpoint = torch.load(args.resume, map_location='cpu')
  84. # checkpoint state dict
  85. try:
  86. checkpoint_state_dict = checkpoint.pop("model")
  87. print('Load model from the checkpoint: ', args.resume)
  88. model.load_state_dict(checkpoint_state_dict)
  89. del checkpoint, checkpoint_state_dict
  90. except:
  91. print("No model in the given checkpoint.")
  92. return model, criterion
  93. else:
  94. return model