cfg_det_db.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2020/5/19 21:44
  3. # @Author : xiangjing
  4. # ####################rec_train_options 参数说明##########################
  5. # 识别训练参数
  6. # base_lr:初始学习率
  7. # fine_tune_stage:
  8. # if you want to freeze some stage, and tune the others.
  9. # ['backbone', 'neck', 'head'], 所有参数都参与调优
  10. # ['backbone'], 只调优backbone部分的参数
  11. # 后续更新: 1、添加bn层freeze的代码
  12. # optimizer 和 optimizer_step:
  13. # 优化器的配置, 成对
  14. # example1: 'optimizer':['SGD'], 'optimizer_step':[],表示一直用SGD优化器
  15. # example2: 'optimizer':['SGD', 'Adam'], 'optimizer_step':[160], 表示前[0,160)个epoch使用SGD优化器,
  16. # [160,~]采用Adam优化器
  17. # lr_scheduler和lr_scheduler_info:
  18. # 学习率scheduler的设置
  19. # ckpt_save_type作用是选择模型保存的方式
  20. # HighestAcc:只保存在验证集上精度最高的模型(还是在训练集上loss最小)
  21. # FixedEpochStep: 按一定间隔保存模型
  22. ###
  23. from addict import Dict
  24. config = Dict()
  25. config.exp_name = 'DBNet'
  26. config.train_options = {
  27. # for train
  28. 'resume_from': '', # 继续训练地址
  29. 'third_party_name': '', # 加载paddle模型可选
  30. 'checkpoint_save_dir': f"./output/{config.exp_name}/checkpoint", # 模型保存地址,log文件也保存在这里
  31. 'device': 'cuda:0', # 不建议修改
  32. 'epochs': 1200,
  33. 'fine_tune_stage': ['backbone', 'neck', 'head'],
  34. 'print_interval': 1, # step为单位
  35. 'val_interval': 1, # epoch为单位
  36. 'ckpt_save_type': 'HighestAcc', # HighestAcc:只保存最高准确率模型 ;FixedEpochStep:每隔ckpt_save_epoch个epoch保存一个
  37. 'ckpt_save_epoch': 4, # epoch为单位, 只有ckpt_save_type选择FixedEpochStep时,该参数才有效
  38. }
  39. config.SEED = 927
  40. config.optimizer = {
  41. 'type': 'Adam',
  42. 'lr': 0.001,
  43. 'weight_decay': 1e-4,
  44. }
  45. # backbone设置为swin_transformer时使用
  46. # config.optimizer = {
  47. # 'type': 'AdamW',
  48. # 'lr': 0.0001,
  49. # 'betas': (0.9, 0.999),
  50. # 'weight_decay': 0.05,
  51. # }
  52. config.model = {
  53. # backbone 可以设置'pretrained': False/True
  54. 'type': "DetModel",
  55. 'backbone': {"type": "ResNet", 'layers': 18, 'pretrained': True}, # ResNet or MobileNetV3
  56. # 'backbone': {"type": "SwinTransformer", 'pretrained': True},#swin_transformer
  57. # 'backbone': {"type": "ConvNeXt", 'pretrained': True},
  58. 'neck': {"type": 'DB_fpn', 'out_channels': 256},
  59. 'head': {"type": "DBHead"},
  60. 'in_channels': 3,
  61. }
  62. config.loss = {
  63. 'type': 'DBLoss',
  64. 'alpha': 1,
  65. 'beta': 10
  66. }
  67. config.post_process = {
  68. 'type': 'DBPostProcess',
  69. 'thresh': 0.3, # 二值化输出map的阈值
  70. 'box_thresh': 0.7, # 低于此阈值的box丢弃
  71. 'unclip_ratio': 1.5 # 扩大框的比例
  72. }
  73. # for dataset
  74. # ##lable文件
  75. ### 存在问题,gt中str-->label 是放在loss中还是放在dataloader中
  76. config.dataset = {
  77. 'train': {
  78. 'dataset': {
  79. 'type': 'JsonDataset',
  80. 'file': r'train.json',
  81. 'mean': [0.485, 0.456, 0.406],
  82. 'std': [0.229, 0.224, 0.225],
  83. # db 预处理,不需要修改
  84. 'pre_processes': [{'type': 'IaaAugment', 'args': [{'type': 'Fliplr', 'args': {'p': 0.5}},
  85. {'type': 'Affine', 'args': {'rotate': [-10, 10]}},
  86. {'type': 'Resize', 'args': {'size': [0.5, 3]}}]},
  87. {'type': 'EastRandomCropData', 'args': {'size': [640, 640], 'max_tries': 50, 'keep_ratio': True}},
  88. {'type': 'MakeShrinkMap', 'args': {'shrink_ratio': 0.4, 'min_text_size': 8}},
  89. {'type': 'MakeBorderMap', 'args': {'shrink_ratio': 0.4, 'thresh_min': 0.3, 'thresh_max': 0.7}}
  90. ],
  91. 'filter_keys': ['img_path', 'img_name', 'text_polys', 'texts', 'ignore_tags', 'shape'], # 需要从data_dict里过滤掉的key
  92. 'ignore_tags': ['*', '###'],
  93. 'img_mode': 'RGB'
  94. },
  95. 'loader': {
  96. 'type': 'DataLoader', # 使用torch dataloader只需要改为 DataLoader
  97. 'batch_size': 8,
  98. 'shuffle': True,
  99. 'num_workers': 1,
  100. 'collate_fn': {
  101. 'type': ''
  102. }
  103. }
  104. },
  105. 'eval': {
  106. 'dataset': {
  107. 'type': 'JsonDataset',
  108. 'file': r'test.json',
  109. 'mean': [0.485, 0.456, 0.406],
  110. 'std': [0.229, 0.224, 0.225],
  111. 'pre_processes': [{'type': 'ResizeShortSize', 'args': {'short_size': 736, 'resize_text_polys': False}}],
  112. 'filter_keys': [], # 需要从data_dict里过滤掉的key
  113. 'ignore_tags': ['*', '###'],
  114. 'img_mode': 'RGB'
  115. },
  116. 'loader': {
  117. 'type': 'DataLoader',
  118. 'batch_size': 1, # 必须为1
  119. 'shuffle': False,
  120. 'num_workers': 1,
  121. 'collate_fn': {
  122. 'type': 'DetCollectFN'
  123. }
  124. }
  125. }
  126. }
  127. # 转换为 Dict
  128. for k, v in config.items():
  129. if isinstance(v, dict):
  130. config[k] = Dict(v)