Skip to content

Commit 2c9991b

Browse files
authored
Account for numerical instability in background removal pipeline pre-normalization (#1255)
1 parent dde0b49 commit 2c9991b

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/pipelines.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2204,12 +2204,17 @@ export class ImageSegmentationPipeline extends (/** @type {new (options: ImagePi
22042204
/** @type {ImageSegmentationPipelineOutput[]} */
22052205
const annotation = [];
22062206
if (!subtask) {
2207+
// We define an epsilon to safeguard against numerical/precision issues when detecting
2208+
// the normalization mode of the output (i.e., sigmoid already applied, or not).
2209+
// See https://github.com/microsoft/onnxruntime/issues/23943 for more information.
2210+
const epsilon = 1e-5;
2211+
22072212
// Perform standard image segmentation
22082213
const result = output[outputNames[0]];
22092214
for (let i = 0; i < imageSizes.length; ++i) {
22102215
const size = imageSizes[i];
22112216
const item = result[i];
2212-
if (item.data.some(x => x < 0 || x > 1)) {
2217+
if (item.data.some(x => x < -epsilon || x > 1 + epsilon)) {
22132218
item.sigmoid_();
22142219
}
22152220
const mask = await RawImage.fromTensor(item.mul_(255).to('uint8')).resize(size[1], size[0]);

0 commit comments

Comments
 (0)