yolov5_config.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # yolo Config
  2. def build_yolov5_config(args):
  3. if args.model == 'yolov5_n':
  4. return Yolov5NConfig()
  5. elif args.model == 'yolov5_s':
  6. return Yolov5SConfig()
  7. elif args.model == 'yolov5_m':
  8. return Yolov5MConfig()
  9. elif args.model == 'yolov5_l':
  10. return Yolov5LConfig()
  11. elif args.model == 'yolov5_x':
  12. return Yolov5XConfig()
  13. else:
  14. raise NotImplementedError("No config for model: {}".format(args.model))
  15. # YOLOv5-Base config
  16. class Yolov5BaseConfig(object):
  17. def __init__(self) -> None:
  18. # ---------------- Model config ----------------
  19. self.width = 1.0
  20. self.depth = 1.0
  21. self.out_stride = [8, 16, 32]
  22. self.max_stride = 32
  23. self.num_levels = 3
  24. self.scale = "b"
  25. ## Backbone
  26. self.bk_act = 'silu'
  27. self.bk_norm = 'BN'
  28. self.bk_depthwise = False
  29. self.use_pretrained = True
  30. ## Neck
  31. self.neck_act = 'silu'
  32. self.neck_norm = 'BN'
  33. self.neck_depthwise = False
  34. self.neck_expand_ratio = 0.5
  35. self.spp_pooling_size = 5
  36. ## FPN
  37. self.fpn_act = 'silu'
  38. self.fpn_norm = 'BN'
  39. self.fpn_depthwise = False
  40. ## Head
  41. self.head_act = 'silu'
  42. self.head_norm = 'BN'
  43. self.head_depthwise = False
  44. self.head_dim = 256
  45. self.num_cls_head = 2
  46. self.num_reg_head = 2
  47. self.anchor_size = {0: [[10, 13], [16, 30], [33, 23]],
  48. 1: [[30, 61], [62, 45], [59, 119]],
  49. 2: [[116, 90], [156, 198], [373, 326]]}
  50. # ---------------- Post-process config ----------------
  51. ## Post process
  52. self.val_topk = 1000
  53. self.val_conf_thresh = 0.001
  54. self.val_nms_thresh = 0.7
  55. self.test_topk = 100
  56. self.test_conf_thresh = 0.3
  57. self.test_nms_thresh = 0.5
  58. # ---------------- Assignment config ----------------
  59. ## Matcher
  60. self.anchor_thresh = 4.0
  61. ## Loss weight
  62. self.loss_obj = 1.0
  63. self.loss_cls = 1.0
  64. self.loss_box = 5.0
  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 = 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 = "linear"
  83. self.max_epoch = 300
  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 = 1.0
  91. self.mixup_prob = 0.15
  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. # YOLOv5-N
  116. class Yolov5NConfig(Yolov5BaseConfig):
  117. def __init__(self) -> None:
  118. super().__init__()
  119. # ---------------- Model config ----------------
  120. self.width = 0.25
  121. self.depth = 0.34
  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.0
  127. # YOLOv5-S
  128. class Yolov5SConfig(Yolov5BaseConfig):
  129. def __init__(self) -> None:
  130. super().__init__()
  131. # ---------------- Model config ----------------
  132. self.width = 0.50
  133. self.depth = 0.34
  134. self.scale = "s"
  135. # ---------------- Data process config ----------------
  136. self.mosaic_prob = 1.0
  137. self.mixup_prob = 0.0
  138. self.copy_paste = 0.0
  139. # YOLOv5-M
  140. class Yolov5MConfig(Yolov5BaseConfig):
  141. def __init__(self) -> None:
  142. super().__init__()
  143. # ---------------- Model config ----------------
  144. self.width = 0.75
  145. self.depth = 0.67
  146. self.scale = "m"
  147. # ---------------- Data process config ----------------
  148. self.mosaic_prob = 1.0
  149. self.mixup_prob = 0.1
  150. self.copy_paste = 0.0
  151. # YOLOv5-L
  152. class Yolov5LConfig(Yolov5BaseConfig):
  153. def __init__(self) -> None:
  154. super().__init__()
  155. # ---------------- Model config ----------------
  156. self.width = 1.0
  157. self.depth = 1.0
  158. self.scale = "l"
  159. # ---------------- Data process config ----------------
  160. self.mosaic_prob = 1.0
  161. self.mixup_prob = 0.1
  162. self.copy_paste = 0.0
  163. # YOLOv5-X
  164. class Yolov5XConfig(Yolov5BaseConfig):
  165. def __init__(self) -> None:
  166. super().__init__()
  167. # ---------------- Model config ----------------
  168. self.width = 1.25
  169. self.depth = 1.34
  170. self.scale = "x"
  171. # ---------------- Data process config ----------------
  172. self.mosaic_prob = 1.0
  173. self.mixup_prob = 0.1
  174. self.copy_paste = 0.0