Upload folder using huggingface_hub
Browse files- test/check_pearson.py +8 -9
- test/load_model.py +33 -15
- test/overfit_test.py +0 -1
test/check_pearson.py
CHANGED
|
@@ -16,7 +16,7 @@ sys.path.append(str(src_dir))
|
|
| 16 |
|
| 17 |
from training import make_data_loaders, evaluate_stage1, evaluate_stage2, SUBJECTS
|
| 18 |
from medarc_architecture import MultiSubjectConvLinearEncoder
|
| 19 |
-
from
|
| 20 |
|
| 21 |
def plot_heatmap(acc_map, title, save_path):
|
| 22 |
"""
|
|
@@ -108,20 +108,19 @@ def main():
|
|
| 108 |
|
| 109 |
print("\n--- Stage 2 Checkout ---")
|
| 110 |
cfm_params = cfg.stage2.cfm
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
stage2_models = nn.ModuleDict()
|
| 114 |
|
| 115 |
for sub in subjects_list:
|
| 116 |
sub_key = str(sub)
|
| 117 |
cfm_model = CFM(
|
| 118 |
-
|
| 119 |
-
out_channel=target_dim,
|
| 120 |
cfm_params=cfm_params,
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
latent_dim=latent_dim,
|
| 125 |
).to(device)
|
| 126 |
stage2_models[sub_key] = cfm_model
|
| 127 |
|
|
|
|
| 16 |
|
| 17 |
from training import make_data_loaders, evaluate_stage1, evaluate_stage2, SUBJECTS
|
| 18 |
from medarc_architecture import MultiSubjectConvLinearEncoder
|
| 19 |
+
from stage2.CFM import CFM
|
| 20 |
|
| 21 |
def plot_heatmap(acc_map, title, save_path):
|
| 22 |
"""
|
|
|
|
| 108 |
|
| 109 |
print("\n--- Stage 2 Checkout ---")
|
| 110 |
cfm_params = cfg.stage2.cfm
|
| 111 |
+
velocity_net_params = cfg.stage2.velocity_net
|
| 112 |
+
source_ve_params = cfg.stage2.source_ve
|
| 113 |
+
transport_params = cfg.stage2.transport
|
| 114 |
stage2_models = nn.ModuleDict()
|
| 115 |
|
| 116 |
for sub in subjects_list:
|
| 117 |
sub_key = str(sub)
|
| 118 |
cfm_model = CFM(
|
| 119 |
+
feat_dim=target_dim,
|
|
|
|
| 120 |
cfm_params=cfm_params,
|
| 121 |
+
velocity_net_params=velocity_net_params,
|
| 122 |
+
source_ve_params=source_ve_params,
|
| 123 |
+
transport_params=transport_params,
|
|
|
|
| 124 |
).to(device)
|
| 125 |
stage2_models[sub_key] = cfm_model
|
| 126 |
|
test/load_model.py
CHANGED
|
@@ -54,17 +54,16 @@ def export_stage2(cfg, target_dim=1000):
|
|
| 54 |
print('\nExporting Stage 2...')
|
| 55 |
|
| 56 |
cfm_params = cfg.stage2.cfm
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
model = CFM(
|
| 61 |
-
|
| 62 |
-
out_channel=target_dim,
|
| 63 |
cfm_params=cfm_params,
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
latent_dim=latent_dim,
|
| 68 |
)
|
| 69 |
|
| 70 |
weights_path = ROOT / 'output' / 'debug_run' / 'stage2_epoch_0.pt'
|
|
@@ -74,22 +73,31 @@ def export_stage2(cfg, target_dim=1000):
|
|
| 74 |
print('Loaded Stage 2 weights.')
|
| 75 |
|
| 76 |
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# Dummy input: (B, C, T)
|
| 79 |
B, C, T = 1, target_dim, 10
|
| 80 |
mu = torch.randn(B, C, T)
|
| 81 |
-
|
| 82 |
-
mask = torch.ones(B, 1, T)
|
| 83 |
-
n_timesteps = torch.tensor(10)
|
| 84 |
|
| 85 |
save_path = 'stage2_model.onnx'
|
| 86 |
try:
|
| 87 |
torch.onnx.export(
|
| 88 |
-
|
| 89 |
-
|
| 90 |
save_path,
|
| 91 |
opset_version=14,
|
| 92 |
-
input_names=['mu'
|
| 93 |
output_names=['output']
|
| 94 |
)
|
| 95 |
print(f'Saved {save_path}')
|
|
@@ -102,7 +110,17 @@ if __name__ == '__main__':
|
|
| 102 |
cfg = OmegaConf.load(config_path)
|
| 103 |
else:
|
| 104 |
# Dummy config if config not found
|
| 105 |
-
cfg = OmegaConf.create(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
export_stage1(cfg)
|
| 108 |
export_stage2(cfg, target_dim=1000)
|
|
|
|
| 54 |
print('\nExporting Stage 2...')
|
| 55 |
|
| 56 |
cfm_params = cfg.stage2.cfm
|
| 57 |
+
velocity_net_params = cfg.stage2.velocity_net
|
| 58 |
+
source_ve_params = cfg.stage2.source_ve
|
| 59 |
+
transport_params = cfg.stage2.transport
|
| 60 |
|
| 61 |
model = CFM(
|
| 62 |
+
feat_dim=target_dim,
|
|
|
|
| 63 |
cfm_params=cfm_params,
|
| 64 |
+
velocity_net_params=velocity_net_params,
|
| 65 |
+
source_ve_params=source_ve_params,
|
| 66 |
+
transport_params=transport_params,
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
weights_path = ROOT / 'output' / 'debug_run' / 'stage2_epoch_0.pt'
|
|
|
|
| 73 |
print('Loaded Stage 2 weights.')
|
| 74 |
|
| 75 |
model.eval()
|
| 76 |
+
|
| 77 |
+
class Stage2ExportWrapper(nn.Module):
|
| 78 |
+
def __init__(self, cfm_model: nn.Module, steps: int):
|
| 79 |
+
super().__init__()
|
| 80 |
+
self.cfm_model = cfm_model
|
| 81 |
+
self.steps = steps
|
| 82 |
+
|
| 83 |
+
def forward(self, mu):
|
| 84 |
+
return self.cfm_model(mu, n_timesteps=self.steps)
|
| 85 |
+
|
| 86 |
+
export_wrapper = Stage2ExportWrapper(model, steps=10)
|
| 87 |
+
export_wrapper.eval()
|
| 88 |
|
| 89 |
# Dummy input: (B, C, T)
|
| 90 |
B, C, T = 1, target_dim, 10
|
| 91 |
mu = torch.randn(B, C, T)
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
save_path = 'stage2_model.onnx'
|
| 94 |
try:
|
| 95 |
torch.onnx.export(
|
| 96 |
+
export_wrapper,
|
| 97 |
+
mu,
|
| 98 |
save_path,
|
| 99 |
opset_version=14,
|
| 100 |
+
input_names=['mu'],
|
| 101 |
output_names=['output']
|
| 102 |
)
|
| 103 |
print(f'Saved {save_path}')
|
|
|
|
| 110 |
cfg = OmegaConf.load(config_path)
|
| 111 |
else:
|
| 112 |
# Dummy config if config not found
|
| 113 |
+
cfg = OmegaConf.create(
|
| 114 |
+
{
|
| 115 |
+
'stage1': {'model': {}},
|
| 116 |
+
'stage2': {
|
| 117 |
+
'cfm': {'solver': 'euler', 'kld_weight': 3.0, 'kld_target_std': 1.0, 'detach_ut': False},
|
| 118 |
+
'velocity_net': {'hidden_dim': 256, 'n_blocks': 2, 'n_heads': 4, 'dropout': 0.1, 'max_seq_len': 2048, 'temporal_attn_layers': 1},
|
| 119 |
+
'source_ve': {'depth': 2, 'num_heads': 4, 'num_queries': 8, 'dropout': 0.1, 'use_variational': True},
|
| 120 |
+
'transport': {'path_type': 'Linear', 'prediction': 'velocity', 'time_dist_type': 'uniform', 'time_dist_shift': 1.0},
|
| 121 |
+
},
|
| 122 |
+
}
|
| 123 |
+
)
|
| 124 |
|
| 125 |
export_stage1(cfg)
|
| 126 |
export_stage2(cfg, target_dim=1000)
|
test/overfit_test.py
CHANGED
|
@@ -68,7 +68,6 @@ def main():
|
|
| 68 |
i = args.subject_idx
|
| 69 |
x1 = fmri[:, i].transpose(1, 2) # (B, V, T)
|
| 70 |
mu = mu_anchor[:, i].transpose(1, 2) # (B, V, T)
|
| 71 |
-
mask = torch.ones(x1.shape[0], 1, x1.shape[2], device=device)
|
| 72 |
|
| 73 |
# --- Stage 2: single-subject CFM ---
|
| 74 |
cfm = CFM(
|
|
|
|
| 68 |
i = args.subject_idx
|
| 69 |
x1 = fmri[:, i].transpose(1, 2) # (B, V, T)
|
| 70 |
mu = mu_anchor[:, i].transpose(1, 2) # (B, V, T)
|
|
|
|
| 71 |
|
| 72 |
# --- Stage 2: single-subject CFM ---
|
| 73 |
cfm = CFM(
|