File size: 4,917 Bytes
69c77fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | // Pre- and post-processing of the polyp U-Net, ported from src/polypseg (no DOM, so Node can
// run it too: scripts/web_parity checks it against the Python pipeline).
export const SIZE = 320;
export const THRESHOLD = 0.5;
// OpenCV INTER_AREA downscale weights along one axis: each output pixel averages the source
// span it covers, with fractional weights at the edges.
function areaTable(src, dst) {
const scale = src / dst;
const tab = [];
for (let d = 0; d < dst; d++) {
const f0 = d * scale;
const f1 = f0 + scale;
const s0 = Math.ceil(f0);
const s1 = Math.floor(f1);
const taps = [];
if (s0 - f0 > 1e-3) taps.push([s0 - 1, (s0 - f0) / scale]);
for (let s = s0; s < s1; s++) taps.push([s, 1 / scale]);
if (f1 - s1 > 1e-3 && s1 < src) taps.push([s1, Math.min(1, f1 - s1) / scale]);
tab.push(taps);
}
return tab;
}
// OpenCV INTER_LINEAR weights along one axis (half-pixel centres, edges clamped).
function linearTable(src, dst) {
const scale = src / dst;
const tab = [];
for (let d = 0; d < dst; d++) {
const f = (d + 0.5) * scale - 0.5;
let s = Math.floor(f);
let t = f - s;
if (s < 0) { s = 0; t = 0; }
if (s >= src - 1) { s = src - 1; t = 0; }
tab.push(t === 0 ? [[s, 1]] : [[s, 1 - t], [s + 1, t]]);
}
return tab;
}
// Separable resize of interleaved 8-bit pixels (RGB or RGBA) to an RGB uint8 SIZE x SIZE
// image, rounded like cv2.resize: INTER_AREA when shrinking (every Kvasir frame), bilinear
// for small uploads.
export function resizeToInput(pixels, srcW, srcH, channels) {
const shrink = srcW >= SIZE && srcH >= SIZE;
const xt = shrink ? areaTable(srcW, SIZE) : linearTable(srcW, SIZE);
const yt = shrink ? areaTable(srcH, SIZE) : linearTable(srcH, SIZE);
const rows = new Float64Array(SIZE * 3);
const out = new Uint8Array(SIZE * SIZE * 3);
for (let dy = 0; dy < SIZE; dy++) {
const acc = new Float64Array(SIZE * 3);
for (const [sy, wy] of yt[dy]) {
const base = sy * srcW * channels;
for (let dx = 0; dx < SIZE; dx++) {
let r = 0, g = 0, b = 0;
for (const [sx, wx] of xt[dx]) {
const i = base + sx * channels;
r += pixels[i] * wx;
g += pixels[i + 1] * wx;
b += pixels[i + 2] * wx;
}
rows[dx * 3] = r;
rows[dx * 3 + 1] = g;
rows[dx * 3 + 2] = b;
}
for (let k = 0; k < acc.length; k++) acc[k] += rows[k] * wy;
}
for (let k = 0; k < acc.length; k++) {
out[dy * SIZE * 3 + k] = Math.min(255, Math.max(0, Math.round(acc[k])));
}
}
return out;
}
// Resized RGB uint8 (SIZE, SIZE, 3) -> CHW float32 in [0, 1]: the exported graph's input.
export function toInput(rgb) {
const plane = SIZE * SIZE;
const out = new Float32Array(3 * plane);
for (let i = 0; i < plane; i++) {
out[i] = rgb[i * 3] / 255;
out[plane + i] = rgb[i * 3 + 1] / 255;
out[2 * plane + i] = rgb[i * 3 + 2] / 255;
}
return out;
}
// SIZE x SIZE float map -> (w, h) with cv2.INTER_LINEAR, as PolypSegmenter does.
export function upsample(map, w, h) {
const xt = linearTable(SIZE, w);
const yt = linearTable(SIZE, h);
const out = new Float32Array(w * h);
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
let v = 0;
for (const [sy, wy] of yt[y]) {
for (const [sx, wx] of xt[x]) v += map[sy * SIZE + sx] * wx * wy;
}
out[y * w + x] = v;
}
}
return out;
}
export function binarize(probs, threshold = THRESHOLD) {
const mask = new Uint8Array(probs.length);
for (let i = 0; i < probs.length; i++) mask[i] = probs[i] >= threshold ? 1 : 0;
return mask;
}
// Mask pixels with a 4-neighbour outside the mask: the outline drawn on the image.
export function outline(mask, w, h) {
const edge = new Uint8Array(mask.length);
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const i = y * w + x;
if (!mask[i]) continue;
if (x === 0 || y === 0 || x === w - 1 || y === h - 1 || !mask[i - 1] || !mask[i + 1]
|| !mask[i - w] || !mask[i + w]) edge[i] = 1;
}
}
return edge;
}
// Full deterministic pipeline: pixels -> {probs, mask} at the input resolution.
export async function segment(session, ort, pixels, w, h, channels) {
const input = toInput(resizeToInput(pixels, w, h, channels));
const out = await session.run({ image: new ort.Tensor("float32", input, [1, 3, SIZE, SIZE]) });
const probs = upsample(out.probs.data, w, h);
return { probs, mask: binarize(probs) };
}
// MC-dropout graph: pixels -> {mean, std} at the input resolution.
export async function segmentMC(session, ort, pixels, w, h, channels) {
const input = toInput(resizeToInput(pixels, w, h, channels));
const out = await session.run({ image: new ort.Tensor("float32", input, [1, 3, SIZE, SIZE]) });
return { mean: upsample(out.mean.data, w, h), std: upsample(out.std.data, w, h) };
}
|