Commit ·
571ae9a
1
Parent(s): 67cd713
example run
Browse files- .gitignore +1 -0
- README.md +14 -25
- infer.py +0 -42
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
*nii.gz
|
README.md
CHANGED
|
@@ -15,7 +15,7 @@ datasets:
|
|
| 15 |
|
| 16 |
# MindGrab (BrainChop MeshNet)
|
| 17 |
|
| 18 |
-
MindGrab is a MeshNet-based skull-stripping model from the [BrainChop](https://github.com/neuroneural/brainchop) project. It takes 256
|
| 19 |
|
| 20 |
## Files
|
| 21 |
|
|
@@ -27,36 +27,25 @@ MindGrab is a MeshNet-based skull-stripping model from the [BrainChop](https://g
|
|
| 27 |
|
| 28 |
Optionally, you can upload the WebGPU export (`net_mindgrab.js` + `net_mindgrab.safetensors`) from [`niivue-tinygrad`](https://github.com/spikedoanz/niivue-tinygrad) if you want to distribute browser-ready assets alongside the raw weights.
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
```python
|
| 33 |
-
from huggingface_hub import hf_hub_download
|
| 34 |
-
from brainchop.tiny_meshnet import load_meshnet
|
| 35 |
|
| 36 |
-
|
| 37 |
-
weights_path = hf_hub_download("neuroneural/mindgrab", "model.pth")
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
mask = out.argmax(axis=1)
|
| 42 |
```
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
## Upload Instructions
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
huggingface-cli repo create mindgrab --type model --org neuroneural
|
| 51 |
-
git clone git@hf.co:neuroneural/mindgrab
|
| 52 |
-
cd mindgrab
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
```
|
| 61 |
-
|
| 62 |
-
Once published, link the model repo to the [MindGrab Space](https://huggingface.co/spaces/neuroneural/mindgrab) and the paper page on [hf.co/papers](https://huggingface.co/papers/submit) so people can discover the CLI, the Space, and the checkpoint from one central page.
|
|
|
|
| 15 |
|
| 16 |
# MindGrab (BrainChop MeshNet)
|
| 17 |
|
| 18 |
+
MindGrab is a MeshNet-based skull-stripping model from the [BrainChop](https://github.com/neuroneural/brainchop) project. It takes 256^3 conformed T1 volumes and produces a binary brain mask. The checkpoint runs entirely in [tinygrad](https://github.com/tinygrad/tinygrad) and powers the in-browser BrainChop demos (WebGPU/WebGL).
|
| 19 |
|
| 20 |
## Files
|
| 21 |
|
|
|
|
| 27 |
|
| 28 |
Optionally, you can upload the WebGPU export (`net_mindgrab.js` + `net_mindgrab.safetensors`) from [`niivue-tinygrad`](https://github.com/spikedoanz/niivue-tinygrad) if you want to distribute browser-ready assets alongside the raw weights.
|
| 29 |
|
| 30 |
+
Optionally, you can also run the model through the official frontend at [brainchop.org](https://brainchop.org/)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
## Usage
|
|
|
|
| 34 |
|
| 35 |
+
```sh
|
| 36 |
+
uv pip install hf brainchop
|
|
|
|
| 37 |
```
|
| 38 |
|
| 39 |
+
```python
|
| 40 |
+
from pathlib import Path
|
|
|
|
| 41 |
|
| 42 |
+
from huggingface_hub import hf_hub_download
|
| 43 |
+
from brainchop import load, save, api
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
model_dir = Path(hf_hub_download("neuroneural/mindgrab", "model.json")).parent
|
| 46 |
+
hf_hub_download(repo_id, "model.pth")
|
| 47 |
|
| 48 |
+
vol = load("t1_crop.nii.gz")
|
| 49 |
+
mask = api.segment(vol, str(model_dir))
|
| 50 |
+
save(mask, "mindgrab_mask.nii.gz")
|
| 51 |
```
|
|
|
|
|
|
infer.py
DELETED
|
@@ -1,42 +0,0 @@
|
|
| 1 |
-
"""Example inference using the MindGrab checkpoint on Hugging Face."""
|
| 2 |
-
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
|
| 5 |
-
from huggingface_hub import hf_hub_download
|
| 6 |
-
from tinygrad.tensor import Tensor
|
| 7 |
-
|
| 8 |
-
from brainchop import Volume, load as bc_load, save
|
| 9 |
-
from brainchop.tiny_meshnet import load_meshnet
|
| 10 |
-
|
| 11 |
-
REPO_ID = "neuroneural/mindgrab"
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def download_model(repo_id: str = REPO_ID):
|
| 15 |
-
config_path = hf_hub_download(repo_id, "model.json")
|
| 16 |
-
weights_path = hf_hub_download(repo_id, "model.pth")
|
| 17 |
-
return config_path, weights_path
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def run_mindgrab(input_path: str, output_path: str = "mindgrab_mask.nii.gz") -> str:
|
| 21 |
-
config_path, weights_path = download_model()
|
| 22 |
-
model = load_meshnet(config_path, weights_path)
|
| 23 |
-
|
| 24 |
-
vol = bc_load(input_path)
|
| 25 |
-
input_tensor = vol.data.reshape(1, 1, 256, 256, 256).cast("float32")
|
| 26 |
-
logits = model(input_tensor)
|
| 27 |
-
mask = logits.argmax(axis=1).reshape(256, 256, 256).cast("uint8")
|
| 28 |
-
|
| 29 |
-
save(Volume(mask, vol.header), output_path)
|
| 30 |
-
return output_path
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
if __name__ == "__main__":
|
| 34 |
-
import argparse
|
| 35 |
-
|
| 36 |
-
parser = argparse.ArgumentParser(description="Run MindGrab skull stripping")
|
| 37 |
-
parser.add_argument("input", help="Input conformed NIfTI volume (.nii/.nii.gz)")
|
| 38 |
-
parser.add_argument("-o", "--output", default="mindgrab_mask.nii.gz", help="Output mask path")
|
| 39 |
-
args = parser.parse_args()
|
| 40 |
-
|
| 41 |
-
output = run_mindgrab(args.input, args.output)
|
| 42 |
-
print(f"Saved mask to {output}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|