|
|
""" |
|
|
Script to find best cases for different visualizations. |
|
|
""" |
|
|
import os |
|
|
import sys |
|
|
import glob |
|
|
import numpy as np |
|
|
import torch |
|
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "src")) |
|
|
|
|
|
def main(): |
|
|
|
|
|
ckpt_path = "/root/githubs/gliomasam3_moe/logs/segmamba/model/ckpt_step600.pt" |
|
|
data_path = "/data/yty/brats23_segmamba_processed" |
|
|
|
|
|
|
|
|
npz_files = sorted(glob.glob(os.path.join(data_path, "*.npz"))) |
|
|
case_ids = [os.path.basename(f).replace(".npz", "") for f in npz_files] |
|
|
|
|
|
print(f"Found {len(case_ids)} cases") |
|
|
|
|
|
|
|
|
from vis_publication import ModelRunner |
|
|
import yaml |
|
|
|
|
|
with open("visualizations/vis_config.yaml", "r") as f: |
|
|
vis_cfg = yaml.safe_load(f) |
|
|
|
|
|
runner = ModelRunner(vis_cfg, "configs/train.yaml", ckpt_path, "cuda") |
|
|
model = runner.model |
|
|
|
|
|
print("Model loaded") |
|
|
|
|
|
|
|
|
et_absent_candidates = [] |
|
|
moe_routing_candidates = [] |
|
|
|
|
|
test_cases = case_ids[:50] |
|
|
|
|
|
for case_id in test_cases: |
|
|
try: |
|
|
|
|
|
img_t, _ = runner.load_case_tensor(case_id) |
|
|
|
|
|
|
|
|
with torch.no_grad(): |
|
|
out = runner.forward_intermediate(img_t) |
|
|
|
|
|
pi_et = float(out["pi_et"].cpu().numpy().reshape(-1)[0]) |
|
|
et_pre = out["et_pre"].cpu().numpy()[0, 0] |
|
|
et_post = out["et_post"].cpu().numpy()[0, 0] |
|
|
moe_gamma = out["moe_gamma"].cpu().numpy()[0] |
|
|
|
|
|
|
|
|
pre_sum = (et_pre > 0.5).sum() |
|
|
post_sum = (et_post > 0.5).sum() |
|
|
diff_ratio = abs(pre_sum - post_sum) / max(pre_sum, 1) |
|
|
|
|
|
et_absent_candidates.append({ |
|
|
"case_id": case_id, |
|
|
"pi_et": pi_et, |
|
|
"pre_sum": int(pre_sum), |
|
|
"post_sum": int(post_sum), |
|
|
"diff_ratio": diff_ratio, |
|
|
"score": (1 - pi_et) * diff_ratio |
|
|
}) |
|
|
|
|
|
|
|
|
gamma_mean = moe_gamma.mean(axis=0) |
|
|
nonzero_count = (gamma_mean > 0.05).sum() |
|
|
gamma_std = gamma_mean.std() |
|
|
|
|
|
moe_routing_candidates.append({ |
|
|
"case_id": case_id, |
|
|
"gamma_mean": gamma_mean.tolist(), |
|
|
"nonzero_count": int(nonzero_count), |
|
|
"gamma_std": float(gamma_std), |
|
|
"score": nonzero_count * gamma_std |
|
|
}) |
|
|
|
|
|
print(f"{case_id}: pi_et={pi_et:.3f}, pre={pre_sum}, post={post_sum}, diff={diff_ratio:.2f}, moe_nonzero={nonzero_count}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error on {case_id}: {e}") |
|
|
continue |
|
|
|
|
|
|
|
|
print("\n" + "="*60) |
|
|
print("Best ET-absent candidates (high score = low pi_et + big diff):") |
|
|
et_absent_candidates.sort(key=lambda x: x["score"], reverse=True) |
|
|
for c in et_absent_candidates[:10]: |
|
|
print(f" {c['case_id']}: pi_et={c['pi_et']:.3f}, pre={c['pre_sum']}, post={c['post_sum']}, score={c['score']:.3f}") |
|
|
|
|
|
print("\n" + "="*60) |
|
|
print("Best MoE routing candidates (high score = diverse weights):") |
|
|
moe_routing_candidates.sort(key=lambda x: x["score"], reverse=True) |
|
|
for c in moe_routing_candidates[:10]: |
|
|
print(f" {c['case_id']}: nonzero={c['nonzero_count']}, std={c['gamma_std']:.3f}, weights={[f'{w:.2f}' for w in c['gamma_mean']]}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|