1+ from torch .optim .adamw import AdamW
2+ from mmengine .config import read_base
3+ from mmengine .runner .loops import EpochBasedTrainLoop , TestLoop , ValLoop
4+ from mmengine .optim .optimizer import OptimWrapper
5+ from mmengine .optim .scheduler .lr_scheduler import LinearLR
6+ from mmengine .hooks .ema_hook import EMAHook
7+ from mmdet .models .data_preprocessors import DetDataPreprocessor
8+ from mmdet .models .necks import ChannelMapper
9+ from mmdet .models .losses import L1Loss
10+ from mmdet .models .task_modules import FocalLossCost , HungarianAssigner
11+ from mmdet .models .layers .ema import ExpMomentumEMA
12+ from ai4rs .models .losses import GDLoss
13+ from projects .rotated_dino .rotated_dino .match_cost import ChamferCost , GDCost
14+ from projects .rotated_rtdetr .rotated_rtdetr import (RotatedRTDETR , RTDETRFPN , ResNetV1dPaddle ,
15+ RotatedRTDETRHead , RTDETRVarifocalLoss )
16+
17+ with read_base ():
18+ from configs ._base_ .datasets .dior import *
19+ from .default_runtime import *
20+
21+ pretrained = ('https://www.modelscope.cn/models/wokaikaixinxin/ai4rs/resolve/'
22+ 'master/rtdetr/resnet50vd_ssld_v2_pretrained_d037e232.pth' ) # noqa
23+
24+ angle_cfg = dict (
25+ width_longer = True ,
26+ start_angle = 0 ,
27+ )
28+ angle_factor = 3.1415926535897932384626433832795
29+
30+
31+ model = dict (
32+ type = RotatedRTDETR ,
33+ num_queries = 300 , # num_matching_queries, 900 for DINO
34+ with_box_refine = True ,
35+ as_two_stage = True ,
36+ data_preprocessor = dict (
37+ type = DetDataPreprocessor ,
38+ mean = [103.53 , 116.28 , 123.675 ],
39+ std = [57.375 , 57.12 , 58.395 ],
40+ bgr_to_rgb = False ,
41+ boxtype2tensor = False ,
42+ batch_augments = None ),
43+ backbone = dict (
44+ type = ResNetV1dPaddle , # ResNet for DINO
45+ depth = 50 ,
46+ num_stages = 4 ,
47+ out_indices = (1 , 2 , 3 ),
48+ frozen_stages = 0 , # -1 for DINO
49+ norm_cfg = dict (type = 'BN' , requires_grad = False ), # BN for DINO
50+ norm_eval = True ,
51+ style = 'pytorch' ,
52+ init_cfg = dict (type = 'Pretrained' , checkpoint = pretrained )),
53+ neck = dict (
54+ type = ChannelMapper ,
55+ in_channels = [512 , 1024 , 2048 ],
56+ kernel_size = 1 ,
57+ out_channels = 256 ,
58+ act_cfg = None ,
59+ norm_cfg = dict (type = 'BN' , requires_grad = True ), # GN for DINO
60+ num_outs = 3 , # 4 for DINO
61+ init_cfg = dict (
62+ type = 'Kaiming' ,
63+ layer = 'Conv2d' ,
64+ a = 5 ** 0.5 ,
65+ distribution = 'uniform' ,
66+ mode = 'fan_in' ,
67+ nonlinearity = 'leaky_relu' )),
68+ encoder = dict (
69+ use_encoder_idx = [- 1 ],
70+ num_encoder_layers = 1 ,
71+ in_channels = [256 , 256 , 256 ],
72+ fpn_cfg = dict (
73+ type = RTDETRFPN ,
74+ in_channels = [256 , 256 , 256 ],
75+ out_channels = 256 ,
76+ expansion = 1.0 ,
77+ norm_cfg = dict (type = 'BN' , requires_grad = True )),
78+ layer_cfg = dict (
79+ self_attn_cfg = dict (embed_dims = 256 , num_heads = 8 , dropout = 0.0 ),
80+ ffn_cfg = dict (
81+ embed_dims = 256 ,
82+ feedforward_channels = 1024 , # 2048 for DINO
83+ ffn_drop = 0.0 ,
84+ act_cfg = dict (type = 'GELU' )))), # ReLU for DINO
85+ decoder = dict (
86+ num_layers = 6 ,
87+ return_intermediate = True ,
88+ angle_factor = angle_factor ,
89+ layer_cfg = dict (
90+ self_attn_cfg = dict (embed_dims = 256 , num_heads = 8 , dropout = 0.0 ),
91+ cross_attn_cfg = dict (
92+ embed_dims = 256 ,
93+ num_levels = 3 , # 4 for DINO
94+ dropout = 0.0 ),
95+ ffn_cfg = dict (
96+ embed_dims = 256 ,
97+ feedforward_channels = 1024 , # 2048 for DINO
98+ ffn_drop = 0.0 )),
99+ post_norm_cfg = None ),
100+ bbox_head = dict (
101+ type = RotatedRTDETRHead ,
102+ num_classes = 20 ,
103+ angle_cfg = angle_cfg ,
104+ angle_factor = angle_factor ,
105+ sync_cls_avg_factor = True ,
106+ loss_cls = dict (
107+ type = RTDETRVarifocalLoss ,
108+ varifocal_loss_iou_type = 'hbox_iou' , # hbox_iou, rbox_iou, prob_iou
109+ use_sigmoid = True ,
110+ alpha = 0.75 ,
111+ gamma = 2.0 ,
112+ iou_weighted = True ,
113+ loss_weight = 1.0 ),
114+ loss_bbox = dict (type = L1Loss , loss_weight = 5.0 ),
115+ loss_iou = dict (
116+ type = GDLoss ,
117+ loss_type = 'kld' ,
118+ fun = 'log1p' ,
119+ tau = 1 ,
120+ sqrt = False ,
121+ loss_weight = 2.0 )),
122+ dn_cfg = dict ( # TODO: Move to model.train_cfg ?
123+ label_noise_scale = 0.5 ,
124+ box_noise_scale = 1.0 ,
125+ angle_cfg = angle_cfg ,
126+ angle_factor = angle_factor ,
127+ noise_mode = 'only_xyxy' , # 'only_xyxy', 'only_angle', 'only_xywh', 'all_xyxya'
128+ group_cfg = dict (dynamic = True ,
129+ num_groups = None ,
130+ num_dn_queries = 100 )), # TODO: half num_dn_queries
131+ # training and testing settings
132+ train_cfg = dict (
133+ assigner = dict (
134+ type = HungarianAssigner ,
135+ match_costs = [
136+ dict (type = FocalLossCost , weight = 2.0 ),
137+ dict (type = ChamferCost , weight = 5.0 , box_format = 'xywha' ),
138+ dict (
139+ type = GDCost ,
140+ loss_type = 'kld' ,
141+ fun = 'log1p' ,
142+ tau = 1 ,
143+ sqrt = False ,
144+ weight = 2.0 )])),
145+ test_cfg = dict (max_per_img = 300 ))
146+
147+ train_dataloader .update (batch_size = 4 , num_workers = 4 )
148+ val_dataloader .update (batch_size = 4 , num_workers = 4 )
149+ test_dataloader .update (batch_size = 4 , num_workers = 4 )
150+
151+ # optimizer
152+ optim_wrapper = dict (
153+ type = OptimWrapper ,
154+ optimizer = dict (type = AdamW , lr = 0.0001 , weight_decay = 0.0001 ),
155+ clip_grad = dict (max_norm = 0.1 , norm_type = 2 ),
156+ paramwise_cfg = dict (
157+ custom_keys = {'backbone' : dict (lr_mult = 0.1 )},
158+ norm_decay_mult = 0 ,
159+ bypass_duplicate = True ))
160+
161+ # learning policy
162+ max_epochs = 72
163+ train_cfg = dict (
164+ type = EpochBasedTrainLoop , max_epochs = max_epochs , val_interval = 6 )
165+ val_cfg = dict (type = ValLoop )
166+ test_cfg = dict (type = TestLoop )
167+ param_scheduler = [
168+ dict (
169+ type = LinearLR , start_factor = 0.001 , by_epoch = False , begin = 0 , end = 2000 )
170+ ]
171+ custom_hooks = [
172+ dict (
173+ type = EMAHook ,
174+ ema_type = ExpMomentumEMA ,
175+ momentum = 0.0001 ,
176+ update_buffers = True ,
177+ priority = 49 )
178+ ]
179+
180+ # NOTE: `auto_scale_lr` is for automatically scaling LR,
181+ # USER SHOULD NOT CHANGE ITS VALUES.
182+ # base_batch_size = (2 GPUs) x (4 samples per GPU)
183+ auto_scale_lr = dict (enable = False , base_batch_size = 8 )
0 commit comments