glenn-jocher commited on
Commit
c21da59
1 Parent(s): a5c9057

Fix `hyp_evolve.yaml` indexing bug (#6604)

Browse files

* Fix `hyp_evolve.yaml` indexing bug

Bug caused hyp_evolve.yaml to display latest generation result rather than best generation result.

* Update plots.py

* Update general.py

* Update general.py

* Update general.py

Files changed (2) hide show
  1. utils/general.py +10 -8
  2. utils/plots.py +1 -0
utils/general.py CHANGED
@@ -783,7 +783,7 @@ def strip_optimizer(f='best.pt', s=''): # from utils.general import *; strip_op
783
  LOGGER.info(f"Optimizer stripped from {f},{(' saved as %s,' % s) if s else ''} {mb:.1f}MB")
784
 
785
 
786
- def print_mutation(results, hyp, save_dir, bucket):
787
  evolve_csv = save_dir / 'evolve.csv'
788
  evolve_yaml = save_dir / 'hyp_evolve.yaml'
789
  keys = ('metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95',
@@ -803,21 +803,23 @@ def print_mutation(results, hyp, save_dir, bucket):
803
  with open(evolve_csv, 'a') as f:
804
  f.write(s + ('%20.5g,' * n % vals).rstrip(',') + '\n')
805
 
806
- # Print to screen
807
- LOGGER.info(colorstr('evolve: ') + ', '.join(f'{x.strip():>20s}' for x in keys))
808
- LOGGER.info(colorstr('evolve: ') + ', '.join(f'{x:20.5g}' for x in vals) + '\n\n')
809
-
810
  # Save yaml
811
  with open(evolve_yaml, 'w') as f:
812
  data = pd.read_csv(evolve_csv)
813
  data = data.rename(columns=lambda x: x.strip()) # strip keys
814
- i = np.argmax(fitness(data.values[:, :7])) #
 
815
  f.write('# YOLOv5 Hyperparameter Evolution Results\n' +
816
  f'# Best generation: {i}\n' +
817
- f'# Last generation: {len(data) - 1}\n' +
818
  '# ' + ', '.join(f'{x.strip():>20s}' for x in keys[:7]) + '\n' +
819
  '# ' + ', '.join(f'{x:>20.5g}' for x in data.values[i, :7]) + '\n\n')
820
- yaml.safe_dump(hyp, f, sort_keys=False)
 
 
 
 
 
821
 
822
  if bucket:
823
  os.system(f'gsutil cp {evolve_csv} {evolve_yaml} gs://{bucket}') # upload
 
783
  LOGGER.info(f"Optimizer stripped from {f},{(' saved as %s,' % s) if s else ''} {mb:.1f}MB")
784
 
785
 
786
+ def print_mutation(results, hyp, save_dir, bucket, prefix=colorstr('evolve: ')):
787
  evolve_csv = save_dir / 'evolve.csv'
788
  evolve_yaml = save_dir / 'hyp_evolve.yaml'
789
  keys = ('metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95',
 
803
  with open(evolve_csv, 'a') as f:
804
  f.write(s + ('%20.5g,' * n % vals).rstrip(',') + '\n')
805
 
 
 
 
 
806
  # Save yaml
807
  with open(evolve_yaml, 'w') as f:
808
  data = pd.read_csv(evolve_csv)
809
  data = data.rename(columns=lambda x: x.strip()) # strip keys
810
+ i = np.argmax(fitness(data.values[:, :4])) #
811
+ generations = len(data)
812
  f.write('# YOLOv5 Hyperparameter Evolution Results\n' +
813
  f'# Best generation: {i}\n' +
814
+ f'# Last generation: {generations - 1}\n' +
815
  '# ' + ', '.join(f'{x.strip():>20s}' for x in keys[:7]) + '\n' +
816
  '# ' + ', '.join(f'{x:>20.5g}' for x in data.values[i, :7]) + '\n\n')
817
+ yaml.safe_dump(data.loc[i][7:].to_dict(), f, sort_keys=False)
818
+
819
+ # Print to screen
820
+ LOGGER.info(prefix + f'{generations} generations finished, current result:\n' +
821
+ prefix + ', '.join(f'{x.strip():>20s}' for x in keys) + '\n' +
822
+ prefix + ', '.join(f'{x:20.5g}' for x in vals) + '\n\n')
823
 
824
  if bucket:
825
  os.system(f'gsutil cp {evolve_csv} {evolve_yaml} gs://{bucket}') # upload
utils/plots.py CHANGED
@@ -381,6 +381,7 @@ def plot_evolve(evolve_csv='path/to/evolve.csv'): # from utils.plots import *;
381
  j = np.argmax(f) # max fitness index
382
  plt.figure(figsize=(10, 12), tight_layout=True)
383
  matplotlib.rc('font', **{'size': 8})
 
384
  for i, k in enumerate(keys[7:]):
385
  v = x[:, 7 + i]
386
  mu = v[j] # best single result
 
381
  j = np.argmax(f) # max fitness index
382
  plt.figure(figsize=(10, 12), tight_layout=True)
383
  matplotlib.rc('font', **{'size': 8})
384
+ print(f'Best results from row {j} of {evolve_csv}:')
385
  for i, k in enumerate(keys[7:]):
386
  v = x[:, 7 + i]
387
  mu = v[j] # best single result