Repository navigation
Expand file tree
/
Copy pathtrain.py
More file actions
1472 lines (1219 loc) · 50.1 KB
/
Copy pathtrain.py
File metadata and controls
1472 lines (1219 loc) · 50.1 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import json
import math
import time
import random
import logging
import argparse
from datetime import timedelta
from typing import Any, Dict, List, Tuple
from collections import defaultdict
import numpy as np
import torch
import torch.nn as nn
import torch.distributed as dist
from torch.utils.data import DataLoader, Dataset
from torch.utils.data.distributed import DistributedSampler
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.amp import autocast, GradScaler
from torch.distributed.elastic.multiprocessing.errors import record
from tqdm import tqdm
# CUDA_VISIBLE_DEVICES=0,2,3,4 torchrun --standalone --nproc_per_node=4 train.py
# =========================================================
# Make project root importable
# =========================================================
def add_project_root_to_path():
this_file = os.path.abspath(__file__)
cur_dir = os.path.dirname(this_file)
candidates = [
cur_dir,
os.path.dirname(cur_dir),
os.path.dirname(os.path.dirname(cur_dir)),
os.path.dirname(os.path.dirname(os.path.dirname(cur_dir))),
]
for c in candidates:
if os.path.exists(os.path.join(c, "datasets")) and os.path.exists(os.path.join(c, "models")):
if c not in sys.path:
sys.path.insert(0, c)
return c
return None
PROJECT_ROOT = add_project_root_to_path()
if PROJECT_ROOT is None:
print("[WARN] Could not confidently find project root. Make sure datasets/ and models/ are importable.")
from datasets.dataset import OVSAMDataset
from datasets.prompt_sampler import load_prompt_bank, PromptBankSampler
from image_feature_cache import get_batched_cached_image_features, resolve_feature_cache_dir
from models.detector.detector_builder import build_detector
from losses.detection_loss import build_detection_criterion
from models.detector.matcher import box_cxcywh_to_xyxy
# =========================================================
# Distributed helpers
# =========================================================
def is_dist_avail_and_initialized():
return dist.is_available() and dist.is_initialized()
def get_rank():
return dist.get_rank() if is_dist_avail_and_initialized() else 0
def get_world_size():
return dist.get_world_size() if is_dist_avail_and_initialized() else 1
def is_main_process():
return get_rank() == 0
def unwrap_model(model):
return model.module if isinstance(model, DDP) else model
def setup_distributed(args):
"""
torchrun 启动:
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --standalone --nproc_per_node=4 train.py ...
也兼容单卡 python 直接运行。
"""
if "RANK" in os.environ and "WORLD_SIZE" in os.environ and "LOCAL_RANK" in os.environ:
args.rank = int(os.environ["RANK"])
args.world_size = int(os.environ["WORLD_SIZE"])
args.local_rank = int(os.environ["LOCAL_RANK"])
torch.cuda.set_device(args.local_rank)
dist.init_process_group(
backend="nccl",
init_method="env://",
timeout=timedelta(hours=2),
)
else:
args.rank = 0
args.world_size = 1
args.local_rank = 0
torch.cuda.set_device(0 if torch.cuda.is_available() else "cpu")
def cleanup_distributed():
if is_dist_avail_and_initialized():
dist.destroy_process_group()
def barrier():
if is_dist_avail_and_initialized():
dist.barrier()
# =========================================================
# Reproducibility
# =========================================================
def set_seed(seed: int, rank: int = 0):
seed = seed + rank
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# 正式训练先别强制 deterministic,太容易拖速度
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = True
# =========================================================
# Logging / IO
# =========================================================
def ensure_dir(path: str):
os.makedirs(path, exist_ok=True)
def setup_logger(save_dir: str):
logger = logging.getLogger("train_ddp")
logger.setLevel(logging.INFO)
logger.handlers.clear()
formatter = logging.Formatter("[%(asctime)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
if is_main_process():
fh = logging.FileHandler(os.path.join(save_dir, "train.log"), mode="a", encoding="utf-8")
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
logger.addHandler(fh)
sh = logging.StreamHandler(sys.stdout)
sh.setLevel(logging.INFO)
sh.setFormatter(formatter)
logger.addHandler(sh)
return logger
def log_jsonl(save_dir: str, record: Dict[str, Any]):
if not is_main_process():
return
path = os.path.join(save_dir, "history.jsonl")
with open(path, "a", encoding="utf-8") as f:
f.write(json.dumps(record, ensure_ascii=False) + "\n")
# =========================================================
# Dataset helpers
# =========================================================
def choose_prompt_from_positive_task(task: Dict[str, Any]) -> str:
return task["primary_prompt"]
def build_target_boxes_tensor(target_boxes: List[List[float]]) -> torch.Tensor:
if len(target_boxes) == 0:
return torch.zeros((0, 4), dtype=torch.float32)
return torch.as_tensor(target_boxes, dtype=torch.float32).reshape(-1, 4)
def stack_padded_images(image_tensors: List[torch.Tensor]) -> Tuple[torch.Tensor, Tuple[int, int]]:
"""
Stack variable-sized CHW tensors by zero-padding to the max H/W in the batch.
Returns:
batched_images: [B,C,Hmax,Wmax]
padded_hw: (Hmax, Wmax)
"""
if len(image_tensors) == 0:
raise ValueError("image_tensors is empty.")
shapes = [tuple(x.shape) for x in image_tensors]
if len(set(shapes)) == 1:
h, w = int(image_tensors[0].shape[-2]), int(image_tensors[0].shape[-1])
return torch.stack(image_tensors, dim=0), (h, w)
max_h = max(int(x.shape[-2]) for x in image_tensors)
max_w = max(int(x.shape[-1]) for x in image_tensors)
c = int(image_tensors[0].shape[0])
dtype = image_tensors[0].dtype
out = torch.zeros((len(image_tensors), c, max_h, max_w), dtype=dtype)
for i, x in enumerate(image_tensors):
h, w = int(x.shape[-2]), int(x.shape[-1])
out[i, :, :h, :w] = x
return out, (max_h, max_w)
def expand_train_prompt_tasks(sample: Dict[str, Any]) -> List[Dict[str, Any]]:
positive_tasks = sample.get("positive_tasks", None)
if positive_tasks is None:
positive_tasks = [{
"primary_prompt": sample.get("primary_prompt", ""),
"aux_prompt": sample.get("aux_prompt", None),
"target_boxes": sample.get("target_boxes", []),
"task_type": sample.get("task_type", ""),
"canonical_name": sample.get("canonical_name", ""),
}]
tasks = []
for pos_task in positive_tasks:
tasks.append({
"prompt": choose_prompt_from_positive_task(pos_task),
"target_boxes": pos_task.get("target_boxes", []),
"task_type": pos_task.get("task_type", ""),
"canonical_name": pos_task.get("canonical_name", ""),
"is_negative": False,
})
for neg in sample.get("negative_prompts", []):
if isinstance(neg, dict):
neg_prompt = neg.get("prompt", "")
neg_canonical_name = neg.get("canonical_name", "")
else:
neg_prompt = str(neg)
neg_canonical_name = ""
if neg_prompt == "":
continue
tasks.append({
"prompt": neg_prompt,
"target_boxes": [],
"task_type": "negative",
"canonical_name": neg_canonical_name,
"is_negative": True,
})
return tasks
def choose_prompt_from_eval_sample(sample: Dict[str, Any], prompt_type: str = "canonical", index: int = 0) -> str:
if prompt_type == "canonical":
queries = sample.get("canonical_queries", [])
elif prompt_type == "heldout":
queries = sample.get("heldout_queries", [])
else:
raise ValueError(f"Unknown prompt_type: {prompt_type}")
if len(queries) == 0:
raise RuntimeError(f"No queries found for prompt_type={prompt_type}")
index = min(index, len(queries) - 1)
return queries[index]["prompt"]
def build_train_dataset(args):
prompt_bank = load_prompt_bank(args.prompt_bank)
sampler = PromptBankSampler(
prompt_bank=prompt_bank,
mode="train",
positive_sampling=args.train_positive_sampling,
train_use_template=bool(args.train_use_template),
p_negative=args.p_negative,
max_negatives=args.max_negatives,
seed=args.seed,
)
if args.train_positive_sampling == "single":
train_batch_unit = "image"
elif args.train_positive_sampling == "all":
train_batch_unit = "task"
else:
raise ValueError(f"Unknown train_positive_sampling: {args.train_positive_sampling}")
base_dataset = OVSAMDataset(
ann_path=args.train_ann_path,
image_root=args.image_root,
prompt_sampler=(sampler if train_batch_unit == "image" else None),
return_raw_if_empty=True,
)
if train_batch_unit == "image":
return base_dataset
if train_batch_unit == "task":
return TrainTaskDataset(
base_dataset=base_dataset,
prompt_sampler=sampler,
positive_sampling=args.train_positive_sampling,
)
raise ValueError(f"Unknown train_batch_unit: {train_batch_unit}")
def build_val_dataset(args):
prompt_bank = load_prompt_bank(args.prompt_bank)
sampler = PromptBankSampler(
prompt_bank=prompt_bank,
mode="eval",
eval_use_template=bool(getattr(args, "eval_use_template", 0)),
p_negative=args.p_negative,
max_negatives=args.max_negatives,
seed=args.seed,
)
dataset = OVSAMDataset(
ann_path=args.val_ann_path,
image_root=args.image_root,
prompt_sampler=sampler,
return_raw_if_empty=True,
)
return dataset
# =========================================================
# Validation task dataset (expand one image to multiple organ tasks)
# =========================================================
def _pick_one_query_per_canonical(queries: List[Dict[str, Any]], prompt_index: int = 0) -> List[Dict[str, Any]]:
"""
For each canonical organ, only keep one prompt description.
If there are multiple descriptions for the same canonical organ,
choose the prompt_index-th one (clamped).
"""
grouped = defaultdict(list)
for q in queries:
cname = q.get("canonical_name", "unknown")
grouped[cname].append(q)
picked = []
for cname, qs in grouped.items():
idx = min(prompt_index, len(qs) - 1)
picked.append(qs[idx])
return picked
class ValTaskDataset(Dataset):
"""
Expand base eval dataset:
one image -> multiple organ-level eval tasks
but only keep one prompt description per organ.
"""
def __init__(
self,
base_dataset: OVSAMDataset,
eval_prompt_type: str = "canonical",
eval_prompt_index: int = 0,
):
self.base_dataset = base_dataset
self.eval_prompt_type = eval_prompt_type
self.eval_prompt_index = eval_prompt_index
self.tasks: List[Dict[str, Any]] = []
for sample_idx in range(len(base_dataset)):
sample = base_dataset[sample_idx]
if eval_prompt_type == "canonical":
queries = sample.get("canonical_queries", [])
elif eval_prompt_type == "heldout":
queries = sample.get("heldout_queries", [])
else:
raise ValueError(f"Unknown eval_prompt_type: {eval_prompt_type}")
queries = _pick_one_query_per_canonical(
queries=queries,
prompt_index=eval_prompt_index,
)
for q in queries:
self.tasks.append({
"sample_idx": sample_idx,
"query": q,
})
def __len__(self):
return len(self.tasks)
def __getitem__(self, idx):
task = self.tasks[idx]
sample = self.base_dataset[task["sample_idx"]]
q = task["query"]
return {
"image": sample["image"],
"image_path": sample.get("image_path", ""),
"prompt": q["prompt"],
"target_boxes": q["target_boxes"], # list of xyxy abs boxes
"image_size": (int(sample["height"]), int(sample["width"])),
"case_id": sample.get("case_id", ""),
"slice_z": sample.get("slice_z", -1),
"canonical_name": q.get("canonical_name", sample.get("canonical_name", "unknown")),
"eval_prompt_type": self.eval_prompt_type,
}
class TrainTaskDataset(Dataset):
"""
Expand base train dataset to task-level samples.
- positive_sampling=single:
one task slot per image, and the present organ is still sampled randomly
- positive_sampling=all:
one task slot per (image, canonical organ) pair
Negative prompt sampling remains unchanged, but is attached per task sample
instead of being expanded from a whole-image batch. This keeps the effective
prompt count per batch much more stable.
"""
def __init__(
self,
base_dataset: OVSAMDataset,
prompt_sampler: PromptBankSampler,
positive_sampling: str = "single",
):
self.base_dataset = base_dataset
self.prompt_sampler = prompt_sampler
self.positive_sampling = positive_sampling
self.task_specs: List[Dict[str, Any]] = []
for sample_idx, record in enumerate(base_dataset.samples):
if positive_sampling == "single":
self.task_specs.append({
"sample_idx": sample_idx,
"canonical_name": None,
})
continue
if positive_sampling != "all":
raise ValueError(f"Unknown positive_sampling: {positive_sampling}")
organ_names = sorted({inst.phrase for inst in record.instances})
for canonical_name in organ_names:
self.task_specs.append({
"sample_idx": sample_idx,
"canonical_name": canonical_name,
})
def __len__(self):
return len(self.task_specs)
def __getitem__(self, idx):
task = self.task_specs[idx]
sample = self.base_dataset[task["sample_idx"]]
instances = sample["instances"]
if self.positive_sampling == "single":
chosen_inst = self.prompt_sampler.rng.choice(instances)
canonical_name = chosen_inst["phrase"]
matched_instances = [chosen_inst]
else:
canonical_name = task["canonical_name"]
matched_instances = [x for x in instances if x["phrase"] == canonical_name]
if len(matched_instances) == 0:
raise RuntimeError(
f"No instances found for canonical_name={canonical_name} "
f"in sample_idx={task['sample_idx']}"
)
positive_task = self.prompt_sampler._build_positive_train_task(
canonical_name=canonical_name,
matched_instances=matched_instances,
all_instances=instances,
)
negative_prompts = []
present_names = [x["phrase"] for x in instances]
if self.prompt_sampler.rng.random() < self.prompt_sampler.p_negative:
negative_prompts = self.prompt_sampler._sample_negative_prompts(present_names)
return {
"image": sample["image"],
"case_id": sample["case_id"],
"slice_z": sample["slice_z"],
"image_path": sample["image_path"],
"height": sample["height"],
"width": sample["width"],
"instances": instances,
"positive_tasks": [positive_task],
"task_type": positive_task["task_type"],
"canonical_name": positive_task["canonical_name"],
"primary_prompt": positive_task["primary_prompt"],
"aux_prompt": positive_task["aux_prompt"],
"negative_prompts": negative_prompts,
"target_boxes": positive_task["target_boxes"],
"target_category_ids": positive_task["target_category_ids"],
"target_instance_indices": positive_task["target_instance_indices"],
}
def collate_as_list(batch):
return batch
def build_dataloader(dataset, args, train: bool):
sampler = DistributedSampler(
dataset,
num_replicas=get_world_size(),
rank=get_rank(),
shuffle=train,
drop_last=train,
)
loader = DataLoader(
dataset,
batch_size=args.batch_size_per_gpu,
sampler=sampler,
shuffle=False,
num_workers=args.num_workers,
pin_memory=True,
drop_last=train,
collate_fn=collate_as_list,
persistent_workers=(args.num_workers > 0),
)
return loader, sampler
def prepare_batch(
batch_samples: List[Dict[str, Any]],
device: str,
mode: str = "train",
eval_prompt_type: str = "canonical",
eval_prompt_index: int = 0,
):
images = []
prompts = []
targets = []
num_negative_tasks = 0
for s in batch_samples:
if mode == "train":
prompt_tasks = expand_train_prompt_tasks(sample=s)
else:
prompt = choose_prompt_from_eval_sample(
s,
prompt_type=eval_prompt_type,
index=eval_prompt_index,
)
if eval_prompt_type == "canonical":
queries = s.get("canonical_queries", [])
elif eval_prompt_type == "heldout":
queries = s.get("heldout_queries", [])
else:
raise ValueError(f"Unknown eval_prompt_type: {eval_prompt_type}")
if len(queries) == 0:
raise RuntimeError("No eval queries found in sample.")
q = queries[min(eval_prompt_index, len(queries) - 1)]
prompt_tasks = [{
"prompt": prompt,
"target_boxes": q["target_boxes"],
"task_type": s.get("task_type", ""),
"canonical_name": s.get("canonical_name", ""),
"is_negative": False,
}]
for task in prompt_tasks:
images.append(s["image"])
prompts.append(task["prompt"])
target_boxes_tensor = build_target_boxes_tensor(task["target_boxes"])
target = {
"boxes": target_boxes_tensor.to(device), # absolute xyxy
"image_size": None,
"case_id": s.get("case_id", ""),
"slice_z": s.get("slice_z", -1),
"task_type": task.get("task_type", s.get("task_type", "")),
"canonical_name": task.get("canonical_name", s.get("canonical_name", "")),
}
targets.append(target)
num_negative_tasks += int(task.get("is_negative", False))
images, padded_hw = stack_padded_images(images)
padded_hw = (int(padded_hw[0]), int(padded_hw[1]))
for tgt in targets:
tgt["image_size"] = padded_hw
images = images.to(device, non_blocking=True)
return images, prompts, targets, num_negative_tasks
def union_boxes_xyxy(boxes: List[List[float]]) -> torch.Tensor:
"""
Union multiple xyxy boxes into one xyxy box.
"""
boxes_t = torch.as_tensor(boxes, dtype=torch.float32)
x1 = boxes_t[:, 0].min()
y1 = boxes_t[:, 1].min()
x2 = boxes_t[:, 2].max()
y2 = boxes_t[:, 3].max()
return torch.stack([x1, y1, x2, y2], dim=0)
def prepare_val_task_batch(
batch_samples: List[Dict[str, Any]],
device: str,
move_images_to_device: bool = True,
):
images = []
prompts = []
gt_boxes_abs = []
image_sizes = []
image_paths = []
metas = []
for s in batch_samples:
images.append(s["image"])
prompts.append(s["prompt"])
gt_union = union_boxes_xyxy(s["target_boxes"])
gt_boxes_abs.append(gt_union.to(device))
image_sizes.append(s["image_size"])
image_paths.append(s.get("image_path", ""))
metas.append({
"case_id": s.get("case_id", ""),
"slice_z": s.get("slice_z", -1),
"canonical_name": s.get("canonical_name", "unknown"),
"eval_prompt_type": s.get("eval_prompt_type", "unknown"),
"image_path": s.get("image_path", ""),
})
images, padded_hw = stack_padded_images(images)
padded_hw = (int(padded_hw[0]), int(padded_hw[1]))
image_sizes = [padded_hw for _ in image_sizes]
if move_images_to_device:
images = images.to(device, non_blocking=True)
gt_boxes_abs = torch.stack(gt_boxes_abs, dim=0).to(device, non_blocking=True)
return images, prompts, gt_boxes_abs, image_sizes, image_paths, metas
# =========================================================
# Config builders
# =========================================================
def build_detector_cfg(args) -> Dict[str, Any]:
return {
"sam3_ckpt": args.sam3_ckpt,
"image_backbone_name": args.image_backbone_name,
# 两个 encoder 都冻结
"freeze_image_encoder": True,
"freeze_text_encoder": True,
"return_image_last_hidden_state": False,
"return_image_position_encoding": False,
# text adapter
"use_medical_lexicon_adapter": bool(args.use_medical_lexicon_adapter),
"adapter_bottleneck_dim": args.adapter_bottleneck_dim,
"adapter_phrase_out_dim": args.adapter_phrase_out_dim,
"adapter_dropout": args.adapter_dropout,
"adapter_gate_init": args.adapter_gate_init,
"max_length": args.max_length,
# decoder
"text_token_dim": 1024,
"phrase_dim": (args.adapter_phrase_out_dim if bool(args.use_medical_lexicon_adapter) else 512),
"embed_dim": args.embed_dim,
"num_queries": args.num_queries,
"num_decoder_layers": args.num_decoder_layers,
"num_heads": args.num_heads,
"num_points": args.num_points,
"ffn_dim": args.ffn_dim,
"dropout": args.dropout,
"topk": args.topk,
"fusion_num_layers": args.fusion_num_layers,
"fusion_top_levels": args.fusion_top_levels,
"fusion_use_text_bias": bool(args.fusion_use_text_bias),
"fusion_gate_init": args.fusion_gate_init,
"use_presence_branch": bool(args.use_presence_branch),
}
def build_criterion_cfg(args) -> Dict[str, Any]:
return {
"target_box_format": "xyxy_abs",
# matcher
"matcher_cost_bbox": args.matcher_cost_bbox,
"matcher_cost_giou": args.matcher_cost_giou,
"matcher_cost_class": args.matcher_cost_class,
# losses
"loss_bbox_weight": args.loss_bbox_weight,
"loss_giou_weight": args.loss_giou_weight,
"loss_class_weight": args.loss_class_weight,
"loss_presence_weight": args.loss_presence_weight,
"use_presence_branch": bool(args.use_presence_branch),
# aux
"use_aux_loss": bool(args.use_aux_loss),
"aux_loss_weight": args.aux_loss_weight,
# BCE weights
"class_pos_weight": args.class_pos_weight,
"presence_pos_weight": args.presence_pos_weight,
}
# =========================================================
# Frozen encoder guard
# =========================================================
def keep_frozen_encoders_in_eval(detector: nn.Module):
model = unwrap_model(detector)
if hasattr(model, "image_backbone") and hasattr(model.image_backbone, "vision_encoder"):
model.image_backbone.vision_encoder.eval()
if hasattr(model, "text_backbone") and hasattr(model.text_backbone, "text_encoder"):
model.text_backbone.text_encoder.eval()
# =========================================================
# Param / grad helpers
# =========================================================
def get_trainable_params(model: nn.Module):
return [p for p in model.parameters() if p.requires_grad]
def count_params(model: nn.Module):
total = sum(p.numel() for p in model.parameters())
trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
return total, trainable
def get_grad_norm(parameters) -> float:
total_sq = 0.0
has_grad = False
for p in parameters:
if p.grad is not None:
g = p.grad.detach()
total_sq += float((g * g).sum().item())
has_grad = True
if not has_grad:
return 0.0
return math.sqrt(total_sq)
# =========================================================
# Metric helpers
# =========================================================
def normalize_xyxy_abs(boxes_xyxy_abs: torch.Tensor, image_size) -> torch.Tensor:
h, w = image_size
out = boxes_xyxy_abs.clone().float()
out[:, 0] /= float(w)
out[:, 2] /= float(w)
out[:, 1] /= float(h)
out[:, 3] /= float(h)
return out.clamp(0.0, 1.0)
def box_iou_diag(boxes1_xyxy: torch.Tensor, boxes2_xyxy: torch.Tensor) -> torch.Tensor:
x1 = torch.max(boxes1_xyxy[:, 0], boxes2_xyxy[:, 0])
y1 = torch.max(boxes1_xyxy[:, 1], boxes2_xyxy[:, 1])
x2 = torch.min(boxes1_xyxy[:, 2], boxes2_xyxy[:, 2])
y2 = torch.min(boxes1_xyxy[:, 3], boxes2_xyxy[:, 3])
inter_w = (x2 - x1).clamp(min=0)
inter_h = (y2 - y1).clamp(min=0)
inter = inter_w * inter_h
area1 = (boxes1_xyxy[:, 2] - boxes1_xyxy[:, 0]).clamp(min=0) * \
(boxes1_xyxy[:, 3] - boxes1_xyxy[:, 1]).clamp(min=0)
area2 = (boxes2_xyxy[:, 2] - boxes2_xyxy[:, 0]).clamp(min=0) * \
(boxes2_xyxy[:, 3] - boxes2_xyxy[:, 1]).clamp(min=0)
union = area1 + area2 - inter
return inter / (union + 1e-6)
def compute_matched_mean_iou(
outputs: Dict[str, torch.Tensor],
targets: List[Dict[str, Any]],
indices: List[Tuple[torch.Tensor, torch.Tensor]],
) -> float:
pred_boxes = outputs["pred_boxes"]
pred_boxes_xyxy = box_cxcywh_to_xyxy(pred_boxes)
ious = []
for b, (src_idx, tgt_idx) in enumerate(indices):
if len(src_idx) == 0:
continue
tgt_boxes_abs = targets[b]["boxes"].float()
tgt_xyxy_norm = normalize_xyxy_abs(tgt_boxes_abs, targets[b]["image_size"]).to(pred_boxes.device)
src_xyxy = pred_boxes_xyxy[b, src_idx]
tgt_xyxy = tgt_xyxy_norm[tgt_idx]
pair_ious = box_iou_diag(src_xyxy, tgt_xyxy)
ious.extend(pair_ious.detach().cpu().tolist())
if len(ious) == 0:
return 0.0
return float(np.mean(ious))
def box_iou_one_to_many(boxes_xyxy: torch.Tensor, gt_box_xyxy: torch.Tensor) -> torch.Tensor:
"""
boxes_xyxy: [K,4]
gt_box_xyxy: [4]
return: [K]
"""
x1 = torch.maximum(boxes_xyxy[:, 0], gt_box_xyxy[0])
y1 = torch.maximum(boxes_xyxy[:, 1], gt_box_xyxy[1])
x2 = torch.minimum(boxes_xyxy[:, 2], gt_box_xyxy[2])
y2 = torch.minimum(boxes_xyxy[:, 3], gt_box_xyxy[3])
inter_w = (x2 - x1).clamp(min=0)
inter_h = (y2 - y1).clamp(min=0)
inter = inter_w * inter_h
area1 = (boxes_xyxy[:, 2] - boxes_xyxy[:, 0]).clamp(min=0) * \
(boxes_xyxy[:, 3] - boxes_xyxy[:, 1]).clamp(min=0)
area2 = (gt_box_xyxy[2] - gt_box_xyxy[0]).clamp(min=0) * \
(gt_box_xyxy[3] - gt_box_xyxy[1]).clamp(min=0)
union = area1 + area2 - inter
return inter / (union + 1e-6)
def reduce_scalar(value: float, device: str) -> float:
if get_world_size() == 1:
return value
t = torch.tensor([value], device=device, dtype=torch.float32)
dist.all_reduce(t, op=dist.ReduceOp.SUM)
t /= get_world_size()
return float(t.item())
def gather_class_metric_stats(local_stats: Dict[str, Dict[str, float]]) -> Dict[str, Dict[str, float]]:
if get_world_size() == 1:
return local_stats
gathered = [None for _ in range(get_world_size())]
dist.all_gather_object(gathered, local_stats)
merged: Dict[str, Dict[str, float]] = defaultdict(
lambda: {
"n": 0.0,
"top1_sum": 0.0,
"oracle_sum": 0.0,
"r50_sum": 0.0,
"r75_sum": 0.0,
}
)
for rank_stats in gathered:
for canonical_name, stats in rank_stats.items():
dst = merged[canonical_name]
dst["n"] += float(stats["n"])
dst["top1_sum"] += float(stats["top1_sum"])
dst["oracle_sum"] += float(stats["oracle_sum"])
dst["r50_sum"] += float(stats["r50_sum"])
dst["r75_sum"] += float(stats["r75_sum"])
return dict(merged)
# =========================================================
# Save checkpoint
# =========================================================
def save_checkpoint(
save_path: str,
detector: nn.Module,
save_fp16: bool = True,
save_frozen_encoders: bool = False,
):
if not is_main_process():
return
model = unwrap_model(detector)
state_dict = model.state_dict()
compact_state_dict = {}
frozen_encoder_prefixes = (
"image_backbone.vision_encoder.",
"text_backbone.text_encoder.",
)
for name, tensor in state_dict.items():
if (not save_frozen_encoders) and any(
name.startswith(prefix) for prefix in frozen_encoder_prefixes
):
continue
value = tensor.detach().cpu()
if save_fp16 and torch.is_floating_point(value):
value = value.to(dtype=torch.float16)
compact_state_dict[name] = value
torch.save(compact_state_dict, save_path)
# =========================================================
# Epoch loops
# =========================================================
def train_one_epoch(
epoch: int,
detector: nn.Module,
criterion: nn.Module,
loader: DataLoader,
optimizer: torch.optim.Optimizer,
scaler: GradScaler,
scheduler,
args,
):
detector.train()
criterion.train()
keep_frozen_encoders_in_eval(detector)
total_loss = 0.0
total_bbox = 0.0
total_giou = 0.0
total_cls = 0.0
total_presence = 0.0
total_iou = 0.0
total_negative_tasks = 0
total_prompt_tasks = 0
n_steps = 0
optimizer.zero_grad(set_to_none=True)
progress = tqdm(
loader,
total=len(loader),
desc=f"Train {epoch:03d}",
dynamic_ncols=True,
leave=False,
disable=not is_main_process(),
)
for step, batch_samples in enumerate(progress):
images, prompts, targets, num_negative_tasks = prepare_batch(
batch_samples=batch_samples,
device=args.device,
mode="train",
)
with autocast("cuda", enabled=bool(args.amp)):
outputs = detector(
images=images,
prompts=prompts,
topk=None,
return_image_features=False,
return_text_outputs=False,
return_aux=True,
return_image_aux=False,
)
loss, loss_dict, indices = criterion(outputs, targets)
mean_iou = compute_matched_mean_iou(outputs, targets, indices)
loss_to_backward = loss / args.accum_steps
scaler.scale(loss_to_backward).backward()
if (step + 1) % args.accum_steps == 0:
if args.grad_clip is not None and args.grad_clip > 0:
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(get_trainable_params(detector), args.grad_clip)
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad(set_to_none=True)
total_loss += float(loss.detach().item())
total_bbox += float(loss_dict["loss_bbox"].detach().item())
total_giou += float(loss_dict["loss_giou"].detach().item())
total_cls += float(loss_dict["loss_class"].detach().item())
total_presence += float(loss_dict["loss_presence"].detach().item())
total_iou += mean_iou
total_negative_tasks += int(num_negative_tasks)
total_prompt_tasks += int(len(targets))
n_steps += 1
if is_main_process():
progress.set_postfix({
"loss": f"{total_loss / n_steps:.4f}",
"miou": f"{total_iou / n_steps:.4f}",
"cls": f"{total_cls / n_steps:.4f}",
"neg": f"{total_negative_tasks / max(total_prompt_tasks, 1):.2f}",
})
# 处理最后不足 accum_steps 的残留梯度
if n_steps > 0 and (n_steps % args.accum_steps != 0):
if args.grad_clip is not None and args.grad_clip > 0:
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(get_trainable_params(detector), args.grad_clip)
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad(set_to_none=True)
if scheduler is not None:
scheduler.step()
stats = {
"loss_total": total_loss / max(n_steps, 1),