Metadata-Version: 2.5
Name: semsketch
Version: 0.1.1
Summary: Dense per-pixel image features from a classical filter bank and foundation models, and the classifiers SemSketch trains on them
Project-URL: Homepage, https://github.com/Radical-AI/SemSketch
Author: Radical AI
License-Expression: GPL-3.0-or-later
License-File: LICENSE
License-File: NOTICE
License-File: THIRD_PARTY_NOTICES.md
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Image Processing
Requires-Python: <3.13,>=3.12.0
Requires-Dist: cupy-cuda12x<15,>=14.0.1; sys_platform == 'linux'
Requires-Dist: diffusers>=0.35
Requires-Dist: einops>=0.8.0
Requires-Dist: huggingface-hub>=0.34
Requires-Dist: numpy>=2.3
Requires-Dist: open-clip-torch>=3.3.0
Requires-Dist: opencv-python-headless<5,>=4.10.0
Requires-Dist: pillow>=10.4.0
Requires-Dist: psutil>=6.0.0
Requires-Dist: pycocotools>=2.0.7
Requires-Dist: safetensors>=0.4
Requires-Dist: sam3>=0.1.0
Requires-Dist: scikit-learn>=1.6.1
Requires-Dist: scipy>=1.14.0
Requires-Dist: setuptools<81,>=68
Requires-Dist: timm>=1.0.20
Requires-Dist: torch>=2.11
Requires-Dist: torchvision>=0.26
Requires-Dist: transformers>=5
Requires-Dist: xgboost>=3.0.5; sys_platform == 'linux'
Description-Content-Type: text/markdown

# semsketch

Dense per-pixel image features from a classical filter bank and foundation models, as used by
[SemSketch](https://github.com/Radical-AI/SemSketch)'s interactive segmentation, plus the classifiers it trains on them.

```python
import torch
from semsketch import Embedder, available_models

# Classical features only: runs anywhere, including the CPU.
features = Embedder(classical=True)(torch.rand(2, 1, 256, 256))  # (2, 37, 256, 256)

# Foundation models (CUDA or Apple's MPS); weights download on first use.
print(available_models())
embedder = Embedder(classical=False, models=["diffusion/ssd1b", "sam3/neck12"])
features = embedder(images)  # (B, 640 + 320, H, W)

# Each source separately.
by_source = embedder.embed_sources(images)  # {"diffusion/ssd1b": ..., "sam3/neck12": ...}

# Each model reduced to its own number of PCA components; None keeps a model's raw channels.
embedder = Embedder(models={"diffusion/ssd1b": 32, "sam3/neck12": 16, "alibi-dinov3": None})
features = embedder(images)  # (B, 37 * image channels + 32 + 16 + 384, H, W)
```

A reduced model's PCA is fitted per image on its backbone grid, as SemSketch's segmenter fits it, so components are
not comparable across images. Each reduced model is centered and scaled to unit mean variance, so the sources weigh
equally in a classifier.

Images are `(B, 1 or 3, H, W)` tensors, float in `[0, 1]` or `uint8`; a single `(C, H, W)` or `(H, W)` image is
treated as a batch of one. The output is `(B, C, H, W)` float32, the sources concatenated in `embedder.sources` order.

## Models

- `classical`: 37 channels per image channel (Gaussian, Sobel, Hessian and difference-of-Gaussians responses at
  several scales, and membrane projections).
- `diffusion/<unet>`: Stable Diffusion UNet activations (`ssd1b` is SemSketch's default).
- `sam3/neck`, `sam3/neck12`: SAM3 image-encoder features.
- `vulture/dinov2_vits14`: Vulture's FeatUp-upsampled DINOv2. It needs tldr-group's Vulture, which PyPI does not
  carry: `pip install https://github.com/tldr-group/vulture/archive/c3c728a326d99217bac6956c936af277a92bacda.tar.gz`.
- `alibi-dinov3`: SemSketch's ALiBi DINOv3 backbone.
- Any DINOv3 or C-RADIO Hugging Face ID, or a local `.pth` backbone checkpoint.

Each model's grid is lifted bilinearly to every pixel; the features are the raw lifted channels, without the per-image
PCA SemSketch's segmenter fits on them. OpenAI embeddings are not supported.

## Downloads

Weights go to Hugging Face's cache (`~/.cache/huggingface`, or `HF_HOME`) unless `Embedder(model_dir=...)` or
`SEMSKETCH_MODEL_DIR` names another folder. Gated models (SAM3, DINOv3) ask for a Hugging Face token when none is set
(`HF_TOKEN` or `hf auth login`), and say where to accept their licence.

## Licence

GPL-3.0-or-later. The models it downloads have their own licences; see
[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
