Skip to content

Commit

Permalink
Handle scipy.cluster.vq.kmeans too few points
Browse files Browse the repository at this point in the history
Resolves #6664
  • Loading branch information
glenn-jocher authored Feb 17, 2022
1 parent 7b80545 commit 7de1647
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions utils/autoanchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7de1647

Please sign in to comment.