text: num_classes = 3
  
  # model settings
  model = dict(
      type='HybridTaskCascade',
      backbone=dict(
          type='Res2Net',
          depth=101,
          scales=4,
          base_width=26,
          num_stages=4,
          out_indices=(0, 1, 2, 3),
          frozen_stages=1,
          norm_cfg=dict(type='BN', requires_grad=True),
          norm_eval=True,
          style='pytorch',
          init_cfg=dict()),
      neck=dict(
          type='FPN',
          in_channels=[256, 512, 1024, 2048],
          out_channels=256,
          num_outs=5),
      rpn_head=dict(
          type='RPNHead',
          in_channels=256,
          feat_channels=256,
          anchor_generator=dict(
              type='AnchorGenerator',
              scales=[8],
              ratios=[0.5, 1.0, 2.0],
              strides=[4, 8, 16, 32, 64]),
          bbox_coder=dict(
              type='DeltaXYWHBBoxCoder',
              target_means=[.0, .0, .0, .0],
              target_stds=[1.0, 1.0, 1.0, 1.0]),
          loss_cls=dict(
              type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0),
          loss_bbox=dict(type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.0)),
      roi_head=dict(
          type='HybridTaskCascadeRoIHead',
          interleaved=True,
          mask_info_flow=True,
          num_stages=3,
          stage_loss_weights=[1, 0.5, 0.25],
          bbox_roi_extractor=dict(
              type='SingleRoIExtractor',
              roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0),
              out_channels=256,
              featmap_strides=[4, 8, 16, 32]),
          bbox_head=[
              dict(
                  type='Shared2FCBBoxHead',
                  in_channels=256,
                  fc_out_channels=1024,
                  roi_feat_size=7,
                  num_classes=num_classes,
                  bbox_coder=dict(
                      type='DeltaXYWHBBoxCoder',
                      target_means=[0., 0., 0., 0.],
                      target_stds=[0.1, 0.1, 0.2, 0.2]),
                  reg_class_agnostic=True,
                  loss_cls=dict(
                      type='CrossEntropyLoss',
                      use_sigmoid=False,
                      loss_weight=1.0),
                  loss_bbox=dict(type='SmoothL1Loss', beta=1.0,
                                 loss_weight=1.0)),
              dict(
                  type='Shared2FCBBoxHead',
                  in_channels=256,
                  fc_out_channels=1024,
                  roi_feat_size=7,
                  num_classes=num_classes,
                  bbox_coder=dict(
                      type='DeltaXYWHBBoxCoder',
                      target_means=[0., 0., 0., 0.],
                      target_stds=[0.05, 0.05, 0.1, 0.1]),
                  reg_class_agnostic=True,
                  loss_cls=dict(
                      type='CrossEntropyLoss',
                      use_sigmoid=False,
                      loss_weight=1.0),
                  loss_bbox=dict(type='SmoothL1Loss', beta=1.0,
                                 loss_weight=1.0)),
              dict(
                  type='Shared2FCBBoxHead',
                  in_channels=256,
                  fc_out_channels=1024,
                  roi_feat_size=7,
                  num_classes=num_classes,
                  bbox_coder=dict(
                      type='DeltaXYWHBBoxCoder',
                      target_means=[0., 0., 0., 0.],
                      target_stds=[0.033, 0.033, 0.067, 0.067]),
                  reg_class_agnostic=True,
                  loss_cls=dict(
                      type='CrossEntropyLoss',
                      use_sigmoid=False,
                      loss_weight=1.0),
                  loss_bbox=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0))
          ],
          mask_roi_extractor=dict(
              type='SingleRoIExtractor',
              roi_layer=dict(type='RoIAlign', output_size=14, sampling_ratio=0),
              out_channels=256,
              featmap_strides=[4, 8, 16, 32]),
          mask_head=[
              dict(
                  type='HTCMaskHead',
                  with_conv_res=False,
                  num_convs=4,
                  in_channels=256,
                  conv_out_channels=256,
                  num_classes=num_classes,
                  loss_mask=dict(
                      type='CrossEntropyLoss', use_mask=True, loss_weight=1.0)),
              dict(
                  type='HTCMaskHead',
                  num_convs=4,
                  in_channels=256,
                  conv_out_channels=256,
                  num_classes=num_classes,
                  loss_mask=dict(
                      type='CrossEntropyLoss', use_mask=True, loss_weight=1.0)),
              dict(
                  type='HTCMaskHead',
                  num_convs=4,
                  in_channels=256,
                  conv_out_channels=256,
                  num_classes=num_classes,
                  loss_mask=dict(
                      type='CrossEntropyLoss', use_mask=True, loss_weight=1.0))
          ]),
      # model training and testing settings
      train_cfg=dict(
          rpn=dict(
              assigner=dict(
                  type='MaxIoUAssigner',
                  pos_iou_thr=0.7,
                  neg_iou_thr=0.3,
                  min_pos_iou=0.3,
                  ignore_iof_thr=-1),
              sampler=dict(
                  type='RandomSampler',
                  num=256,
                  pos_fraction=0.5,
                  neg_pos_ub=-1,
                  add_gt_as_proposals=False),
              allowed_border=0,
              pos_weight=-1,
              debug=False),
          rpn_proposal=dict(
              nms_pre=2000,
              max_per_img=2000,
              nms=dict(type='nms', iou_threshold=0.7),
              min_bbox_size=0),
          rcnn=[
              dict(
                  assigner=dict(
                      type='MaxIoUAssigner',
                      pos_iou_thr=0.5,
                      neg_iou_thr=0.5,
                      min_pos_iou=0.5,
                      ignore_iof_thr=-1),
                  sampler=dict(
                      type='RandomSampler',
                      num=512,
                      pos_fraction=0.25,
                      neg_pos_ub=-1,
                      add_gt_as_proposals=True),
                  mask_size=28,
                  pos_weight=-1,
                  debug=False),
              dict(
                  assigner=dict(
                      type='MaxIoUAssigner',
                      pos_iou_thr=0.6,
                      neg_iou_thr=0.6,
                      min_pos_iou=0.6,
                      ignore_iof_thr=-1),
                  sampler=dict(
                      type='RandomSampler',
                      num=512,
                      pos_fraction=0.25,
                      neg_pos_ub=-1,
                      add_gt_as_proposals=True),
                  mask_size=28,
                  pos_weight=-1,
                  debug=False),
              dict(
                  assigner=dict(
                      type='MaxIoUAssigner',
                      pos_iou_thr=0.7,
                      neg_iou_thr=0.7,
                      min_pos_iou=0.7,
                      ignore_iof_thr=-1),
                  sampler=dict(
                      type='RandomSampler',
                      num=512,
                      pos_fraction=0.25,
                      neg_pos_ub=-1,
                      add_gt_as_proposals=True),
                  mask_size=28,
                  pos_weight=-1,
                  debug=False)
          ]),
      test_cfg=dict(
          rpn=dict(
              nms_pre=1000,
              max_per_img=1000,
              nms=dict(type='nms', iou_threshold=0.7),
              min_bbox_size=0),
          rcnn=dict(
              score_thr=0.001,
              nms=dict(type='nms', iou_threshold=0.5),
              max_per_img=100,
              mask_thr_binary=0.5)))
  dataset_type = 'CocoDataset'
  data_root = 'data/cell/'
  classes = ['shsy5y', 'astro', 'cort']
  
  albu_train_transforms = [
      dict(type='VerticalFlip', p=0.5),
      dict(type='RandomRotate90', p=0.5),
      # dict(type='ColorJitter', p=0.5)
      # dict(type='ShiftScaleRotate', shift_limit=0.0625, scale_limit=0.9, rotate_limit=30, interpolation=1, p=0.5)
  ]
  
  img_norm_cfg = dict(
      mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
  train_pipeline = [
      dict(type='LoadImageFromFile'),
      dict(type='LoadAnnotations', with_bbox=True, with_mask=True),
      dict(type='Resize', img_scale=[(1333, 1333), (400, 400)], keep_ratio=True),
      dict(type='RandomFlip', flip_ratio=0.5),
      # dict(type='AutoAugmentPolicy', autoaug_type="v2"),
      # dict(type='MixUp'),
      # dict(type='BoxPaste', objects_from="./data/rich/cuts", sample_thr=0.15, sample_n=2, p=0.8),
      # dict(type='InstaBoost', scale=(0.95, 1.05), color_prob=0),
      dict(type='Normalize', **img_norm_cfg),
      dict(type='Pad', size_divisor=32),
      dict(type='Albu',
           transforms=albu_train_transforms,
           bbox_params=dict(type='BboxParams',
                            format='pascal_voc',
                            label_fields=['gt_labels'],
                            min_visibility=0.0,
                            filter_lost_elements=True),
           keymap={'img': 'image', 'gt_bboxes': 'bboxes', 'gt_masks': 'masks'},
           update_pad_shape=False,
           skip_img_without_anno=True),
      dict(type='DefaultFormatBundle'),
      dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_masks', 'gt_labels']),
  ]
  test_pipeline = [
      dict(type='LoadImageFromFile'),
      dict(
          type='MultiScaleFlipAug',
          img_scale=[(1333, 1333), (850, 850), (400, 400)],
          flip=True,
          flip_direction=["horizontal","vertical"],
          transforms=[
              dict(type='Resize', keep_ratio=True),
              dict(type='RandomFlip'),
              dict(type='Normalize', **img_norm_cfg),
              dict(type='Pad', size_divisor=32),
              dict(type='ImageToTensor', keys=['img']),
              dict(type='Collect', keys=['img']),
          ])
  ]
  holdout = 0
  data = dict(
      samples_per_gpu=4,
      workers_per_gpu=2,
      train=[dict(
              classes=classes,
              type="DownSampleCocoDataset",
              downsample=2,
              ann_file=data_root + f'train_tiny/annotations/fold_{fold}.json',
              img_prefix=data_root + 'train_tiny/images/',
              pipeline=train_pipeline) for fold in range(5) if fold != holdout] + \
             [dict(
              classes=classes,
              type="DownSampleCocoDataset",
              downsample=2,
              ann_file=data_root + f'train_semi/annotations/train_semi_with_ann.json',
              img_prefix=data_root + 'train_semi/images/',
              pipeline=train_pipeline)],
      val=dict(
              classes=classes,
              type=dataset_type,
              ann_file=data_root + f'train_tiny/annotations/fold_{holdout}.json',
              img_prefix=data_root + 'train_tiny/images/',
              pipeline=test_pipeline),
      test=dict(
              classes=classes,
              type=dataset_type,
              ann_file=data_root + f'train_tiny/annotations/fold_{holdout}.json',
              img_prefix=data_root + 'train_tiny/images/',
              pipeline=test_pipeline)
  )
  
  nx = 1
  work_dir = f'./work_dirs/cell/htcr2101_{nx}x_d2_800_f{holdout}_ps'
  evaluation = dict(
      classwise=True, 
      interval=12, 
      metric=['bbox', 'segm'],
      jsonfile_prefix=f"{work_dir}/valid")
  optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001)
  optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2))
  lr_config = dict(
      policy='step',
      warmup='linear',
      warmup_iters=500,
      warmup_ratio=1/3,
      step=[8 * nx, 11 * nx])
  custom_hooks = [dict(type='NumClassCheckHook')]
  total_epochs = 12 * nx
  runner = dict(type='EpochBasedRunner', max_epochs=total_epochs)
  checkpoint_config = dict(interval=total_epochs, save_optimizer=False)
  log_config = dict(interval=50, hooks=[dict(type='TextLoggerHook')])
  dist_params = dict(backend='nccl')
  log_level = 'INFO'
  load_from = './weights/htc_r2_101_fpn_20e_coco-3a8d2112.pth'
  resume_from = None
  workflow = [('train', 1)]
  fp16 = dict(loss_scale=512.0)
  gpu_ids=(2,3)
  
  
  only_swa_training = False
  # whether to perform swa training
  swa_training = True
  # load the best pre_trained model as the starting model for swa training
  swa_load_from = work_dir + f'/epoch_{total_epochs}.pth'
  swa_resume_from = None
  
  # swa optimizer
  swa_optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001)
  swa_optimizer_config = dict(grad_clip=None)
  
  # swa learning policy
  swa_lr_config = dict(
      policy='cyclic',
      target_ratio=(1, 0.01),
      cyclic_times=12,
      step_ratio_up=0.0)
  swa_runner = dict(type='EpochBasedRunner', max_epochs=12)
  # the epoch interval to perform swa
  swa_interval = 1
  
  # swa checkpoint setting
  swa_checkpoint_config = dict(interval=1, filename_tmpl='swa_epoch_{}.pth', save_optimizer=False)
  