Upload folder using huggingface_hub
Browse files- README.md +8 -8
- app.js +8 -8
- index.html +2 -2
- segmenter.js +7 -7
- style.css +1 -1
README.md
CHANGED
|
@@ -26,7 +26,7 @@ pinned: false
|
|
| 26 |
- **Photo only**:不带任何标注的原图,用来对比
|
| 27 |
4. 查看分割结果表格和每个区域的覆盖率。
|
| 28 |
5. 保持图片不变,切换方法,结果会保留在下方的对比栏里,可以并排比较。
|
| 29 |
-
6. 对
|
| 30 |
|
| 31 |
### 四个实验
|
| 32 |
|
|
@@ -35,7 +35,7 @@ pinned: false
|
|
| 35 |
| 1 | Threshold(传统阈值) | Classic CV | 一条手写规则切出的前景像素,按连通域分组 |
|
| 36 |
| 2 | SegFormer | Semantic | 每个像素一个场景类别,不区分同类个体 |
|
| 37 |
| 3 | MaskFormer | Instance | 每个物体一张独立 mask,带类别和置信度 |
|
| 38 |
-
| 4 |
|
| 39 |
|
| 40 |
第 1 个实验包含三种阈值规则:手动阈值、Otsu 自动阈值(最大化类间方差)、自适应局部均值阈值(应对不均匀光照),外加形态学开闭运算去噪和二遍连通域标记。这部分不依赖任何模型,**断网也能跑**。
|
| 41 |
|
|
@@ -72,7 +72,7 @@ pinned: false
|
|
| 72 |
|
| 73 |
这是一个 Static Space,通过 Transformers.js(v4.3.0,从 jsDelivr CDN 加载)在浏览器内运行模型。Transformers.js 是**用到时才动态加载**的,所以页面秒开,而且第一个实验在完全离线的情况下依然可用。
|
| 74 |
|
| 75 |
-
每个模型第一次运行需要下载权重(SegFormer 最小,MaskFormer 最大),之后复用浏览器缓存。跑过一次
|
| 76 |
|
| 77 |
打开浏览器控制台时,第一次加载每个模型都会看到一条 `tokenizer_config.json` 的 404。这是正常的:Transformers.js 会探测这个可选文件,而这两个分割模型是纯视觉的,仓库里没有这个文件,库会跳过它继续用 `preprocessor_config.json`。它不影响任何结果。
|
| 78 |
|
|
@@ -93,7 +93,7 @@ pinned: false
|
|
| 93 |
- 四张方法卡片都能选中,右侧面板会跟着切换;
|
| 94 |
- 第一次未缓存运行时能显示进度条,而不是看起来卡住;
|
| 95 |
- 四种方法的结果都能出现在下方对比栏里;
|
| 96 |
-
-
|
| 97 |
- 窄屏 / 手机宽度(390 px)下页面没有横向溢出。
|
| 98 |
|
| 99 |
---
|
|
@@ -114,7 +114,7 @@ Object detection answers "where is this thing". Segmentation asks a harder quest
|
|
| 114 |
- **Photo only** — the untouched photo, for comparison
|
| 115 |
4. Inspect the result table and each region's coverage.
|
| 116 |
5. Keep the image fixed, switch methods, and compare them side by side in the strip below.
|
| 117 |
-
6. For
|
| 118 |
|
| 119 |
### The four experiments
|
| 120 |
|
|
@@ -123,7 +123,7 @@ Object detection answers "where is this thing". Segmentation asks a harder quest
|
|
| 123 |
| 1 | Threshold | Classic CV | Foreground pixels from one hand-written rule, grouped into connected blobs |
|
| 124 |
| 2 | SegFormer | Semantic | One scene class per pixel, with no notion of separate objects |
|
| 125 |
| 3 | MaskFormer | Instance | A separate mask per object, with class and confidence |
|
| 126 |
-
| 4 |
|
| 127 |
|
| 128 |
Experiment 1 offers three threshold rules: a manual cutoff, Otsu's automatic cutoff (maximising between-class variance), and an adaptive local-mean threshold for uneven lighting, plus morphological opening/closing cleanup and two-pass connected-component labelling. None of it touches a model, so **it works with no network at all**.
|
| 129 |
|
|
@@ -160,7 +160,7 @@ The bundled COCO examples give the classroom reliable starting inputs. COCO does
|
|
| 160 |
|
| 161 |
This is a Static Space. It runs models in the browser with Transformers.js (v4.3.0, loaded from the jsDelivr CDN). Transformers.js is imported **lazily, on first use**, so the page paints immediately and the first experiment keeps working completely offline.
|
| 162 |
|
| 163 |
-
The first run of each model downloads its weights — SegFormer is the smallest, MaskFormer the largest — and later runs reuse the browser cache. Once
|
| 164 |
|
| 165 |
If you open the browser console, the first load of each model logs a 404 for `tokenizer_config.json`. That is expected: Transformers.js probes for that optional file, and both segmentation checkpoints are vision-only, so the repo does not ship one and the library falls through to `preprocessor_config.json`. It does not affect any result.
|
| 166 |
|
|
@@ -181,5 +181,5 @@ If you copied `.gitattributes` over from `test/`, delete it. It has no `*.jpg` w
|
|
| 181 |
- all four method cards select, and the right-hand panel follows;
|
| 182 |
- the first uncached run shows a progress bar instead of appearing frozen;
|
| 183 |
- all four methods appear in the comparison strip;
|
| 184 |
-
- clicking the image under
|
| 185 |
- the page still works at a narrow/mobile width (390 px) with no horizontal overflow.
|
|
|
|
| 26 |
- **Photo only**:不带任何标注的原图,用来对比
|
| 27 |
4. 查看分割结果表格和每个区域的覆盖率。
|
| 28 |
5. 保持图片不变,切换方法,结果会保留在下方的对比栏里,可以并排比较。
|
| 29 |
+
6. 对 SlimSAM:先 Run 一次计算图像编码,然后在图上**任意点击**,它会分割光标下的物体。
|
| 30 |
|
| 31 |
### 四个实验
|
| 32 |
|
|
|
|
| 35 |
| 1 | Threshold(传统阈值) | Classic CV | 一条手写规则切出的前景像素,按连通域分组 |
|
| 36 |
| 2 | SegFormer | Semantic | 每个像素一个场景类别,不区分同类个体 |
|
| 37 |
| 3 | MaskFormer | Instance | 每个物体一张独立 mask,带类别和置信度 |
|
| 38 |
+
| 4 | SlimSAM | Promptable | 没有固定类别表,给一个点就切出那个物体 |
|
| 39 |
|
| 40 |
第 1 个实验包含三种阈值规则:手动阈值、Otsu 自动阈值(最大化类间方差)、自适应局部均值阈值(应对不均匀光照),外加形态学开闭运算去噪和二遍连通域标记。这部分不依赖任何模型,**断网也能跑**。
|
| 41 |
|
|
|
|
| 72 |
|
| 73 |
这是一个 Static Space,通过 Transformers.js(v4.3.0,从 jsDelivr CDN 加载)在浏览器内运行模型。Transformers.js 是**用到时才动态加载**的,所以页面秒开,而且第一个实验在完全离线的情况下依然可用。
|
| 74 |
|
| 75 |
+
每个模型第一次运行需要下载权重(SegFormer 最小,MaskFormer 最大),之后复用浏览器缓存。跑过一次 SlimSAM 的图像编码后,后续每次点击几乎是即时的——因为编码器只跑一次,点击只是拿缓存的 embedding 做一次很轻的解码。这一点本身就是 SlimSAM 的设计要点。
|
| 76 |
|
| 77 |
打开浏览器控制台时,第一次加载每个模型都会看到一条 `tokenizer_config.json` 的 404。这是正常的:Transformers.js 会探测这个可选文件,而这两个分割模型是纯视觉的,仓库里没有这个文件,库会跳过它继续用 `preprocessor_config.json`。它不影响任何结果。
|
| 78 |
|
|
|
|
| 93 |
- 四张方法卡片都能选中,右侧面板会跟着切换;
|
| 94 |
- 第一次未缓存运行时能显示进度条,而不是看起来卡住;
|
| 95 |
- 四种方法的结果都能出现在下方对比栏里;
|
| 96 |
+
- SlimSAM 点击图片后能返回 3 个候选 mask;
|
| 97 |
- 窄屏 / 手机宽度(390 px)下页面没有横向溢出。
|
| 98 |
|
| 99 |
---
|
|
|
|
| 114 |
- **Photo only** — the untouched photo, for comparison
|
| 115 |
4. Inspect the result table and each region's coverage.
|
| 116 |
5. Keep the image fixed, switch methods, and compare them side by side in the strip below.
|
| 117 |
+
6. For SlimSAM: run once to compute the image embedding, then **click anywhere** on the picture to segment the object under the cursor.
|
| 118 |
|
| 119 |
### The four experiments
|
| 120 |
|
|
|
|
| 123 |
| 1 | Threshold | Classic CV | Foreground pixels from one hand-written rule, grouped into connected blobs |
|
| 124 |
| 2 | SegFormer | Semantic | One scene class per pixel, with no notion of separate objects |
|
| 125 |
| 3 | MaskFormer | Instance | A separate mask per object, with class and confidence |
|
| 126 |
+
| 4 | SlimSAM | Promptable | No fixed class list — give it a point, it cuts out that object |
|
| 127 |
|
| 128 |
Experiment 1 offers three threshold rules: a manual cutoff, Otsu's automatic cutoff (maximising between-class variance), and an adaptive local-mean threshold for uneven lighting, plus morphological opening/closing cleanup and two-pass connected-component labelling. None of it touches a model, so **it works with no network at all**.
|
| 129 |
|
|
|
|
| 160 |
|
| 161 |
This is a Static Space. It runs models in the browser with Transformers.js (v4.3.0, loaded from the jsDelivr CDN). Transformers.js is imported **lazily, on first use**, so the page paints immediately and the first experiment keeps working completely offline.
|
| 162 |
|
| 163 |
+
The first run of each model downloads its weights — SegFormer is the smallest, MaskFormer the largest — and later runs reuse the browser cache. Once SlimSAM's image encoder has run for an image, every following click is nearly instant, because the encoder runs once and a click is only a cheap decode against the cached embedding. That is the design point of SlimSAM, and the caching here is what makes it visible.
|
| 164 |
|
| 165 |
If you open the browser console, the first load of each model logs a 404 for `tokenizer_config.json`. That is expected: Transformers.js probes for that optional file, and both segmentation checkpoints are vision-only, so the repo does not ship one and the library falls through to `preprocessor_config.json`. It does not affect any result.
|
| 166 |
|
|
|
|
| 181 |
- all four method cards select, and the right-hand panel follows;
|
| 182 |
- the first uncached run shows a progress bar instead of appearing frozen;
|
| 183 |
- all four methods appear in the comparison strip;
|
| 184 |
+
- clicking the image under SlimSAM returns 3 candidate masks;
|
| 185 |
- the page still works at a narrow/mobile width (390 px) with no horizontal overflow.
|
app.js
CHANGED
|
@@ -234,7 +234,7 @@ function paintMask(overlay, region, alpha) {
|
|
| 234 |
|
| 235 |
for (let x = 0; x < width; x++) {
|
| 236 |
const maskX = Math.min(maskWidth - 1, (x * stepX) | 0);
|
| 237 |
-
// Truthiness, not a 0/255 test: classic.js and
|
| 238 |
// the pipelines emit 0/255, and summarizeMask already reads them this way.
|
| 239 |
if (!mask[maskRow + maskX]) continue;
|
| 240 |
|
|
@@ -932,17 +932,17 @@ async function runOnce() {
|
|
| 932 |
}
|
| 933 |
|
| 934 |
if (experiment.kind === 'promptable') {
|
| 935 |
-
statusEl.textContent = 'Computing the
|
| 936 |
state.samHandle = await prepareSam(state.working.dataUrl, { progressCallback: setProgress });
|
| 937 |
state.samResult = null;
|
| 938 |
|
| 939 |
drawBase();
|
| 940 |
-
resetResult('
|
| 941 |
resultKicker.textContent = experiment.family;
|
| 942 |
-
resultTitle.textContent = '
|
| 943 |
samHint.hidden = false;
|
| 944 |
canvas.classList.add('clickable');
|
| 945 |
-
setBadge('
|
| 946 |
statusEl.textContent = 'Click anywhere on the image. Each click is a new prompt.';
|
| 947 |
}
|
| 948 |
|
|
@@ -986,14 +986,14 @@ async function handleCanvasClick(event) {
|
|
| 986 |
updatePromptableSummary(result, 0);
|
| 987 |
|
| 988 |
resultKicker.textContent = 'Promptable';
|
| 989 |
-
resultTitle.textContent = '
|
| 990 |
recordCompare('sam', `Clicked (${result.point.x}, ${result.point.y}) — best IoU ${result.candidates[0].score.toFixed(3)}.`);
|
| 991 |
|
| 992 |
setBadge(`${seconds}s`, 'ready');
|
| 993 |
-
statusEl.textContent = `
|
| 994 |
} catch (error) {
|
| 995 |
console.error(error);
|
| 996 |
-
statusEl.textContent = '
|
| 997 |
setBadge('Error', 'error');
|
| 998 |
} finally {
|
| 999 |
state.busy = false;
|
|
|
|
| 234 |
|
| 235 |
for (let x = 0; x < width; x++) {
|
| 236 |
const maskX = Math.min(maskWidth - 1, (x * stepX) | 0);
|
| 237 |
+
// Truthiness, not a 0/255 test: classic.js and SlimSAM emit 0/1 masks while
|
| 238 |
// the pipelines emit 0/255, and summarizeMask already reads them this way.
|
| 239 |
if (!mask[maskRow + maskX]) continue;
|
| 240 |
|
|
|
|
| 932 |
}
|
| 933 |
|
| 934 |
if (experiment.kind === 'promptable') {
|
| 935 |
+
statusEl.textContent = 'Computing the SlimSAM image embedding. The first run downloads model files.';
|
| 936 |
state.samHandle = await prepareSam(state.working.dataUrl, { progressCallback: setProgress });
|
| 937 |
state.samResult = null;
|
| 938 |
|
| 939 |
drawBase();
|
| 940 |
+
resetResult('SlimSAM is ready. Click the image to segment an object.');
|
| 941 |
resultKicker.textContent = experiment.family;
|
| 942 |
+
resultTitle.textContent = 'SlimSAM — click to segment';
|
| 943 |
samHint.hidden = false;
|
| 944 |
canvas.classList.add('clickable');
|
| 945 |
+
setBadge('SlimSAM ready', 'ready');
|
| 946 |
statusEl.textContent = 'Click anywhere on the image. Each click is a new prompt.';
|
| 947 |
}
|
| 948 |
|
|
|
|
| 986 |
updatePromptableSummary(result, 0);
|
| 987 |
|
| 988 |
resultKicker.textContent = 'Promptable';
|
| 989 |
+
resultTitle.textContent = 'SlimSAM — 3 candidate masks';
|
| 990 |
recordCompare('sam', `Clicked (${result.point.x}, ${result.point.y}) — best IoU ${result.candidates[0].score.toFixed(3)}.`);
|
| 991 |
|
| 992 |
setBadge(`${seconds}s`, 'ready');
|
| 993 |
+
statusEl.textContent = `SlimSAM answered in ${seconds}s. Click again for a different object.`;
|
| 994 |
} catch (error) {
|
| 995 |
console.error(error);
|
| 996 |
+
statusEl.textContent = 'SlimSAM failed on that point. Try another spot.';
|
| 997 |
setBadge('Error', 'error');
|
| 998 |
} finally {
|
| 999 |
state.busy = false;
|
index.html
CHANGED
|
@@ -54,7 +54,7 @@
|
|
| 54 |
|
| 55 |
<span>
|
| 56 |
<b>Methods</b>
|
| 57 |
-
Threshold · SegFormer · MaskFormer ·
|
| 58 |
</span>
|
| 59 |
</div>
|
| 60 |
|
|
@@ -434,7 +434,7 @@
|
|
| 434 |
here disagree so much. Thresholding uses one hand-chosen rule about
|
| 435 |
brightness. SegFormer labels every pixel with a scene class but cannot
|
| 436 |
tell two people apart. MaskFormer predicts one mask per object.
|
| 437 |
-
|
| 438 |
under that point, without any fixed class list.
|
| 439 |
</p>
|
| 440 |
|
|
|
|
| 54 |
|
| 55 |
<span>
|
| 56 |
<b>Methods</b>
|
| 57 |
+
Threshold · SegFormer · MaskFormer · SlimSAM
|
| 58 |
</span>
|
| 59 |
</div>
|
| 60 |
|
|
|
|
| 434 |
here disagree so much. Thresholding uses one hand-chosen rule about
|
| 435 |
brightness. SegFormer labels every pixel with a scene class but cannot
|
| 436 |
tell two people apart. MaskFormer predicts one mask per object.
|
| 437 |
+
SlimSAM takes a click as its question and returns whatever object sits
|
| 438 |
under that point, without any fixed class list.
|
| 439 |
</p>
|
| 440 |
|
segmenter.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
* Three pretrained models, one per rung of the segmentation ladder:
|
| 5 |
* semantic - SegFormer: "every pixel gets a class", no notion of instances
|
| 6 |
* instance - MaskFormer: "each object gets its own mask"
|
| 7 |
-
* promptable -
|
| 8 |
*
|
| 9 |
* The traditional thresholding baseline lives in classic.js and needs no model.
|
| 10 |
*
|
|
@@ -72,11 +72,11 @@ export const EXPERIMENTS = [
|
|
| 72 |
},
|
| 73 |
{
|
| 74 |
key: 'sam',
|
| 75 |
-
label: '
|
| 76 |
family: 'Promptable',
|
| 77 |
tagline: 'Tell it where, it cuts it out',
|
| 78 |
blurb:
|
| 79 |
-
'Segment Anything. It has no fixed list of classes: you click a point, and it returns the object under that point.',
|
| 80 |
note: 'After Run, click anywhere on the image. Each click is a fresh prompt.',
|
| 81 |
kind: 'promptable',
|
| 82 |
},
|
|
@@ -216,7 +216,7 @@ export async function runInstance(dataUrl, options = {}) {
|
|
| 216 |
}
|
| 217 |
|
| 218 |
/* ------------------------------------------------------------------ *
|
| 219 |
-
*
|
| 220 |
* ------------------------------------------------------------------ */
|
| 221 |
|
| 222 |
let samBundle = null;
|
|
@@ -238,11 +238,11 @@ async function loadSam(progressCallback) {
|
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
-
* Run
|
| 242 |
*
|
| 243 |
* The encoder is the expensive half (1024x1024 through a ViT). Caching its
|
| 244 |
* output is what makes clicking around the image feel instant, and it is also a
|
| 245 |
-
* neat illustration of how
|
| 246 |
* cheap prompt decodes.
|
| 247 |
*/
|
| 248 |
export async function prepareSam(dataUrl, options = {}) {
|
|
@@ -259,7 +259,7 @@ export async function prepareSam(dataUrl, options = {}) {
|
|
| 259 |
/**
|
| 260 |
* One point prompt -> three candidate masks, ordered by predicted IoU.
|
| 261 |
*
|
| 262 |
-
*
|
| 263 |
* (the whole crowd, or just the one person). Surfacing all three is the most
|
| 264 |
* honest way to show what "promptable" actually means.
|
| 265 |
*/
|
|
|
|
| 4 |
* Three pretrained models, one per rung of the segmentation ladder:
|
| 5 |
* semantic - SegFormer: "every pixel gets a class", no notion of instances
|
| 6 |
* instance - MaskFormer: "each object gets its own mask"
|
| 7 |
+
* promptable - SlimSAM: "tell me where, and I will cut out that thing"
|
| 8 |
*
|
| 9 |
* The traditional thresholding baseline lives in classic.js and needs no model.
|
| 10 |
*
|
|
|
|
| 72 |
},
|
| 73 |
{
|
| 74 |
key: 'sam',
|
| 75 |
+
label: 'SlimSAM',
|
| 76 |
family: 'Promptable',
|
| 77 |
tagline: 'Tell it where, it cuts it out',
|
| 78 |
blurb:
|
| 79 |
+
'A slimmed-down Segment Anything. It has no fixed list of classes: you click a point, and it returns the object under that point.',
|
| 80 |
note: 'After Run, click anywhere on the image. Each click is a fresh prompt.',
|
| 81 |
kind: 'promptable',
|
| 82 |
},
|
|
|
|
| 216 |
}
|
| 217 |
|
| 218 |
/* ------------------------------------------------------------------ *
|
| 219 |
+
* SlimSAM (promptable)
|
| 220 |
* ------------------------------------------------------------------ */
|
| 221 |
|
| 222 |
let samBundle = null;
|
|
|
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
+
* Run SlimSAM's vision encoder once per image and keep the embeddings.
|
| 242 |
*
|
| 243 |
* The encoder is the expensive half (1024x1024 through a ViT). Caching its
|
| 244 |
* output is what makes clicking around the image feel instant, and it is also a
|
| 245 |
+
* neat illustration of how SlimSAM is actually built: one image embedding, many
|
| 246 |
* cheap prompt decodes.
|
| 247 |
*/
|
| 248 |
export async function prepareSam(dataUrl, options = {}) {
|
|
|
|
| 259 |
/**
|
| 260 |
* One point prompt -> three candidate masks, ordered by predicted IoU.
|
| 261 |
*
|
| 262 |
+
* SlimSAM is trained to return several plausible readings of an ambiguous click
|
| 263 |
* (the whole crowd, or just the one person). Surfacing all three is the most
|
| 264 |
* honest way to show what "promptable" actually means.
|
| 265 |
*/
|
style.css
CHANGED
|
@@ -502,7 +502,7 @@ h1 {
|
|
| 502 |
|
| 503 |
/*
|
| 504 |
* Size the canvas by its intrinsic aspect ratio, never by a fixed box: the
|
| 505 |
-
* element box must match the painted image exactly, because
|
| 506 |
* click through getBoundingClientRect() into image coordinates. A letterboxed
|
| 507 |
* box (from `width:100%` + `object-fit:contain`) would offset every click on a
|
| 508 |
* portrait photo. max-height only caps how tall a portrait frame can get.
|
|
|
|
| 502 |
|
| 503 |
/*
|
| 504 |
* Size the canvas by its intrinsic aspect ratio, never by a fixed box: the
|
| 505 |
+
* element box must match the painted image exactly, because SlimSAM converts a
|
| 506 |
* click through getBoundingClientRect() into image coordinates. A letterboxed
|
| 507 |
* box (from `width:100%` + `object-fit:contain`) would offset every click on a
|
| 508 |
* portrait photo. max-height only caps how tall a portrait frame can get.
|