Skip to main content

Normalized Cut and Spectral Embedding

Project description

🌐Documentation | 🤗HuggingFace Demo

Nyström Normalized Cut

Normalized Cut with Nyström approximation, handle large-scale graph with $O(n)$ time complexity, $O(1)$ space complexity. Solve million-scale graph in milliseconds.

https://github.com/user-attachments/assets/cdf53e33-bb34-4a84-b1f4-679b66da1d48

Video: Ncut spectral embedding eigenvectors, on SAM features.

Installation

pip install -U ncut-pytorch

Quick Start: plain Ncut

import torch
from ncut_pytorch import Ncut, kway_ncut
from ncut_pytorch.color import umap_color, mspace_color

features = torch.rand(1960, 768)
eigvecs = Ncut(n_eig=100).fit_transform(features)  # (1960, 20)

# Color visualizations
rgb_umap = umap_color(eigvecs[:, :20])      # UMAP-based RGB
rgb_mspace = mspace_color(features, n_eig=20)  # M-space RGB

# Discrete segmentation
n_cluster = 10
kway_eigvecs = kway_ncut(eigvecs[:, :n_cluster])
cluster_assignment = kway_eigvecs.argmax(1)
cluster_centroids = kway_eigvecs.argmax(0)

Quick Start: Ncut DINOv3 Predictor

from ncut_pytorch.predictor import NcutDinov3Predictor
from PIL import Image

predictor = NcutDinov3Predictor(model_cfg="dinov3_vitl16")
predictor = predictor.to('cuda')

images = [Image.open(f"images/view_{i}.jpg") for i in range(4)]
predictor.set_images(images)

image = predictor.summary(n_segments=[10, 25, 50, 100], draw_border=True)
display(image)

summary

More examples and detailed usage can be found in the examples directory.

Performance

  • ncut_pytorch.Ncut is $O(n)$ time complexity

  • sklearn.SpectralEmbedding is $O(n^2)$ time complexity.

Setup:

CPU: Intel(R) Core(TM) i9-13900K CPU

RAM: 128 GiB

GPU: RTX 4090 24 GiB

SYSTEM: Ubuntu 22.04.3 LTS

Run benchmark:

pytest unit_tests/bench_speed.py --benchmark-columns=mean,stddev --benchmark-sort=mean

Results:

------------- benchmark 'ncut-pytorch (CPU) vs sklearn': 8 tests ------------
Name (time in ms)                        Mean                StdDev          
-----------------------------------------------------------------------------
test_ncut_cpu_100_data_10_eig          2.5536 (1.0)          0.2782 (1.0)    
test_sklearn_100_data_10_eig           4.0913 (1.60)         1.6749 (6.02)   
test_ncut_cpu_300_data_10_eig          4.9034 (1.92)         1.6575 (5.96)   
test_sklearn_300_data_10_eig          10.1861 (3.99)         3.8870 (13.97)  
test_ncut_cpu_1000_data_10_eig        11.1968 (4.38)         1.7070 (6.13)   
test_ncut_cpu_3000_data_10_eig        38.6101 (15.12)        1.6379 (5.89)   
test_sklearn_1000_data_10_eig        193.5934 (75.81)        8.1933 (29.45)  
test_sklearn_3000_data_10_eig      1,246.4295 (488.11)   1,047.0191 (>1000.0)
-----------------------------------------------------------------------------
------------- benchmark 'ncut-pytorch (GPU) n_data': 5 tests -------------
Name (time in ms)                         Mean            StdDev          
--------------------------------------------------------------------------
test_ncut_gpu_100_data_10_eig           2.9564 (1.0)      0.1816 (1.0)    
test_ncut_gpu_1000_data_10_eig          4.6938 (1.59)     0.3933 (2.17)   
test_ncut_gpu_10000_data_10_eig        67.9607 (22.98)    4.0902 (22.52)  
test_ncut_gpu_100000_data_10_eig      396.9994 (134.29)   3.6202 (19.93)  
test_ncut_gpu_1000000_data_10_eig     798.4598 (270.08)   1.5704 (8.65)   
--------------------------------------------------------------------------
------------- benchmark 'ncut-pytorch (GPU) n_eig': 3 tests --------------
Name (time in ms)                         Mean            StdDev          
--------------------------------------------------------------------------
test_ncut_gpu_10000_data_10_eig        67.9607 (1.0)      4.0902 (10.76)  
test_ncut_gpu_10000_data_100_eig       74.0033 (1.09)     0.7856 (2.07)   
test_ncut_gpu_10000_data_1000_eig     179.8690 (2.65)     0.3801 (1.0)    
--------------------------------------------------------------------------

Run benchmark:

python unit_tests/bench_memory.py

Results:

ncut-pytorch.Ncut is $O(1)$ space complexity

+---------------+------------------------+
| Data Points   |   Peak GPU Memory (MB) |
+===============+========================+
| 1,000         |                   8.14 |
+---------------+------------------------+
| 10,000        |                   0.1  |
+---------------+------------------------+
| 100,000       |                   0.39 |
+---------------+------------------------+
| 1,000,000     |                   0.39 |
+---------------+------------------------+

Citation

@misc{yang2024alignedcutvisualconceptsdiscovery,
      title={AlignedCut: Visual Concepts Discovery on Brain-Guided Universal Feature Space}, 
      author={Huzheng Yang and James Gee and Jianbo Shi},
      year={2024},
      eprint={2406.18344},
      archivePrefix={arXiv},
      primaryClass={cs.CV},
      url={https://arxiv.org/abs/2406.18344}, 
}

Project details


Release history Release notifications | RSS feed

This version

2.4.0

Download files

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

Source Distribution

ncut_pytorch-2.4.0.tar.gz (45.3 kB view details)

Uploaded Source

Built Distribution

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

ncut_pytorch-2.4.0-py3-none-any.whl (53.0 kB view details)

Uploaded Python 3

File details

Details for the file ncut_pytorch-2.4.0.tar.gz.

File metadata

  • Download URL: ncut_pytorch-2.4.0.tar.gz
  • Upload date:
  • Size: 45.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ncut_pytorch-2.4.0.tar.gz
Algorithm Hash digest
SHA256 14de6eeb824d727987118d6b208e9944e8c6f927c430140f7ace3401577f69e9
MD5 81699593634b77ce742668de9e46bfa6
BLAKE2b-256 078cd6a7ba626e07886caa0d3c54317539079cedaf32458308094ea2dad3f6c9

See more details on using hashes here.

File details

Details for the file ncut_pytorch-2.4.0-py3-none-any.whl.

File metadata

  • Download URL: ncut_pytorch-2.4.0-py3-none-any.whl
  • Upload date:
  • Size: 53.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ncut_pytorch-2.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78e9598063e5a948377e33ca9461dba775a3fb99ee33373d5622eec0d5ce8d98
MD5 c73f2ed778c5921ec551ce60c9cbcc5a
BLAKE2b-256 3d7bf5c91ea38bb171bf7cbd4ac491c89d8318686bb3366149c8908509798a26

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