Code changes
Browse files- README.md +4 -2
- inference_brain2vec_PCA.py +21 -6
- train_brain2vec_PCA.py +1 -1
README.md
CHANGED
|
@@ -48,9 +48,11 @@ nohup python train_brain2vec_PCA.py \
|
|
| 48 |
|
| 49 |
# model inference
|
| 50 |
python inference_brain2vec_PCA.py \
|
| 51 |
-
--pca_model
|
| 52 |
--input_images /path/to/img1.nii.gz /path/to/img2.nii.gz \
|
| 53 |
-
--output_dir
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# or if you have a CSV with image paths:
|
| 56 |
python inference_brain2vec_PCA.py \
|
|
|
|
| 48 |
|
| 49 |
# model inference
|
| 50 |
python inference_brain2vec_PCA.py \
|
| 51 |
+
--pca_model pca_model.joblib \
|
| 52 |
--input_images /path/to/img1.nii.gz /path/to/img2.nii.gz \
|
| 53 |
+
--output_dir pca_output \
|
| 54 |
+
--embeddings_filename pca_output/pca_embeddings_2 \
|
| 55 |
+
--save_recons
|
| 56 |
|
| 57 |
# or if you have a CSV with image paths:
|
| 58 |
python inference_brain2vec_PCA.py \
|
inference_brain2vec_PCA.py
CHANGED
|
@@ -163,6 +163,18 @@ def main() -> None:
|
|
| 163 |
"--csv_input", type=str, default=None,
|
| 164 |
help="Path to a CSV containing column 'image_path'."
|
| 165 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
args = parser.parse_args()
|
| 167 |
|
| 168 |
os.makedirs(args.output_dir, exist_ok=True)
|
|
@@ -205,15 +217,18 @@ def main() -> None:
|
|
| 205 |
all_embeddings.append(embedding_np)
|
| 206 |
|
| 207 |
# Optionally save or visualize reconstructions
|
| 208 |
-
|
| 209 |
-
args.output_dir, f"reconstruction_{i}.npy"
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
print(f"[INFO] Saved reconstruction to: {out_recon_path}")
|
| 213 |
|
| 214 |
# Save all embeddings stacked
|
| 215 |
stacked_embeddings = np.vstack(all_embeddings) # (N, n_components)
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
np.save(out_embed_path, stacked_embeddings)
|
| 218 |
print(f"[INFO] Saved embeddings of shape {stacked_embeddings.shape} to: {out_embed_path}")
|
| 219 |
|
|
|
|
| 163 |
"--csv_input", type=str, default=None,
|
| 164 |
help="Path to a CSV containing column 'image_path'."
|
| 165 |
)
|
| 166 |
+
parser.add_argument(
|
| 167 |
+
"--embeddings_filename",
|
| 168 |
+
type=str,
|
| 169 |
+
required=True,
|
| 170 |
+
help="Filename (without path) to save the stacked embeddings (e.g., 'pca_embeddings.npy')."
|
| 171 |
+
)
|
| 172 |
+
parser.add_argument(
|
| 173 |
+
"--save_recons",
|
| 174 |
+
action="store_true",
|
| 175 |
+
help="If set, save each reconstruction as .npy. Default is not to save."
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
args = parser.parse_args()
|
| 179 |
|
| 180 |
os.makedirs(args.output_dir, exist_ok=True)
|
|
|
|
| 217 |
all_embeddings.append(embedding_np)
|
| 218 |
|
| 219 |
# Optionally save or visualize reconstructions
|
| 220 |
+
if args.save_recons:
|
| 221 |
+
out_recon_path = os.path.join(args.output_dir, f"reconstruction_{i}.npy")
|
| 222 |
+
np.save(out_recon_path, recon_np)
|
| 223 |
+
print(f"[INFO] Saved reconstruction to: {out_recon_path}")
|
|
|
|
| 224 |
|
| 225 |
# Save all embeddings stacked
|
| 226 |
stacked_embeddings = np.vstack(all_embeddings) # (N, n_components)
|
| 227 |
+
filename = args.embeddings_filename
|
| 228 |
+
if not filename.lower().endswith(".npy"):
|
| 229 |
+
filename += ".npy"
|
| 230 |
+
|
| 231 |
+
out_embed_path = os.path.join(args.output_dir, filename)
|
| 232 |
np.save(out_embed_path, stacked_embeddings)
|
| 233 |
print(f"[INFO] Saved embeddings of shape {stacked_embeddings.shape} to: {out_embed_path}")
|
| 234 |
|
train_brain2vec_PCA.py
CHANGED
|
@@ -276,7 +276,7 @@ def main() -> None:
|
|
| 276 |
)
|
| 277 |
|
| 278 |
# Fit the PCA model
|
| 279 |
-
print(f"Fitting {args.pca_type.capitalize()}PCA
|
| 280 |
model.fit(X)
|
| 281 |
print("Done fitting PCA. Transforming data to embeddings...")
|
| 282 |
|
|
|
|
| 276 |
)
|
| 277 |
|
| 278 |
# Fit the PCA model
|
| 279 |
+
print(f"Fitting {args.pca_type.capitalize()}PCA")
|
| 280 |
model.fit(X)
|
| 281 |
print("Done fitting PCA. Transforming data to embeddings...")
|
| 282 |
|