hacksider/Deep-Live-Cam · error · ValueError
max_k must be at least 1
Error message
max_k must be at least 1
What it means
ValueError raised by find_cluster_centroids in modules/cluster_analysis.py when the max_k argument is less than 1. max_k caps how many candidate cluster counts (K = 1..max_k) the elbow search evaluates, so 0 or negative values make the search range empty and meaningless. The function validates this up front and fails fast.
Source
Thrown at modules/cluster_analysis.py:11
import numpy as np
from sklearn.cluster import KMeans
from typing import Any
def find_cluster_centroids(embeddings, max_k=10) -> Any:
n_samples = len(embeddings)
if n_samples == 0:
raise ValueError("embeddings must not be empty")
if max_k < 1:
raise ValueError("max_k must be at least 1")
max_k = min(max_k, n_samples)
if max_k == 1:
kmeans = KMeans(n_clusters=1, random_state=0)
kmeans.fit(embeddings)
return kmeans.cluster_centers_
inertia = []
cluster_centroids = []
K = range(1, max_k+1)
for k in K:
kmeans = KMeans(n_clusters=k, random_state=0)
kmeans.fit(embeddings)
inertia.append(kmeans.inertia_)
cluster_centroids.append({"k": k, "centroids": kmeans.cluster_centers_})
diffs = [inertia[i] - inertia[i+1] for i in range(len(inertia)-1)]View on GitHub (pinned to 987f6b392b)
Solutions
- Pass an explicit max_k >= 1 (the default of 10 is safe; it is internally clamped via min(max_k, n_samples)).
- If max_k is computed, clamp it: max_k = max(1, min(max_k, n_samples)) before the call.
- Fix the config/CLI source that produced 0 or a negative value.
Example fix
# before max_k = len(embeddings) // 10 centroids = find_cluster_centroids(embeddings, max_k) # after max_k = max(1, len(embeddings) // 10) centroids = find_cluster_centroids(embeddings, max_k)
Defensive patterns
Strategy: validation
Validate before calling
max_k = max(1, min(max_k, len(embeddings))) if embeddings else 1 centroids = find_cluster_centroids(embeddings, max_k=max_k)
Try / catch
try:
centroids = find_cluster_centroids(embeddings, max_k=k)
except ValueError as e:
if "max_k must be at least 1" in str(e):
k = max(1, k)
centroids = find_cluster_centroids(embeddings, max_k=k)
else:
raise Prevention
- Never derive max_k with unclamped integer math (n // divisor can hit 0).
- Validate config-supplied k values against 1 <= k <= n_samples at load time.
- Remember the function already clamps max_k to n_samples, so only the >= 1 floor is your job.
When it happens
Trigger: Calling find_cluster_centroids(embeddings, max_k=0) or with a negative max_k. Most often max_k is computed from data or config (e.g. max_k = n // 10, or a CLI/config value) and the computation yields 0 for a small n or a misconfigured setting.
Common situations: max_k derived as a fraction of sample count with small inputs (n=5, max_k=n//10 -> 0); a config file or CLI flag with an unset/zero k value; unit tests parameterized with edge-case k values; off-by-one when converting an inclusive/exclusive upper bound.
Related errors
- embeddings must not be empty
- {NAME}: Model not found at {model_path}
- {NAME}: Failed to load GFPGAN ONNX model: {e}
- {NAME}: Failed to initialize GFPGAN ONNX session. Check logs
- Model file not found: {model_path}
AI-assisted analysis of hacksider/Deep-Live-Cam@987f6b392b (2026-08-14).
Data as JSON: /api/errors/d7e23d432fb21f17.
Report an issue: GitHub.