From 7de16475e1a9e6a19a3512222ca60e91c0a085f0 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 17 Feb 2022 12:44:52 +0100 Subject: [PATCH] Handle `scipy.cluster.vq.kmeans` too few points Resolves #6664 --- utils/autoanchor.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/utils/autoanchor.py b/utils/autoanchor.py index eef8f6499194..d055de312f69 100644 --- a/utils/autoanchor.py +++ b/utils/autoanchor.py @@ -126,9 +126,10 @@ def print_results(k, verbose=True): # Kmeans calculation LOGGER.info(f'{PREFIX}Running kmeans for {n} anchors on {len(wh)} points...') s = wh.std(0) # sigmas for whitening - k, dist = kmeans(wh / s, n, iter=30) # points, mean distance - assert len(k) == n, f'{PREFIX}ERROR: scipy.cluster.vq.kmeans requested {n} points but returned only {len(k)}' - k *= s + k = kmeans(wh / s, n, iter=30)[0] * s # points + if len(k) != n: # kmeans may return fewer points than requested if wh is insufficient or too similar + LOGGER.warning(f'{PREFIX}WARNING: scipy.cluster.vq.kmeans returned only {len(k)} of {n} requested points') + k = np.sort(np.random.rand(n * 2)).reshape(n, 2) * img_size # random init wh = torch.tensor(wh, dtype=torch.float32) # filtered wh0 = torch.tensor(wh0, dtype=torch.float32) # unfiltered k = print_results(k, verbose=False)