__init__.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. # YOLOv7
  41. elif args.model in ['yolov7_tiny', 'yolov7', 'yolov7_x']:
  42. model, criterion = build_yolov7(
  43. args, model_cfg, device, num_classes, trainable, deploy)
  44. # YOLOv8
  45. elif args.model in ['yolov8_n', 'yolov8_s', 'yolov8_m', 'yolov8_l', 'yolov8_x']:
  46. model, criterion = build_yolov8(
  47. args, model_cfg, device, num_classes, trainable, deploy)
  48. # YOLOX
  49. elif args.model in ['yolox_n', 'yolox_s', 'yolox_m', 'yolox_l', 'yolox_x']:
  50. model, criterion = build_yolox(
  51. args, model_cfg, device, num_classes, trainable, deploy)
  52. if trainable:
  53. # Load pretrained weight
  54. if args.pretrained is not None:
  55. print('Loading COCO pretrained weight ...')
  56. checkpoint = torch.load(args.pretrained, map_location='cpu')
  57. # checkpoint state dict
  58. checkpoint_state_dict = checkpoint.pop("model")
  59. # model state dict
  60. model_state_dict = model.state_dict()
  61. # check
  62. for k in list(checkpoint_state_dict.keys()):
  63. if k in model_state_dict:
  64. shape_model = tuple(model_state_dict[k].shape)
  65. shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
  66. if shape_model != shape_checkpoint:
  67. checkpoint_state_dict.pop(k)
  68. print(k)
  69. else:
  70. checkpoint_state_dict.pop(k)
  71. print(k)
  72. model.load_state_dict(checkpoint_state_dict, strict=False)
  73. # keep training
  74. if args.resume and args.resume != "None":
  75. print('keep training: ', args.resume)
  76. checkpoint = torch.load(args.resume, map_location='cpu')
  77. # checkpoint state dict
  78. checkpoint_state_dict = checkpoint.pop("model")
  79. model.load_state_dict(checkpoint_state_dict)
  80. del checkpoint, checkpoint_state_dict
  81. return model, criterion
  82. else:
  83. return model