fcos_config.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. # Fully Convolutional One-Stage object detector
  2. def build_fcos_config(args):
  3. # Standard FCOS 1x
  4. if args.model == 'fcos_r18_1x':
  5. return Fcos_R18_1x_Config()
  6. elif args.model == 'fcos_r50_1x':
  7. return Fcos_R50_1x_Config()
  8. # Standard FCOS 3x
  9. elif args.model == 'fcos_r18_3x':
  10. return Fcos_R18_3x_Config()
  11. elif args.model == 'fcos_r50_3x':
  12. return Fcos_R50_3x_Config()
  13. # Real-time FCOS 3x
  14. elif args.model == 'fcos_rt_r18_3x':
  15. return FcosRT_R18_3x_Config()
  16. elif args.model == 'fcos_rt_r50_3x':
  17. return FcosRT_R50_3x_Config()
  18. # E2E FCOS 3x
  19. elif args.model == 'fcos_e2e_r18_3x':
  20. return FcosE2E_R18_3x_Config()
  21. else:
  22. raise NotImplementedError("No config for model: {}".format(args.model))
  23. # --------------- Base configuration ---------------
  24. class FcosBaseConfig(object):
  25. def __init__(self):
  26. # --------- Backbone ---------
  27. self.backbone = "resnet50"
  28. self.bk_norm = "FrozeBN"
  29. self.res5_dilation = False
  30. self.use_pretrained = True
  31. self.freeze_at = 1
  32. self.max_stride = 128
  33. self.out_stride = [8, 16, 32, 64, 128]
  34. # --------- Neck ---------
  35. self.neck = 'basic_fpn'
  36. self.fpn_p6_feat = True
  37. self.fpn_p7_feat = True
  38. self.fpn_p6_from_c5 = False
  39. # --------- Head ---------
  40. self.head = 'fcos_head'
  41. self.head_dim = 256
  42. self.num_cls_head = 4
  43. self.num_reg_head = 4
  44. self.head_act = 'relu'
  45. self.head_norm = 'GN'
  46. # --------- Post-process ---------
  47. self.train_topk = 1000
  48. self.train_conf_thresh = 0.05
  49. self.train_nms_thresh = 0.6
  50. self.test_topk = 100
  51. self.test_conf_thresh = 0.5
  52. self.test_nms_thresh = 0.45
  53. self.nms_class_agnostic = True
  54. # --------- Label Assignment ---------
  55. self.matcher = 'fcos_matcher'
  56. self.matcher_hpy = {'center_sampling_radius': 1.5,
  57. 'object_sizes_of_interest': [[-1, 64],
  58. [64, 128],
  59. [128, 256],
  60. [256, 512],
  61. [512, float('inf')]]
  62. }
  63. # --------- Loss weight ---------
  64. self.focal_loss_alpha = 0.25
  65. self.focal_loss_gamma = 2.0
  66. self.loss_cls_weight = 1.0
  67. self.loss_reg_weight = 1.0
  68. self.loss_ctn_weight = 1.0
  69. # --------- Optimizer ---------
  70. self.optimizer = 'sgd'
  71. self.batch_size_base = 16
  72. self.per_image_lr = 0.01 / 16
  73. self.bk_lr_ratio = 1.0 / 1.0
  74. self.momentum = 0.9
  75. self.weight_decay = 1e-4
  76. self.clip_max_norm = -1.0
  77. # --------- LR Scheduler ---------
  78. self.lr_scheduler = 'step'
  79. self.warmup = 'linear'
  80. self.warmup_iters = 500
  81. self.warmup_factor = 0.00066667
  82. # --------- Train epoch ---------
  83. self.max_epoch = 12 # 1x
  84. self.lr_epoch = [8, 11] # 1x
  85. self.eval_epoch = 2
  86. # --------- Data process ---------
  87. ## input size
  88. self.train_min_size = [800] # short edge of image
  89. self.train_max_size = 1333
  90. self.test_min_size = [800]
  91. self.test_max_size = 1333
  92. ## Pixel mean & std
  93. self.pixel_mean = [0.485, 0.456, 0.406]
  94. self.pixel_std = [0.229, 0.224, 0.225]
  95. ## Transforms
  96. self.box_format = 'xyxy'
  97. self.normalize_coords = False
  98. self.detr_style = False
  99. self.trans_config = [
  100. {'name': 'RandomHFlip'},
  101. {'name': 'RandomResize'},
  102. ]
  103. def print_config(self):
  104. config_dict = {key: value for key, value in self.__dict__.items() if not key.startswith('__')}
  105. for k, v in config_dict.items():
  106. print("{} : {}".format(k, v))
  107. # --------------- 1x scheduler ---------------
  108. class Fcos_R18_1x_Config(FcosBaseConfig):
  109. def __init__(self) -> None:
  110. super().__init__()
  111. ## Backbone
  112. self.backbone = "resnet18"
  113. class Fcos_R50_1x_Config(Fcos_R18_1x_Config):
  114. def __init__(self) -> None:
  115. super().__init__()
  116. self.backbone = "resnet50"
  117. # --------------- 3x scheduler ---------------
  118. class Fcos_R18_3x_Config(Fcos_R18_1x_Config):
  119. def __init__(self) -> None:
  120. super().__init__()
  121. # --------- Train epoch ---------
  122. self.max_epoch = 36 # 3x
  123. self.lr_epoch = [24, 33] # 3x
  124. self.eval_epoch = 2
  125. # --------- Data process ---------
  126. ## input size
  127. self.train_min_size = [480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800] # short edge of image
  128. self.train_max_size = 1333
  129. self.test_min_size = [800]
  130. self.test_max_size = 1333
  131. class Fcos_R50_3x_Config(Fcos_R18_3x_Config):
  132. def __init__(self) -> None:
  133. super().__init__()
  134. ## Backbone
  135. self.backbone = "resnet50"
  136. # --------------- RT-FCOS & 3x scheduler ---------------
  137. class FcosRT_R18_3x_Config(FcosBaseConfig):
  138. def __init__(self) -> None:
  139. super().__init__()
  140. ## Backbone
  141. self.backbone = "resnet18"
  142. self.max_stride = 32
  143. self.out_stride = [8, 16, 32]
  144. # --------- Neck ---------
  145. self.neck = 'basic_fpn'
  146. self.fpn_p6_feat = False
  147. self.fpn_p7_feat = False
  148. self.fpn_p6_from_c5 = False
  149. # --------- Head ---------
  150. self.head = 'fcos_rt_head'
  151. self.head_dim = 256
  152. self.num_cls_head = 4
  153. self.num_reg_head = 4
  154. self.head_act = 'relu'
  155. self.head_norm = 'GN'
  156. # --------- Post-process ---------
  157. self.train_topk = 1000
  158. self.train_conf_thresh = 0.05
  159. self.train_nms_thresh = 0.6
  160. self.test_topk = 100
  161. self.test_conf_thresh = 0.4
  162. self.test_nms_thresh = 0.45
  163. self.nms_class_agnostic = True
  164. # --------- Label Assignment ---------
  165. self.matcher = 'simota'
  166. self.matcher_hpy = {'soft_center_radius': 3.0,
  167. 'topk_candidates': 13}
  168. # --------- Loss weight ---------
  169. self.focal_loss_alpha = 0.25
  170. self.focal_loss_gamma = 2.0
  171. self.loss_cls_weight = 1.0
  172. self.loss_reg_weight = 2.0
  173. # --------- Train epoch ---------
  174. self.max_epoch = 36 # 3x
  175. self.lr_epoch = [24, 33] # 3x
  176. # --------- Data process ---------
  177. ## input size
  178. self.train_min_size = [256, 288, 320, 352, 384, 416, 448, 480, 512, 544, 576, 608] # short edge of image
  179. self.train_max_size = 900
  180. self.test_min_size = [512]
  181. self.test_max_size = 736
  182. ## Pixel mean & std
  183. self.pixel_mean = [0.485, 0.456, 0.406]
  184. self.pixel_std = [0.229, 0.224, 0.225]
  185. ## Transforms
  186. self.box_format = 'xyxy'
  187. self.normalize_coords = False
  188. self.detr_style = False
  189. self.trans_config = [
  190. {'name': 'RandomHFlip'},
  191. {'name': 'RandomResize'},
  192. ]
  193. class FcosRT_R50_3x_Config(FcosRT_R18_3x_Config):
  194. def __init__(self) -> None:
  195. super().__init__()
  196. # --------- Backbone ---------
  197. self.backbone = "resnet50"
  198. # --------------- E2E-FCOS & 3x scheduler ---------------
  199. class FcosE2E_R18_3x_Config(FcosBaseConfig):
  200. def __init__(self) -> None:
  201. super().__init__()
  202. ## Backbone
  203. self.backbone = "resnet18"
  204. self.max_stride = 32
  205. self.out_stride = [8, 16, 32]
  206. # --------- Neck ---------
  207. self.neck = 'basic_fpn'
  208. self.fpn_p6_feat = False
  209. self.fpn_p7_feat = False
  210. self.fpn_p6_from_c5 = False
  211. # --------- Head ---------
  212. self.head = 'fcos_rt_head'
  213. self.head_dim = 256
  214. self.num_cls_head = 4
  215. self.num_reg_head = 4
  216. self.head_act = 'relu'
  217. self.head_norm = 'GN'
  218. # --------- Post-process ---------
  219. self.train_topk = 100
  220. self.train_conf_thresh = 0.05
  221. self.test_topk = 100
  222. self.test_conf_thresh = 0.4
  223. # --------- Label Assignment ---------
  224. self.matcher = 'simota'
  225. self.matcher_hpy = {'soft_center_radius': 3.0,
  226. 'topk_candidates': 13}
  227. # --------- Loss weight ---------
  228. self.focal_loss_alpha = 0.25
  229. self.focal_loss_gamma = 2.0
  230. self.loss_cls_weight = 1.0
  231. self.loss_reg_weight = 2.0
  232. # --------- Train epoch ---------
  233. self.max_epoch = 36 # 3x
  234. self.lr_epoch = [24, 33] # 3x
  235. # --------- Data process ---------
  236. ## input size
  237. self.train_min_size = [256, 288, 320, 352, 384, 416, 448, 480, 512, 544, 576, 608] # short edge of image
  238. self.train_max_size = 900
  239. self.test_min_size = [512]
  240. self.test_max_size = 736
  241. ## Pixel mean & std
  242. self.pixel_mean = [0.485, 0.456, 0.406]
  243. self.pixel_std = [0.229, 0.224, 0.225]
  244. ## Transforms
  245. self.box_format = 'xyxy'
  246. self.normalize_coords = False
  247. self.detr_style = False
  248. self.trans_config = [
  249. {'name': 'RandomHFlip'},
  250. {'name': 'RandomResize'},
  251. ]