Fix `hyp_evolve.yaml` indexing bug (#6604)
* 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.pypull/6612/head
parent
a5c9057dcc
commit
c21da596f3
|
@ -783,7 +783,7 @@ def strip_optimizer(f='best.pt', s=''): # from utils.general import *; strip_op
|
||||||
LOGGER.info(f"Optimizer stripped from {f},{(' saved as %s,' % s) if s else ''} {mb:.1f}MB")
|
LOGGER.info(f"Optimizer stripped from {f},{(' saved as %s,' % s) if s else ''} {mb:.1f}MB")
|
||||||
|
|
||||||
|
|
||||||
def print_mutation(results, hyp, save_dir, bucket):
|
def print_mutation(results, hyp, save_dir, bucket, prefix=colorstr('evolve: ')):
|
||||||
evolve_csv = save_dir / 'evolve.csv'
|
evolve_csv = save_dir / 'evolve.csv'
|
||||||
evolve_yaml = save_dir / 'hyp_evolve.yaml'
|
evolve_yaml = save_dir / 'hyp_evolve.yaml'
|
||||||
keys = ('metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95',
|
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):
|
||||||
with open(evolve_csv, 'a') as f:
|
with open(evolve_csv, 'a') as f:
|
||||||
f.write(s + ('%20.5g,' * n % vals).rstrip(',') + '\n')
|
f.write(s + ('%20.5g,' * n % vals).rstrip(',') + '\n')
|
||||||
|
|
||||||
# Print to screen
|
|
||||||
LOGGER.info(colorstr('evolve: ') + ', '.join(f'{x.strip():>20s}' for x in keys))
|
|
||||||
LOGGER.info(colorstr('evolve: ') + ', '.join(f'{x:20.5g}' for x in vals) + '\n\n')
|
|
||||||
|
|
||||||
# Save yaml
|
# Save yaml
|
||||||
with open(evolve_yaml, 'w') as f:
|
with open(evolve_yaml, 'w') as f:
|
||||||
data = pd.read_csv(evolve_csv)
|
data = pd.read_csv(evolve_csv)
|
||||||
data = data.rename(columns=lambda x: x.strip()) # strip keys
|
data = data.rename(columns=lambda x: x.strip()) # strip keys
|
||||||
i = np.argmax(fitness(data.values[:, :7])) #
|
i = np.argmax(fitness(data.values[:, :4])) #
|
||||||
|
generations = len(data)
|
||||||
f.write('# YOLOv5 Hyperparameter Evolution Results\n' +
|
f.write('# YOLOv5 Hyperparameter Evolution Results\n' +
|
||||||
f'# Best generation: {i}\n' +
|
f'# Best generation: {i}\n' +
|
||||||
f'# Last generation: {len(data) - 1}\n' +
|
f'# Last generation: {generations - 1}\n' +
|
||||||
'# ' + ', '.join(f'{x.strip():>20s}' for x in keys[:7]) + '\n' +
|
'# ' + ', '.join(f'{x.strip():>20s}' for x in keys[:7]) + '\n' +
|
||||||
'# ' + ', '.join(f'{x:>20.5g}' for x in data.values[i, :7]) + '\n\n')
|
'# ' + ', '.join(f'{x:>20.5g}' for x in data.values[i, :7]) + '\n\n')
|
||||||
yaml.safe_dump(hyp, f, sort_keys=False)
|
yaml.safe_dump(data.loc[i][7:].to_dict(), f, sort_keys=False)
|
||||||
|
|
||||||
|
# Print to screen
|
||||||
|
LOGGER.info(prefix + f'{generations} generations finished, current result:\n' +
|
||||||
|
prefix + ', '.join(f'{x.strip():>20s}' for x in keys) + '\n' +
|
||||||
|
prefix + ', '.join(f'{x:20.5g}' for x in vals) + '\n\n')
|
||||||
|
|
||||||
if bucket:
|
if bucket:
|
||||||
os.system(f'gsutil cp {evolve_csv} {evolve_yaml} gs://{bucket}') # upload
|
os.system(f'gsutil cp {evolve_csv} {evolve_yaml} gs://{bucket}') # upload
|
||||||
|
|
|
@ -381,6 +381,7 @@ def plot_evolve(evolve_csv='path/to/evolve.csv'): # from utils.plots import *;
|
||||||
j = np.argmax(f) # max fitness index
|
j = np.argmax(f) # max fitness index
|
||||||
plt.figure(figsize=(10, 12), tight_layout=True)
|
plt.figure(figsize=(10, 12), tight_layout=True)
|
||||||
matplotlib.rc('font', **{'size': 8})
|
matplotlib.rc('font', **{'size': 8})
|
||||||
|
print(f'Best results from row {j} of {evolve_csv}:')
|
||||||
for i, k in enumerate(keys[7:]):
|
for i, k in enumerate(keys[7:]):
|
||||||
v = x[:, 7 + i]
|
v = x[:, 7 + i]
|
||||||
mu = v[j] # best single result
|
mu = v[j] # best single result
|
||||||
|
|
Loading…
Reference in New Issue