__init__.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 .yolovx.build import build_yolovx
  12. # My custom YOLO
  13. from .yolox.build import build_yolox
  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. # YOLOvx
  46. elif args.model in ['yolovx_n', 'yolovx_t', 'yolovx_s', 'yolovx_m', 'yolovx_l', 'yolovx_x']:
  47. model, criterion = build_yolovx(
  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. if trainable:
  54. # Load pretrained weight
  55. if args.pretrained is not None:
  56. print('Loading COCO pretrained weight ...')
  57. checkpoint = torch.load(args.pretrained, map_location='cpu')
  58. # checkpoint state dict
  59. checkpoint_state_dict = checkpoint.pop("model")
  60. # model state dict
  61. model_state_dict = model.state_dict()
  62. # check
  63. for k in list(checkpoint_state_dict.keys()):
  64. if k in model_state_dict:
  65. shape_model = tuple(model_state_dict[k].shape)
  66. shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
  67. if shape_model != shape_checkpoint:
  68. checkpoint_state_dict.pop(k)
  69. print(k)
  70. else:
  71. checkpoint_state_dict.pop(k)
  72. print(k)
  73. model.load_state_dict(checkpoint_state_dict, strict=False)
  74. # keep training
  75. if args.resume is not None:
  76. print('keep training: ', args.resume)
  77. checkpoint = torch.load(args.resume, map_location='cpu')
  78. # checkpoint state dict
  79. checkpoint_state_dict = checkpoint.pop("model")
  80. model.load_state_dict(checkpoint_state_dict)
  81. del checkpoint, checkpoint_state_dict
  82. return model, criterion
  83. else:
  84. return model