Tenbatsu24 commited on
Commit
60c3107
Β·
1 Parent(s): a10ce46

add: css and links to models and code

Browse files
Files changed (1) hide show
  1. app.py +37 -9
app.py CHANGED
@@ -69,11 +69,22 @@ def preprocess(image_tensor: torch.Tensor) -> torch.Tensor:
69
  ).unsqueeze(0)
70
 
71
 
 
 
 
 
 
 
 
 
 
 
 
72
  # ── PCA visualisation ─────────────────────────────────────────────────────────
73
 
74
 
75
  def pca_vis(model: torch.nn.Module, image_tensor: torch.Tensor) -> Image.Image:
76
- """Run image through model, PCA patch features β†’ upscaled RGB PIL image."""
77
  model_input = preprocess(image_tensor).to(DEVICE)
78
 
79
  with torch.inference_mode():
@@ -92,13 +103,14 @@ def pca_vis(model: torch.nn.Module, image_tensor: torch.Tensor) -> Image.Image:
92
  vis = torch.sigmoid(projected_t * 2.0)
93
  pca_array = (vis.numpy() * 255).astype(np.uint8) # (H_p, W_p, 3)
94
 
95
- # nearest-neighbour upscale back to model input resolution
96
- return Image.fromarray(pca_array, mode="RGB").resize((W, H), Image.NEAREST)
 
97
 
98
 
99
  # ── streaming inference ───────────────────────────────────────────────────────
100
 
101
- PENDING = Image.new("RGB", (64, 64), color=(240, 240, 240)) # placeholder tile
102
 
103
 
104
  def run(pil_image: Image.Image):
@@ -145,6 +157,14 @@ CSS = """
145
  align-items: center;
146
  gap: 0.25rem;
147
  }
 
 
 
 
 
 
 
 
148
  footer { display: none !important; }
149
  """
150
 
@@ -157,8 +177,10 @@ with gr.Blocks(css=CSS, title="SSL ViT PCA Visualiser") as demo:
157
  </h1>
158
  </div>
159
  <div class="subtitle-row">
160
- ViT-S/16 &nbsp;Β·&nbsp; ImageNet-1K pre-training
161
- &nbsp;Β·&nbsp; DiNO &nbsp;Β·&nbsp; iBOT &nbsp;Β·&nbsp; LeJEPA
 
 
162
  </div>
163
  """)
164
 
@@ -177,18 +199,24 @@ with gr.Blocks(css=CSS, title="SSL ViT PCA Visualiser") as demo:
177
  3 components, then scaled with sigmoid for colour display.
178
  Results appear as each model finishes.
179
  </p>
 
 
 
 
 
 
180
  """)
181
 
182
  with gr.Column(scale=3):
183
  with gr.Row(equal_height=True):
184
  with gr.Column(elem_classes="output-col"):
185
- gr.HTML('<div class="model-label">DiNO</div>')
186
  out_dino = gr.Image(show_label=False, interactive=False)
187
  with gr.Column(elem_classes="output-col"):
188
- gr.HTML('<div class="model-label">iBOT</div>')
189
  out_ibot = gr.Image(show_label=False, interactive=False)
190
  with gr.Column(elem_classes="output-col"):
191
- gr.HTML('<div class="model-label">LeJEPA</div>')
192
  out_lejepa = gr.Image(show_label=False, interactive=False)
193
 
194
  run_btn.click(
 
69
  ).unsqueeze(0)
70
 
71
 
72
+ def pad_to_square(img: Image.Image) -> Image.Image:
73
+ """Letterbox/pillarbox img onto a square canvas with a dark background.
74
+ Ensures all output images share the same dimensions so the Gradio row
75
+ never reflows or stretches when aspect ratios differ."""
76
+ w, h = img.size
77
+ size = max(w, h)
78
+ canvas = Image.new("RGB", (size, size), color=(18, 18, 18))
79
+ canvas.paste(img, ((size - w) // 2, (size - h) // 2))
80
+ return canvas
81
+
82
+
83
  # ── PCA visualisation ─────────────────────────────────────────────────────────
84
 
85
 
86
  def pca_vis(model: torch.nn.Module, image_tensor: torch.Tensor) -> Image.Image:
87
+ """Run image through model, PCA patch features β†’ square-padded RGB PIL image."""
88
  model_input = preprocess(image_tensor).to(DEVICE)
89
 
90
  with torch.inference_mode():
 
103
  vis = torch.sigmoid(projected_t * 2.0)
104
  pca_array = (vis.numpy() * 255).astype(np.uint8) # (H_p, W_p, 3)
105
 
106
+ # nearest-neighbour upscale β†’ pad to square so all outputs are the same size
107
+ upscaled = Image.fromarray(pca_array, mode="RGB").resize((W, H), Image.NEAREST)
108
+ return pad_to_square(upscaled)
109
 
110
 
111
  # ── streaming inference ───────────────────────────────────────────────────────
112
 
113
+ PENDING = Image.new("RGB", (IMAGE_SIZE, IMAGE_SIZE), color=(18, 18, 18))
114
 
115
 
116
  def run(pil_image: Image.Image):
 
157
  align-items: center;
158
  gap: 0.25rem;
159
  }
160
+ .subtitle-row a, .model-label a {
161
+ color: inherit;
162
+ text-decoration: underline;
163
+ text-decoration-color: #d1d5db;
164
+ }
165
+ .model-label a:hover, .subtitle-row a:hover {
166
+ text-decoration-color: currentColor;
167
+ }
168
  footer { display: none !important; }
169
  """
170
 
 
177
  </h1>
178
  </div>
179
  <div class="subtitle-row">
180
+ ViT-S/16 &nbsp;Β·&nbsp; ImageNet-1K pre-training &nbsp;Β·&nbsp;
181
+ <a href="https://huggingface.co/OK-AI/dino-vits16-pretrain-in1k" target="_blank">DiNO</a> &nbsp;Β·&nbsp;
182
+ <a href="https://huggingface.co/OK-AI/ibot-vits16-pretrain-in1k" target="_blank">iBOT</a> &nbsp;Β·&nbsp;
183
+ <a href="https://huggingface.co/OK-AI/lejepa-vits16-pretrain-in1k" target="_blank">LeJEPA</a>
184
  </div>
185
  """)
186
 
 
199
  3 components, then scaled with sigmoid for colour display.
200
  Results appear as each model finishes.
201
  </p>
202
+
203
+ <p style="font-size:0.75rem; color:#9ca3af; margin-top:0.25rem;">
204
+ Models: <a href="https://huggingface.co/OK-AI" target="_blank">OK-AI on HuggingFace</a>
205
+ &nbsp;Β·&nbsp;
206
+ Code: <a href="https://github.com/Open-Knowledge-AI/lite_ssl" target="_blank">lite_ssl</a>
207
+ </p>
208
  """)
209
 
210
  with gr.Column(scale=3):
211
  with gr.Row(equal_height=True):
212
  with gr.Column(elem_classes="output-col"):
213
+ gr.HTML('<div class="model-label"><a href="https://huggingface.co/OK-AI/dino-vits16-pretrain-in1k" target="_blank">DiNO</a></div>')
214
  out_dino = gr.Image(show_label=False, interactive=False)
215
  with gr.Column(elem_classes="output-col"):
216
+ gr.HTML('<div class="model-label"><a href="https://huggingface.co/OK-AI/ibot-vits16-pretrain-in1k" target="_blank">iBOT</a></div>')
217
  out_ibot = gr.Image(show_label=False, interactive=False)
218
  with gr.Column(elem_classes="output-col"):
219
+ gr.HTML('<div class="model-label"><a href="https://huggingface.co/OK-AI/lejepa-vits16-pretrain-in1k" target="_blank">LeJEPA</a></div>"""')
220
  out_lejepa = gr.Image(show_label=False, interactive=False)
221
 
222
  run_btn.click(