fix python parity in preprocess + dynamics; expand test coverage#8
Merged
Conversation
Five corrections after a code review against cellpose (Python):
1. Preprocess order: normalize FIRST, then resize. Matches
`models.py:_run_net`. Resizing before normalize was changing the
per-channel percentile statistics.
2. Replace canvas-based resize with pure-JS Float32 bilinear. The
old path quantized each channel to uint8 (~1/255 error per pixel)
and required OffscreenCanvas/HTMLCanvas. New impl uses OpenCV's
INTER_LINEAR pixel-center mapping with edge replication.
3. Scale niter by image_scaling. Python computes
`niter = int(200 / (30 / diameter))`; JS was fixed at 200
regardless of diameter. Explicit user override still wins.
4. Math.round → Math.trunc in follow_flows. PyTorch's `.int()` cast
truncates toward zero; JS was rounding-half-up, which shifted
converged points by up to 1 pixel.
5. Fix README quickstart: `cellprob_threshold` was at the wrong
nesting (should be `dynamics: { cellprobThreshold }`); also
removed a stale "once published" parenthetical.
Quality impact:
- `three_cells_192` dynamics parity: 0.601 → 1.000 mean IoU (all 5
per-cell IoUs at 1.000). Gate tightened from 0.55 → 0.99.
- Total tests 14 → 61. New files cover `buildCpsamChannels`,
`percentile`, `taperMask`, `followFlows`, `getMasks`, the new
bilinear `diameterResize`, and an end-to-end postprocess pipeline
test that mocks the inference step.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
… hooks
Three issues surfaced while smoke-testing the demo:
1. Image picker silently failed. `loadImageFile` uses `createImageBitmap`,
which throws on formats it can't decode (TIFF microscopy images, etc).
The change handler had no try/catch so the rejection was swallowed and
the input canvas stayed blank. Now: try/catch around the load + draw +
`log()` the error visibly. The `createImageBitmap` catch path also
nudges the user toward PNG/JPEG.
2. Worker module failed to load with no diagnostics. Vite's worker plugin
transforms `new URL('./inference.worker.js', import.meta.url)` into a
URL ending in `inference.worker.js?worker_file`. The library source
uses `.js` so the tsc-built prod artifact resolves correctly, but in
dev the file on disk is `.ts` and the worker plugin doesn't auto-swap
extensions like the regular resolver does. Vite then returned the SPA
fallback (200 OK index.html with `text/html` content-type), which the
browser failed to parse as a JS module - silently, because the existing
`worker.onerror` handler only rejected pending tile promises (empty
during init).
Two fixes: (a) add a small middleware to the demo's vite.config.ts that
rewrites `.js?worker_file` -> `.ts?worker_file` when the .ts source
exists. Dev-only; prod build unaffected. (b) make `worker.onerror`,
`worker.messageerror`, and worker-side `error` messages reject the
`_workerReady` promise during init, instead of being dropped.
3. Abort didn't cancel a stuck model load. The abort signal was only
wired to `segment()`, not to `Cellpose.fromPretrained(...)`. If the
user hit Abort during the preload phase (fetch / IDB write / ORT
session create) nothing happened. Now: `FromPretrainedOptions.signal`
propagates into `_ensureWorker`, where an abort terminates the worker
and rejects the init promise with `AbortError`.
Bonus: new `FromPretrainedOptions.onStatus` callback. The worker now
posts pre-ready status strings ('worker module loaded', 'init message
received', 'configuring ORT', 'creating ORT session', 'session created
in <ms>'), and the demo logs each with timing info. Removes the
"fetch:100% then silence" mystery.
Verified end-to-end in Firefox: 462x346 image -> 6 tiles, 626 ms/tile
median, 75 masks, 4.26 s total.
0.2.0 is a pre-1.0 minor bump because `segment()` outputs can shift vs 0.1.1 (Math.trunc fix, preprocess reorder, niter scaling). New additive API surface: `FromPretrainedOptions.onStatus` and `signal` honored during preload. No type-level breaking changes. Consumers who pinned mask outputs in their snapshots should expect drift toward Python parity.
f0d6cdd to
4e631f9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Five corrections to Cellpose.js's port of cellpose (Python), surfaced by a fresh review against
cellpose/{transforms,dynamics,models,core}.py:models.py:_run_net; JS was resizing first, which changed the per-channel percentile statistics that drive normalization.OffscreenCanvas/HTMLCanvas. New impl uses OpenCV'sINTER_LINEARpixel-center mapping with edge replication. Also removes the canvas dependency, so the resize is now testable in Node.niterbyimage_scaling. Python computesniter = int(200 / (30/diameter)); JS was fixed at 200 regardless. Upscaled images get fewer steps, downscaled get more. Explicit user override still wins.Math.round→Math.truncinfollow_flows. PyTorch's.int()cast truncates toward zero; JS was rounding-half-up, shifting converged points by up to 1 pixel.cellprob_thresholdwas at the wrong nesting — it lives underdynamics: { cellprobThreshold }. Also removed a stale "once published in M6 follow-up" parenthetical (model is already on HF Hub).Quality impact
three_cells_192dynamics parity: 0.601 → 1.000 mean IoU (all 5 per-cell IoUs at 1.000). The Math.trunc fix alone closed the gap to Python entirely. Parity gate tightened from 0.55 → 0.99 to catch regressions.buildCpsamChannels,percentile,taperMask,followFlows,getMasks, the new bilineardiameterResize, and an end-to-end postprocess pipeline test that mocks the inference step.Test plan
npm run typecheck— cleannpm run lint— cleannpm run build— cleannpm run test— 61/61 passexamples/demo/, segment a real image, verify masks still render sensibly (this PR doesn't touch inference, but does change preprocess order and resize impl)