From 31a04e6b85398e1effc1382a26d77d5793ca4dee Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 4 Mar 2022 10:18:59 +0100 Subject: [PATCH 1/3] Update AutoAnchor --- utils/autoanchor.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/utils/autoanchor.py b/utils/autoanchor.py index 51d4de306efd..59766801fa40 100644 --- a/utils/autoanchor.py +++ b/utils/autoanchor.py @@ -126,12 +126,17 @@ def print_results(k, verbose=True): # wh = wh * (npr.rand(wh.shape[0], 1) * 0.9 + 0.1) # multiply by random scale 0-1 # Kmeans calculation - LOGGER.info(f'{PREFIX}Running kmeans for {n} anchors on {len(wh)} points...') - s = wh.std(0) # sigmas for whitening - 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') + try: + LOGGER.info(f'{PREFIX}Running kmeans for {n} anchors on {len(wh)} points...') + assert n <= len(wh) # apply overdetermined constraint + s = wh.std(0) # sigmas for whitening + k = kmeans(wh / s, n, iter=30)[0] * s # points + assert n == len(k) # kmeans may return fewer points than requested if wh is insufficient or too similar + except Exception: + LOGGER.warning(f'{PREFIX}WARNING: switching strategies from kmeans to random init') k = np.sort(npr.rand(n * 2)).reshape(n, 2) * img_size # random init + + # k, wh, wh0 = [x.to(wh.device).type(torch.float32) for x in (k, wh, wh0)] wh = torch.tensor(wh, dtype=torch.float32) # filtered wh0 = torch.tensor(wh0, dtype=torch.float32) # unfiltered k = print_results(k, verbose=False) From 1df411ea62d5b27edcb03b0d8035f97648490816 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 4 Mar 2022 10:21:34 +0100 Subject: [PATCH 2/3] Update AutoAnchor --- utils/autoanchor.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/utils/autoanchor.py b/utils/autoanchor.py index 59766801fa40..1a2bfe36c1b1 100644 --- a/utils/autoanchor.py +++ b/utils/autoanchor.py @@ -125,7 +125,7 @@ def print_results(k, verbose=True): wh = wh0[(wh0 >= 2.0).any(1)] # filter > 2 pixels # wh = wh * (npr.rand(wh.shape[0], 1) * 0.9 + 0.1) # multiply by random scale 0-1 - # Kmeans calculation + # Kmeans init try: LOGGER.info(f'{PREFIX}Running kmeans for {n} anchors on {len(wh)} points...') assert n <= len(wh) # apply overdetermined constraint @@ -135,10 +135,7 @@ def print_results(k, verbose=True): except Exception: LOGGER.warning(f'{PREFIX}WARNING: switching strategies from kmeans to random init') k = np.sort(npr.rand(n * 2)).reshape(n, 2) * img_size # random init - - # k, wh, wh0 = [x.to(wh.device).type(torch.float32) for x in (k, wh, wh0)] - wh = torch.tensor(wh, dtype=torch.float32) # filtered - wh0 = torch.tensor(wh0, dtype=torch.float32) # unfiltered + wh, wh0 = [torch.tensor(x, dtype=torch.float32) for x in (wh, wh0)] k = print_results(k, verbose=False) # Plot From c9d0cfec3c9ec05c6f34cfcc07329a1eff2d3bec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Mar 2022 09:25:12 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- utils/autoanchor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/autoanchor.py b/utils/autoanchor.py index 1a2bfe36c1b1..a631c21a3b26 100644 --- a/utils/autoanchor.py +++ b/utils/autoanchor.py @@ -135,7 +135,7 @@ def print_results(k, verbose=True): except Exception: LOGGER.warning(f'{PREFIX}WARNING: switching strategies from kmeans to random init') k = np.sort(npr.rand(n * 2)).reshape(n, 2) * img_size # random init - wh, wh0 = [torch.tensor(x, dtype=torch.float32) for x in (wh, wh0)] + wh, wh0 = (torch.tensor(x, dtype=torch.float32) for x in (wh, wh0)) k = print_results(k, verbose=False) # Plot