rtcdet_config.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # RTCDet config
  2. def build_rtcdet_config(args):
  3. if args.model == 'rtcdet_n':
  4. return RTCDet_Nano_Config()
  5. elif args.model == 'rtcdet_t':
  6. return RTCDet_Tiny_Config()
  7. elif args.model == 'rtcdet_s':
  8. return RTCDet_Small_Config()
  9. elif args.model == 'rtcdet_m':
  10. return RTCDet_Medium_Config()
  11. elif args.model == 'rtcdet_l':
  12. return RTCDet_Large_Config()
  13. elif args.model == 'rtcdet_x':
  14. return RTCDet_xLarge_Config()
  15. else:
  16. raise NotImplementedError("No config for model: {}".format(args.model))
  17. # RTCDet-Base config
  18. class RTCDetBaseConfig(object):
  19. def __init__(self) -> None:
  20. # ---------------- Model config ----------------
  21. self.stage_dims = [64, 128, 256, 512, 512]
  22. self.stage_depth = [3, 6, 6, 3]
  23. self.width = 1.0
  24. self.depth = 1.0
  25. self.ratio = 1.0
  26. self.reg_max = 16
  27. self.out_stride = [8, 16, 32]
  28. self.max_stride = 32
  29. self.num_levels = 3
  30. ## Backbone
  31. self.bk_block = 'elan_layer'
  32. self.bk_ds_block = 'conv'
  33. self.bk_act = 'silu'
  34. self.bk_norm = 'bn'
  35. self.bk_depthwise = False
  36. ## Neck
  37. self.neck_act = 'silu'
  38. self.neck_norm = 'bn'
  39. self.neck_depthwise = False
  40. self.neck_expand_ratio = 0.5
  41. self.spp_pooling_size = 5
  42. ## FPN
  43. self.fpn_block = 'elan_layer'
  44. self.fpn_ds_block = 'conv'
  45. self.fpn_act = 'silu'
  46. self.fpn_norm = 'bn'
  47. self.fpn_depthwise = False
  48. ## Head
  49. self.head_act = 'silu'
  50. self.head_norm = 'bn'
  51. self.head_depthwise = False
  52. self.num_cls_head = 2
  53. self.num_reg_head = 2
  54. # ---------------- Post-process config ----------------
  55. ## Post process
  56. self.val_topk = 1000
  57. self.val_conf_thresh = 0.001
  58. self.val_nms_thresh = 0.7
  59. self.test_topk = 100
  60. self.test_conf_thresh = 0.2
  61. self.test_nms_thresh = 0.5
  62. # ---------------- Assignment & Loss config ----------------
  63. self.loss_cls_type = "bce"
  64. self.matcher_dict = {"tal_alpha": 0.5, "tal_beta": 6.0, "topk_candidates": 10}
  65. self.weight_dict = {"loss_cls": 0.5, "loss_box": 7.5, "loss_dfl": 1.5}
  66. # ---------------- Assignment & Loss config ----------------
  67. # self.loss_cls_type = "vfl"
  68. # self.matcher_dict = {"tal_alpha": 1.0, "tal_beta": 6.0, "topk_candidates": 13} # For VFL
  69. # self.weight_dict = {"loss_cls": 1.0, "loss_box": 2.5, "loss_dfl": 0.5} # For VFL
  70. # ---------------- ModelEMA config ----------------
  71. self.use_ema = True
  72. self.ema_decay = 0.9998
  73. self.ema_tau = 2000
  74. # ---------------- Optimizer config ----------------
  75. self.trainer = 'yolo'
  76. self.no_norm_decay = True
  77. self.no_bias_decay = True
  78. self.batch_size_base = 64
  79. self.optimizer = 'adamw'
  80. self.base_lr = 0.001
  81. self.min_lr_ratio = 0.05 # min_lr = base_lr * min_lr_ratio
  82. self.momentum = 0.9
  83. self.weight_decay = 0.05
  84. self.clip_max_norm = 35.0
  85. self.warmup_bias_lr = 0.1
  86. self.warmup_momentum = 0.8
  87. self.use_fp16 = True # use mixing precision
  88. # ---------------- Lr Scheduler config ----------------
  89. self.warmup_epoch = 3
  90. self.lr_scheduler = "cosine"
  91. self.max_epoch = 500
  92. self.eval_epoch = 10
  93. self.no_aug_epoch = 15
  94. # ---------------- Data process config ----------------
  95. self.aug_type = 'yolo'
  96. self.box_format = 'xyxy'
  97. self.normalize_coords = False
  98. self.mosaic_prob = 0.0
  99. self.mixup_prob = 0.0
  100. self.copy_paste = 0.0 # approximated by the YOLOX's mixup
  101. self.multi_scale = [0.5, 1.5] # multi scale: [img_size * 0.5, img_size * 1.5]
  102. ## Pixel mean & std
  103. self.pixel_mean = [0., 0., 0.]
  104. self.pixel_std = [255., 255., 255.]
  105. ## Transforms
  106. self.train_img_size = 640
  107. self.test_img_size = 640
  108. self.affine_params = {
  109. 'degrees': 0.0,
  110. 'translate': 0.2,
  111. 'scale': [0.1, 2.0],
  112. 'shear': 0.0,
  113. 'perspective': 0.0,
  114. 'hsv_h': 0.015,
  115. 'hsv_s': 0.7,
  116. 'hsv_v': 0.4,
  117. }
  118. def print_config(self):
  119. config_dict = {key: value for key, value in self.__dict__.items() if not key.startswith('__')}
  120. for k, v in config_dict.items():
  121. print("{} : {}".format(k, v))
  122. # RTCDet-N
  123. class RTCDet_Nano_Config(RTCDetBaseConfig):
  124. def __init__(self) -> None:
  125. super().__init__()
  126. # ---------------- Model config ----------------
  127. self.width = 0.25
  128. self.depth = 0.34
  129. self.ratio = 2.0
  130. # ---------------- Data process config ----------------
  131. self.mosaic_prob = 1.0
  132. self.mixup_prob = 0.0
  133. self.copy_paste = 1.0
  134. # RTCDet-T
  135. class RTCDet_Tiny_Config(RTCDetBaseConfig):
  136. def __init__(self) -> None:
  137. super().__init__()
  138. # ---------------- Model config ----------------
  139. self.width = 0.375
  140. self.depth = 0.34
  141. self.ratio = 2.0
  142. # ---------------- Data process config ----------------
  143. self.mosaic_prob = 1.0
  144. self.mixup_prob = 0.0
  145. self.copy_paste = 1.0
  146. # RTCDet-S
  147. class RTCDet_Small_Config(RTCDetBaseConfig):
  148. def __init__(self) -> None:
  149. super().__init__()
  150. # ---------------- Model config ----------------
  151. self.width = 0.50
  152. self.depth = 0.34
  153. self.ratio = 2.0
  154. # ---------------- Data process config ----------------
  155. self.mosaic_prob = 1.0
  156. self.mixup_prob = 0.05
  157. self.copy_paste = 1.0
  158. # RTCDet-M
  159. class RTCDet_Medium_Config(RTCDetBaseConfig):
  160. def __init__(self) -> None:
  161. super().__init__()
  162. # ---------------- Model config ----------------
  163. self.width = 0.75
  164. self.depth = 0.67
  165. self.ratio = 1.5
  166. # ---------------- Data process config ----------------
  167. self.mosaic_prob = 1.0
  168. self.mixup_prob = 0.1
  169. self.copy_paste = 1.0
  170. # RTCDet-L
  171. class RTCDet_Large_Config(RTCDetBaseConfig):
  172. def __init__(self) -> None:
  173. super().__init__()
  174. # ---------------- Model config ----------------
  175. self.width = 1.0
  176. self.depth = 1.0
  177. self.ratio = 1.0
  178. # ---------------- Data process config ----------------
  179. self.mosaic_prob = 1.0
  180. self.mixup_prob = 0.15
  181. self.copy_paste = 1.0
  182. # RTCDet-X
  183. class RTCDet_xLarge_Config(RTCDetBaseConfig):
  184. def __init__(self) -> None:
  185. super().__init__()
  186. # ---------------- Model config ----------------
  187. self.width = 1.25
  188. self.depth = 1.0
  189. self.ratio = 1.0
  190. # ---------------- Data process config ----------------
  191. self.mosaic_prob = 1.0
  192. self.mixup_prob = 0.2
  193. self.copy_paste = 1.0