train.py的修改

master
qinxiaonan_branch 5 months ago
parent 6316da5942
commit 56e71b2fb0

@ -558,45 +558,48 @@ def main(opt, callbacks=Callbacks()):
if opt.bucket: if opt.bucket:
os.system(f'gsutil cp gs://{opt.bucket}/evolve.csv {save_dir}') # 下载evolve.csv os.system(f'gsutil cp gs://{opt.bucket}/evolve.csv {save_dir}') # 下载evolve.csv
for _ in range(opt.evolve): # generations to evolve
if evolve_csv.exists(): # if evolve.csv exists: select best hyps and mutate #进行opt.evolve指定次数的超参数演化
# Select parent(s) for _ in range(opt.evolve):
parent = 'single' # parent selection method: 'single' or 'weighted' if evolve_csv.exists(): # 如果存在演化过程中生成的csv文件选择最佳超参数并进行变异
# 选择父方法
parent = 'single' # 'single' 表示随机选择一个最佳超参数集作为父代;'weighted' 表示加权选择
x = np.loadtxt(evolve_csv, ndmin=2, delimiter=',', skiprows=1) x = np.loadtxt(evolve_csv, ndmin=2, delimiter=',', skiprows=1)
n = min(5, len(x)) # number of previous results to consider n = min(5, len(x)) # 加载超参数数据
x = x[np.argsort(-fitness(x))][:n] # top n mutations x = x[np.argsort(-fitness(x))][:n] # 考虑前n个历史结果
w = fitness(x) - fitness(x).min() + 1E-6 # weights (sum > 0) w = fitness(x) - fitness(x).min() + 1E-6
if parent == 'single' or len(x) == 1: if parent == 'single' or len(x) == 1:
# x = x[random.randint(0, n - 1)] # random selection # 随机选择一个超参数集
x = x[random.choices(range(n), weights=w)[0]] # weighted selection x = x[random.choices(range(n), weights=w)[0]] # 权重选择
elif parent == 'weighted': elif parent == 'weighted':
x = (x * w.reshape(n, 1)).sum(0) / w.sum() # weighted combination x = (x * w.reshape(n, 1)).sum(0) / w.sum() # 权重组合
# 变异操作
mp, s = 0.8, 0.2 # 变异概率,标准差
# Mutate
mp, s = 0.8, 0.2 # mutation probability, sigma
npr = np.random npr = np.random
npr.seed(int(time.time())) npr.seed(int(time.time()))
g = np.array([meta[k][0] for k in hyp.keys()]) # gains 0-1 g = np.array([meta[k][0] for k in hyp.keys()]) #
ng = len(meta) ng = len(meta)
v = np.ones(ng) v = np.ones(ng)
while all(v == 1): # mutate until a change occurs (prevent duplicates) while all(v == 1): # 进行变异,直到发生改变,防止生成重复的超参数集
v = (g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1).clip(0.3, 3.0) v = (g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1).clip(0.3, 3.0)
for i, k in enumerate(hyp.keys()): # plt.hist(v.ravel(), 300) for i, k in enumerate(hyp.keys()):
hyp[k] = float(x[i + 7] * v[i]) # mutate hyp[k] = float(x[i + 7] * v[i])
# Constrain to limits # 约束超参数在设定的范围内
for k, v in meta.items(): for k, v in meta.items():
hyp[k] = max(hyp[k], v[1]) # lower limit hyp[k] = max(hyp[k], v[1]) # 下限
hyp[k] = min(hyp[k], v[2]) # upper limit hyp[k] = min(hyp[k], v[2]) # 上限
hyp[k] = round(hyp[k], 5) # significant digits hyp[k] = round(hyp[k], 5) # 5位有效数字
# Train mutation # 变异训练
results = train(hyp.copy(), opt, device, callbacks) results = train(hyp.copy(), opt, device, callbacks)
# Write mutation results #记录结果
print_mutation(results, hyp.copy(), save_dir, opt.bucket) print_mutation(results, hyp.copy(), save_dir, opt.bucket)
# Plot results # 绘制结果
plot_evolve(evolve_csv) plot_evolve(evolve_csv)
LOGGER.info(f'Hyperparameter evolution finished\n' LOGGER.info(f'Hyperparameter evolution finished\n'
f"Results saved to {colorstr('bold', save_dir)}\n" f"Results saved to {colorstr('bold', save_dir)}\n"

Loading…
Cancel
Save