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="rbf",
    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.2.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.2-py3-none-any.whl (33.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nystrom_ncut-0.4.2.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.2.tar.gz
Algorithm Hash digest
SHA256 93e41328826035c0fb6b2d105be088f6321b622fbd5a3fdd10a07ba599045752
MD5 7b0edd2faabf2b53179cde02e2cffb57
BLAKE2b-256 143d597887d761a17dc13f284f0aee7644ac6875a9f91f6773af67a70c91928e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nystrom_ncut-0.4.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9d257ac47fa83aa95023c09b6836d712747f3ab1e84fee96368a3f35df96338d
MD5 a3e04e009c1fe5117f7b51ca642bb20b
BLAKE2b-256 ec738883f4eb1c8b806c8b20ccb52e399cede771ea7b162f55ed82cb5c79d18e

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