Updates
Browse files- .DS_Store +0 -0
- README.md +85 -1
- autoencoder-ep-4.pth → autoencoder_final.pth +2 -2
- brlp_lite.py → brain2vec.py +3 -3
- create_csv.py +39 -0
- discriminator-ep-4.pth +0 -3
- runs/Jan31_14-52-36_SOM-YT7DYVX-DT/events.out.tfevents.1738363956.SOM-YT7DYVX-DT.46314.0 → discriminator_final.pth +2 -2
- inputs_example.csv +6 -0
- inputs_local.csv +0 -0
- requirements.txt +3 -2
.DS_Store
CHANGED
|
Binary files a/.DS_Store and b/.DS_Store differ
|
|
|
README.md
CHANGED
|
@@ -12,4 +12,88 @@ pretty_name: 3D Brain Structure MRI Autoencoder
|
|
| 12 |
---
|
| 13 |
|
| 14 |
## 🧠 Model Summary
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
## 🧠 Model Summary
|
| 15 |
+
# brain2vec
|
| 16 |
+
An autoencoder model for brain structure T1 MRIs based on [Brain Latent Progression](https://github.com/LemuelPuglisi/BrLP/tree/main). The autoencoder takes in a 3d MRI NIfTI file and compresses to 1200 latent dimensions before reconstructing the image. The loss functions for training the autoencoder are:
|
| 17 |
+
- [L1Loss](https://pytorch.org/docs/stable/generated/torch.nn.L1Loss.html)
|
| 18 |
+
- [KLDivergenceLoss](https://pytorch.org/docs/stable/generated/torch.nn.KLDivLoss.html)
|
| 19 |
+
- [PatchAdversarialLoss](https://docs.monai.io/en/stable/losses.html#patchadversarialloss)
|
| 20 |
+
- [PerceptualLoss](https://docs.monai.io/en/stable/losses.html#perceptualloss)
|
| 21 |
+
|
| 22 |
+
# License
|
| 23 |
+
MIT License
|
| 24 |
+
|
| 25 |
+
Copyright (c) 2025
|
| 26 |
+
|
| 27 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 28 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 29 |
+
in the Software without restriction, including without limitation the rights
|
| 30 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 31 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 32 |
+
furnished to do so, subject to the following conditions:
|
| 33 |
+
|
| 34 |
+
The above copyright notice and this permission notice shall be included in all
|
| 35 |
+
copies or substantial portions of the Software.
|
| 36 |
+
|
| 37 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 38 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 39 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 40 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 41 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 42 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 43 |
+
SOFTWARE.
|
| 44 |
+
|
| 45 |
+
# Training data
|
| 46 |
+
[Radiata brain-structure](https://huggingface.co/datasets/radiata-ai/brain-structure): 3066 scans from 2085 individuals in the 'train' split. Mean age = 45.1 +- 24.5, including 2847 scans from cognitively normal subjects and 219 scans from individuals with an Alzheimer's disease clinical diagnosis.
|
| 47 |
+
|
| 48 |
+
# Example usage
|
| 49 |
+
```
|
| 50 |
+
# get brain2vec model repository
|
| 51 |
+
git clone https://huggingface.co/radiata-ai/brain2vec
|
| 52 |
+
cd brain2vec
|
| 53 |
+
|
| 54 |
+
# set up virtual environemt
|
| 55 |
+
python3 -m venv venv_brain2vec
|
| 56 |
+
source venv_brain2vec/bin/activate
|
| 57 |
+
|
| 58 |
+
# install Python libraries
|
| 59 |
+
pip install -r requirements.txt
|
| 60 |
+
|
| 61 |
+
# create the csv file listing the scan paths and other info
|
| 62 |
+
python create_csv.py
|
| 63 |
+
|
| 64 |
+
mkdir ae_cache
|
| 65 |
+
mkdir ae_output
|
| 66 |
+
|
| 67 |
+
# train the model
|
| 68 |
+
nohup python brain2vec.py train \
|
| 69 |
+
--dataset_csv /home/ubuntu/brain2vec/inputs.csv \
|
| 70 |
+
--cache_dir ./ae_cache \
|
| 71 |
+
--output_dir ./ae_output \
|
| 72 |
+
--n_epochs 10 \
|
| 73 |
+
> train_log.txt 2>&1 &
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
# Methods
|
| 77 |
+
transform:
|
| 78 |
+
(80, 96, 80)
|
| 79 |
+
pixdim=2
|
| 80 |
+
10 epochs
|
| 81 |
+
max_batch_size: int = 2,
|
| 82 |
+
batch_size: int = 16,
|
| 83 |
+
lr: float = 1e-4,
|
| 84 |
+
|
| 85 |
+
# References
|
| 86 |
+
Puglisi
|
| 87 |
+
Pinaya
|
| 88 |
+
|
| 89 |
+
# Citation
|
| 90 |
+
```
|
| 91 |
+
@dataset{Radiata-Brain-Structure,
|
| 92 |
+
author = {Jesse Brown and Clayton Young},
|
| 93 |
+
title = {Brain-Structure: Processed Structural MRI Brain Scans Across the Lifespan},
|
| 94 |
+
year = {2025},
|
| 95 |
+
url = {https://huggingface.co/datasets/radiata-ai/brain-structure},
|
| 96 |
+
note = {Version 1.0},
|
| 97 |
+
publisher = {Hugging Face}
|
| 98 |
+
}
|
| 99 |
+
```
|
autoencoder-ep-4.pth → autoencoder_final.pth
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:85c85a2a826d4667983e66ebb4f9ee6aad91cad1928bd05373dfcee0d8c1e18c
|
| 3 |
+
size 55071236
|
brlp_lite.py → brain2vec.py
RENAMED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# MIT License
|
| 2 |
|
| 3 |
-
# Copyright (c)
|
| 4 |
|
| 5 |
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
# of this software and associated documentation files (the "Software"), to deal
|
|
@@ -85,7 +85,7 @@ INPUT_SHAPE_1p5mm = (122, 146, 122)
|
|
| 85 |
INPUT_SHAPE_AE = (80, 96, 80)
|
| 86 |
|
| 87 |
# Latent shape of the autoencoder
|
| 88 |
-
LATENT_SHAPE_AE = (1, 10, 12, 10)
|
| 89 |
|
| 90 |
|
| 91 |
def load_if(checkpoints_path: Optional[str], network: nn.Module) -> nn.Module:
|
|
@@ -409,7 +409,7 @@ def train(
|
|
| 409 |
batch_size=max_batch_size,
|
| 410 |
shuffle=True,
|
| 411 |
persistent_workers=True,
|
| 412 |
-
pin_memory=True
|
| 413 |
)
|
| 414 |
|
| 415 |
print('Device is %s' %(device))
|
|
|
|
| 1 |
# MIT License
|
| 2 |
|
| 3 |
+
# Copyright (c) 2025
|
| 4 |
|
| 5 |
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
| 85 |
INPUT_SHAPE_AE = (80, 96, 80)
|
| 86 |
|
| 87 |
# Latent shape of the autoencoder
|
| 88 |
+
LATENT_SHAPE_AE = (1, 10, 12, 10)
|
| 89 |
|
| 90 |
|
| 91 |
def load_if(checkpoints_path: Optional[str], network: nn.Module) -> nn.Module:
|
|
|
|
| 409 |
batch_size=max_batch_size,
|
| 410 |
shuffle=True,
|
| 411 |
persistent_workers=True,
|
| 412 |
+
pin_memory=True,
|
| 413 |
)
|
| 414 |
|
| 415 |
print('Device is %s' %(device))
|
create_csv.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import os
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
|
| 6 |
+
def row_to_dict(row, split_name):
|
| 7 |
+
return {
|
| 8 |
+
"image_uid": row["id"],
|
| 9 |
+
"age": int(row["metadata"]["age"]),
|
| 10 |
+
"sex": 1 if row["metadata"]["sex"].lower() == "male" else 2,
|
| 11 |
+
"image_path": os.path.abspath(row["nii_filepath"]),
|
| 12 |
+
"split": split_name
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
def main():
|
| 16 |
+
# Load the datasets
|
| 17 |
+
ds_train = load_dataset("radiata-ai/brain-structure", split="train", trust_remote_code=True)
|
| 18 |
+
ds_val = load_dataset("radiata-ai/brain-structure", split="validation", trust_remote_code=True)
|
| 19 |
+
ds_test = load_dataset("radiata-ai/brain-structure", split="test", trust_remote_code=True)
|
| 20 |
+
|
| 21 |
+
rows = []
|
| 22 |
+
|
| 23 |
+
# Process each split
|
| 24 |
+
for data_row in ds_train:
|
| 25 |
+
rows.append(row_to_dict(data_row, "train"))
|
| 26 |
+
for data_row in ds_val:
|
| 27 |
+
rows.append(row_to_dict(data_row, "validation"))
|
| 28 |
+
for data_row in ds_test:
|
| 29 |
+
rows.append(row_to_dict(data_row, "test"))
|
| 30 |
+
|
| 31 |
+
# Create a DataFrame and write it to CSV
|
| 32 |
+
df = pd.DataFrame(rows)
|
| 33 |
+
output_csv = "inputs.csv"
|
| 34 |
+
df.to_csv(output_csv, index=False)
|
| 35 |
+
print(f"CSV file created: {output_csv}")
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
main()
|
| 39 |
+
|
discriminator-ep-4.pth
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:83d59c14472ce1cc582798762f7980361e0239e9f524b6b8b6861dab43fd664e
|
| 3 |
-
size 11098603
|
|
|
|
|
|
|
|
|
|
|
|
runs/Jan31_14-52-36_SOM-YT7DYVX-DT/events.out.tfevents.1738363956.SOM-YT7DYVX-DT.46314.0 → discriminator_final.pth
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:aa193bfe35924accf8960e23f16ead89275c38839d1d3a0019fa0f3e9176199b
|
| 3 |
+
size 11099058
|
inputs_example.csv
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
image_uid,age,sex,image_path,split
|
| 2 |
+
0,81,2,/Users/jbrown2/.cache/huggingface/datasets/downloads/extracted/6429865a89f9ae54df1c3c2db5d0f1f25cf7dd43cb87704d76ed08cf8c194aba/OASIS-2/sub-OASIS20133/ses-03/anat/msub-OASIS20133_ses-03_T1w_brain_affine_mni.nii.gz,train
|
| 3 |
+
1,78,2,/Users/jbrown2/.cache/huggingface/datasets/downloads/extracted/6429865a89f9ae54df1c3c2db5d0f1f25cf7dd43cb87704d76ed08cf8c194aba/OASIS-2/sub-OASIS20133/ses-01/anat/msub-OASIS20133_ses-01_T1w_brain_affine_mni.nii.gz,train
|
| 4 |
+
2,87,1,/Users/jbrown2/.cache/huggingface/datasets/downloads/extracted/6429865a89f9ae54df1c3c2db5d0f1f25cf7dd43cb87704d76ed08cf8c194aba/OASIS-2/sub-OASIS20105/ses-02/anat/msub-OASIS20105_ses-02_T1w_brain_affine_mni.nii.gz,train
|
| 5 |
+
3,86,1,/Users/jbrown2/.cache/huggingface/datasets/downloads/extracted/6429865a89f9ae54df1c3c2db5d0f1f25cf7dd43cb87704d76ed08cf8c194aba/OASIS-2/sub-OASIS20105/ses-01/anat/msub-OASIS20105_ses-01_T1w_brain_affine_mni.nii.gz,train
|
| 6 |
+
4,84,1,/Users/jbrown2/.cache/huggingface/datasets/downloads/extracted/6429865a89f9ae54df1c3c2db5d0f1f25cf7dd43cb87704d76ed08cf8c194aba/OASIS-2/sub-OASIS20102/ses-02/anat/msub-OASIS20102_ses-02_T1w_brain_affine_mni.nii.gz,train
|
inputs_local.csv
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
torch>=1.12
|
| 5 |
|
| 6 |
# MONAI v1.2+ has the 'generative' subpackage with AutoencoderKL, PatchDiscriminator, etc.
|
| 7 |
-
monai
|
| 8 |
monai-generative
|
| 9 |
|
| 10 |
# For perceptual losses in MONAI's generative module.
|
|
@@ -16,4 +16,5 @@ numpy
|
|
| 16 |
nibabel
|
| 17 |
tqdm
|
| 18 |
tensorboard
|
| 19 |
-
matplotlib
|
|
|
|
|
|
| 4 |
torch>=1.12
|
| 5 |
|
| 6 |
# MONAI v1.2+ has the 'generative' subpackage with AutoencoderKL, PatchDiscriminator, etc.
|
| 7 |
+
monai-weekly
|
| 8 |
monai-generative
|
| 9 |
|
| 10 |
# For perceptual losses in MONAI's generative module.
|
|
|
|
| 16 |
nibabel
|
| 17 |
tqdm
|
| 18 |
tensorboard
|
| 19 |
+
matplotlib
|
| 20 |
+
datasets
|