yolov8_config.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # yolo Config
  2. def build_yolov8_config(args):
  3. if args.model == 'yolov8_n':
  4. return Yolov8NConfig()
  5. elif args.model == 'yolov8_s':
  6. return Yolov8SConfig()
  7. else:
  8. raise NotImplementedError("No config for model: {}".format(args.model))
  9. # YOLOv8-Base config
  10. class Yolov8BaseConfig(object):
  11. def __init__(self) -> None:
  12. # ---------------- Model config ----------------
  13. self.width = 1.0
  14. self.depth = 1.0
  15. self.ratio = 1.0
  16. self.reg_max = 16
  17. self.out_stride = [8, 16, 32]
  18. self.max_stride = 32
  19. self.num_levels = 3
  20. self.scale = "b"
  21. ## Backbone
  22. self.bk_act = 'silu'
  23. self.bk_norm = 'BN'
  24. self.bk_depthwise = False
  25. self.use_pretrained = False
  26. ## Neck
  27. self.neck_act = 'silu'
  28. self.neck_norm = 'BN'
  29. self.neck_depthwise = False
  30. self.neck_expand_ratio = 0.5
  31. self.spp_pooling_size = 5
  32. ## FPN
  33. self.fpn_act = 'silu'
  34. self.fpn_norm = 'BN'
  35. self.fpn_depthwise = False
  36. ## Head
  37. self.head_act = 'silu'
  38. self.head_norm = 'BN'
  39. self.head_depthwise = False
  40. self.num_cls_head = 2
  41. self.num_reg_head = 2
  42. # ---------------- Post-process config ----------------
  43. ## Post process
  44. self.val_topk = 1000
  45. self.val_conf_thresh = 0.001
  46. self.val_nms_thresh = 0.7
  47. self.test_topk = 100
  48. self.test_conf_thresh = 0.2
  49. self.test_nms_thresh = 0.5
  50. # ---------------- Assignment config ----------------
  51. ## Matcher
  52. self.tal_topk_candidates = 10
  53. self.tal_alpha = 0.5
  54. self.tal_beta = 6.0
  55. ## Loss weight
  56. self.loss_cls = 0.5
  57. self.loss_box = 7.5
  58. self.loss_dfl = 1.5
  59. # ---------------- ModelEMA config ----------------
  60. self.use_ema = True
  61. self.ema_decay = 0.9998
  62. self.ema_tau = 2000
  63. # ---------------- Optimizer config ----------------
  64. self.trainer = 'yolo'
  65. self.optimizer = 'adamw'
  66. self.per_image_lr = 0.001 / 64
  67. self.base_lr = None # base_lr = per_image_lr * batch_size
  68. self.min_lr_ratio = 0.01 # min_lr = base_lr * min_lr_ratio
  69. self.momentum = 0.9
  70. self.weight_decay = 0.05
  71. self.clip_max_norm = -1.
  72. self.warmup_bias_lr = 0.1
  73. self.warmup_momentum = 0.8
  74. # ---------------- Lr Scheduler config ----------------
  75. self.warmup_epoch = 3
  76. self.lr_scheduler = "cosine"
  77. self.max_epoch = 500
  78. self.eval_epoch = 10
  79. self.no_aug_epoch = 20
  80. # ---------------- Data process config ----------------
  81. self.aug_type = 'yolo'
  82. self.box_format = 'xyxy'
  83. self.normalize_coords = False
  84. self.mosaic_prob = 0.0
  85. self.mixup_prob = 0.0
  86. self.copy_paste = 0.0 # approximated by the YOLOX's mixup
  87. self.multi_scale = [0.5, 1.25] # multi scale: [img_size * 0.5, img_size * 1.25]
  88. ## Pixel mean & std
  89. self.pixel_mean = [0., 0., 0.]
  90. self.pixel_std = [255., 255., 255.]
  91. ## Transforms
  92. self.train_img_size = 640
  93. self.test_img_size = 640
  94. self.use_ablu = True
  95. self.affine_params = {
  96. 'degrees': 0.0,
  97. 'translate': 0.2,
  98. 'scale': [0.1, 2.0],
  99. 'shear': 0.0,
  100. 'perspective': 0.0,
  101. 'hsv_h': 0.015,
  102. 'hsv_s': 0.7,
  103. 'hsv_v': 0.4,
  104. }
  105. def print_config(self):
  106. config_dict = {key: value for key, value in self.__dict__.items() if not key.startswith('__')}
  107. for k, v in config_dict.items():
  108. print("{} : {}".format(k, v))
  109. # YOLOv8-N
  110. class Yolov8NConfig(Yolov8BaseConfig):
  111. def __init__(self) -> None:
  112. super().__init__()
  113. # ---------------- Model config ----------------
  114. self.width = 0.25
  115. self.depth = 0.34
  116. self.ratio = 2.0
  117. self.scale = "n"
  118. # ---------------- Data process config ----------------
  119. self.mosaic_prob = 1.0
  120. self.mixup_prob = 0.0
  121. self.copy_paste = 0.5
  122. # YOLOv8-S
  123. class Yolov8SConfig(Yolov8BaseConfig):
  124. def __init__(self) -> None:
  125. super().__init__()
  126. # ---------------- Model config ----------------
  127. self.width = 0.50
  128. self.depth = 0.34
  129. self.ratio = 2.0
  130. self.scale = "s"
  131. # ---------------- Data process config ----------------
  132. self.mosaic_prob = 1.0
  133. self.mixup_prob = 0.0
  134. self.copy_paste = 0.5
  135. # YOLOv8-M
  136. class Yolov8MConfig(Yolov8BaseConfig):
  137. def __init__(self) -> None:
  138. super().__init__()
  139. # ---------------- Model config ----------------
  140. self.width = 0.75
  141. self.depth = 0.67
  142. self.ratio = 1.5
  143. self.scale = "m"
  144. # ---------------- Data process config ----------------
  145. self.mosaic_prob = 1.0
  146. self.mixup_prob = 0.1
  147. self.copy_paste = 0.5
  148. # YOLOv8-L
  149. class Yolov8MConfig(Yolov8BaseConfig):
  150. def __init__(self) -> None:
  151. super().__init__()
  152. # ---------------- Model config ----------------
  153. self.width = 1.0
  154. self.depth = 1.0
  155. self.ratio = 1.0
  156. self.scale = "l"
  157. # ---------------- Data process config ----------------
  158. self.mosaic_prob = 1.0
  159. self.mixup_prob = 0.1
  160. self.copy_paste = 0.5
  161. # YOLOv8-X
  162. class Yolov8MConfig(Yolov8BaseConfig):
  163. def __init__(self) -> None:
  164. super().__init__()
  165. # ---------------- Model config ----------------
  166. self.width = 1.25
  167. self.depth = 1.0
  168. self.ratio = 1.0
  169. self.scale = "x"
  170. # ---------------- Data process config ----------------
  171. self.mosaic_prob = 1.0
  172. self.mixup_prob = 0.1
  173. self.copy_paste = 0.5