Spaces:
Runtime error
Rename to OneVision Encoder; global top-K; codec-vs-uniform charts
Browse filesUI / brand
- Rename Space and headings to "OneVision Encoder".
- Hero now carries Homepage / Models / Tech Report / Model Card /
Data Card links as pill-style buttons (white -> gradient on hover).
- Card hover lift; tighter typography rhythm.
Algorithm
- Patch budget is now a single global Top-K spent across the whole
video, not per-frame. High-saliency frames may receive many
patches; low-saliency frames may receive zero.
- Slider renamed to "Total patches budget (whole video)" with
explanatory `info` text; range bumped to 64..8192, default 1024.
Charts (gr.Plot)
- Replaced the score histogram with a codec-vs-uniform comparison:
* Left : x = time (s), y = patches selected at that timestamp by
the codec saliency, including zero bars where the budget
skipped the frame.
* Right : x = time (s), y = full grid size, drawn for the
N_uniform = budget // grid frames a uniform sampler
would extract under the same total budget.
- Both panels share scale so the contrast in allocation is obvious.
Run info
- `top_k_per_frame` -> `total_patches_budget`.
- Adds `actual_selected_total` and a `uniform_baseline` block with
frames / patches_per_frame / total_patches / explanation, so the
chart numbers are reproducible from the JSON alone.
123453 unmodified lines9101112131415---title: OneVision Patch Inspectoremoji: πcolorFrom: bluecolorTo: indigo3 unmodified linesapp_file: app.pypinned: falselicense: apache-2.0short_description: Inspect which video patches a codec-style saliency picks---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123453 unmodified lines9101112131415---title: OneVision Encoderemoji: πcolorFrom: bluecolorTo: indigo3 unmodified linesapp_file: app.pypinned: falselicense: apache-2.0short_description: Codec-style patch saliency for video understanding---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
172 unmodified lines1731741751761771783 unmodified lines182183184185186187131 unmodified lines3193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523534 unmodified lines35835936036136236336461 unmodified lines42642742842943043143230 unmodified lines4634644654664674684694704714724734747 unmodified lines4824834844854864874884894904 unmodified lines49549649749849950050161 unmodified lines56356456556656756810 unmodified lines57958058158258358458558658780 unmodified lines6686696706716726736746756766776786796806816826832 unmodified lines68668768868969069169236 unmodified lines72973073173273373473561 unmodified lines79779879980080180280317 unmodified lines821822823824825826827828829172 unmodified linesdef topk_mask(score: np.ndarray, k: int) -> np.ndarray:flat = score.flatten()if k >= flat.size:return np.ones_like(score, dtype=np.uint8)3 unmodified linesreturn (score >= thresh).astype(np.uint8)def faded_background(frame_bgr: np.ndarray, fade: float = 0.55) -> np.ndarray:"""Convert to gray-white wash: gray * (1-fade) + white * fade."""gray = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2GRAY)131 unmodified linesdef make_charts(grids: List[np.ndarray], masks: List[np.ndarray], saliency_signal: str,):"""Two side-by-side panels:- patch score histogram across all sampled frames (log-y)- selected patch count per sampled frame index"""fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8.6, 3.0), constrained_layout=True)all_scores = np.concatenate([g.flatten().astype(np.float32) for g in grids])ax1.hist(all_scores, bins=40, color="#4f46e5", alpha=0.85,edgecolor="#312e81", linewidth=0.4)ax1.set_yscale("log")ax1.set_title(f"Patch score distribution Β· {saliency_signal}",fontsize=10, color="#1e293b")ax1.set_xlabel("score", fontsize=9)ax1.set_ylabel("patches (log)", fontsize=9)ax1.tick_params(axis="both", which="both", labelsize=8)ax1.grid(True, alpha=0.25, linestyle="--")ax1.spines[["top", "right"]].set_visible(False)counts = [int(m.sum()) for m in masks]xs = list(range(len(counts)))ax2.plot(xs, counts, "o-", color="#06b6d4", linewidth=2.0,markersize=5, markeredgecolor="#0e7490")ax2.fill_between(xs, counts, alpha=0.15, color="#06b6d4")ax2.set_title("Selected patches per sampled frame",fontsize=10, color="#1e293b")ax2.set_xlabel("sampled frame index", fontsize=9)ax2.set_ylabel("# selected", fontsize=9)ax2.tick_params(axis="both", which="both", labelsize=8)ax2.grid(True, alpha=0.25, linestyle="--")ax2.spines[["top", "right"]].set_visible(False)fig.patch.set_facecolor("white")4 unmodified linesvideo_path,sample_frames: int,patch_size: int,top_k_per_frame: int,max_pixels: int,viz_mode: str = "selection",heatmap_alpha: float = 0.55,61 unmodified linesgrids = compute_score_grids(resized, int(patch_size), saliency_signal)if score_log_scale:grids = [np.log1p(np.clip(g, 0.0, None)) for g in grids]masks = [topk_mask(g, int(top_k_per_frame)) for g in grids]norm_scores = _normalize_scores(grids, pct=float(bitcost_pct))mode = (viz_mode or "selection").lower()30 unmodified linescv2.imwrite(canvas_path, canvas)hb, wb = grids[0].shapeinfo = {"input": meta,"params": {"sample_frames": int(sample_frames),"patch_size": int(patch_size),"top_k_per_frame": int(top_k_per_frame),"max_pixels": int(max_pixels),"start_sec": float(s_sec),"end_sec": float(e_sec) if e_sec > 0 else None,7 unmodified lines"last_decoded": int(f_end),"actual_frame_ids": [int(x) for x in fids],},"resized_frame_size": f"{tw}x{th}","patch_grid_per_frame": f"{hb}x{wb} = {hb * wb} patches","selected_per_frame": int(min(top_k_per_frame, hb * wb)),"total_selected_patches": int(n_selected),"canvas_resolution": f"{canvas.shape[1]}x{canvas.shape[0]}","vis_video_fps": round(vis_fps, 2),4 unmodified lines"elapsed_sec": round(time.time() - t0, 2),}progress(0.95, desc="Building charts")chart_fig = make_charts(grids, masks, saliency_signal)progress(1.0, desc="Done")return (61 unmodified linesmax-width: 760px;line-height: 1.6;}#ovc-hero .pills { display:flex; flex-wrap:wrap; gap:8px; justify-content:center; margin-top:8px; }#ovc-hero .pill {font-size: 0.78rem;10 unmodified lines.ovc-card {border-radius: 16px !important;padding: 14px 16px !important;border: 1px solid rgba(148,163,184,0.30) !important;background: var(--background-fill-primary) !important;box-shadow: 0 1px 3px rgba(15,23,42,0.04);}.ovc-card h3 {font-size: 0.78rem !important;80 unmodified linesHERO_HTML = """<div id="ovc-hero"><h1>OneVision Patch Inspector</h1><p class="tagline">See which patches a codec-style saliency picks from your video,then pack them into the canvas LLaVA-OneVision2 consumes.A visual lab for inspecting <i>where</i> the encoder — and the model — is actually looking.</p><div class="pills"><span class="pill">selection Β· heatmap Β· side-by-side</span><span class="pill">gradient + motion saliency</span><span class="pill">presets Β· charts Β· canvas export</span></div></div>"""2 unmodified lines_GR_MAJOR = int(gr.__version__.split(".")[0])except Exception:_GR_MAJOR = 4_BLOCK_KW: dict = {"title": "OneVision Patch Inspector"}_LAUNCH_KW: dict = {}if _GR_MAJOR >= 6:# In Gradio 6.0 these moved off Blocks(...) onto launch(...).36 unmodified lines4, 64, value=16, step=1, label="Sampled frames",)top_k = gr.Slider(4, 1024, value=64, step=4, label="Top-K patches per frame",)patch_size = gr.Radio(PATCH_CHOICES, value=14, label="Patch size (px)",61 unmodified lines)with gr.Group(elem_classes="ovc-card"):gr.Markdown("### Score distribution & per-frame patch count")chart_out = gr.Plot(label="", show_label=False)with gr.Row():17 unmodified linesgr.HTML('<div id="ovc-footer">''Approximation of the bitcost-driven patch selection in ''<code>codec_tools/</code> Β· Sobel + frame-diff used as a stand-in ''for the ffmpeg bitcost patch.''</div>')172 unmodified lines1731741751761771781793 unmodified lines183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213131 unmodified lines3453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284 unmodified lines43343443543643743843961 unmodified lines50150250350450550650730 unmodified lines5385395405415425435445455465475485495505517 unmodified lines5595605615625635645655665675685695705715725735745755765774 unmodified lines58258358458558658758858959059159261 unmodified lines65465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969010 unmodified lines70170270370470570670770870971071171271371480 unmodified lines7957967977987998008018028038048058068078088098108118128138148158168172 unmodified lines82082182282382482582636 unmodified lines86386486586686786886987087187287387461 unmodified lines93693793893994094194294394494594694794894995017 unmodified lines968969970971972973974975976172 unmodified linesdef topk_mask(score: np.ndarray, k: int) -> np.ndarray:"""Per-frame top-K mask (legacy helper, no longer used by process())."""flat = score.flatten()if k >= flat.size:return np.ones_like(score, dtype=np.uint8)3 unmodified linesreturn (score >= thresh).astype(np.uint8)def global_topk_masks(grids: List[np.ndarray], total_k: int,) -> Tuple[List[np.ndarray], int]:"""Pick the top `total_k` highest-scoring patches GLOBALLY across allsampled frames, return one mask per frame plus the actual count.Some frames may end up with zero patches (low energy throughout) whileothers may contribute many β that's the whole point: the codec-stylesaliency lets the budget concentrate where it matters."""if not grids:return [], 0arr = np.stack(grids, axis=0).astype(np.float32) # [N, hb, wb]N, hb, wb = arr.shapeflat = arr.reshape(-1)if total_k >= flat.size:masks = [np.ones((hb, wb), dtype=np.uint8) for _ in range(N)]return masks, int(flat.size)if total_k <= 0:return [np.zeros((hb, wb), dtype=np.uint8) for _ in range(N)], 0thresh = np.partition(flat, -total_k)[-total_k]bool_mask = (arr >= thresh)actual = int(bool_mask.sum())return [bool_mask[i].astype(np.uint8) for i in range(N)], actualdef faded_background(frame_bgr: np.ndarray, fade: float = 0.55) -> np.ndarray:"""Convert to gray-white wash: gray * (1-fade) + white * fade."""gray = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2GRAY)131 unmodified linesdef make_charts(grids: List[np.ndarray],masks: List[np.ndarray],frame_ids: List[int],fps: float,total_duration_sec: float,total_patches_budget: int,saliency_signal: str,):"""Two side-by-side panels comparing codec selection vs uniform sampling.Both panels share x = time (seconds), y = number of patches selected atthat timestamp. Same total patch budget on both sides β only the*allocation* differs.Left (codec): patches go where saliency wants them β often spikyand concentrated on a few moments.Right (uniform): the same budget is spent on full frames sampledevenly in time; every uniform frame contributes thefull grid's worth of patches."""fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9.2, 3.2), constrained_layout=True)fps_safe = float(fps) if fps and fps > 0 else 25.0if grids:hb, wb = grids[0].shapeelse:hb = wb = 1grid_size = hb * wbduration = float(total_duration_sec) if total_duration_sec and total_duration_sec > 0 else ((max(frame_ids) / fps_safe) if frame_ids else 1.0)# βββ Left: codec selection over time βββββββββββββββββββββββββββββββββtimes = [fid / fps_safe for fid in frame_ids]counts = [int(m.sum()) for m in masks]if not times:times, counts = [0.0], [0]bar_w = max(duration / max(1, len(times)) * 0.55, 0.04)ax1.bar(times, counts, width=bar_w,color="#4f46e5", alpha=0.88,edgecolor="#312e81", linewidth=0.4,)total_selected = sum(counts)ax1.set_title(f"Codec selection Β· {saliency_signal} Β· {total_selected} patches",fontsize=10, color="#1e293b",)ax1.set_xlabel("time (s)", fontsize=9)ax1.set_ylabel("# patches selected", fontsize=9)ax1.set_xlim(-duration * 0.02, duration * 1.02)ax1.tick_params(axis="both", labelsize=8)ax1.grid(True, alpha=0.25, linestyle="--", axis="y")ax1.spines[["top", "right"]].set_visible(False)# βββ Right: uniform-sampling baseline at the same budget ββββββββββββn_uniform = max(1, int(total_patches_budget // max(1, grid_size)))uniform_times = ([duration * 0.5] if n_uniform == 1else list(np.linspace(0.0, duration, n_uniform)))uniform_counts = [grid_size] * n_uniformbar_w_u = max(duration / max(1, n_uniform) * 0.55, 0.04)ax2.bar(uniform_times, uniform_counts, width=bar_w_u,color="#06b6d4", alpha=0.88,edgecolor="#0e7490", linewidth=0.4,)ax2.set_title(f"Uniform baseline Β· {n_uniform} frames Γ {grid_size} patches "f"= {n_uniform * grid_size}",fontsize=10, color="#1e293b",)ax2.set_xlabel("time (s)", fontsize=9)ax2.set_ylabel("# patches per uniform frame", fontsize=9)ax2.set_xlim(-duration * 0.02, duration * 1.02)ax2.tick_params(axis="both", labelsize=8)ax2.grid(True, alpha=0.25, linestyle="--", axis="y")ax2.spines[["top", "right"]].set_visible(False)fig.patch.set_facecolor("white")4 unmodified linesvideo_path,sample_frames: int,patch_size: int,total_patches: int,max_pixels: int,viz_mode: str = "selection",heatmap_alpha: float = 0.55,61 unmodified linesgrids = compute_score_grids(resized, int(patch_size), saliency_signal)if score_log_scale:grids = [np.log1p(np.clip(g, 0.0, None)) for g in grids]masks, actual_selected = global_topk_masks(grids, int(total_patches))norm_scores = _normalize_scores(grids, pct=float(bitcost_pct))mode = (viz_mode or "selection").lower()30 unmodified linescv2.imwrite(canvas_path, canvas)hb, wb = grids[0].shapegrid_size = int(grids[0].shape[0] * grids[0].shape[1]) if grids else 0n_uniform = max(1, int(int(total_patches) // max(1, grid_size)))info = {"input": meta,"params": {"sample_frames": int(sample_frames),"patch_size": int(patch_size),"total_patches_budget": int(total_patches),"max_pixels": int(max_pixels),"start_sec": float(s_sec),"end_sec": float(e_sec) if e_sec > 0 else None,7 unmodified lines"last_decoded": int(f_end),"actual_frame_ids": [int(x) for x in fids],},"uniform_baseline": {"frames": int(n_uniform),"patches_per_frame": int(grid_size),"total_patches": int(n_uniform * grid_size),"explanation": ("Same patch budget spent on whole frames sampled uniformly in ""time β every patch in those frames kept, vs codec's selective ""concentration on high-saliency patches."),},"resized_frame_size": f"{tw}x{th}","patch_grid_per_frame": f"{hb}x{wb} = {hb * wb} patches","actual_selected_total": int(actual_selected),"total_selected_patches": int(n_selected),"canvas_resolution": f"{canvas.shape[1]}x{canvas.shape[0]}","vis_video_fps": round(vis_fps, 2),4 unmodified lines"elapsed_sec": round(time.time() - t0, 2),}progress(0.95, desc="Building charts")duration_sec = (total / fps) if fps > 0 else 0.0chart_fig = make_charts(grids, masks, fids, fps, duration_sec,int(total_patches), saliency_signal,)progress(1.0, desc="Done")return (61 unmodified linesmax-width: 760px;line-height: 1.6;}.ovc-links {display: flex; flex-wrap: wrap; gap: 10px;justify-content: center; margin: 14px auto 6px;position: relative; z-index: 1;}.ovc-links a {text-decoration: none;font-weight: 600;font-size: 0.9rem;padding: 7px 14px;border-radius: 999px;background: var(--background-fill-primary, #fff);border: 1px solid rgba(99,102,241,0.32);color: #4338ca;transition: transform 0.12s ease, box-shadow 0.18s ease,background 0.18s ease, color 0.18s ease, border-color 0.18s ease;display: inline-flex; align-items: center;box-shadow: 0 1px 2px rgba(15,23,42,0.04);}.ovc-links a:hover {background: var(--ovc-grad);color: #fff;border-color: transparent;transform: translateY(-1px);box-shadow: 0 6px 16px rgba(79,70,229,0.32);}.gradio-container.dark .ovc-links a {background: rgba(30,41,59,0.7);color: #c7d2fe;border-color: rgba(99,102,241,0.4);}#ovc-hero .pills { display:flex; flex-wrap:wrap; gap:8px; justify-content:center; margin-top:8px; }#ovc-hero .pill {font-size: 0.78rem;10 unmodified lines.ovc-card {border-radius: 16px !important;padding: 14px 16px !important;border: 1px solid rgba(148,163,184,0.28) !important;background: var(--background-fill-primary) !important;box-shadow: 0 1px 3px rgba(15,23,42,0.04);transition: box-shadow 0.18s ease, border-color 0.18s ease;}.ovc-card:hover {border-color: rgba(99,102,241,0.32) !important;box-shadow: 0 6px 18px rgba(15,23,42,0.06);}.ovc-card h3 {font-size: 0.78rem !important;80 unmodified linesHERO_HTML = """<div id="ovc-hero"><h1>OneVision Encoder</h1><p class="tagline">Codec-style patch saliency for video understanding — see whichpatches the encoder picks from your video and pack them into thecanvas LLaVA-OneVision consumes.</p><div class="ovc-links"><a href="https://www.lmms-lab.com/onevision-encoder/index.html" target="_blank" rel="noopener">π Homepage</a><a href="https://huggingface.co/collections/lmms-lab-encoder/onevision-encoder" target="_blank" rel="noopener">π€ Models</a><a href="https://arxiv.org/abs/2602.08683" target="_blank" rel="noopener">π Tech Report</a><a href="docs/model_card.md" target="_blank" rel="noopener">π Model Card</a><a href="docs/data_card.md" target="_blank" rel="noopener">π Data Card</a></div><div class="pills"><span class="pill">selection Β· heatmap Β· side-by-side</span><span class="pill">gradient + motion saliency</span><span class="pill">codec vs uniform baseline</span></div></div>"""2 unmodified lines_GR_MAJOR = int(gr.__version__.split(".")[0])except Exception:_GR_MAJOR = 4_BLOCK_KW: dict = {"title": "OneVision Encoder"}_LAUNCH_KW: dict = {}if _GR_MAJOR >= 6:# In Gradio 6.0 these moved off Blocks(...) onto launch(...).36 unmodified lines4, 64, value=16, step=1, label="Sampled frames",)top_k = gr.Slider(64, 8192, value=1024, step=32,label="Total patches budget (whole video)",info="The single budget shared across the whole video. ""The codec saliency picks these patches GLOBALLY β ""high-energy frames may contribute many, low-energy ""frames may contribute zero.",)patch_size = gr.Radio(PATCH_CHOICES, value=14, label="Patch size (px)",61 unmodified lines)with gr.Group(elem_classes="ovc-card"):gr.Markdown("### Codec selection vs uniform baseline")gr.Markdown("<small>Same total patch budget on both sides. ""<b>Left</b>: where the codec saliency actually placed ""patches (often spiky, concentrated on important moments). ""<b>Right</b>: how a naive uniform frame sampler would ""spend the same budget β spread evenly in time, every ""patch in the chosen frames kept.</small>")chart_out = gr.Plot(label="", show_label=False)with gr.Row():17 unmodified linesgr.HTML('<div id="ovc-footer">''<b>OneVision Encoder</b> Β· codec-style patch saliency demo Β· ''Sobel + frame-diff stand in for the ffmpeg bitcost patch Β· ''global top-K selection across all sampled frames.''</div>')