Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher committed Jul 25, 2019
1 parent df4f25e commit 8df3601
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
40 changes: 4 additions & 36 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,27 +320,7 @@ def train(cfg,
return results


def print_mutation(hyp, results):
# Write mutation results
a = '%11s' * len(hyp) % tuple(hyp.keys()) # hyperparam keys
b = '%11.3g' * len(hyp) % tuple(hyp.values()) # hyperparam values
c = '%11.3g' * len(results) % results # results (P, R, mAP, F1, test_loss)
print('\n%s\n%s\nEvolved fitness: %s\n' % (a, b, c))

if opt.bucket:
os.system('gsutil cp gs://%s/evolve.txt .' % opt.bucket) # download evolve.txt
with open('evolve.txt', 'a') as f: # append result
f.write(c + b + '\n')
x = np.unique(np.loadtxt('evolve.txt', ndmin=2), axis=0) # load unique rows
np.savetxt('evolve.txt', x[np.argsort(-fitness(x))], '%11.3g') # save sort by fitness
os.system('gsutil cp evolve.txt gs://%s' % opt.bucket) # upload evolve.txt
else:
with open('evolve.txt', 'a') as f:
f.write(c + b + '\n')


def fitness(x): # returns fitness of hyp evolution vectors
return 0.5 * x[:, 2] + 0.5 * x[:, 3] # fitness = 0.5 * mAP + 0.5 * F1


if __name__ == '__main__':
Expand Down Expand Up @@ -409,19 +389,7 @@ def fitness(x): # returns fitness of hyp evolution vectors
accumulate=opt.accumulate)

# Write mutation results
print_mutation(hyp, results)

# # Plot results
# import numpy as np
# import matplotlib.pyplot as plt
# a = np.loadtxt('evolve.txt')
# x = fitness(a)
# weights = (x - x.min()) ** 2
# fig = plt.figure(figsize=(10, 10))
# for i in range(len(hyp)):
# y = a[:, i + 5]
# mu = (y * weights).sum() / weights.sum()
# plt.subplot(4, 5, i + 1)
# plt.plot(x.max(), mu, 'o')
# plt.plot(x, y, '.')
# print(list(hyp.keys())[i], '%.4g' % mu)
print_mutation(hyp, results, opt.bucket)

# Plot results
plot_evolution_results(hyp)
44 changes: 44 additions & 0 deletions utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,30 @@ def kmeans_targets(path='./data/coco_64img.txt', n=9, img_size=320): # from uti
print('%.1f, ' % x, end='') # drop-in replacement for *.cfg anchors


def print_mutation(hyp, results, bucket=''):
# Print mutation results to evolve.txt (for use with train.py --evolve)
a = '%11s' * len(hyp) % tuple(hyp.keys()) # hyperparam keys
b = '%11.3g' * len(hyp) % tuple(hyp.values()) # hyperparam values
c = '%11.3g' * len(results) % results # results (P, R, mAP, F1, test_loss)
print('\n%s\n%s\nEvolved fitness: %s\n' % (a, b, c))

if bucket:
os.system('gsutil cp gs://%s/evolve.txt .' % bucket) # download evolve.txt
with open('evolve.txt', 'a') as f: # append result
f.write(c + b + '\n')
x = np.unique(np.loadtxt('evolve.txt', ndmin=2), axis=0) # load unique rows
np.savetxt('evolve.txt', x[np.argsort(-fitness(x))], '%11.3g') # save sort by fitness
os.system('gsutil cp evolve.txt gs://%s' % bucket) # upload evolve.txt
else:
with open('evolve.txt', 'a') as f:
f.write(c + b + '\n')


def fitness(x):
# Returns fitness (for use with results.txt or evolve.txt)
return 0.5 * x[:, 2] + 0.5 * x[:, 3] # fitness = 0.5 * mAP + 0.5 * F1


# Plotting functions ---------------------------------------------------------------------------------------------------
def plot_one_box(x, img, color=None, label=None, line_thickness=None):
# Plots one bounding box on image img
Expand Down Expand Up @@ -679,6 +703,26 @@ def plot_targets_txt(): # from utils.utils import *; plot_targets_txt()
plt.savefig('targets.jpg', dpi=200)


def plot_evolution_results(hyp): # from utils.utils import *; plot_evolution_results()
# Plot hyperparameter evolution results in evolve.txt
x = np.loadtxt('evolve.txt')
f = fitness(x)
weights = (f - f.min()) ** 2 # for weighted results
fig = plt.figure(figsize=(12, 10))
matplotlib.rc('font', **{'size': 8})
for i, (k, v) in enumerate(hyp.items()):
y = x[:, i + 5]
# mu = (y * weights).sum() / weights.sum() # best weighted result
mu = y[f.argmax()] # best single result
plt.subplot(4, 5, i + 1)
plt.plot(mu, f.max(), 'o', markersize=10)
plt.plot(y, f, '.')
plt.title('%s = %g' % (k, v), fontdict={'size': 8}) # limit to 40 characters
print(list(hyp.keys())[i], '%.4g' % mu)
fig.tight_layout()
plt.savefig('evolve.png', dpi=200)


def plot_results(start=0, stop=0): # from utils.utils import *; plot_results()
# Plot training results files 'results*.txt'
# import os; os.system('wget https://storage.googleapis.com/ultralytics/yolov3/results_v3.txt')
Expand Down

0 comments on commit 8df3601

Please sign in to comment.