Skip to content

Commit

Permalink
Merge pull request #530 from Deep-MI/mac
Browse files Browse the repository at this point in the history
Improve Mac support and clean up dependencies
  • Loading branch information
m-reuter authored Jun 19, 2024
2 parents 2c04d15 + ae9460d commit d4767d9
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 378 deletions.
2 changes: 1 addition & 1 deletion FastSurferCNN/segstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ def read_classes_from_lut(lut_file: str | Path):
}
return pd.read_csv(
lut_file,
delim_whitespace=True,
sep='\s+',
index_col=False,
skip_blank_lines=True,
comment="#",
Expand Down
2 changes: 1 addition & 1 deletion FastSurferCNN/utils/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ def __init__(

self._data = pandas.read_csv(
file_or_buffer,
delim_whitespace=True,
sep='\s+',
index_col=0,
skip_blank_lines=True,
comment="#",
Expand Down
35 changes: 15 additions & 20 deletions doc/overview/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
sudo apt install -y g++-11
```

You also need to have bash-4.0 or higher (check with `bash --version`).
You also need to have bash-3.2 or higher (check with `bash --version`).

You also need a working version of python3 (we recommend python 3.10 -- we do not support other versions). These packages should be sufficient to install python dependencies and then run the FastSurfer neural network segmentation. If you want to run the full pipeline, you also need a [working installation of FreeSurfer](https://surfer.nmr.mgh.harvard.edu/fswiki/rel7downloads) (including its dependencies).
You also need a working version of python3.10 (we do not support other versions). These packages should be sufficient to install python dependencies and then run the FastSurfer neural network segmentation. If you want to run the full pipeline, you also need a [working installation of FreeSurfer](https://surfer.nmr.mgh.harvard.edu/fswiki/rel7downloads) (including its dependencies).

If you are using pip, make sure pip is updated as older versions will fail.

Expand Down Expand Up @@ -165,26 +165,24 @@ Continue with the example in [Example 1](EXAMPLES.md#example-1-fastsurfer-docker

### Native

On modern Macs with the Apple Silicon M1 or M2 ARM-based chips, we recommend a native installation as it runs much faster than Docker in our tests. The experimental support for the built-in AI Accelerator is also only available on native installations. Native installation also supports older Intel chips.
On modern Macs with the Apple Silicon M1 or M2 ARM-based chips, we recommend a native installation as it runs much faster than Docker in our tests. The experimental support for the built-in AI accelerator (MPS) is also only available on native installations. Native installation also supports older Intel chips.

#### 1. Git and Bash
If you do not have git and a recent bash (version > 4.0 required!) installed, install them via the packet manager, e.g. brew.
This installs brew and then bash:
#### 1. Dependency packages
If you do not have git, python3.10 or bash (at least 3.2) you can install these via the packet manager brew.
This installs brew and then git and python3.10:

```sh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install bash
brew install git python@3.10
```

Make sure you use this bash and not the older one provided with MacOS!

#### 2. Python
Create a python environment, activate it, and upgrade pip. Here we use pip, but you should also be able to use conda for python:

```sh
python3 -m venv $HOME/python-envs/fastsurfer
python3.10 -m venv $HOME/python-envs/fastsurfer
source $HOME/python-envs/fastsurfer/bin/activate
python3 -m pip install --upgrade pip
python3.10 -m pip install --upgrade pip
```

#### 3. FastSurfer and Requirements
Expand All @@ -195,10 +193,9 @@ cd FastSurfer
export PYTHONPATH="${PYTHONPATH}:$PWD"
```


Install the FastSurfer requirements
```sh
python3 -m pip install -r requirements.mac.txt
python3.10 -m pip install -r requirements.mac.txt
```

If this step fails, you may need to edit ```requirements.mac.txt``` and adjust version number to what is available.
Expand All @@ -212,24 +209,22 @@ pip3 install --no-binary=h5py h5py

You can also download all network checkpoint files (this should be done if you are installing for multiple users):
```sh
python3 FastSurferCNN/download_checkpoints.py --all
python3.10 FastSurferCNN/download_checkpoints.py --all
```

Once all dependencies are installed, run the FastSurfer segmentation only (!!) by calling ```bash ./run_fastsurfer.sh --seg_only ....``` with the appropriate command line flags, see the [commandline documentation](../../README.md#usage).

Note: You may always need to prepend the command with `bash` (i.e. `bash run_fastsurfer.sh <...>`) to ensure that bash 4.0 is used instead of the system default.
Once all dependencies are installed, you can run the FastSurfer segmentation only by calling ```./run_fastsurfer.sh --seg_only ....``` with the appropriate command line flags, see the [commandline documentation](../../README.md#usage).

To run the full pipeline, install and source also the supported FreeSurfer version according to their [Instructions](https://surfer.nmr.mgh.harvard.edu/fswiki/rel7downloads). There is a freesurfer email list, if you run into problems during this step.
To run the full pipeline, install and source also the supported FreeSurfer version according to their [Instructions](https://surfer.nmr.mgh.harvard.edu/fswiki/rel7downloads). There is a freesurfer email list, if you run into problems during this step. Note, that currently FreeSurfer for MacOS supports no ARM, but only Intel, so on modern M-chips it might be slow due to the emulation.

#### 4. Apple AI Accelerator support
You can also try the experimental support for the Apple Silicon AI Accelerator by setting `PYTORCH_ENABLE_MPS_FALLBACK` and passing `--device mps`:
You can also try the experimental support for the Apple Silicon AI Accelerator by setting `PYTORCH_ENABLE_MPS_FALLBACK` and passing `--device mps` for the segmentation module to make use of the fast GPU:

```sh
export PYTORCH_ENABLE_MPS_FALLBACK=1
./run_fastsurfer.sh --seg_only --device mps ....
```

This will be at least twice as fast as `--device cpu`. The fallback environment variable is necessary as one function is not yet implemented for the GPU and will fall back to CPU.
This will be at least twice as fast as `--device cpu`. The fallback environment variable is necessary as `aten::max_unpool2d` is not yet implemented for MPS and will fall back to CPU.

## Windows

Expand Down
27 changes: 15 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,23 @@ classifiers = [
]
dependencies = [
'h5py>=3.7',
'lapy>=0.4.1',
'matplotlib>=3.5.1',
'nibabel>=3.2.2',
'numpy>=1.21',
'pandas>=1.4.3',
'torch>=1.12.0',
'lapy>=1.0.1',
'matplotlib>=3.7.1',
'nibabel>=5.1.0',
'numpy>=1.25,<2',
'pandas>=1.5.3',
'pyyaml>=6.0',
'scipy>=1.8.0',
'yacs>=0.1.8',
'simpleitk>=2.1.1',
'scipy>=1.8.0',
'tensorboard>=2.9.1',
'requests>=2.31.0',
'scikit-image>=0.19.3',
'scikit-learn>=1.2.2',
'scipy>=1.10.1,!=1.13.0',
'simpleitk>=2.2.1',
'tensorboard>=2.12.1',
'torch>=2.0.1',
'torchio>=0.18.83',
'tqdm>=4.64',
'torchvision>=0.15.2',
'tqdm>=4.65',
'yacs>=0.1.8',
]

[project.optional-dependencies]
Expand Down
31 changes: 16 additions & 15 deletions recon_surf/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ function RunIt()
if [[ $# -eq 3 ]]
then
local CMDF=$3
echo "echo ${cmd@Q}" |& tee -a $CMDF
echo "$timecmd $cmd" |& tee -a $CMDF
printf -v tmp %q "$cmd"
echo "echo $tmp" 2>&1 | tee -a $CMDF
echo "$timecmd $cmd" 2>&1 | tee -a $CMDF
echo "if [ \${PIPESTATUS[0]} -ne 0 ] ; then exit 1 ; fi" >> $CMDF
else
echo $cmd |& tee -a $LF
$timecmd $cmd |& tee -a $LF
echo $cmd 2>&1 | tee -a $LF
$timecmd $cmd 2>&1 | tee -a $LF
if [ ${PIPESTATUS[0]} -ne 0 ] ; then exit 1 ; fi
fi
}
Expand Down Expand Up @@ -107,21 +108,21 @@ function softlink_or_copy()
if [[ $# -eq 4 ]]
then
local CMDF=$4
echo "echo \"$ln_cmd\" " |& tee -a $CMDF
echo "$timecmd $ln_cmd " |& tee -a $CMDF
echo "if [ \${PIPESTATUS[0]} -ne 0 ]" |& tee -a $CMDF
echo "then " |& tee -a $CMDF
echo " echo \"$cp_cmd\" " |& tee -a $CMDF
echo " $timecmd $cp_cmd " |& tee -a $CMDF
echo "echo \"$ln_cmd\" " 2>&1 | tee -a $CMDF
echo "$timecmd $ln_cmd " 2>&1 | tee -a $CMDF
echo "if [ \${PIPESTATUS[0]} -ne 0 ]" 2>&1 | tee -a $CMDF
echo "then " 2>&1 | tee -a $CMDF
echo " echo \"$cp_cmd\" " 2>&1 | tee -a $CMDF
echo " $timecmd $cp_cmd " 2>&1 | tee -a $CMDF
echo " if [ \${PIPESTATUS[0]} -ne 0 ] ; then exit 1 ; fi" >> $CMDF
echo "fi" |& tee -a $CMDF
echo "fi" 2>&1 | tee -a $CMDF
else
echo $ln_cmd |& tee -a $LF
$timecmd $ln_cmd |& tee -a $LF
echo $ln_cmd 2>&1 | tee -a $LF
$timecmd $ln_cmd 2>&1 | tee -a $LF
if [ ${PIPESTATUS[0]} -ne 0 ]
then
echo $cp_cmd |& tee -a $LF
$timecmd $cp_cmd |& tee -a $LF
echo $cp_cmd 2>&1 | tee -a $LF
$timecmd $cp_cmd 2>&1 | tee -a $LF
if [ ${PIPESTATUS[0]} -ne 0 ] ; then exit 1 ; fi
fi
fi
Expand Down
Loading

0 comments on commit d4767d9

Please sign in to comment.