Spaces:
Running on Zero
Running on Zero
Run kway ncut on CPU
Browse files- dino_correspondence.py +10 -2
dino_correspondence.py
CHANGED
|
@@ -24,6 +24,14 @@ def _get_compute_device(tensor: torch.Tensor) -> str:
|
|
| 24 |
device_type = tensor.device.type
|
| 25 |
return device_type if device_type in {"cpu", "cuda"} else "cpu"
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def ncut_tsne_multiple_images(image_embeds, n_eig=50, gamma=None, degree=0.5):
|
| 28 |
"""
|
| 29 |
Apply NCut and t-SNE coloring to multiple image embeddings.
|
|
@@ -68,7 +76,7 @@ def _kway_cluster_single_image(image_embeds, n_clusters, gamma=None, degree=0.5)
|
|
| 68 |
flattened_input, n_eig=n_eig, gamma=gamma, device=compute_device
|
| 69 |
)
|
| 70 |
|
| 71 |
-
continuous_clusters =
|
| 72 |
return continuous_clusters
|
| 73 |
|
| 74 |
|
|
@@ -111,7 +119,7 @@ def kway_cluster_multiple_images(image_embeds, n_clusters, gamma=None, degree=0.
|
|
| 111 |
flattened_input, n_eig=n_eig, gamma=gamma, device=compute_device
|
| 112 |
)
|
| 113 |
|
| 114 |
-
continuous_clusters =
|
| 115 |
continuous_clusters = rearrange(
|
| 116 |
continuous_clusters, '(b l) c -> b l c', b=batch_size
|
| 117 |
)
|
|
|
|
| 24 |
device_type = tensor.device.type
|
| 25 |
return device_type if device_type in {"cpu", "cuda"} else "cpu"
|
| 26 |
|
| 27 |
+
|
| 28 |
+
def _run_kway_ncut_on_cpu(eigenvectors: torch.Tensor, n_clusters: int) -> torch.Tensor:
|
| 29 |
+
"""Keep NCut discretization on CPU to avoid unsupported CUDA kernels on HF ZeroGPU."""
|
| 30 |
+
return kway_ncut(
|
| 31 |
+
eigenvectors[:, :n_clusters].cpu(),
|
| 32 |
+
device="cpu",
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
def ncut_tsne_multiple_images(image_embeds, n_eig=50, gamma=None, degree=0.5):
|
| 36 |
"""
|
| 37 |
Apply NCut and t-SNE coloring to multiple image embeddings.
|
|
|
|
| 76 |
flattened_input, n_eig=n_eig, gamma=gamma, device=compute_device
|
| 77 |
)
|
| 78 |
|
| 79 |
+
continuous_clusters = _run_kway_ncut_on_cpu(eigenvectors, n_clusters)
|
| 80 |
return continuous_clusters
|
| 81 |
|
| 82 |
|
|
|
|
| 119 |
flattened_input, n_eig=n_eig, gamma=gamma, device=compute_device
|
| 120 |
)
|
| 121 |
|
| 122 |
+
continuous_clusters = _run_kway_ncut_on_cpu(eigenvectors, n_clusters)
|
| 123 |
continuous_clusters = rearrange(
|
| 124 |
continuous_clusters, '(b l) c -> b l c', b=batch_size
|
| 125 |
)
|