Skip to content
Open
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
5 changes: 5 additions & 0 deletions docs/source/guides/inference_module_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions napari_cellseg3d/_tests/test_weight_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
5 changes: 3 additions & 2 deletions napari_cellseg3d/code_models/workers_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 2 additions & 4 deletions napari_cellseg3d/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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())


################
Expand Down
19 changes: 19 additions & 0 deletions napari_cellseg3d/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import math
import os
from datetime import datetime
from pathlib import Path
from typing import Union
Expand All @@ -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
Expand Down