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.1.tar.gz (32.8 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.1-py3-none-any.whl (33.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nystrom_ncut-0.4.1.tar.gz
  • Upload date:
  • Size: 32.8 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.1.tar.gz
Algorithm Hash digest
SHA256 e1d820c1fc1a22dfa53c772203bde2c0e8f772a76fa6bf71c476a14ce2425991
MD5 fed9de4daa7306b61ddf3a80391a595e
BLAKE2b-256 581fc122738002381e3f61475c13a995d8c700eb2dd6be42c66a728e6ab8a2be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nystrom_ncut-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 33.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5c9d32be98384a333c467da58a1c2e6e47d7cfa9c728587a33ae4b590f2dac4a
MD5 8f0481f3783a7b9734eb90a854b8a7c5
BLAKE2b-256 483085d5fd9b6d0be42f91bd4a19cb6f10b77e934d8a03b23989140812fc6a0a

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