Skip to content

Instantly share code, notes, and snippets.

View jeanc18rlos's full-sized avatar

Jean Rojas jeanc18rlos

View GitHub Profile
@jeanc18rlos
jeanc18rlos / cog.yaml
Created May 13, 2026 19:35
SAM2 hybrid — Replicate encoder microservice. cog package (cog.yaml + predict.py + push.sh) and browser-side client. Companion to https://jeanrojas.com/blog/splitting-sam2-encoder-decoder
build:
gpu: true
cuda: "12.1"
python_version: "3.11"
python_packages:
- "torch==2.4.0"
- "torchvision==0.19.0"
- "onnxruntime-gpu==1.20.0"
- "Pillow==10.4.0"
- "numpy==1.26.4"
@jeanc18rlos
jeanc18rlos / sam2-decoder.ts
Created May 13, 2026 19:32
SAM2 hybrid — browser-side decoder (lib/sam2-decoder.ts). Loads decoder.onnx on WebGPU, fetches the encoder embedding bundle, runs per-click segmentation. Companion to https://jeanrojas.com/blog/splitting-sam2-encoder-decoder
// lib/sam2-decoder.ts
// Browser-side decoder for SAM2 hybrid deployment.
// Loads sam2.1_hiera_large.decoder.onnx (~16 MB) via onnxruntime-web on WebGPU.
// Pairs with the encoder bundle produced by the companion notebook.
// Full article: https://jeanrojas.com/blog/splitting-sam2-encoder-decoder
import * as ort from "onnxruntime-web/webgpu";
export interface Embedding {
imageEmbed: ort.Tensor;
@jeanc18rlos
jeanc18rlos / 1_install.sh
Created May 13, 2026 19:30
SAM2 hybrid encoder notebook — 5 cells: install, download checkpoint, export ONNX, encode image, package for browser. Companion to https://jeanrojas.com/blog/splitting-sam2-encoder-decoder
# Cell 1 — pin a working torch + the ONNX toolchain.
pip install --quiet \
torch==2.4.0 \
torchvision==0.19.0 \
onnx onnxscript onnxsim onnxruntime samexporter
pip install --quiet git+https://github.com/facebookresearch/segment-anything-2.git