fcos_config.py 7.2 KB

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