Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localdownload #8

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 77 additions & 17 deletions DLICV/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,62 @@
warnings.simplefilter(action="ignore", category=FutureWarning)
warnings.simplefilter(action="ignore", category=UserWarning)

# VERSION = pkg_resources.require("NiChart_DLMUSE")[0].version
VERSION = 1.0

def main() -> None:
prog="DLICV"
parser = argparse.ArgumentParser(
description="Use this to run inference with nnU-Net. This function is used when "
"you want to manually specify a folder containing a trained nnU-Net "
"model. This is useful when the nnunet environment variables "
"(nnUNet_results) are not set."
prog=prog,
description="DLICV - Deep Learning Intra Cranial Volume.",
usage = """
DLICV v{VERSION}
ICV calculation for structural MRI data.

Required arguments:
[-i, --in_dir] The filepath of the input directory
[-o, --out_dir] The filepath of the output directory
Optional arguments:
[-device] cpu|cuda|mps - Depending on your system configuration (default: cuda)
[-h, --help] Show this help message and exit.
[-V, --version] Show program's version number and exit.
EXAMPLE USAGE:
DLICV -i /path/to/input \
-o /path/to/output \
-device cpu|cuda|mps

""".format(VERSION=VERSION),
)

# Required Arguments
parser.add_argument(
"-i",
type=str,
required=True,
help="input folder or list or image (nii.gz). Remember to use the correct channel numberings for your files (_0000 etc). File endings must be the same as the training dataset!",
help="[REQUIRED] Input folder with T1 sMRI images (nii.gz).",
)
parser.add_argument(
"-o",
type=str,
required=True,
help="Output folder. If it does not exist it will be created. Predicted segmentations will "
"have the same name as their source images.",
help="[REQUIRED] Output folder. If it does not exist it will be created. Predicted segmentations will have the same name as their source images.",
)

# Optional Arguments
parser.add_argument(
"-device",
type=str,
default="cuda",
required=False,
help="[Recommended] Use this to set the device the inference should run with. Available options are 'cuda' (GPU), "
"'cpu' (CPU) or 'mps' (Apple M-series chips supporting 3D CNN).",
)
parser.add_argument(
"-V",
"--version",
action="version",
version=prog + ": v{VERSION}.".format(VERSION=VERSION),
help="Show the version and exit",
)
parser.add_argument(
"-d",
Expand Down Expand Up @@ -144,15 +180,6 @@ def main() -> None:
"5 and use -part_id 0, 1, 2, 3 and 4. Simple, right? Note: You are yourself responsible "
"to make these run on separate GPUs! Use CUDA_VISIBLE_DEVICES (google, yo!)",
)
parser.add_argument(
"-device",
type=str,
default="cuda",
required=False,
help="Use this to set the device the inference should run with. Available options are 'cuda' (GPU), "
"'cpu' (CPU) or 'mps' (Apple M-series chips supporting 3D CNN)."
"Use CUDA_VISIBLE_DEVICES=X nnUNetv2_predict [...] instead!",
)
parser.add_argument(
"--disable_progress_bar",
action="store_true",
Expand All @@ -161,10 +188,33 @@ def main() -> None:
help="Set this flag to disable progress bar. Recommended for HPC environments (non interactive "
"jobs)",
)
parser.add_argument(
"--clear_cache",
action="store_true",
required=False,
default=False,
help="Set this flag to clear any cached models before running. This is recommended if a previous download failed.",
)

args = parser.parse_args()
args.f = [0]

if args.clear_cache:
shutil.rmtree(os.path.join(
Path(__file__).parent,
"nnunet_results"
))
shutil.rmtree(os.path.join(
Path(__file__).parent,
".cache"
))
if not args.i or not args.o:
print("Cache cleared and missing either -i / -o. Exiting.")
sys.exit(0)

if not args.i or not args.o:
parser.error("The following arguments are required: -i, -o")

# data conversion
src_folder = args.i # input folder
if not os.path.exists(args.o): # create output folder if it does not exist
Expand All @@ -189,6 +239,16 @@ def main() -> None:
% (args.d, args.d),
)

if args.clear_cache:
shutil.rmtree(os.path.join(
Path(__file__).parent,
"nnunet_results"
))
shutil.rmtree(os.path.join(
Path(__file__).parent,
".cache"
))

# Check if model exists. If not exist, download using HuggingFace
if not os.path.exists(model_folder):
# HF download model
Expand Down Expand Up @@ -280,7 +340,7 @@ def main() -> None:
if os.path.exists(des_folder):
shutil.rmtree(des_folder)

print("Inference Process Done!")
print("DLICV Process Done!")


if __name__ == "__main__":
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ Feel free to use it under the package's [licence](LICENCE)
DLICV -i "input_folder" -o "output_folder" -device cpu
```

#### Troubleshooting model download failures
Our model download process creates several deep directory structures. If you are on Windows and your model download process fails, it may be due to Windows file path limitations.

To enable long path support in Windows 10, version 1607, and later, the registry key `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD)` must exist and be set to 1.

If this affects you, we recommend re-running DLICV with the `--clear_cache` flag set on the first run.


## Contact
For more information, please contact [CBICA Software](mailto:[email protected]).
Expand Down