2026-01-15 20:10:19 +08:00
|
|
|
|
<!--
|
|
|
|
|
|
PatchifyDemo.vue
|
|
|
|
|
|
视觉分词(Patchify)演示
|
|
|
|
|
|
-->
|
|
|
|
|
|
<template>
|
|
|
|
|
|
<div class="patchify-demo">
|
|
|
|
|
|
<div class="control-panel">
|
|
|
|
|
|
<div class="controls">
|
2026-01-18 12:21:49 +08:00
|
|
|
|
<button
|
|
|
|
|
|
class="action-btn"
|
2026-01-18 10:24:35 +08:00
|
|
|
|
:disabled="currentStep === 0"
|
2026-02-18 17:38:10 +08:00
|
|
|
|
@click="prevStep"
|
2026-01-18 10:24:35 +08:00
|
|
|
|
>
|
|
|
|
|
|
⬅ 上一步 (Prev)
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</button>
|
2026-01-18 10:24:35 +08:00
|
|
|
|
<span class="step-indicator">Step {{ currentStep + 1 }} / 4</span>
|
2026-01-18 12:21:49 +08:00
|
|
|
|
<button
|
|
|
|
|
|
class="action-btn primary"
|
2026-01-18 10:24:35 +08:00
|
|
|
|
:disabled="currentStep === 3"
|
2026-02-18 17:38:10 +08:00
|
|
|
|
@click="nextStep"
|
2026-01-18 10:24:35 +08:00
|
|
|
|
>
|
|
|
|
|
|
{{ currentStep === 3 ? '完成 (Done)' : '下一步 (Next) ➡' }}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="step-desc">
|
|
|
|
|
|
{{ stepDescriptions[currentStep] }}
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="visual-area">
|
|
|
|
|
|
<!-- 原始/切分视图容器 -->
|
2026-01-18 10:24:35 +08:00
|
|
|
|
<!--
|
|
|
|
|
|
Step 0: Show container background, cells hidden
|
|
|
|
|
|
Step 1: Show container background, grid overlay visible (cells with border)
|
|
|
|
|
|
Step 2+: Container background hidden, cells visible with individual backgrounds
|
|
|
|
|
|
-->
|
2026-01-18 12:21:49 +08:00
|
|
|
|
<div
|
|
|
|
|
|
class="image-container"
|
|
|
|
|
|
:class="{
|
2026-01-18 10:24:35 +08:00
|
|
|
|
'is-pixelated': currentStep >= 1,
|
2026-01-18 12:21:49 +08:00
|
|
|
|
'is-patchified': currentStep >= 2
|
2026-01-18 10:24:35 +08:00
|
|
|
|
}"
|
|
|
|
|
|
>
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div
|
|
|
|
|
|
v-if="currentStep === 1"
|
|
|
|
|
|
class="grid-overlay"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<div
|
|
|
|
|
|
v-for="n in 196"
|
|
|
|
|
|
:key="n"
|
|
|
|
|
|
class="patch"
|
|
|
|
|
|
:style="getPatchStyle(n)"
|
|
|
|
|
|
>
|
2026-01-18 10:24:35 +08:00
|
|
|
|
<!-- Show number only in Pixelated stage to represent 'digitization' -->
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<span
|
|
|
|
|
|
v-if="currentStep === 1"
|
|
|
|
|
|
class="pixel-val"
|
|
|
|
|
|
>{{
|
2026-01-18 12:21:49 +08:00
|
|
|
|
Math.floor(Math.random() * 9)
|
|
|
|
|
|
}}</span>
|
2026-01-18 10:24:35 +08:00
|
|
|
|
<!-- Show ID in Patchified stage -->
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<span
|
|
|
|
|
|
v-if="currentStep >= 2"
|
|
|
|
|
|
class="patch-id"
|
|
|
|
|
|
>{{ n }}</span>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div
|
|
|
|
|
|
v-if="currentStep >= 3"
|
|
|
|
|
|
class="arrow-down"
|
|
|
|
|
|
>
|
|
|
|
|
|
⬇
|
|
|
|
|
|
</div>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
|
2026-01-15 20:10:19 +08:00
|
|
|
|
<!-- 线性序列视图 -->
|
2026-02-18 17:38:10 +08:00
|
|
|
|
<div
|
|
|
|
|
|
v-if="currentStep >= 3"
|
|
|
|
|
|
class="sequence-container"
|
|
|
|
|
|
>
|
2026-01-18 12:21:49 +08:00
|
|
|
|
<div class="sequence-label">
|
|
|
|
|
|
Token Sequence: 196×D (每个 Token 是 D 维向量)
|
|
|
|
|
|
</div>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
<div class="token-stream">
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div
|
|
|
|
|
|
v-for="n in 196"
|
|
|
|
|
|
:key="n"
|
2026-01-15 20:10:19 +08:00
|
|
|
|
class="mini-patch"
|
2026-01-18 10:24:35 +08:00
|
|
|
|
:style="getMiniPatchStyle(n)"
|
2026-02-18 17:38:10 +08:00
|
|
|
|
/>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2026-01-18 10:24:35 +08:00
|
|
|
|
import { ref, computed } from 'vue'
|
|
|
|
|
|
|
|
|
|
|
|
const currentStep = ref(0)
|
2026-01-15 20:10:19 +08:00
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
const stepDescriptions = [
|
2026-01-18 12:21:49 +08:00
|
|
|
|
'1. 原始图片 (Original Image): 计算机看到的原始输入。',
|
|
|
|
|
|
'2. 数字化 (Digitization): 图片本质上是一个数字矩阵 (H x W x C)。',
|
|
|
|
|
|
'3. 切块 (Patchify): 典型设置:224×224 按 16×16 切成 14×14=196 个 Patch(此处等比示意)。',
|
|
|
|
|
|
'4. 序列化 (Serialize): 将二维分布的 Patch “拍扁”成一维序列 (Spatial Flatten)。现在它看起来就像一串“视觉单词”,可以被 Transformer 逐个读取。'
|
2026-01-18 10:24:35 +08:00
|
|
|
|
]
|
2026-01-15 20:10:19 +08:00
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
const nextStep = () => {
|
|
|
|
|
|
if (currentStep.value < 3) currentStep.value++
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const prevStep = () => {
|
|
|
|
|
|
if (currentStep.value > 0) currentStep.value--
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟一张风景图的 CSS 渐变
|
|
|
|
|
|
// Sky (Blue) -> Mountains (Green/Grey) -> Sun (Yellow)
|
2026-01-18 12:21:49 +08:00
|
|
|
|
const bgImage =
|
|
|
|
|
|
'linear-gradient(to bottom, #87CEEB 0%, #87CEEB 50%, #228B22 50%, #228B22 100%)'
|
2026-01-18 10:24:35 +08:00
|
|
|
|
// Add a sun using radial gradient
|
2026-01-18 12:21:49 +08:00
|
|
|
|
const complexBg =
|
|
|
|
|
|
'radial-gradient(circle at 70% 20%, #FFD700 0%, #FFD700 10%, transparent 10.5%), linear-gradient(to bottom, #87CEEB 0%, #87CEEB 60%, #4CA1AF 60%, #2C3E50 100%)'
|
2026-01-18 10:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
const getPatchStyle = (n) => {
|
|
|
|
|
|
const row = Math.floor((n - 1) / 14)
|
|
|
|
|
|
const col = (n - 1) % 14
|
2026-01-18 12:21:49 +08:00
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
// Calculate background position for each patch to match the original image
|
|
|
|
|
|
// The container is 280px, each patch is 20px.
|
|
|
|
|
|
// 14 cols.
|
|
|
|
|
|
const posX = col * -20
|
|
|
|
|
|
const posY = row * -20
|
2026-01-18 12:21:49 +08:00
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
const isPatchified = currentStep.value >= 2
|
2026-01-18 12:21:49 +08:00
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
return {
|
|
|
|
|
|
backgroundImage: complexBg,
|
|
|
|
|
|
backgroundPosition: `${posX}px ${posY}px`,
|
|
|
|
|
|
backgroundSize: '280px 280px',
|
|
|
|
|
|
// In Step 0, patches are hidden to show pure container background
|
|
|
|
|
|
// In Step 1, patches are visible but transparent background to show numbers/borders over container background
|
|
|
|
|
|
// In Step 2, patches take over with their own background
|
|
|
|
|
|
opacity: currentStep.value === 0 ? 0 : 1,
|
|
|
|
|
|
// In Step 1, background must be transparent to see container bg
|
2026-01-18 12:21:49 +08:00
|
|
|
|
backgroundImage: isPatchified ? complexBg : 'none',
|
2026-01-18 10:24:35 +08:00
|
|
|
|
transform: isPatchified ? 'scale(0.9)' : 'scale(1)',
|
2026-01-18 12:21:49 +08:00
|
|
|
|
transition: 'all 0.5s ease'
|
2026-01-18 10:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const getMiniPatchStyle = (n) => {
|
|
|
|
|
|
const row = Math.floor((n - 1) / 14)
|
|
|
|
|
|
const col = (n - 1) % 14
|
|
|
|
|
|
const posX = col * -20
|
|
|
|
|
|
const posY = row * -20
|
2026-01-18 12:21:49 +08:00
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
return {
|
|
|
|
|
|
backgroundImage: complexBg,
|
|
|
|
|
|
backgroundPosition: `${posX}px ${posY}px`,
|
2026-01-18 12:21:49 +08:00
|
|
|
|
backgroundSize: '280px 280px'
|
2026-01-18 10:24:35 +08:00
|
|
|
|
}
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.patchify-demo {
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-02-14 20:23:34 +08:00
|
|
|
|
border-radius: 6px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
padding: 20px;
|
|
|
|
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
|
|
margin: 20px 0;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
user-select: none;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.control-panel {
|
|
|
|
|
|
margin-bottom: 20px;
|
|
|
|
|
|
display: flex;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 15px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.controls {
|
|
|
|
|
|
display: flex;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
gap: 15px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
align-items: center;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
.step-indicator {
|
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.step-desc {
|
|
|
|
|
|
font-size: 0.9em;
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
text-align: center;
|
|
|
|
|
|
background: var(--vp-c-bg-mute);
|
2026-01-15 20:10:19 +08:00
|
|
|
|
padding: 8px 16px;
|
|
|
|
|
|
border-radius: 4px;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
min-height: 40px;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.action-btn {
|
|
|
|
|
|
background: var(--vp-c-bg-mute);
|
|
|
|
|
|
color: var(--vp-c-text-1);
|
|
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
|
|
|
|
|
padding: 6px 12px;
|
|
|
|
|
|
border-radius: 4px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
cursor: pointer;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
transition: all 0.2s;
|
|
|
|
|
|
font-size: 0.9em;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
.action-btn.primary {
|
|
|
|
|
|
background: var(--vp-c-brand);
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
border-color: var(--vp-c-brand);
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
.action-btn:disabled {
|
|
|
|
|
|
opacity: 0.5;
|
|
|
|
|
|
cursor: not-allowed;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.action-btn:not(:disabled):hover {
|
|
|
|
|
|
opacity: 0.8;
|
|
|
|
|
|
transform: translateY(-1px);
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.visual-area {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
gap: 20px;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
min-height: 350px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.image-container {
|
|
|
|
|
|
display: grid;
|
|
|
|
|
|
grid-template-columns: repeat(14, 1fr);
|
|
|
|
|
|
width: 280px;
|
|
|
|
|
|
height: 280px;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
/* Step 0 & 1 Background */
|
2026-01-18 12:21:49 +08:00
|
|
|
|
background-image:
|
|
|
|
|
|
radial-gradient(
|
|
|
|
|
|
circle at 70% 20%,
|
|
|
|
|
|
#ffd700 0%,
|
|
|
|
|
|
#ffd700 10%,
|
|
|
|
|
|
transparent 10.5%
|
|
|
|
|
|
),
|
|
|
|
|
|
linear-gradient(
|
|
|
|
|
|
to bottom,
|
|
|
|
|
|
#87ceeb 0%,
|
|
|
|
|
|
#87ceeb 60%,
|
|
|
|
|
|
#4ca1af 60%,
|
|
|
|
|
|
#2c3e50 100%
|
|
|
|
|
|
);
|
2026-01-18 10:24:35 +08:00
|
|
|
|
position: relative;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
transition: all 0.5s ease;
|
2026-01-18 12:21:49 +08:00
|
|
|
|
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
/* Step 2+: Remove container background, let patches show */
|
2026-01-15 20:10:19 +08:00
|
|
|
|
.image-container.is-patchified {
|
2026-01-18 10:24:35 +08:00
|
|
|
|
background-image: none;
|
|
|
|
|
|
background-color: transparent;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
gap: 2px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.patch {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
font-size: 8px;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
color: rgba(255, 255, 255, 0.8);
|
|
|
|
|
|
position: relative;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Step 1: Pixelated Overlay Effect */
|
|
|
|
|
|
.image-container.is-pixelated:not(.is-patchified) .patch {
|
|
|
|
|
|
border: 1px solid rgba(255, 255, 255, 0.1);
|
|
|
|
|
|
/* Use pseudo-element or just opacity logic in JS */
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Step 1: Digitization numbers */
|
|
|
|
|
|
.pixel-val {
|
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
|
font-size: 8px;
|
|
|
|
|
|
color: rgba(0, 0, 0, 0.3);
|
|
|
|
|
|
mix-blend-mode: overlay;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
.patch-id {
|
|
|
|
|
|
background: rgba(0, 0, 0, 0.5);
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
padding: 1px 2px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
border-radius: 2px;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
font-size: 7px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.arrow-down {
|
|
|
|
|
|
font-size: 24px;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
animation: bounce 1s infinite;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.sequence-container {
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
background: var(--vp-c-bg);
|
|
|
|
|
|
padding: 15px;
|
2026-02-14 20:23:34 +08:00
|
|
|
|
border-radius: 6px;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
border: 1px solid var(--vp-c-divider);
|
2026-01-18 10:24:35 +08:00
|
|
|
|
animation: slideUp 0.5s ease;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.sequence-label {
|
|
|
|
|
|
font-size: 0.9em;
|
|
|
|
|
|
margin-bottom: 10px;
|
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
|
color: var(--vp-c-text-2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.token-stream {
|
|
|
|
|
|
display: flex;
|
2026-01-18 10:24:35 +08:00
|
|
|
|
flex-wrap: nowrap;
|
|
|
|
|
|
gap: 1px;
|
|
|
|
|
|
overflow-x: auto;
|
|
|
|
|
|
padding: 10px 5px; /* Space for brackets */
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
position: relative;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
/* Add Matrix Brackets */
|
|
|
|
|
|
.token-stream::before,
|
|
|
|
|
|
.token-stream::after {
|
|
|
|
|
|
content: '';
|
|
|
|
|
|
display: block;
|
|
|
|
|
|
width: 6px;
|
|
|
|
|
|
height: 36px; /* Match vector height + padding */
|
|
|
|
|
|
border: 2px solid var(--vp-c-text-3);
|
|
|
|
|
|
flex-shrink: 0;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
.token-stream::before {
|
|
|
|
|
|
border-right: none;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
.token-stream::after {
|
|
|
|
|
|
border-left: none;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.mini-patch {
|
|
|
|
|
|
width: 6px; /* Thinner to allow more density */
|
|
|
|
|
|
height: 32px; /* Taller to represent Vector Dimension D */
|
|
|
|
|
|
border-radius: 1px;
|
|
|
|
|
|
flex-shrink: 0;
|
|
|
|
|
|
opacity: 0.9;
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@keyframes bounce {
|
2026-01-18 12:21:49 +08:00
|
|
|
|
0%,
|
|
|
|
|
|
100% {
|
|
|
|
|
|
transform: translateY(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
50% {
|
|
|
|
|
|
transform: translateY(5px);
|
|
|
|
|
|
}
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 10:24:35 +08:00
|
|
|
|
@keyframes slideUp {
|
2026-01-18 12:21:49 +08:00
|
|
|
|
from {
|
|
|
|
|
|
opacity: 0;
|
|
|
|
|
|
transform: translateY(20px);
|
|
|
|
|
|
}
|
|
|
|
|
|
to {
|
|
|
|
|
|
opacity: 1;
|
|
|
|
|
|
transform: translateY(0);
|
|
|
|
|
|
}
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
</style>
|