Add files using upload-large-folder tool
Browse files- .DS_Store +0 -0
- OASIS-2/.DS_Store +0 -0
- OASIS-2/sub-OASIS20169/.DS_Store +0 -0
- OASIS-2/sub-OASIS20169/ses-02/.DS_Store +0 -0
- README.md +70 -53
- brain-structure.py +1 -0
.DS_Store
CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
|
|
OASIS-2/.DS_Store
CHANGED
Binary files a/OASIS-2/.DS_Store and b/OASIS-2/.DS_Store differ
|
|
OASIS-2/sub-OASIS20169/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
OASIS-2/sub-OASIS20169/ses-02/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
README.md
CHANGED
@@ -84,75 +84,92 @@ brain-structure/
|
|
84 |
|
85 |
# Example usage
|
86 |
```
|
87 |
-
# install Hugging Face Datasets
|
88 |
pip install datasets
|
|
|
|
|
|
|
|
|
89 |
```
|
90 |
|
91 |
```
|
92 |
# load datasets
|
93 |
from datasets import load_dataset
|
94 |
|
95 |
-
ds_train = load_dataset("radiata-ai/brain-structure",
|
96 |
-
ds_val
|
97 |
-
ds_test
|
98 |
```
|
99 |
|
100 |
```
|
101 |
# example PyTorch processing of images
|
|
|
102 |
import torch
|
103 |
import torch.nn.functional as F
|
104 |
from torch.utils.data import Dataset
|
105 |
-
import nibabel as nib
|
106 |
|
107 |
-
|
108 |
"""
|
109 |
-
|
110 |
-
|
111 |
"""
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
```
|
157 |
|
158 |
|
@@ -188,7 +205,7 @@ Scans were partitioned into train/validation/test datasets with a 80%/10%/10% sp
|
|
188 |
```
|
189 |
@dataset{Radiata-Brain-Structure,
|
190 |
author = {Jesse Brown and Clayton Young},
|
191 |
-
title = {Brain-Structure:
|
192 |
year = {2025},
|
193 |
url = {https://huggingface.co/datasets/radiata-ai/brain-structure},
|
194 |
note = {Version 1.0},
|
|
|
84 |
|
85 |
# Example usage
|
86 |
```
|
87 |
+
# install Hugging Face Datasets
|
88 |
pip install datasets
|
89 |
+
|
90 |
+
# optional installs: NiBabel and PyTorch
|
91 |
+
pip install nibabel
|
92 |
+
pip install torch torchvision
|
93 |
```
|
94 |
|
95 |
```
|
96 |
# load datasets
|
97 |
from datasets import load_dataset
|
98 |
|
99 |
+
ds_train = load_dataset("radiata-ai/brain-structure", split="train", trust_remote_code=True)
|
100 |
+
ds_val = load_dataset("radiata-ai/brain-structure", split="validation", trust_remote_code=True)
|
101 |
+
ds_test = load_dataset("radiata-ai/brain-structure", split="test", trust_remote_code=True)
|
102 |
```
|
103 |
|
104 |
```
|
105 |
# example PyTorch processing of images
|
106 |
+
import nibabel as nib
|
107 |
import torch
|
108 |
import torch.nn.functional as F
|
109 |
from torch.utils.data import Dataset
|
|
|
110 |
|
111 |
+
def preprocess_nifti(example):
|
112 |
"""
|
113 |
+
Loads a .nii.gz file, crops, normalizes, and resamples to 96^3.
|
114 |
+
Returns a numpy array (or tensor) in example["img"].
|
115 |
"""
|
116 |
+
nii_path = example["nii_filepath"]
|
117 |
+
# Load volume data
|
118 |
+
vol = nib.load(nii_path).get_fdata()
|
119 |
+
|
120 |
+
# Crop sub-volume
|
121 |
+
vol = vol[7:105, 8:132, :108] # shape: (98, 124, 108)
|
122 |
+
|
123 |
+
# Shift intensities to be non-negative
|
124 |
+
vol = vol + abs(vol.min())
|
125 |
+
# Normalize to [0,1]
|
126 |
+
vol = vol / vol.max()
|
127 |
+
|
128 |
+
# Convert to torch.Tensor: (1,1,D,H,W)
|
129 |
+
t_tensor = torch.from_numpy(vol).float().unsqueeze(0).unsqueeze(0)
|
130 |
+
|
131 |
+
# Scale factor based on (124 -> 96) for the y-dimension
|
132 |
+
scale_factor = 96 / 124
|
133 |
+
downsampled = F.interpolate(
|
134 |
+
t_tensor,
|
135 |
+
scale_factor=(scale_factor, scale_factor, scale_factor),
|
136 |
+
mode="trilinear",
|
137 |
+
align_corners=False
|
138 |
+
)
|
139 |
+
|
140 |
+
# Now pad each dimension to exactly 96 (symmetric padding)
|
141 |
+
_, _, d, h, w = downsampled.shape
|
142 |
+
pad_d = 96 - d
|
143 |
+
pad_h = 96 - h
|
144 |
+
pad_w = 96 - w
|
145 |
+
padding = (
|
146 |
+
pad_w // 2, pad_w - pad_w // 2,
|
147 |
+
pad_h // 2, pad_h - pad_h // 2,
|
148 |
+
pad_d // 2, pad_d - pad_d // 2
|
149 |
+
)
|
150 |
+
final_img = F.pad(downsampled, padding) # shape => (1, 1, 96, 96, 96)
|
151 |
+
final_img = final_img.squeeze(0)
|
152 |
+
|
153 |
+
# Store as numpy or keep as torch.Tensor
|
154 |
+
example["img"] = final_img.numpy()
|
155 |
+
return example
|
156 |
+
```
|
157 |
+
|
158 |
+
```
|
159 |
+
# Apply the preprocessing to each split
|
160 |
+
ds_train = ds_train.map(preprocess_nifti)
|
161 |
+
ds_val = ds_val.map(preprocess_nifti)
|
162 |
+
ds_test = ds_test.map(preprocess_nifti)
|
163 |
+
|
164 |
+
# Set the dataset format to return PyTorch tensors for the 'img' column
|
165 |
+
ds_train.set_format(type='torch', columns=['img'])
|
166 |
+
ds_val.set_format(type='torch', columns=['img'])
|
167 |
+
ds_test.set_format(type='torch', columns=['img'])
|
168 |
+
|
169 |
+
# Set up data loaders for model training
|
170 |
+
train_loader = DataLoader(ds_train, batch_size=16, shuffle=True)
|
171 |
+
val_loader = DataLoader(ds_val, batch_size=16, shuffle=False)
|
172 |
+
test_loader = DataLoader(ds_test, batch_size=16, shuffle=False)
|
173 |
```
|
174 |
|
175 |
|
|
|
205 |
```
|
206 |
@dataset{Radiata-Brain-Structure,
|
207 |
author = {Jesse Brown and Clayton Young},
|
208 |
+
title = {Brain-Structure: Processed Structural MRI Brain Scans Across the Lifespan},
|
209 |
year = {2025},
|
210 |
url = {https://huggingface.co/datasets/radiata-ai/brain-structure},
|
211 |
note = {Version 1.0},
|
brain-structure.py
CHANGED
@@ -102,6 +102,7 @@ class BrainStructure(datasets.GeneratorBasedBuilder):
|
|
102 |
Hugging Face will place (or reference) files in this same folder context.
|
103 |
"""
|
104 |
data_dir = "." # The local folder containing subdirectories like IXI/, DLBS/, etc.
|
|
|
105 |
logger.info(f"BrainStructure: scanning data in {os.path.abspath(data_dir)}")
|
106 |
|
107 |
return [
|
|
|
102 |
Hugging Face will place (or reference) files in this same folder context.
|
103 |
"""
|
104 |
data_dir = "." # The local folder containing subdirectories like IXI/, DLBS/, etc.
|
105 |
+
print(os.path.abspath(data_dir))
|
106 |
logger.info(f"BrainStructure: scanning data in {os.path.abspath(data_dir)}")
|
107 |
|
108 |
return [
|