API Reference
Main Interface
ncut_pytorch.ncut.Ncut
Class interface for Normalized Cut, save states of nystrom approximation, can be used to transform new data.
Source code in ncut_pytorch/ncut.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
__init__(n_eig=100, quantile_sigma=0.25, quantile_sigma_repulsion=0.2, sigma=None, repulsion_sigma=None, repulsion_weight=None, affinity_fn=rbf_affinity, extrapolation_factor=1.0, exact_gradient=False, device=None, **kwargs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_eig
|
int
|
number of eigenvectors |
100
|
n_eig
|
int
|
number of eigenvectors |
100
|
quantile_sigma
|
float
|
quantile of affinity sigma parameter, lower quantile_sigma results in sharper eigenvectors |
0.25
|
quantile_sigma_repulsion
|
float
|
quantile of repulsion sigma parameter, lower quantile_sigma_repulsion results in sharper eigenvectors |
0.2
|
sigma
|
float
|
affinity parameter, override d_sigma if provided |
None
|
repulsion_sigma
|
float
|
(if use repulsion) repulsion sigma parameter, default None (no repulsion) |
None
|
repulsion_weight
|
float
|
(if use repulsion) repulsion weight, default 0.2 |
None
|
affinity_fn
|
callable
|
affinity function, default rbf_affinity. Should accept (X1, X2=None, sigma=float) and return affinity matrix |
rbf_affinity
|
extrapolation_factor
|
float
|
control how far can we extrapolate, larger extrapolation_factor means we can extrapolate further, default 1.0 |
1.0
|
exact_gradient
|
bool
|
use full spectrum and exact gradient, can be slower and unstable, default False device (str): device, default 'auto' (auto detect GPU) |
False
|
Examples:
>>> from ncut_pytorch import Ncut
>>> import torch
>>> X = torch.rand(10000, 100)
>>> ncut = Ncut(n_eig=20)
>>> eigvec = ncut.fit_transform(X)
>>> eigval = ncut.eigval
>>> print(eigvec.shape, eigval.shape) # (10000, 20) (20,)
>>>
>>> # transform new data
>>> new_X = torch.rand(500, 100)
>>> new_eigvec = ncut.transform(new_X)
>>> print(new_eigvec.shape) # (500, 20)
Source code in ncut_pytorch/ncut.py
fit(X)
Fit the Ncut model to the input features. save states of nystrom approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Tensor
|
input features, shape (N, D) |
required |
Returns: ncut (Ncut): Ncut instance
Source code in ncut_pytorch/ncut.py
fit_transform(X)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Tensor
|
input features, shape (N, D) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
eigvec |
Tensor
|
eigenvectors, shape (N, n_eig) |
kway_fit(n_clusters, n_eig, kmeans_iter=10)
Fit and cache a k-way rotation matrix for the fitted eigenvectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_clusters
|
int
|
number of output clusters. |
required |
n_eig
|
int
|
number of leading eigenvectors to use. |
required |
kmeans_iter
|
int
|
number of k-means refinement iterations. |
10
|
Returns:
| Name | Type | Description |
|---|---|---|
Ncut |
Ncut
|
current instance. |
Source code in ncut_pytorch/ncut.py
kway_transform(X, n_clusters, n_eig)
Transform data with a previously fitted k-way rotation matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Tensor
|
input features, shape (N, D). |
required |
n_clusters
|
int
|
number of output clusters. |
required |
n_eig
|
int
|
number of leading eigenvectors to use. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: rotated eigenvectors, shape (N, n_clusters). |
Source code in ncut_pytorch/ncut.py
transform(X)
Transform new data using the fitted Ncut model and it's saved states of nystrom approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Tensor
|
input features, shape (N, D) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
eigvec |
Tensor
|
eigenvectors, shape (N, n_eig) |
Source code in ncut_pytorch/ncut.py
Core Ncut Functions
ncut_pytorch.ncuts.ncut_nystrom.ncut_fn(X, n_eig=100, quantile_sigma=0.25, quantile_sigma_repulsion=0.2, sigma=None, repulsion_sigma=None, repulsion_weight=None, affinity_fn=rbf_affinity, extrapolation_factor=1.0, exact_gradient=False, device=None, make_orthogonal=False, no_propagation=False, **kwargs)
Normalized Cut, balanced sampling and nystrom approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Tensor
|
input features, shape (N, D) |
required |
n_eig
|
int
|
number of eigenvectors |
100
|
quantile_sigma
|
float
|
quantile of affinity sigma parameter, lower quantile_sigma results in sharper eigenvectors |
0.25
|
quantile_sigma_repulsion
|
float
|
quantile of repulsion sigma parameter, lower quantile_sigma_repulsion results in sharper eigenvectors |
0.2
|
sigma
|
float
|
affinity parameter, override d_sigma if provided |
None
|
repulsion_sigma
|
float
|
(if use repulsion) repulsion sigma parameter, default None (no repulsion) |
None
|
repulsion_weight
|
float
|
(if use repulsion) repulsion weight, default 0.2 |
None
|
affinity_fn
|
callable
|
affinity function, default rbf_affinity. Should accept (X1, X2=None, sigma=float) and return affinity matrix |
rbf_affinity
|
extrapolation_factor
|
float
|
control how far can we extrapolate, larger extrapolation_factor means we can extrapolate further, default 1.0 |
1.0
|
exact_gradient
|
bool
|
use full spectrum and exact gradient, can be slower and unstable, default False |
False
|
make_orthogonal
|
bool
|
make eigenvectors orthogonal |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
eigenvectors |
Tensor
|
shape (N, n_eig) |
eigenvalues |
Tensor
|
sorted in descending order, shape (n_eig,) |
Examples:
>>> from ncut_pytorch import ncut_fn
>>> import torch
>>> features = torch.rand(10000, 100)
>>> eigvec, eigval = ncut_fn(features, n_eig=20)
>>> print(eigvec.shape, eigval.shape) # (10000, 20) (20,)
Source code in ncut_pytorch/ncuts/ncut_nystrom.py
ncut_pytorch.ncuts.ncut_nystrom.nystrom_propagate(nystrom_out, X, nystrom_X, extrapolation_factor=1.0, device=None, return_indices=False, **kwargs)
propagate output from nystrom sampled nodes to all nodes, use a weighted sum of the nearest neighbors to propagate the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nystrom_out
|
Tensor
|
output from nystrom sampled nodes, shape (m, D) |
required |
X
|
Tensor
|
input features for all nodes, shape (N, D) |
required |
nystrom_X
|
Tensor
|
input features from nystrom sampled nodes, shape (m, D) |
required |
extrapolation_factor
|
float
|
control how far can we extrapolate, larger extrapolation_factor means we can extrapolate further, default 1.0 |
1.0
|
device
|
str
|
device to use for computation, if 'auto', will detect GPU automatically |
None
|
return_indices
|
bool
|
whether to return the indices used for propagation |
False
|
Returns:
| Type | Description |
|---|---|
Union[Tensor, tuple[Tensor, Tensor]]
|
torch.Tensor: output propagated by nearest neighbors, shape (N, D) |
Source code in ncut_pytorch/ncuts/ncut_nystrom.py
ncut_pytorch.ncuts.ncut_kway.kway_ncut(eigvec, n_clusters=None, device=None, ret_R=False, **kwargs)
K-way Ncut discretization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eigvec
|
Tensor
|
Eigenvectors from Ncut output. |
required |
n_clusters
|
int | None
|
Number of clusters to use. If None, will use the number of eigenvectors. |
None
|
device
|
str | None
|
Device to use for computation. |
None
|
ret_R
|
bool
|
Whether to return the rotation matrix. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor | tuple[Tensor, Tensor]
|
Discretized eigenvectors (rotation matrix if ret_R=True). |
Source code in ncut_pytorch/ncuts/ncut_kway.py
ncut_pytorch.ncuts.ncut_kway.axis_align(eigvec, device=None, max_iter=1000, n_sample=10240, sample_idx=None)
Multiclass Spectral Clustering (SX Yu, J Shi, 2003).
Source code in ncut_pytorch/ncuts/ncut_kway.py
ncut_pytorch.ncuts.ncut_kway.quick_kway(eigvec, n_clusters=10, n_eig=10, n_sample=10240, device=None, kmeans_iter=10, ret_R=False)
Quick K-way Ncut using K-means for rotation matrix.
Source code in ncut_pytorch/ncuts/ncut_kway.py
ncut_pytorch.ncuts.ncut_click.ncut_click_prompt(X, fg_indices, bg_indices=None, click_weight=0.5, bg_weight=0.1, n_eig=2, quantile_sigma=0.25, device=None, sigma=None, affinity_fn=rbf_affinity, exact_gradient=False, no_propagation=False, return_indices_and_sigma=False, **kwargs)
Source code in ncut_pytorch/ncuts/ncut_click.py
Predictors
ncut_pytorch.predictor.predictor.NcutPredictor
Source code in ncut_pytorch/predictor/predictor.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
ncut_pytorch.predictor.vision_predictor.NcutVisionPredictor
Source code in ncut_pytorch/predictor/vision_predictor.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
color_continues()
color the features by continues mspace color palette.
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: RGB image. (b, h, w, 3) |
Source code in ncut_pytorch/predictor/vision_predictor.py
color_discrete(cluster_assignment, draw_border=True)
color the features by discrete mspace color palette.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cluster_assignment
|
Tensor
|
Cluster assignment for the images. (b, h, w) |
required |
draw_boundaries
|
bool
|
Whether to draw boundaries. Defaults to True. |
required |
Returns:
| Type | Description |
|---|---|
List[Image]
|
List[Image.Image]: List of RGB images. |
Source code in ncut_pytorch/predictor/vision_predictor.py
generate(n_segment, n_eig=10)
generate the cluster assignment for the images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_cluster
|
int
|
Number of clusters to generate. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Cluster assignment for the images. (b, h, w) |
Source code in ncut_pytorch/predictor/vision_predictor.py
inference(images)
inference the mask and heatmap for new images, based on the saved states in the predict function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
List[Image]
|
List of images to inference. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
Tuple[torch.Tensor, torch.Tensor]: Mask and heatmap for the images. (b, h, w) |
Source code in ncut_pytorch/predictor/vision_predictor.py
predict(point_coords, point_labels, image_indices, click_weight=0.5, **kwargs)
predict the mask and heatmap for the images, based on the clicks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point_coords
|
ndarray
|
The coordinates of the points to predict, in original image resolution. (n, 2) |
required |
point_labels
|
ndarray
|
The labels of the points to predict, can be 1 (positive) or 0 (negative). (n, ) |
required |
image_indices
|
ndarray
|
The indices of the images corresponde to the point_coords. (n, ) |
required |
click_weight
|
float
|
The weight of the click. Defaults to 0.5. |
0.5
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
Tuple[torch.Tensor, torch.Tensor]: Mask and heatmap for the images. (b, h, w) |
Source code in ncut_pytorch/predictor/vision_predictor.py
preview(point_coord, image_indices)
preview the hierarchy cluster assignment for the images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point_coord
|
Tuple[int, int]
|
The coordinate of the point to preview, in original image resolution. |
required |
image_indices
|
int
|
The index of the image to preview, corresponds to the point_coord. |
required |
Returns:
| Type | Description |
|---|---|
List[Tensor]
|
List[torch.Tensor]: List of masks for each hierarchy level. each mask is (b, h, w) |
Source code in ncut_pytorch/predictor/vision_predictor.py
set_images(images, n_segments=(5, 25, 50, 100, 250))
set the images and save its features in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
List[Image]
|
List of images to set. |
required |
n_segments
|
List[int]
|
Number of segments to cache. n_segments is showed in the preview function. |
(5, 25, 50, 100, 250)
|
Source code in ncut_pytorch/predictor/vision_predictor.py
summary(n_segments=(5, 25, 50, 100, 250), n_eig=10, draw_border=True)
summary the cluster assignment for the images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_segments
|
List[int]
|
Number of segments to summary. |
(5, 25, 50, 100, 250)
|
Source code in ncut_pytorch/predictor/vision_predictor.py
ncut_pytorch.predictor.dino_predictor.NcutDinov3Predictor
Bases: NcutVisionPredictor
Source code in ncut_pytorch/predictor/dino_predictor.py
ncut_pytorch.predictor.dino_predictor.NcutDinoPredictor
ncut_pytorch.predictor.dino_predictor.NcutDinoPredictorFeatUp
ncut_pytorch.predictor.dino_predictor.NcutDinoPredictorSR
Color Utilities
ncut_pytorch.color.coloring.mspace_color(X, q=0.95, n_eig_list=[4, 16, 64], n_dim=3, encoder_training_steps=3000, decoder_training_steps=0, progress_bar=False, **kwargs)
Returns:
| Type | Description |
|---|---|
Tensor
|
Embedding in 2D, shape (n_samples, 2) |
Tensor
|
RGB color for each data sample, shape (n_samples, 3) |
Source code in ncut_pytorch/color/coloring.py
ncut_pytorch.color.coloring.tsne_color(X, num_sample=1000, perplexity=150, n_dim=3, metric='cosine', device=None, seed=None, q=0.95, knn=10, **kwargs)
Returns:
| Type | Description |
|---|---|
Tensor
|
Embedding in 2D, shape (n_samples, 2) |
Tensor
|
RGB color for each data sample, shape (n_samples, 3) |
Source code in ncut_pytorch/color/coloring.py
ncut_pytorch.color.coloring.umap_color(X, num_sample=1000, n_neighbors=150, min_dist=1.0, spread=10.0, n_dim=3, metric='cosine', device=None, seed=None, q=0.95, knn=10, **kwargs)
Returns:
| Type | Description |
|---|---|
Tensor
|
Embedding in 2D, shape (n_samples, 2) |
Tensor
|
RGB color for each data sample, shape (n_samples, 3) |
Source code in ncut_pytorch/color/coloring.py
ncut_pytorch.color.coloring.rotate_rgb_cube(rgb, position=1)
rotate RGB cube to different position
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rgb
|
Tensor
|
RGB color space [0, 1], shape (*, 3) |
required |
position
|
int
|
position to rotate, 0, 1, 2, 3, 4, 5, 6 |
1
|
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: RGB color space, shape (n_samples, 3) |
Source code in ncut_pytorch/color/coloring.py
Math & Utilities
ncut_pytorch.utils.math.rbf_affinity(X1, X2=None, sigma=1.0, zero_diag=False, gamma=None)
Computes RBF affinity matrix: W_ij = exp(-||x_i - x_j||^2 / (2 * sigma^2)).
Source code in ncut_pytorch/utils/math.py
ncut_pytorch.utils.math.cosine_affinity(X1, X2=None, sigma=1.0, repulse=False, zero_diag=False, gamma=None)
Computes cosine-based affinity matrix.
Source code in ncut_pytorch/utils/math.py
ncut_pytorch.utils.math.quantile_normalize(x, q=0.95)
Normalizes each dimension of x to [0, 1] using quantiles, robust to outliers.
Source code in ncut_pytorch/utils/math.py
ncut_pytorch.utils.sigma.find_sigma_by_degree(X, quantile_sigma=0.25, affinity_fn=rbf_affinity, X2=None, init_sigma=0.5, r_tol=0.01, max_iter=100, n_sample=1000)
Find sigma after FPS-based downsampling for efficiency.