From 6703079d5aa0aac176ca0246b67f2d6ceb46863c Mon Sep 17 00:00:00 2001 From: ashesh Date: Tue, 19 Nov 2024 10:38:20 +0100 Subject: [PATCH 1/3] A new enum for a new splitting task. (#270) ### Description Adding a new enum type for a splitting task which I had missed communicating earlier. I am putting the relevant things in microsplit-reproducibility repo after a brief chat with @veegalinova. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Joran Deschamps <6367888+jdeschamps@users.noreply.github.com> --- src/careamics/lvae_training/dataset/types.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/careamics/lvae_training/dataset/types.py b/src/careamics/lvae_training/dataset/types.py index 4174afe4b..5d0d9e0d9 100644 --- a/src/careamics/lvae_training/dataset/types.py +++ b/src/careamics/lvae_training/dataset/types.py @@ -15,6 +15,7 @@ class DataType(Enum): OptiMEM100_014 = 10 SeparateTiffData = 11 BioSR_MRC = 12 + PunctaRemoval = 13 # for the case when we have a set of differently sized crops for each channel. class DataSplitType(Enum): From 5f85f3be49f08794a57fcb41b791fe624ccfa030 Mon Sep 17 00:00:00 2001 From: Melisande Croft <63270704+melisande-c@users.noreply.github.com> Date: Tue, 19 Nov 2024 18:03:13 +0100 Subject: [PATCH 2/3] Fix(BMZ): Relax model output validation kwargs; extract weights and config file following new `spec` and `core` release (#279) ### Description - **What**: Relaxing the model output validation kwargs, both absolute and relative tolerance, from the default, `1e-4`, to `1e-2`. - **Why**: The defaults are pretty strict and some of our uploaded models are stuck in pending because of slightly mismatching input and outputs. - e.g. (Actually maybe absolute tolerance should be put to 0, otherwise it still might not pass after this PR) ```console Output and expected output disagree: Not equal to tolerance rtol=0.0001, atol=0.00015 Mismatched elements: 40202 / 1048576 (3.83%) Max absolute difference: 0.1965332 Max relative difference: 0.0003221 ``` - **How**: In the model description config param, added the new test kwargs. Additionally, updated `bmz_export` so that the test kwargs in the model description are used during model testing at export time. ### Changes Made - **Modified**: Describe existing features or files modified. - `create_model_description`: added test_kwargs to config param - `export_to_bmz`: use test_kwargs in model description for model testing at export time. ### Related Issues - Resolves - last checkbox in #278 EDIT: This PR also fixes loading from BMZ following an incompatible release of `bioimageio/core` (`0.7.0`) and `bioimageio/spec` (`0.5.3.5`). The problem was `load_model_description` no longer unzips the archive file but only streams the `rdf.yaml` file data. This means we have to now extract the weights and careamics config from the zip to load them, which can be done using `bioimageio.spec._internal.io.resolve_and_extract` --- **Please ensure your PR meets the following requirements:** - [x] Code builds and passes tests locally, including doctests - [ ] New tests have been added (for bug fixes/features) - [x] Pre-commit passes - [ ] PR to the documentation exists (for bug fixes / features) --------- Co-authored-by: Joran Deschamps <6367888+jdeschamps@users.noreply.github.com> --- .../model_io/bioimage/model_description.py | 17 +++++++++++++++-- src/careamics/model_io/bmz_io.py | 15 +++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/careamics/model_io/bioimage/model_description.py b/src/careamics/model_io/bioimage/model_description.py index 21ed50b8f..dbd1dfe08 100644 --- a/src/careamics/model_io/bioimage/model_description.py +++ b/src/careamics/model_io/bioimage/model_description.py @@ -4,6 +4,7 @@ from typing import List, Optional, Tuple, Union import numpy as np +from bioimageio.spec._internal.io import resolve_and_extract from bioimageio.spec.model.v0_5 import ( ArchitectureFromLibraryDescr, Author, @@ -280,6 +281,16 @@ def create_model_description( "https://careamics.github.io/latest/", ], license="BSD-3-Clause", + config={ + "bioimageio": { + "test_kwargs": { + "pytorch_state_dict": { + "absolute_tolerance": 1e-2, + "relative_tolerance": 1e-2, + } + } + } + }, version="0.1.0", weights=weights_descr, attachments=[FileDescr(source=config_path)], @@ -304,7 +315,9 @@ def extract_model_path(model_desc: ModelDescr) -> tuple[Path, Path]: """ if model_desc.weights.pytorch_state_dict is None: raise ValueError("No model weights found in model description.") - weights_path = model_desc.weights.pytorch_state_dict.download().path + weights_path = resolve_and_extract( + model_desc.weights.pytorch_state_dict.source + ).path for file in model_desc.attachments: file_path = file.source if isinstance(file.source, Path) else file.source.path @@ -312,7 +325,7 @@ def extract_model_path(model_desc: ModelDescr) -> tuple[Path, Path]: continue file_path = Path(file_path) if file_path.name == "careamics.yaml": - config_path = file.download().path + config_path = resolve_and_extract(file.source).path break else: raise ValueError("Configuration file not found.") diff --git a/src/careamics/model_io/bmz_io.py b/src/careamics/model_io/bmz_io.py index dc4564ecc..65a3ea99c 100644 --- a/src/careamics/model_io/bmz_io.py +++ b/src/careamics/model_io/bmz_io.py @@ -21,7 +21,6 @@ create_env_text, create_model_description, extract_model_path, - get_unzip_path, ) @@ -185,7 +184,12 @@ def export_to_bmz( ) # test model description - summary: ValidationSummary = test_model(model_description) + test_kwargs = ( + model_description.config.get("bioimageio", {}) + .get("test_kwargs", {}) + .get("pytorch_state_dict", {}) + ) + summary: ValidationSummary = test_model(model_description, **test_kwargs) if summary.status == "failed": raise ValueError(f"Model description test failed: {summary}") @@ -219,14 +223,9 @@ def load_from_bmz( # load description, this creates an unzipped folder next to the archive model_desc = load_model_description(path) - # extract relative paths + # extract paths weights_path, config_path = extract_model_path(model_desc) - # create folder path and absolute paths - unzip_path = get_unzip_path(path) - weights_path = unzip_path / weights_path - config_path = unzip_path / config_path - # load configuration config = load_configuration(config_path) From 240e4d34ed5aa503cb8f5bcc3a281fa64c7142b2 Mon Sep 17 00:00:00 2001 From: Melisande Croft <63270704+melisande-c@users.noreply.github.com> Date: Wed, 20 Nov 2024 23:07:37 +0100 Subject: [PATCH 3/3] Fix(dependencies): Set bioimageio-core version greater than 0.7.0 (#280) ### Description - **What**: Set bioimageio-core version greater than 0.7.0 - **Why**: Following the new `bioimage-core` release (0.7.0), we needed to make some fixes (part of PR #279). The most convenient function to solve this problem, `resolve_and_extract` only exists since 0.7.0. - **How**: In pyproject.toml ### Changes Made - **Modified**: pyproject.toml --- **Please ensure your PR meets the following requirements:** - [x] Code builds and passes tests locally, including doctests - [ ] New tests have been added (for bug fixes/features) - [x] Pre-commit passes - [ ] PR to the documentation exists (for bug fixes / features) --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 9c13382c0..b38b1c853 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,7 +45,7 @@ dependencies = [ 'numpy<2.0.0', 'torch>=2.0.0', 'torchvision', - 'bioimageio.core>=0.6.9', + 'bioimageio.core>=0.7.0', 'tifffile', 'psutil', 'pydantic>=2.5,<2.9',