__init__.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 .yolov4.build import build_yolov4
  8. from .yolov5.build import build_yolov5
  9. from .yolov7.build import build_yolov7
  10. from .yolov8.build import build_yolov8
  11. from .yolox.build import build_yolox
  12. # build object detector
  13. def build_model(args,
  14. model_cfg,
  15. device,
  16. num_classes=80,
  17. trainable=False):
  18. # YOLOv1
  19. if args.model == 'yolov1':
  20. model, criterion = build_yolov1(
  21. args, model_cfg, device, num_classes, trainable)
  22. # YOLOv2
  23. elif args.model == 'yolov2':
  24. model, criterion = build_yolov2(
  25. args, model_cfg, device, num_classes, trainable)
  26. # YOLOv3
  27. elif args.model == 'yolov3':
  28. model, criterion = build_yolov3(
  29. args, model_cfg, device, num_classes, trainable)
  30. # YOLOv4
  31. elif args.model == 'yolov4':
  32. model, criterion = build_yolov4(
  33. args, model_cfg, device, num_classes, trainable)
  34. # YOLOv5
  35. elif args.model in ['yolov5_n', 'yolov5_s', 'yolov5_m', 'yolov5_l', 'yolov5_x']:
  36. model, criterion = build_yolov5(
  37. args, model_cfg, device, num_classes, trainable)
  38. # YOLOv7
  39. elif args.model in ['yolov7_t', 'yolov7_l', 'yolov7_x']:
  40. model, criterion = build_yolov7(
  41. args, model_cfg, device, num_classes, trainable)
  42. # YOLOv8
  43. elif args.model in ['yolov8_n', 'yolov8_s', 'yolov8_m', 'yolov8_l', 'yolov8_x']:
  44. model, criterion = build_yolov8(
  45. args, model_cfg, device, num_classes, trainable)
  46. # YOLOX
  47. elif args.model == 'yolox':
  48. model, criterion = build_yolox(
  49. args, model_cfg, device, num_classes, trainable)
  50. if trainable:
  51. # Load pretrained weight
  52. if args.pretrained is not None:
  53. print('Loading COCO pretrained weight ...')
  54. checkpoint = torch.load(args.pretrained, map_location='cpu')
  55. # checkpoint state dict
  56. checkpoint_state_dict = checkpoint.pop("model")
  57. # model state dict
  58. model_state_dict = model.state_dict()
  59. # check
  60. for k in list(checkpoint_state_dict.keys()):
  61. if 'reduce_layer_3' in k:
  62. k_new = k.split('.')
  63. k_new[1] = 'downsample_layer_1'
  64. k_ = k_new[0] + '.' + k_new[1] + '.' + k_new[2] + '.' + k_new[3] + '.' + k_new[4]
  65. checkpoint_state_dict[k_] = checkpoint_state_dict[k]
  66. checkpoint_state_dict.pop(k)
  67. if k in model_state_dict:
  68. shape_model = tuple(model_state_dict[k].shape)
  69. shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
  70. if shape_model != shape_checkpoint:
  71. checkpoint_state_dict.pop(k)
  72. print(k)
  73. else:
  74. checkpoint_state_dict.pop(k)
  75. print(k)
  76. model.load_state_dict(checkpoint_state_dict, strict=False)
  77. # keep training
  78. if args.resume is not None:
  79. print('keep training: ', args.resume)
  80. checkpoint = torch.load(args.resume, map_location='cpu')
  81. # checkpoint state dict
  82. checkpoint_state_dict = checkpoint.pop("model")
  83. model.load_state_dict(checkpoint_state_dict)
  84. return model, criterion
  85. else:
  86. return model