Skip to content

fix python parity in preprocess + dynamics; expand test coverage#8

Merged
belkassaby merged 5 commits into
mainfrom
fix/python-parity-improvements
May 23, 2026
Merged

fix python parity in preprocess + dynamics; expand test coverage#8
belkassaby merged 5 commits into
mainfrom
fix/python-parity-improvements

Conversation

@belkassaby

@belkassaby belkassaby commented May 23, 2026

Copy link
Copy Markdown
Owner

Summary

Five corrections to Cellpose.js's port of cellpose (Python), surfaced by a fresh review against cellpose/{transforms,dynamics,models,core}.py:

  1. Preprocess order: normalize FIRST, then resize. Python normalizes before resizing in models.py:_run_net; JS was resizing first, which changed the per-channel percentile statistics that drive normalization.
  2. Replace canvas resize with pure-JS Float32 bilinear. The old path quantized each channel to uint8 (~1/255 per-pixel error) and required OffscreenCanvas/HTMLCanvas. New impl uses OpenCV's INTER_LINEAR pixel-center mapping with edge replication. Also removes the canvas dependency, so the resize is now testable in Node.
  3. Scale niter by image_scaling. Python computes niter = int(200 / (30/diameter)); JS was fixed at 200 regardless. Upscaled images get fewer steps, downscaled get more. Explicit user override still wins.
  4. Math.roundMath.trunc in follow_flows. PyTorch's .int() cast truncates toward zero; JS was rounding-half-up, shifting converged points by up to 1 pixel.
  5. README example fix. cellprob_threshold was at the wrong nesting — it lives under dynamics: { cellprobThreshold }. Also removed a stale "once published in M6 follow-up" parenthetical (model is already on HF Hub).

Quality impact

  • three_cells_192 dynamics 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.
  • 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.

Test plan

  • npm run typecheck — clean
  • npm run lint — clean
  • npm run build — clean
  • npm run test — 61/61 pass
  • Browser smoke test: load the demo at examples/demo/, segment a real image, verify masks still render sensibly (this PR doesn't touch inference, but does change preprocess order and resize impl)

belkassaby and others added 4 commits May 22, 2026 21:04
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.
@belkassaby belkassaby force-pushed the fix/python-parity-improvements branch from f0d6cdd to 4e631f9 Compare May 23, 2026 02:10
@belkassaby belkassaby merged commit db4170c into main May 23, 2026
2 checks passed
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.

1 participant