Skip to content

v0.9.1: Cellpose weights download on first use, pinned and checked; one torch warning hidden (#574 items 3 and 5) - #577

Draft
guijacquemet wants to merge 2 commits into
mainfrom
claude/cellpose-weights-consent
Draft

guijacquemet wants to merge 2 commits into
mainfrom
claude/cellpose-weights-consent

Conversation

@guijacquemet

@guijacquemet guijacquemet commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Implements items 3 and 5 of #574 for v0.9.1. This is a draft. Do not merge yet.

Summary (by module)

  • segmentation_cellpose_weights (new). Owner decision of 26 November 2026: choosing the model and pressing Preview or Segment (or Load the model in the Cellpose notebook) is the consent to download that model's weights.
    • fetch_weights downloads only the chosen model's file. The source is mouseland/cellpose-sam at a pinned commit, WEIGHTS_REVISION = 7c61431b5fbb…. Cellpose itself uses the moving main branch and turns TLS verification off; this download keeps verification on.
    • The bytes go to a temporary .part file next to the target. They replace the target only if they have the pinned size and SHA-256 (PINNED_WEIGHTS, the LFS hashes that Hugging Face lists at that commit).
    • A cached copy in Cellpose's model folder is hashed too. If it does not match the pin, the load is refused and the file is left where it is, not replaced.
    • A failed or blocked download, an HTTP error, or bytes that do not match all give a refusal. The refusal names the model and the URL. No partial file is kept and no other model is used.
  • segmentation_cellpose_model.
    • load_model(model, device, *, progress=None): the allow_download argument is removed. Cellpose now always gets the absolute path of the checked file, so it never downloads anything and cannot substitute another file.
    • The CPU/GPU behaviour is unchanged: there is still no fallback.
    • LoadedModel gains repository, revision and dinov3_installed.
  • DINOv3.
    • What it is for: it is the Python package that holds the network code of the DINOv3-backbone Cellpose models. Cellpose 4.2.1.1 builds those networks with dinov3_vitl16 / dinov3_vitb16(pretrained=False). All of their weights are in the Cellpose file, so DINOv3 downloads no weights of its own.
    • What replaced the tick: ensure_dinov3 installs DINOv3 at the pinned commit with --no-deps. This happens on the first load of such a model, only when no DINOv3 is installed, and before Cellpose is imported. A DINOv3 already installed at another or unknown commit is refused, with the command to run; it is not replaced.
    • Fix: the check for "Cellpose imported before DINOv3" now looks at cellpose.vit. Cellpose 4.2.1.1 defines CPDINO whether or not DINOv3 imports, so the old hasattr(models, "CPDINO") check could never fail. If Cellpose was already imported, the load asks for a runtime restart.
  • Notebook (notebook_cellpose_fields, _form, notebook_cellpose, notebook_cellpose_download).
    • Removed the Allow the weights download from Hugging Face and Install DINOv3 now ticks. Both notebooks share this form.
    • DownloadLine shows a "Downloading weights…" line while the weights download. It gives the MB received of the total, a bar and the elapsed time, then "done in m:ss" or "stopped after…". The same line shows "Installing DINOv3…" when DINOv3 is installed.
    • The patch of Cellpose's tqdm bar is removed, because Cellpose no longer downloads.
    • The card shown after loading names the repository and revision, and says whether the weights were downloaded now.
  • Item 5: torch warning.
    • segmentation_cellpose.evaluate runs model.eval inside sparse_warning_hidden().
    • That context manager uses warnings.catch_warnings() with one filterwarnings("ignore", message=re.escape("Sparse invariant checks are implicitly disabled"), category=UserWarning, module=r"(cellpose|torch)(\.|$)").
    • There is no global filter; the filters are restored after the call. Nothing is recorded, because the setting does not affect results.
  • Record (per batch).
    • The model entry: name, file, sha256. For a built-in model the SHA-256 equals the pinned one.
    • In parameters: weights_source (the pinned URL, …/resolve/<revision>/<name>), weights_repository, weights_revision and weights_downloaded. For a DINOv3 model, also dinov3_commit, dinov3_source and dinov3_installed. The weights_* source fields are null for a user's own model file.
  • segmentation_cellpose_eval (new, moved code). normalization, eval_arguments and inference_parameters moved here, unchanged apart from the new fields, and are re-exported by segmentation_cellpose. This keeps segmentation_cellpose.py under the 600-line code rule.

Decisions

  • Recorded in docs/plan.md §12, question 86, and in docs/contract.md.
  • Choices I made:
    • The download is pinned to a commit, not main.
    • A mismatching cached copy is refused, not silently replaced.
    • Installing DINOv3 automatically is treated as part of the same consent.
    • A DINOv3 of another commit is refused, not replaced.

Notes for review

  • Overlap with the install and Folders work. I made minimal markdown edits in the combined notebook MorphoBricks_Preprocessing_Cellpose.ipynb:
    • Introduction cell (index 0): its last sentence.
    • Cell-4 markdown (index 7): only bullet "3. Model and preview".
    • The install agent or the Folders agent may also edit cell 7. No code cell changed in either notebook, and RELEASE_REF is untouched (pins move only in the release PR).
  • Not touched: the preview display and the "model running" message. I did not change preview(). Only the load card in _load_model changed.
  • Stale text left as is. cellpose_install.py's docstring and INSTALL_TEXT in notebook_preprocessing_cellpose.py still mention DINOv3 as "a choice of the model step". I left both for the install-cell work.
  • Hashes and revision. I read the pinned SHA-256s and sizes from the Hugging Face API at that commit (LFS object IDs). I did not verify them by downloading the files. Please check them during the Colab test.
  • Not verified:
    • No real weights were downloaded and no real Cellpose or torch was run. All tests use the stand-in, with faked downloads.
    • Widgets were tested headless only; I have not looked at them in Colab or JupyterLab.
    • I assume torch's C++ warning is attributed to cellpose.dynamics, based on reading the source. The filter covers both the cellpose.* and torch.* modules.
  • API change:
    • load_model no longer takes allow_download.
    • CellposeWorkflow.load and load_nuclei take (model, custom_model_file, device).
    • notebook_cellpose_download.BAR_LABEL and notebook_bar are removed.

Tests

  • New: tests/test_segmentation_cellpose_weights.py. It covers:
    • the pins;
    • downloading only the chosen file, with progress;
    • refusals for URLError, timeout and HTTP 403;
    • downloaded bytes that do not match the pin;
    • a mismatching cached copy;
    • the record fields;
    • the sparse warning hidden from cellpose.dynamics, torch and torch.sparse, while other modules, categories and messages still show, and the filters are restored after the call.
  • Updated: the stub (tests/cellpose_stub.py), the notebook, form, stepper and both tests, and the DINOv3 install tests (first use, another commit refused, restart refusal).
  • Results:
    • pytest -k cellpose: 433 passed, 7 skipped.
    • Full python tools/check.py on head 7c6a4c9 (ruff check, ruff format --check, pytest, code rules): 8834 passed, 32 skipped, exit 0.
    • The skips are: skimage not installed, case-insensitive or root-permission platform conditions, an editable install, the mask-pieces parametrisations, and 3 release-only notebook-pin tests.
  • python tools/check.py --rules passes.

Review follow-up (ae64ecc)

  • Weights file identity. The file's identity (device, inode, size, mtime_ns) is taken when the file is hashed. It must be unchanged right before Cellpose opens the file and again after Cellpose has loaded it. This also applies to a user's own model file. Otherwise the load is refused, so Cellpose's fallback (re-downloading from main with TLS off) can no longer pass unnoticed.
  • Write errors. A full disk or other local write error is now reported as a write error naming the folder, not as a network problem. A failed rename gives a SegmentationError naming the target. The download is fsynced before the rename.
  • New tests.
    • The weights file removed, or replaced with the same bytes, during the load.
    • The file gone before the load: refused before Cellpose is called.
    • A user's own model file changed during the load.
    • The sparse-warning filter restored when Cellpose raises.
    • A full disk.
    • A failed rename.
    • fsync happens before the rename.
  • Results. pytest -k cellpose: 441 passed, 7 skipped. Ruff on the changed files and tools/check.py --rules pass. I did not re-run the full check, as the coordinator asked.

Links #574.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a

… torch warning (#574)

Item 3 (owner decision, 26 November 2026): choosing the model and pressing
Preview or Segment (or Load the model) is the consent to download its
weights. The weights-download and DINOv3 ticks are removed from the model
form. The brick downloads only the chosen model's file from the Hugging
Face repository at a pinned commit, keeps it only with its pinned size and
SHA-256 (a cached copy is checked too), and hands Cellpose the absolute
path. A failed or blocked download is a refusal naming the model and the
URL; no other model is used. The notebook shows a "Downloading <model>
weights..." line with progress. DINOv3 is installed at its pinned commit
on a DINOv3 model's first load when none is installed; another commit is
refused, not replaced. The record adds weights_repository,
weights_revision, dinov3_source and dinov3_installed.

Item 5: evaluate runs model.eval inside sparse_warning_hidden(), which
filters only torch's "Sparse invariant checks are implicitly disabled"
UserWarning from the torch or cellpose modules, in catch_warnings.

normalization, eval_arguments and inference_parameters move to
segmentation_cellpose_eval (re-exported) for the 600-line rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a
…rors (review of #577)

- The weights file's identity (device, inode, size, mtime_ns) taken when it
  is hashed must be unchanged right before Cellpose opens it and after it
  loaded it (also for a model file of one's own), else the load is refused:
  a file gone in between would make Cellpose fetch a model from its moving
  branch with TLS off while the record carried the pinned hash.
- A local write error (a full disk) is a write refusal naming the folder,
  not a network one; a failed rename is a SegmentationError naming the
  target; the download is fsynced before the rename.
- Tests: the identity checks (removed, replaced, gone before the load, own
  file), the sparse filter put back when Cellpose raises, the full disk,
  the failed rename and fsync before rename.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WX6rdMV1r5jUPUR94kxV5a
guijacquemet pushed a commit that referenced this pull request Nov 26, 2026
…l-running line (#574 item 4)

#576 shipped in the v0.9.0 tag through the release branch but was never
merged into main, so v0.9.1 would have lost it. Merged here; conflicts with
#577's load signature resolved (clear_preview kept, no download ticks).

#574 item 4 on top: while the model segments the preview image the line
says "Running <model> on <image>…" for each model in turn, with a ticking
time (notebook_progress.Wait), and the batch's progress line names the model
("Running <model> · n of N · <image>"). Tests in test_notebook_cellpose_preview
and test_notebook_cellpose_preview_stale.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VbytvF3Gsa85ix8Ne2yMDW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants