__init__.py 3.7 KB

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