-
Notifications
You must be signed in to change notification settings - Fork 37
/
rsprompter_query-nwpu-peft-512.py
244 lines (223 loc) · 7.23 KB
/
rsprompter_query-nwpu-peft-512.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
_base_ = ['_base_/rsprompter_query.py']
work_dir = './work_dirs/rsprompter/rsprompter_query-nwpu-peft-512'
default_hooks = dict(
timer=dict(type='IterTimerHook'),
logger=dict(type='LoggerHook', interval=5),
param_scheduler=dict(type='ParamSchedulerHook'),
checkpoint=dict(type='CheckpointHook', interval=1, max_keep_ckpts=4, save_best='coco/bbox_mAP', rule='greater', save_last=True),
sampler_seed=dict(type='DistSamplerSeedHook'),
# visualization=dict(type='DetVisualizationHook', draw=True, interval=1, test_out_dir='vis_data')
)
vis_backends = [dict(type='LocalVisBackend'),
# dict(type='WandbVisBackend', init_kwargs=dict(project='rsprompter-nwpu', group='rsprompter-query', name="rsprompter_query-nwpu-peft-512"))
]
visualizer = dict(
type='DetLocalVisualizer', vis_backends=vis_backends, name='visualizer')
num_classes = 10
prompt_shape = (70, 5) # (per img pointset, per pointset point)
#### should be changed when using different pretrain model
# sam base model
hf_sam_pretrain_name = "work_dirs/sam_cache/sam_vit_base"
# huggingface model name, e.g. facebook/sam-vit-base
# or local repo path, e.g. work_dirs/sam_cache/sam_vit_base
hf_sam_pretrain_ckpt_path = "work_dirs/sam_cache/sam_vit_base/pytorch_model.bin"
# # sam large model
# hf_sam_pretrain_name = "facebook/sam-vit-large"
# hf_sam_pretrain_ckpt_path = "~/.cache//huggingface/hub/models--facebook--sam-vit-large/snapshots/70009d56dac23ebb3265377257158b1d6ed4c802/pytorch_model.bin"
# # sam huge model
# hf_sam_pretrain_name = "facebook/sam-vit-huge"
# hf_sam_pretrain_ckpt_path = "~/.cache/huggingface/hub/models--facebook--sam-vit-huge/snapshots/89080d6dcd9a900ebd712b13ff83ecf6f072e798/pytorch_model.bin"
crop_size = (512, 512)
batch_augments = [
dict(
type='BatchFixedSizePad',
size=crop_size,
img_pad_value=0,
pad_mask=True,
mask_pad_value=0,
pad_seg=False)
]
data_preprocessor = dict(
type='DetDataPreprocessor',
mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
std=[0.229 * 255, 0.224 * 255, 0.225 * 255],
bgr_to_rgb=True,
pad_mask=True,
pad_size_divisor=32,
batch_augments=batch_augments
)
model = dict(
decoder_freeze=False,
data_preprocessor=data_preprocessor,
shared_image_embedding=dict(
hf_pretrain_name=hf_sam_pretrain_name,
init_cfg=dict(type='Pretrained', checkpoint=hf_sam_pretrain_ckpt_path),
),
backbone=dict(
_delete_=True,
img_size=crop_size[0],
type='MMPretrainSamVisionEncoder',
hf_pretrain_name=hf_sam_pretrain_name,
init_cfg=dict(type='Pretrained', checkpoint=hf_sam_pretrain_ckpt_path),
peft_config=dict(
peft_type="LORA",
r=16,
target_modules=["qkv"],
lora_alpha=32,
lora_dropout=0.05,
bias="none",
),
),
neck=dict(
feature_aggregator=dict(
_delete_=True,
type='PseudoFeatureAggregator',
in_channels=256,
hidden_channels=512,
out_channels=256,
),
),
panoptic_head=dict(
decoder_plus=True,
mask_decoder=dict(
hf_pretrain_name=hf_sam_pretrain_name,
init_cfg=dict(type='Pretrained', checkpoint=hf_sam_pretrain_ckpt_path)
),
per_pointset_point=prompt_shape[1],
with_sincos=True,
num_things_classes=num_classes,
num_queries=prompt_shape[0],
loss_cls=dict(
class_weight=[1.0] * num_classes + [0.1])
),
panoptic_fusion_head=dict(
num_things_classes=num_classes
),
test_cfg=dict(
max_per_image=prompt_shape[0],
)
)
backend_args = None
train_pipeline = [
dict(type='LoadImageFromFile', backend_args=backend_args, to_float32=True),
dict(type='LoadAnnotations', with_bbox=True, with_mask=True),
dict(type='RandomFlip', prob=0.5),
# large scale jittering
dict(
type='RandomResize',
scale=crop_size,
ratio_range=(0.1, 2.0),
resize_type='Resize',
keep_ratio=True),
dict(
type='RandomCrop',
crop_size=crop_size,
crop_type='absolute',
recompute_bbox=True,
allow_negative_crop=True),
dict(type='FilterAnnotations', min_gt_bbox_wh=(1e-5, 1e-5), by_mask=True),
dict(type='PackDetInputs')
]
test_pipeline = [
dict(type='LoadImageFromFile', backend_args=backend_args, to_float32=True),
dict(type='Resize', scale=crop_size, keep_ratio=True),
dict(type='Pad', size=crop_size, pad_val=dict(img=(0.406 * 255, 0.456 * 255, 0.485 * 255), masks=0)),
# If you don't have a gt annotation, delete the pipeline
dict(type='LoadAnnotations', with_bbox=True, with_mask=True),
dict(
type='PackDetInputs',
meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'pad_shape', 'scale_factor'))
]
dataset_type = 'NWPUInsSegDataset'
#### should be changed align with your code root and data root
code_root = '/mnt/home/cky/Code/RSPrompter'
data_root = '/mnt/home/cky/Code/RSPrompter/data/NWPU'
batch_size_per_gpu = 1
num_workers = 8
persistent_workers = True
train_dataloader = dict(
batch_size=batch_size_per_gpu,
num_workers=num_workers,
persistent_workers=persistent_workers,
dataset=dict(
type=dataset_type,
data_root=data_root,
ann_file=code_root + '/data/NWPU/annotations/NWPU_instances_train.json',
data_prefix=dict(img='imgs'),
pipeline=train_pipeline,
)
)
val_dataloader = dict(
batch_size=batch_size_per_gpu,
num_workers=num_workers,
persistent_workers=persistent_workers,
dataset=dict(
type=dataset_type,
data_root=data_root,
ann_file=code_root + '/data/NWPU/annotations/NWPU_instances_val.json',
data_prefix=dict(img='imgs'),
pipeline=test_pipeline,
)
)
test_dataloader = val_dataloader
resume = False
load_from = None
base_lr = 0.0001
max_epochs = 600
train_cfg = dict(max_epochs=max_epochs)
param_scheduler = [
dict(
type='LinearLR', start_factor=0.001, by_epoch=False, begin=0, end=50),
dict(
type='CosineAnnealingLR',
eta_min=base_lr * 0.001,
begin=1,
end=max_epochs,
T_max=max_epochs,
by_epoch=True
)
]
#### DeepSpeed Configs
runner_type = 'FlexibleRunner'
strategy = dict(
type='DeepSpeedStrategy',
fp16=dict(
enabled=True,
auto_cast=False,
fp16_master_weights_and_grads=False,
loss_scale=0,
loss_scale_window=500,
hysteresis=2,
min_loss_scale=1,
initial_scale_power=15,
),
gradient_clipping=0.1,
inputs_to_half=['inputs'],
zero_optimization=dict(
stage=2,
allgather_partitions=True,
allgather_bucket_size=2e8,
reduce_scatter=True,
reduce_bucket_size='auto',
overlap_comm=True,
contiguous_gradients=True,
),
)
optim_wrapper = dict(
type='DeepSpeedOptimWrapper',
optimizer=dict(
type='AdamW',
lr=base_lr,
weight_decay=0.05
)
)
# #### AMP training config
# runner_type = 'Runner'
# optim_wrapper = dict(
# type='AmpOptimWrapper',
# dtype='float16',
# optimizer=dict(
# type='AdamW',
# lr=base_lr,
# weight_decay=0.05)
# )