Skip to content

Safeguard against background removal pipeline precision issues #1255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
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
7 changes: 6 additions & 1 deletion src/pipelines.js
Original file line number Diff line number Diff line change
Expand Up @@ -2204,12 +2204,17 @@ export class ImageSegmentationPipeline extends (/** @type {new (options: ImagePi
/** @type {ImageSegmentationPipelineOutput[]} */
const annotation = [];
if (!subtask) {
// We define an epsilon to safeguard against numerical/precision issues when detecting
// the normalization mode of the output (i.e., sigmoid already applied, or not).
// See https://github.com/microsoft/onnxruntime/issues/23943 for more information.
const epsilon = 1e-5;

// Perform standard image segmentation
const result = output[outputNames[0]];
for (let i = 0; i < imageSizes.length; ++i) {
const size = imageSizes[i];
const item = result[i];
if (item.data.some(x => x < 0 || x > 1)) {
if (item.data.some(x => x < -epsilon || x > 1 + epsilon)) {
item.sigmoid_();
}
const mask = await RawImage.fromTensor(item.mul_(255).to('uint8')).resize(size[1], size[0]);
Expand Down