yolov8_config.py 6.0 KB

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