Skip to content

Commit

Permalink
Dev (#1)
Browse files Browse the repository at this point in the history
* add: basic gear to change the name of a nifti image (not working)

* fix: rename_and_copy_file

* fix: parser auto-detect

* updates up to 15-03-2024

* add: useful commands md

* remove unused variables from Dockerfile

* add: more useful commands

* moved files and folders in .gitignore

* modified run.py - nnUNet_predict command (local)

* generated manifest.json - adding the variables from Dockerfile

* generated config.json to run the gear locally

* generated files before running

* temporary call to the command

* fix: correct path for nnunet in useful commands

* add: --gpus device=0 to run the inference on GPU - not working

* version 0.0.0
  • Loading branch information
jaimebarran authored Mar 22, 2024
1 parent b98bd46 commit 4fddef3
Show file tree
Hide file tree
Showing 13 changed files with 790 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Exclude everything
**

# Except package dirs, run.py, tests, .git and README
!README.md
!requirements.txt
!manifest.json
!run.py
!fw_gear_aeye
!nnUNet

# Re-ignore pycache which might be inside package dir
**/__pycache__
137 changes: 137 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Custom folders/files to ignore
templates/
*.nii.gz
input/
output/
work/
nnUNet/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
1 change: 1 addition & 0 deletions .input_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"/flywheel/v0/input/nifti/I_Kopf_t1_mpr_tra_iso_p2.nii.gz": "input/nifti/I_Kopf_t1_mpr_tra_iso_p2.nii.gz"}
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Template
# Contains pytorch, torchvision, cuda, cudnn
FROM nvcr.io/nvidia/pytorch:24.02-py3

# Mantainer
LABEL maintainer="Jaime Barranco @jaimebarran"

# Install Flywheel
RUN pip install flywheel-sdk --break-system-packages \
&& pip install nnunet

# nnUNet dir
ARG resources="/opt/nnunet_resources"
ENV nnUNet_raw_data_base=$resources"/nnUNet_raw_data_base"
ENV nnUNet_preprocessed=$resources"/nnUNet_preprocessed"
ENV RESULTS_FOLDER=$resources"/nnUNet_trained_models"

# Set environment variables and workdir
ENV FLYWHEEL="/flywheel/v0"
WORKDIR ${FLYWHEEL}

# Installing main dependencies
COPY requirements.txt ${FLYWHEEL}/requirements.txt
RUN pip install --no-cache-dir -r $FLYWHEEL/requirements.txt

# Installing the current project (most likely to change, above layer can be cached)
COPY README.md ${FLYWHEEL}/README.md
COPY manifest.json ${FLYWHEEL}/manifest.json
COPY run.py ${FLYWHEEL}/run.py
COPY fw_gear_aeye ${FLYWHEEL}/fw_gear_aeye
COPY nnUNet/nnUNet_raw_data_base $resources"/nnUNet_raw_data_base"
COPY nnUNet/nnUNet_preprocessed $resources"/nnUNet_preprocessed"
COPY nnUNet/nnUNet_trained_models $resources"/nnUNet_trained_models"

# Configure entrypoint
RUN chmod a+x ${FLYWHEEL}/run.py
ENTRYPOINT ["python3","/flywheel/v0/run.py"]
151 changes: 151 additions & 0 deletions commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Useful commands

## Docker

- Build docker image

`docker build --no-cache -t jaimebarran/fw_gear_aeye_test .`

- Push docker image

`docker push jaimebarran/fw_gear_aeye_test`

- Run docker image interactively (local image)

`docker run -it --rm docker.io/jaimebarran/fw_gear_aeye_interactive /bin/bash`

- Run docker image interactively (remote image)

`docker run -it --rm jaimebarran/fw_gear_aeye_interactive /bin/bash`

- List images

`docker images` or `docker image ls`

- List containers

`docker ps -a` or `docker container ls -a`

- Remove image

`docker rmi <image_id>`

- Remove container

`docker rm <container_id>`

- Prune images

`docker image prune`

- Prune containers

`docker container prune`

## Flywheel

- Log in

`fw login {FW_API_KEY}`

- Test local gear

`fw gear local --nifti=input/nifti/I_Kopf_t1_mpr_tra_iso_p2.nii.gz --measurement="auto-detect" --debug=true`

- Upload gear

`fw gear upload`

### Flywheel beta

- Log in

`fw-beta login --api-key=${FW_API_KEY}`

- Validate the manifest

`fw-beta gear --validate manifest.json`

- Build the gear - this runs `docker build` and then extracts the `ENV` inside the Docker container and adds it to the `manifest.json`

`fw-beta gear build`

- Create the gear `config.json` (needed to later run the gear locally)

`fw-beta gear config --create`

- List the configuration options for this specific gear

`fw-beta gear config --show`

- Configure the local job (specify the input and config options)

`fw-beta gear config -i nifti=input/nifti/<filename> -c debug=True`

- Run the gear locally

`fw-beta gear run --rm`

## nnUNet

- Test to see if it is installed

`nnUNet_predict -h`

- Run nnUNet locally

```bash
nnUNet_predict -i "/home/jaimebarranco/Downloads/nnUNet_inference/input" -o "/home/jaimebarranco/Downloads/nnUNet_inference/output" -tr nnUNetTrainerV2 -ctr nnUNetTrainerV2CascadeFullRes -m 3d_fullres -p nnUNetPlansv2.1 -t Task313_Eye
```

- Run nnUNet through Docker pushed image

```bash
docker run --rm --gpus device=0 --shm-size=10gb -v /home/jaimebarranco/Desktop/nnUNet:/opt/nnunet_resources jaimebarran/fw_gear_aeye_interactive:latest nnUNet_predict -i /opt/nnunet_resources/nnUNet_inference/input -o /opt/nnunet_resources/nnUNet_inference/output -tr nnUNetTrainerV2 -ctr nnUNetTrainerV2CascadeFullRes -m 3d_fullres -p nnUNetPlansv2.1 -t Task313_Eye
```

- Run nnUNet through Docker pushed image with trained model

```bash
docker run --rm --gpus device=0 --shm-size=10gb -v /home/jaimebarranco/Downloads/nnUNet_inference:/tmp jaimebarran/fw_gear_aeye_interactive:latest nnUNet_predict -i /tmp/input -o /tmp/output -tr nnUNetTrainerV2 -ctr nnUNetTrainerV2CascadeFullRes -m 3d_fullres -p nnUNetPlansv2.1 -t Task313_Eye
```

- Last command but with CDI

```bash
docker run --rm --shm-size=10gb --runtime=nvidia -v /home/jaimebarranco/Downloads/nnUNet_inference:/tmp jaimebarran/fw_gear_aeye_interactive:latest
-e NVIDIA_VISIBLE_DEVICES=nvidia.com/gpu=all nnUNet_predict -i /tmp/input -o /tmp/output -tr nnUNetTrainerV2 -ctr nnUNetTrainerV2CascadeFullRes
-m 3d_fullres -p nnUNetPlansv2.1 -t Task313_Eye
```

- Last command but with CDI set in etc/nvidia-container-runtime/config.toml > nvidia-container-runtime > mode = "cdi" instead of "auto"

```bash
docker run --rm --shm-size=10gb --runtime=nvidia -v /home/jaimebarranco/Downloads/nnUNet_inference:/tmp jaimebarran/fw_gear_aeye_interactive:latest
-e NVIDIA_VISIBLE_DEVICES=all nnUNet_predict -i /tmp/input -o /tmp/output -tr nnUNetTrainerV2 -ctr nnUNetTrainerV2CascadeFullRes
-m 3d_fullres -p nnUNetPlansv2.1 -t Task313_Eye
```

- Export environment variables (required)

- nnUNet_raw

`export nnUNet_raw="/home/jaimebarranco/Desktop/nnUNet/nnUNet_raw_data"`

- nnUNet_preprocessed

`export nnUNet_preprocessed="/home/jaimebarranco/Desktop/nnUNet/nnUNet_preprocessed"`

- nnUNet_results

`export nnUNet_results="/home/jaimebarranco/Desktop/nnUNet/nnUNet_trained_models"`

## File permissions

- Change file permissions

`sudo chmod -R a+rw /mnt/sda1/Repos/flywheel/aeye_flywheel/output/input/`

- Change file owner

`sudo chown jaimebarranco output/input/I_Kopf_t1_mpr_tra_iso_p2.nii.gz`
32 changes: 32 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"config": {
"measurement": "T1",
"debug": true
},
"inputs": {
"nifti": {
"base": "file",
"location": {
"name": "I_Kopf_t1_mpr_tra_iso_p2.nii.gz",
"path": "/flywheel/v0/input/nifti/I_Kopf_t1_mpr_tra_iso_p2.nii.gz"
},
"object": {
"size": 8703369,
"type": null,
"mimetype": "application/octet-stream",
"modality": null,
"classification": {},
"tags": [],
"info": {},
"zip_member_count": null,
"version": 1,
"file_id": "",
"origin": {
"type": "user",
"id": ""
}
}
}
},
"destination": {}
}
Loading

0 comments on commit 4fddef3

Please sign in to comment.