yolof_config.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # You Only Look One-level Feature
  2. def build_yolof_config(args):
  3. if args.model == 'yolof_r18_c5_1x':
  4. return Yolof_R18_C5_1x_Config()
  5. elif args.model == 'yolof_r50_c5_1x':
  6. return Yolof_R50_C5_1x_Config()
  7. elif args.model == 'yolof_r101_c5_1x':
  8. return Yolof_R101_C5_1x_Config()
  9. elif args.model == 'yolof_r50_dc5_1x':
  10. return Yolof_R50_DC5_1x_Config()
  11. elif args.model == 'yolof_r101_dc5_1x':
  12. return Yolof_R101_DC5_1x_Config()
  13. else:
  14. raise NotImplementedError("No config for model: {}".format(args.model))
  15. # --------------- Base configuration ---------------
  16. class YolofBaseConfig(object):
  17. def __init__(self):
  18. # --------- Backbone ---------
  19. self.backbone = "resnet50"
  20. self.bk_norm = "FrozeBN"
  21. self.res5_dilation = False
  22. self.use_pretrained = True
  23. self.freeze_at = 1
  24. self.max_stride = 32
  25. self.out_stride = 32
  26. # --------- Neck ---------
  27. self.neck = 'dilated_encoder'
  28. self.neck_dilations = [2, 4, 6, 8]
  29. self.neck_expand_ratio = 0.25
  30. self.neck_act = 'relu'
  31. self.neck_norm = 'BN'
  32. # --------- Head ---------
  33. self.head = 'yolof_head'
  34. self.head_dim = 512
  35. self.num_cls_head = 2
  36. self.num_reg_head = 4
  37. self.head_act = 'relu'
  38. self.head_norm = 'BN'
  39. self.center_clamp = 32
  40. self.anchor_size = [[32, 32],
  41. [64, 64],
  42. [128, 128],
  43. [256, 256],
  44. [512, 512]]
  45. # --------- Post-process ---------
  46. self.train_topk = 1000
  47. self.train_conf_thresh = 0.05
  48. self.train_nms_thresh = 0.6
  49. self.test_topk = 300
  50. self.test_conf_thresh = 0.3
  51. self.test_nms_thresh = 0.45
  52. self.nms_class_agnostic = True
  53. # --------- Label Assignment ---------
  54. self.matcher = 'yolof_matcher'
  55. self.matcher_hpy = {'topk_candidates': 4,
  56. 'iou_thresh': 0.15,
  57. 'ignore_thresh': 0.7,
  58. }
  59. # --------- Loss weight ---------
  60. self.focal_loss_alpha = 0.25
  61. self.focal_loss_gamma = 2.0
  62. self.loss_cls_weight = 1.0
  63. self.loss_reg_weight = 1.0
  64. # --------- Optimizer ---------
  65. self.optimizer = 'sgd'
  66. self.batch_size_base = 64
  67. self.per_image_lr = 0.12 / 64
  68. self.bk_lr_ratio = 1.0 / 3.0
  69. self.momentum = 0.9
  70. self.weight_decay = 1e-4
  71. self.clip_max_norm = 10.0
  72. # --------- LR Scheduler ---------
  73. self.lr_scheduler = 'step'
  74. self.warmup = 'linear'
  75. self.warmup_iters = 1500
  76. self.warmup_factor = 0.00066667
  77. # --------- Train epoch ---------
  78. self.max_epoch = 12 # 1x
  79. self.lr_epoch = [8, 11] # 1x
  80. self.eval_epoch = 2
  81. # --------- Data process ---------
  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. {'name': 'RandomShift', 'max_shift': 32},
  98. ]
  99. def print_config(self):
  100. config_dict = {key: value for key, value in self.__dict__.items() if not key.startswith('__')}
  101. for k, v in config_dict.items():
  102. print("{} : {}".format(k, v))
  103. # --------------- C5 level & 1x scheduler ---------------
  104. class Yolof_R18_C5_1x_Config(YolofBaseConfig):
  105. def __init__(self) -> None:
  106. super().__init__()
  107. ## Backbone
  108. # --------- Backbone ---------
  109. self.backbone = "resnet18"
  110. class Yolof_R50_C5_1x_Config(Yolof_R18_C5_1x_Config):
  111. def __init__(self) -> None:
  112. super().__init__()
  113. ## Backbone
  114. # --------- Backbone ---------
  115. self.backbone = "resnet50"
  116. class Yolof_R101_C5_1x_Config(Yolof_R18_C5_1x_Config):
  117. def __init__(self) -> None:
  118. super().__init__()
  119. ## Backbone
  120. # --------- Backbone ---------
  121. self.backbone = "resnet101"
  122. # --------------- DC5 level & 1x scheduler ---------------
  123. class Yolof_R50_DC5_1x_Config(YolofBaseConfig):
  124. def __init__(self) -> None:
  125. super().__init__()
  126. ## Backbone
  127. # --------- Backbone ---------
  128. self.backbone = "resnet50"
  129. self.res5_dilation = True
  130. self.use_pretrained = True
  131. self.max_stride = 16
  132. self.out_stride = 16
  133. # --------- Neck ---------
  134. self.neck = 'dilated_encoder'
  135. self.neck_dilations = [4, 8, 12, 16]
  136. self.neck_expand_ratio = 0.25
  137. self.neck_act = 'relu'
  138. self.neck_norm = 'BN'
  139. # --------- Head ---------
  140. self.anchor_size = [[16, 16],
  141. [32, 32],
  142. [64, 64],
  143. [128, 128],
  144. [256, 256],
  145. [512, 512]],
  146. # --------- Label Assignment ---------
  147. self.matcher = 'yolof_matcher'
  148. self.matcher_hpy = {'topk_candidates': 8,
  149. 'iou_thresh': 0.1,
  150. 'ignore_thresh': 0.7,
  151. }
  152. class Yolof_R101_DC5_1x_Config(Yolof_R50_DC5_1x_Config):
  153. def __init__(self) -> None:
  154. super().__init__()
  155. # --------- Backbone ---------
  156. self.backbone = "resnet101"