Skip to content

Commit f6fb58c

Browse files
committed
[update] use fvcore - temp2
1 parent fdaad94 commit f6fb58c

File tree

1 file changed

+6
-37
lines changed

1 file changed

+6
-37
lines changed

compressai_vision/utils/measure_complexity.py

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,13 @@ def calc_complexity_nn_part2_dn53(vision_model, dec_features):
4343
raise NotImplementedError("Image-task path is not implemented yet for DN53 complexity.")
4444
else: # video task
4545
x = dec_features["data"]
46-
#feats_dict = {
47-
# k: v.unsqueeze(0).to(device=device)
48-
# for k, v in zip(vision_model.split_layer_list, x.values())
49-
#}
50-
# Build features dict using x.items() (do NOT rely on dict.values() ordering)
51-
#feats_dict = {}
52-
#for k, v in x.items():
53-
# kk = int(k)
54-
# vv = v
55-
# if vv.dim() == 3: # (C,H,W) -> (1,C,H,W)
56-
# vv = vv.unsqueeze(0)
57-
# feats_dict[kk] = vv.to(device=device)
5846

5947
# NN-part2 (Darknet backbone only): store features in wrapper, pass tensor-only dummy input to fvcore
6048
partial_model = DarknetNNPart2BackboneOnlyFvcoreWrapper(vision_model.darknet, vision_model.features_at_splits).eval()
6149

6250
# fvcore input must be Tensor (wrapper overwrites internal 'x' from stored features)
6351
x_dummy = next(iter(x.values()))
6452
# fvcore input must be a Tensor; pick a deterministic dummy tensor
65-
#first_k = min(feats_dict.keys())
66-
#x_dummy = feats_dict[first_k]
6753
kmacs = measure_kmacs(partial_model, x_dummy)
6854

6955
pixels = sum(
@@ -492,6 +478,7 @@ def __init__(self, darknet: nn.Module, features: dict):
492478
super().__init__()
493479
self.darknet = darknet
494480
self.features = features # dict[int, Tensor]
481+
495482
self.is_nn_part1 = False # fixed for this wrapper
496483

497484
def forward(self, x_dummy: torch.Tensor) -> torch.Tensor:
@@ -506,10 +493,6 @@ def forward(self, x_dummy: torch.Tensor) -> torch.Tensor:
506493
output = [] # not used (we skip yolo), kept for structural similarity
507494
had_yolo = False
508495

509-
# If no injected features are provided, return a tensor to keep tracing alive
510-
if len(features) == 0:
511-
return x_dummy
512-
513496
max_id = max(features.keys())
514497

515498
# Match original nn-part2 logic for sidx/eidx and pre-filling layer_outputs
@@ -518,37 +501,26 @@ def forward(self, x_dummy: torch.Tensor) -> torch.Tensor:
518501

519502
# Pre-fill layer_outputs[0:sidx] with injected features or None
520503
# Also pick a valid initial x from the earliest available injected feature
521-
x_init = None
522504
for idx in range(0, sidx):
523505
if idx not in features:
524506
layer_outputs.append(None)
525507
else:
526-
x_i = features[idx]
527-
layer_outputs.append(x_i)
528-
if x_init is None:
529-
x_init = x_i
530-
531-
# Fallback: if no feature existed in [0:sidx), use the smallest-key feature
532-
if x_init is None:
533-
x_init = features[min(features.keys())]
534-
508+
x = features[idx]
509+
layer_outputs.append(x)
535510
else:
536511
sidx = min(features.keys())
537-
x_init = features[sidx]
538512

539513
eidx = len(module_list)
540-
541514
# IMPORTANT: do NOT start from x_dummy (can cause channel mismatch before injection)
542-
x = x_init
543515

544516
# Main loop (same structure as original, but assumes nn-part2 only)
545517
for i, (module_def, module) in enumerate(
546518
zip(module_defs[sidx:eidx], module_list[sidx:eidx])
547519
):
548520
nn_idx = i + sidx
549-
521+
550522
# --- Feature injection (same as original) ---
551-
if nn_idx in features:
523+
if nn_idx in features.keys():
552524
x = features[nn_idx]
553525
layer_outputs.append(x)
554526
features.pop(nn_idx)
@@ -558,8 +530,6 @@ def forward(self, x_dummy: torch.Tensor) -> torch.Tensor:
558530
continue
559531

560532
mtype = module_def["type"]
561-
# 왜 75번째 레이어에서, feature[74]의 채널수는 1024인데,
562-
# 1024가 아니라 256크기의 feature를 받았다고 하는걸까?
563533
if mtype in ["convolutional", "upsample", "maxpool"]:
564534
x = module(x)
565535

@@ -575,8 +545,7 @@ def forward(self, x_dummy: torch.Tensor) -> torch.Tensor:
575545
x = layer_outputs[-1] + layer_outputs[layer_i]
576546

577547
elif mtype == "yolo":
578-
# Skip YOLO heads for backbone-only FLOPs measurement.
579-
# Keep the flag to preserve original control flow.
548+
x = module[0](x, self.darknet.img_size)
580549
had_yolo = True
581550
# Keep x unchanged
582551

0 commit comments

Comments
 (0)