Skip to main content

Normalized Cut and Nyström Approximation

Project description

nystrom-ncut

PyTorch implementations of Normalized Cut (spectral clustering) for very large graphs, with two complementary approximations:

Both are built on the original normalized-cut formulation (Shi & Malik 2000, Normalized Cuts and Image Segmentation).

Installation

pip install nystrom-ncut

Requires Python >=3.10 and a working torch + pytorch3d installation. For a CUDA build of PyTorch, install it explicitly before installing this package, e.g.:

pip install torch --index-url https://download.pytorch.org/whl/cu121
pip install nystrom-ncut

For development:

pip install -e ".[dev]"

Quick start

Nyström Normalized Cut

import torch
from nystrom_ncut import NystromNCut, SampleConfig

# (B, H, W, C) features from some backbone
model_features = torch.rand(20, 64, 64, 768)
inp = model_features.reshape(-1, 768)

ncut = NystromNCut(
    n_components=100,
    affinity_type="cosine",
    sample_config=SampleConfig(method="fps", num_sample=10000),
)
eigvectors = ncut.fit_transform(inp)              # (20*64*64, 100)
eigvalues = ncut.eigenvalues_                     # (100,)
eigvectors = eigvectors.reshape(20, 64, 64, 100)

Random-feature Kernel Normalized Cut

import torch
from nystrom_ncut import KernelNCut

inp = torch.rand(20 * 64 * 64, 768)
kn = KernelNCut(
    n_components=100,
    kernel_dim=1024,
    affinity_type="rbf",
)
eigvectors = kn.fit_transform(inp)

Distance Realization (MDS-style embedding)

import torch
from nystrom_ncut import DistanceRealization

inp = torch.rand(10000, 768)
dr = DistanceRealization(n_components=64, distance_type="euclidean")
embedding = dr.fit_transform(inp)                 # (10000, 64)

Discretizing eigenvectors into clusters

AxisAlign implements the Yu & Shi 2003 multiclass discretization on top of the spectral embedding:

from nystrom_ncut import AxisAlign

aligner = AxisAlign(sort_method="norm")
hard_labels = aligner.fit_transform(eigvectors, hard=True)  # (N,) int cluster ids

Sampling

SampleConfig controls how anchor points are picked for the Nyström and kernel methods. Available methods are:

  • "full" — use every point (no subsampling).
  • "random" — uniform random subset.
  • "fps" — farthest-point sampling on a low-rank PCA of the features (default for accuracy).
  • "fps_recursive" — iteratively refine FPS anchors using the current spectral embedding.
from nystrom_ncut import SampleConfig

cfg = SampleConfig(method="fps", num_sample=10000, fps_dim=12)

Repository layout

src/nystrom_ncut/
├── kernel/             # Random-feature NCut
├── nystrom/            # Nystrom NCut + distance realization
├── transformer/        # OnlineTransformerSubsampleFit, AxisAlign, mixins
├── common.py
├── distance_utils.py
├── extrapolation.py    # KNN extrapolation of anchors to new points
├── coloring.py         # RGB visualization helpers
└── sampling_utils.py

Citation

If you find this useful, please cite the upstream NCUT writeup:

AlignedCut: Visual Concepts Discovery on Brain-Guided Universal Feature Space. Huzheng Yang, James Gee, Jianbo Shi, 2024.

License

MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nystrom_ncut-0.4.0.tar.gz (27.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nystrom_ncut-0.4.0-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file nystrom_ncut-0.4.0.tar.gz.

File metadata

  • Download URL: nystrom_ncut-0.4.0.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for nystrom_ncut-0.4.0.tar.gz
Algorithm Hash digest
SHA256 642c22bda8449401e82343e977a0d9214fd2604fe1fef9d84becd910bf3be380
MD5 2014b799c983ef0c2c5a97aab99af9dc
BLAKE2b-256 94a4ac32cb94b4f4b13fdaa77bae54da29fe6ba7b143ed1e5e8e35b488919f49

See more details on using hashes here.

File details

Details for the file nystrom_ncut-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: nystrom_ncut-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for nystrom_ncut-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71fa1ebbea8945592fb0df9e253dd8133e4b3b0476f66652a2d4790010444a1a
MD5 706c79ca26513c875ad84a18a81c7d6f
BLAKE2b-256 d717600733b0b612ded08e54b687b2151883f12173bd8d06ee35e37b1d33a0dc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page