glenn-jocher commited on
Commit
8fa3724
1 Parent(s): bdd9fee

kmean_anchors() update

Browse files
Files changed (1) hide show
  1. utils/utils.py +21 -8
utils/utils.py CHANGED
@@ -16,6 +16,7 @@ import numpy as np
16
  import torch
17
  import torch.nn as nn
18
  import torchvision
 
19
  from scipy.signal import butter, filtfilt
20
  from tqdm import tqdm
21
 
@@ -686,12 +687,23 @@ def coco_single_class_labels(path='../coco/labels/train2014/', label_class=43):
686
  shutil.copyfile(src=img_file, dst='new/images/' + Path(file).name.replace('txt', 'jpg')) # copy images
687
 
688
 
689
- def kmean_anchors(path='./data/coco128.txt', n=9, img_size=(640, 640), thr=0.20, gen=1000):
690
- # Creates kmeans anchors for use in *.cfg files: from utils.utils import *; _ = kmean_anchors()
691
- # n: number of anchors
692
- # img_size: (min, max) image size used for multi-scale training (can be same values)
693
- # thr: IoU threshold hyperparameter used for training (0.0 - 1.0)
694
- # gen: generations to evolve anchors using genetic algorithm
 
 
 
 
 
 
 
 
 
 
 
695
  from utils.datasets import LoadImagesAndLabels
696
 
697
  def print_results(k):
@@ -727,7 +739,9 @@ def kmean_anchors(path='./data/coco128.txt', n=9, img_size=(640, 640), thr=0.20,
727
 
728
  # Get label wh
729
  wh = []
730
- dataset = LoadImagesAndLabels(path, augment=True, rect=True)
 
 
731
  nr = 1 if img_size[0] == img_size[1] else 3 # number augmentation repetitions
732
  for s, l in zip(dataset.shapes, dataset.labels):
733
  # wh.append(l[:, 3:5] * (s / s.max())) # image normalized to letterbox normalized wh
@@ -771,7 +785,6 @@ def kmean_anchors(path='./data/coco128.txt', n=9, img_size=(640, 640), thr=0.20,
771
  f, k = fg, kg.copy()
772
  print_results(k)
773
  k = print_results(k)
774
-
775
  return k
776
 
777
 
 
16
  import torch
17
  import torch.nn as nn
18
  import torchvision
19
+ import yaml
20
  from scipy.signal import butter, filtfilt
21
  from tqdm import tqdm
22
 
 
687
  shutil.copyfile(src=img_file, dst='new/images/' + Path(file).name.replace('txt', 'jpg')) # copy images
688
 
689
 
690
+ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20, gen=1000):
691
+ """ Creates kmeans-evolved anchors from training dataset
692
+
693
+ Arguments:
694
+ path: path to dataset *.yaml
695
+ n: number of anchors
696
+ img_size: (min, max) image size used for multi-scale training (can be same values)
697
+ thr: IoU threshold hyperparameter used for training (0.0 - 1.0)
698
+ gen: generations to evolve anchors using genetic algorithm
699
+
700
+ Return:
701
+ k: kmeans evolved anchors
702
+
703
+ Usage:
704
+ from utils.utils import *; _ = kmean_anchors()
705
+ """
706
+
707
  from utils.datasets import LoadImagesAndLabels
708
 
709
  def print_results(k):
 
739
 
740
  # Get label wh
741
  wh = []
742
+ with open(path) as f:
743
+ data_dict = yaml.load(f, Loader=yaml.FullLoader) # model dict
744
+ dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
745
  nr = 1 if img_size[0] == img_size[1] else 3 # number augmentation repetitions
746
  for s, l in zip(dataset.shapes, dataset.labels):
747
  # wh.append(l[:, 3:5] * (s / s.max())) # image normalized to letterbox normalized wh
 
785
  f, k = fg, kg.copy()
786
  print_results(k)
787
  k = print_results(k)
 
788
  return k
789
 
790