diff --git a/docs/source/guides/inference_module_guide.rst b/docs/source/guides/inference_module_guide.rst index 7edf9231..b054ecb1 100644 --- a/docs/source/guides/inference_module_guide.rst +++ b/docs/source/guides/inference_module_guide.rst @@ -66,6 +66,11 @@ Interface and functionalities Currently the SegResNet and SwinUNetR models require you to provide the size of the images the model was trained with. Provided weights use a size of 64, please leave it on the default value if you're not using custom weights. +.. note:: + Pre-trained weights are downloaded automatically when first needed. By default they are stored inside the installed package. + To store them elsewhere, set the ``CELLSEG3D_WEIGHTS_DIR`` environment variable to the desired folder before launching napari, + e.g. ``export CELLSEG3D_WEIGHTS_DIR=~/cellseg3d/weights``. + * **Inference parameters**: * **Window inference**: You can choose to use inference on the entire image at once (disabled) or divide the image (enabled) on smaller chunks, based on your memory constraints. diff --git a/napari_cellseg3d/_tests/test_weight_download.py b/napari_cellseg3d/_tests/test_weight_download.py index 6f6a0c48..cdd854dd 100644 --- a/napari_cellseg3d/_tests/test_weight_download.py +++ b/napari_cellseg3d/_tests/test_weight_download.py @@ -18,3 +18,17 @@ def test_weight_download(): result_path = PRETRAINED_WEIGHTS_DIR / "test.pth" assert result_path.is_file() + + +def test_weights_dir_env_var(monkeypatch, tmp_path): + from napari_cellseg3d.utils import ( + PRETRAINED_URLS_DIR, + WEIGHTS_DIR_ENV_VAR, + get_pretrained_weights_dir, + ) + + monkeypatch.delenv(WEIGHTS_DIR_ENV_VAR, raising=False) + assert get_pretrained_weights_dir() == PRETRAINED_URLS_DIR + + monkeypatch.setenv(WEIGHTS_DIR_ENV_VAR, str(tmp_path)) + assert get_pretrained_weights_dir() == tmp_path.resolve() diff --git a/napari_cellseg3d/code_models/workers_utils.py b/napari_cellseg3d/code_models/workers_utils.py index 587daa4a..ac78a2e3 100644 --- a/napari_cellseg3d/code_models/workers_utils.py +++ b/napari_cellseg3d/code_models/workers_utils.py @@ -20,7 +20,7 @@ if TYPE_CHECKING: from napari_cellseg3d.code_models.instance_segmentation import ImageStats -PRETRAINED_WEIGHTS_DIR = Path(__file__).parent.resolve() / Path("models/pretrained") +PRETRAINED_WEIGHTS_DIR = utils.get_pretrained_weights_dir() class WeightsDownloader: @@ -52,7 +52,8 @@ def show_progress(_, block_size, __): # count, block_size, total_size logger.info("*" * 20) pretrained_folder_path = PRETRAINED_WEIGHTS_DIR - json_path = pretrained_folder_path / Path("pretrained_model_urls.json") + pretrained_folder_path.mkdir(parents=True, exist_ok=True) + json_path = utils.PRETRAINED_URLS_DIR / Path("pretrained_model_urls.json") check_path = pretrained_folder_path / Path(model_weights_filename) diff --git a/napari_cellseg3d/config.py b/napari_cellseg3d/config.py index ac0ffee1..5ccfde3b 100644 --- a/napari_cellseg3d/config.py +++ b/napari_cellseg3d/config.py @@ -16,7 +16,7 @@ from napari_cellseg3d.code_models.models.model_TRAILMAP_MS import TRAILMAP_MS_ from napari_cellseg3d.code_models.models.model_VNet import VNet_ from napari_cellseg3d.code_models.models.model_WNet import WNet_ -from napari_cellseg3d.utils import LOGGER +from napari_cellseg3d.utils import LOGGER, get_pretrained_weights_dir logger = LOGGER @@ -32,9 +32,7 @@ # "test" : DO NOT USE, reserved for testing } -PRETRAINED_WEIGHTS_DIR = str( - Path(__file__).parent.resolve() / Path("code_models/models/pretrained") -) +PRETRAINED_WEIGHTS_DIR = str(get_pretrained_weights_dir()) ################ diff --git a/napari_cellseg3d/utils.py b/napari_cellseg3d/utils.py index e35bbc90..d342743d 100644 --- a/napari_cellseg3d/utils.py +++ b/napari_cellseg3d/utils.py @@ -2,6 +2,7 @@ import logging import math +import os from datetime import datetime from pathlib import Path from typing import Union @@ -28,6 +29,24 @@ rand_gen = Generator(PCG64(12345)) +WEIGHTS_DIR_ENV_VAR = "CELLSEG3D_WEIGHTS_DIR" +# Bundled folder holding pretrained_model_urls.json (default download location) +PRETRAINED_URLS_DIR = ( + Path(__file__).parent.resolve() / "code_models" / "models" / "pretrained" +) + + +def get_pretrained_weights_dir() -> Path: + """Returns the folder where pretrained weights are downloaded and loaded from. + + Uses the path set in the CELLSEG3D_WEIGHTS_DIR environment variable if defined, + otherwise the pretrained folder inside the installed package. + """ + custom_dir = os.environ.get(WEIGHTS_DIR_ENV_VAR) + if custom_dir: + return Path(custom_dir).expanduser().resolve() + return PRETRAINED_URLS_DIR + #################### # viewer utils