yolov8_config.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.base_lr = 0.001 # base_lr = per_image_lr * batch_size
  73. self.min_lr_ratio = 0.01 # min_lr = base_lr * min_lr_ratio
  74. self.batch_size_base = 64
  75. self.momentum = 0.9
  76. self.weight_decay = 0.05
  77. self.clip_max_norm = 35.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.5] # multi scale: [img_size * 0.5, img_size * 1.5]
  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.affine_params = {
  101. 'degrees': 0.0,
  102. 'translate': 0.2,
  103. 'scale': [0.1, 2.0],
  104. 'shear': 0.0,
  105. 'perspective': 0.0,
  106. 'hsv_h': 0.015,
  107. 'hsv_s': 0.7,
  108. 'hsv_v': 0.4,
  109. }
  110. def print_config(self):
  111. config_dict = {key: value for key, value in self.__dict__.items() if not key.startswith('__')}
  112. for k, v in config_dict.items():
  113. print("{} : {}".format(k, v))
  114. # YOLOv8-N
  115. class Yolov8NConfig(Yolov8BaseConfig):
  116. def __init__(self) -> None:
  117. super().__init__()
  118. # ---------------- Model config ----------------
  119. self.width = 0.25
  120. self.depth = 0.34
  121. self.ratio = 2.0
  122. self.scale = "n"
  123. # ---------------- Data process config ----------------
  124. self.mosaic_prob = 1.0
  125. self.mixup_prob = 0.0
  126. self.copy_paste = 0.5
  127. # YOLOv8-S
  128. class Yolov8SConfig(Yolov8BaseConfig):
  129. def __init__(self) -> None:
  130. super().__init__()
  131. # ---------------- Model config ----------------
  132. self.width = 0.50
  133. self.depth = 0.34
  134. self.ratio = 2.0
  135. self.scale = "s"
  136. # ---------------- Data process config ----------------
  137. self.mosaic_prob = 1.0
  138. self.mixup_prob = 0.0
  139. self.copy_paste = 0.5
  140. # YOLOv8-M
  141. class Yolov8MConfig(Yolov8BaseConfig):
  142. def __init__(self) -> None:
  143. super().__init__()
  144. # ---------------- Model config ----------------
  145. self.width = 0.75
  146. self.depth = 0.67
  147. self.ratio = 1.5
  148. self.scale = "m"
  149. # ---------------- Data process config ----------------
  150. self.mosaic_prob = 1.0
  151. self.mixup_prob = 0.1
  152. self.copy_paste = 0.5
  153. # YOLOv8-L
  154. class Yolov8LConfig(Yolov8BaseConfig):
  155. def __init__(self) -> None:
  156. super().__init__()
  157. # ---------------- Model config ----------------
  158. self.width = 1.0
  159. self.depth = 1.0
  160. self.ratio = 1.0
  161. self.scale = "l"
  162. # ---------------- Data process config ----------------
  163. self.mosaic_prob = 1.0
  164. self.mixup_prob = 0.1
  165. self.copy_paste = 0.5
  166. # YOLOv8-X
  167. class Yolov8XConfig(Yolov8BaseConfig):
  168. def __init__(self) -> None:
  169. super().__init__()
  170. # ---------------- Model config ----------------
  171. self.width = 1.25
  172. self.depth = 1.0
  173. self.ratio = 1.0
  174. self.scale = "x"
  175. # ---------------- Data process config ----------------
  176. self.mosaic_prob = 1.0
  177. self.mixup_prob = 0.1
  178. self.copy_paste = 0.5