Metadata-Version: 2.4
Name: tasnif
Version: 0.2.0
Summary: Unsupervised image clustering with modern deep embeddings (timm, CLIP), PCA and K-Means.
Project-URL: Homepage, https://github.com/cobanov/tasnif
Project-URL: Repository, https://github.com/cobanov/tasnif
Project-URL: Issues, https://github.com/cobanov/tasnif/issues
Project-URL: Changelog, https://github.com/cobanov/tasnif/blob/main/CHANGELOG.md
Author-email: Mert Cobanov <mertcobanov@gmail.com>
License: MIT License
        
        Copyright (c) 2024 Mert Cobanov
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: clip,computer-vision,embeddings,image-clustering,kmeans,timm,unsupervised-learning
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Image Processing
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: matplotlib>=3.7
Requires-Dist: numpy>=1.24
Requires-Dist: pillow>=10.0
Requires-Dist: rich>=13.0
Requires-Dist: scikit-learn>=1.3
Requires-Dist: timm>=1.0
Requires-Dist: torch>=2.1
Requires-Dist: torchvision>=0.16
Requires-Dist: tqdm>=4.65
Provides-Extra: all
Requires-Dist: open-clip-torch>=2.24; extra == 'all'
Requires-Dist: typer>=0.12; extra == 'all'
Requires-Dist: umap-learn>=0.5; extra == 'all'
Provides-Extra: cli
Requires-Dist: typer>=0.12; extra == 'cli'
Provides-Extra: clip
Requires-Dist: open-clip-torch>=2.24; extra == 'clip'
Provides-Extra: dev
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: open-clip-torch>=2.24; extra == 'dev'
Requires-Dist: pre-commit>=3.7; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Requires-Dist: typer>=0.12; extra == 'dev'
Requires-Dist: types-tqdm; extra == 'dev'
Provides-Extra: umap
Requires-Dist: umap-learn>=0.5; extra == 'umap'
Description-Content-Type: text/markdown

<p align="center">
  <img src="assets/asdd.png" width="350" alt="Tasnif">
</p>

<p align="center">
  <strong>Unsupervised image clustering with modern deep embeddings.</strong>
</p>

<p align="center">
  <a href="https://pypi.org/project/tasnif/"><img alt="PyPI" src="https://img.shields.io/pypi/v/tasnif"></a>
  <a href="https://pypi.org/project/tasnif/"><img alt="Python" src="https://img.shields.io/pypi/pyversions/tasnif"></a>
  <a href="https://github.com/cobanov/tasnif/actions"><img alt="CI" src="https://github.com/cobanov/tasnif/actions/workflows/ci.yml/badge.svg"></a>
  <a href="LICENSE"><img alt="License" src="https://img.shields.io/badge/license-MIT-blue"></a>
</p>

`tasnif` turns a folder of images into clusters you can browse on disk — no
labels required. It uses modern pretrained vision backbones (`timm` by default,
optionally CLIP via `open_clip`), reduces the embedding with PCA, and runs
K-Means on top.

## Highlights

- **Modern backbones** — any [`timm`](https://github.com/huggingface/pytorch-image-models) model (ResNet, ConvNeXt, ViT, DINOv2, ...) or [CLIP](https://github.com/mlfoundations/open_clip) via the `[clip]` extra.
- **GPU / MPS / CPU** with automatic device detection.
- **scikit-learn-style API**: `fit`, `predict`, `fit_predict`, `transform`.
- **Rich export**: per-cluster folders, CSV manifest, JSON summary, preview grids, raw embeddings.
- **Multiple materialization modes**: `copy`, `symlink`, `move`, or `none` (metadata only).
- **First-class CLI** powered by [Typer](https://typer.tiangolo.com/).
- **Pluggable embedders** — register your own backend.
- **Deterministic** with a `random_state` seed.

## Installation

`tasnif` is built and packaged with [uv](https://docs.astral.sh/uv/), but plain pip works too.

```bash
pip install tasnif                # core
pip install "tasnif[cli]"         # core + CLI
pip install "tasnif[clip]"        # core + CLIP backend
pip install "tasnif[all]"         # everything
```

Development setup:

```bash
git clone https://github.com/cobanov/tasnif
cd tasnif
uv sync --extra dev
uv run pytest
```

## Quickstart — Python API

```python
from tasnif import TasnifClusterer

clf = TasnifClusterer(n_clusters=5, embedder="timm", pca_dim=16)
clf.fit("photos/")
result = clf.result_

# Inspect without exporting
print(result.counts)            # e.g. [42, 31, 28, 19, 7]
print(result.silhouette)        # None unless compute_silhouette=True
mapping = result.as_dict()      # {Path('photos/a.jpg'): 2, ...}
buckets = result.by_cluster()   # {0: [Path(...), ...], 1: [...]}

# Export to disk
clf.export("output/", mode="copy")
```

## One-shot helper

```python
from tasnif import cluster_directory

cluster_directory("photos/", "output/", n_clusters=5, mode="symlink")
```

## CLI

```bash
# Cluster a directory with the default ResNet-50 (timm) backbone
tasnif cluster photos/ -k 5 -o output/

# Use CLIP and copy via symlink (fast, non-destructive)
tasnif cluster photos/ -k 8 --embedder clip --model ViT-B-32 --mode symlink

# Just compute embeddings, save .npy + .json
tasnif embed photos/ --embedder timm --model convnext_base -o embeddings.npy

# List available backends
tasnif backends
```

Run `tasnif --help` to see all options.

## Use a different model

Default is `timm:resnet50`. Pass any `timm` model name:

```python
clf = TasnifClusterer(
    n_clusters=8,
    embedder="timm",
    embedder_kwargs={"model": "vit_base_patch14_dinov2.lvd142m", "device": "cuda"},
)
```

CLIP:

```python
clf = TasnifClusterer(
    n_clusters=8,
    embedder="clip",
    embedder_kwargs={"model": "ViT-L-14", "pretrained": "laion2b_s32b_b82k"},
)
```

## Custom backend

Anything implementing the [`Embedder`](src/tasnif/embeddings/base.py) protocol works:

```python
import numpy as np
from tasnif import TasnifClusterer, register_embedder

class MyEncoder:
    name = "my-encoder"
    @property
    def dim(self): return 128
    @property
    def device(self): return "cpu"
    def embed(self, images, *, batch_size=32, show_progress=True):
        return np.stack([extract(img) for img in images])

register_embedder("my-encoder", lambda: MyEncoder())

clf = TasnifClusterer(n_clusters=5, embedder="my-encoder")
```

## Building blocks

All pieces are independently usable:

```python
from tasnif import (
    discover_images, create_embedder,
    reduce_pca, fit_kmeans, export_clusters, ClusterResult,
)

paths = discover_images("photos/")
embedder = create_embedder("timm", model="resnet50")
embeddings = embedder.embed([open_pil(p) for p in paths])
reduced = reduce_pca(embeddings, n_components=16)
fit = fit_kmeans(reduced, n_clusters=5, compute_silhouette=True)

result = ClusterResult(
    labels=fit.labels, centroids=fit.centroids, counts=fit.counts,
    paths=tuple(paths), n_clusters=5, inertia=fit.inertia, silhouette=fit.silhouette,
    embedder=embedder.name, pca_dim=16,
)
export_clusters(result, "output/")
```

## Migrating from 0.1.x

The 0.1.x API (`Tasnif().read().calculate().export()`) was removed in 0.2.0.
Replace with:

```diff
- from tasnif import Tasnif
- c = Tasnif(num_classes=5, pca_dim=16, use_gpu=False)
- c.read("photos/")
- c.calculate()
- c.export("output/")
+ from tasnif import TasnifClusterer
+ clf = TasnifClusterer(n_clusters=5, pca_dim=16, embedder_kwargs={"device": "auto"})
+ clf.fit("photos/")
+ clf.export("output/")
```

See [CHANGELOG.md](CHANGELOG.md) for the full list of breaking changes.

## Contributing

Issues and PRs welcome. Before submitting, run:

```bash
uv run ruff check . && uv run ruff format --check .
uv run mypy
uv run pytest
```

Pre-commit is configured — install hooks with `uv run pre-commit install`.

## License

MIT — see [LICENSE](LICENSE).
