lorien-danger commited on
Commit
956b427
·
verified ·
1 Parent(s): aa15779

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +12 -46
index.html CHANGED
@@ -77,7 +77,6 @@
77
  // ===== 1) Config =====
78
  const MODEL_ID = "onnx-community/ijepa_vith14_1k"; // <-- I-JEPA ViT-H/14, ImageNet-1k
79
  const EXAMPLE_IMAGE_URL = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png";
80
- const SUPPORTED_RESOLUTIONS = [224, 336, 448]; // Specific resolutions I-JEPA ONNX might expect
81
 
82
  // DOM
83
  const imageLoader = document.getElementById("imageLoader");
@@ -181,12 +180,6 @@
181
  function handleSliderInput(e){ imageScale = parseFloat(e.target.value); scaleValue.textContent = `${imageScale.toFixed(2)}x`; }
182
  function handleSliderChange(){ if (currentImageUrl) loadImageOntoCanvas(currentImageUrl); }
183
 
184
- function findClosestSupportedResolution(targetDim) {
185
- return SUPPORTED_RESOLUTIONS.reduce((prev, curr) =>
186
- Math.abs(curr - targetDim) < Math.abs(prev - targetDim) ? curr : prev
187
- );
188
- }
189
-
190
  function loadImageOntoCanvas(url){
191
  currentImageUrl = url;
192
  originalImage = new Image();
@@ -204,45 +197,27 @@
204
  newW *= r; newH *= r;
205
  }
206
 
207
- // --- MODIFIED LOGIC HERE ---
208
- // Determine target dimensions based on the smallest dimension to maintain aspect ratio
209
- // and then find the closest supported resolution.
210
- let targetDim = Math.min(newW, newH);
211
- let chosenResolution = findClosestSupportedResolution(targetDim);
212
-
213
- // Calculate final scaled dimensions based on chosenResolution
214
- let finalW, finalH;
215
- if (newW < newH) { // Portrait or square
216
- finalW = chosenResolution;
217
- finalH = Math.round(newH * (chosenResolution / newW));
218
- } else { // Landscape
219
- finalH = chosenResolution;
220
- finalW = Math.round(newW * (chosenResolution / newH));
221
- }
222
-
223
- // Ensure dimensions are multiples of patchSize after scaling
224
- finalW = Math.floor(finalW / patchSize) * patchSize;
225
- finalH = Math.floor(finalH / patchSize) * patchSize;
226
-
227
- if (finalW < patchSize || finalH < patchSize){
228
- updateStatus("Scaled image is too small to process, or chosen resolution is too small.");
229
  imageCanvas.style.display = "none";
230
  canvasPlaceholder.style.display = "block";
231
- canvasPlaceholder.textContent = "Scaled image too small.";
232
  return;
233
  }
234
 
235
- imageCanvas.width = finalW;
236
- imageCanvas.height = finalH;
237
- ctx.drawImage(originalImage, 0, 0, finalW, finalH);
238
- await processImage(chosenResolution); // Pass chosenResolution for status message
239
  setTimeout(() => { canvasContainer.scrollIntoView({ behavior: "smooth", block: "center" }); }, 100);
240
  };
241
  originalImage.onerror = () => { updateStatus("Failed to load the selected image."); canvasPlaceholder.style.display = "block"; imageCanvas.style.display = "none"; };
242
  originalImage.src = url;
243
  }
244
 
245
- async function processImage(chosenResolution = null){ // Accept chosenResolution as argument
246
  if (!extractor) return;
247
  updateStatus("Analyzing with I‑JEPA... 🧠", true);
248
  similarityScores = null; lastHoverData = null;
@@ -261,19 +236,10 @@
261
  const sims = await matmul(normalized, normalized.permute(0,2,1)); // [1, nPatches, nPatches]
262
  similarityScores = (await sims.tolist())[0];
263
 
264
- let statusMsg = `Image processed (${imageCanvas.width}×${imageCanvas.height}). Hover to explore. ✨`;
265
- if (chosenResolution && (imageCanvas.width !== chosenResolution || imageCanvas.height !== chosenResolution)) {
266
- // We only mention resolution if the image was actually scaled to one of the supported resolutions,
267
- // and if the final dimensions aren't exactly the square chosen resolution (e.g. landscape 448x224 processed as 224)
268
- statusMsg = `Image resized to ${imageCanvas.width}×${imageCanvas.height} (based on ${chosenResolution}). Hover to explore. ✨`;
269
- } else if (chosenResolution) {
270
- statusMsg = `Image processed at ${chosenResolution}×${chosenResolution}. Hover to explore. ✨`;
271
- }
272
-
273
- updateStatus(statusMsg);
274
  }catch(err){
275
  console.error("I‑JEPA processing error:", err);
276
- updateStatus("An error occurred during image processing. It might be due to an unsupported image size.");
277
  }
278
  }
279
 
 
77
  // ===== 1) Config =====
78
  const MODEL_ID = "onnx-community/ijepa_vith14_1k"; // <-- I-JEPA ViT-H/14, ImageNet-1k
79
  const EXAMPLE_IMAGE_URL = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png";
 
80
 
81
  // DOM
82
  const imageLoader = document.getElementById("imageLoader");
 
180
  function handleSliderInput(e){ imageScale = parseFloat(e.target.value); scaleValue.textContent = `${imageScale.toFixed(2)}x`; }
181
  function handleSliderChange(){ if (currentImageUrl) loadImageOntoCanvas(currentImageUrl); }
182
 
 
 
 
 
 
 
183
  function loadImageOntoCanvas(url){
184
  currentImageUrl = url;
185
  originalImage = new Image();
 
197
  newW *= r; newH *= r;
198
  }
199
 
200
+ const croppedW = Math.floor(newW / patchSize) * patchSize;
201
+ const croppedH = Math.floor(newH / patchSize) * patchSize;
202
+ if (croppedW < patchSize || croppedH < patchSize){
203
+ updateStatus("Scaled image is too small to process.");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  imageCanvas.style.display = "none";
205
  canvasPlaceholder.style.display = "block";
206
+ canvasPlaceholder.textContent = "Scaled image is too small.";
207
  return;
208
  }
209
 
210
+ imageCanvas.width = croppedW;
211
+ imageCanvas.height = croppedH;
212
+ ctx.drawImage(originalImage, 0, 0, croppedW, croppedH);
213
+ await processImage();
214
  setTimeout(() => { canvasContainer.scrollIntoView({ behavior: "smooth", block: "center" }); }, 100);
215
  };
216
  originalImage.onerror = () => { updateStatus("Failed to load the selected image."); canvasPlaceholder.style.display = "block"; imageCanvas.style.display = "none"; };
217
  originalImage.src = url;
218
  }
219
 
220
+ async function processImage(){
221
  if (!extractor) return;
222
  updateStatus("Analyzing with I‑JEPA... 🧠", true);
223
  similarityScores = null; lastHoverData = null;
 
236
  const sims = await matmul(normalized, normalized.permute(0,2,1)); // [1, nPatches, nPatches]
237
  similarityScores = (await sims.tolist())[0];
238
 
239
+ updateStatus(`Image processed (${imageCanvas.width}×${imageCanvas.height}). Hover to explore. ✨`);
 
 
 
 
 
 
 
 
 
240
  }catch(err){
241
  console.error("I‑JEPA processing error:", err);
242
+ updateStatus("An error occurred during image processing.");
243
  }
244
  }
245