Skip to content

Instantly share code, notes, and snippets.

@JossWhittle
Last active July 5, 2020 08:14
Show Gist options
  • Save JossWhittle/65b97bb316a301a5d9d5a8f193c349bb to your computer and use it in GitHub Desktop.
Save JossWhittle/65b97bb316a301a5d9d5a8f193c349bb to your computer and use it in GitHub Desktop.
# Keep last N weights and one weight every H hours, discard others
old_weight_paths = sorted(list(glob(os.path.join(weight_path, 'style_weights-*.h5'))))
# Iterate over weights from newest to oldest, discard oldest weights if multiple were saved that hour
prev_weight_age = -1
for old_weight_path in reversed(old_weight_paths[:-keep_last_n_weights]):
# Age of this weight file in hours
weight_age = int((time() - os.path.getmtime(old_weight_path)) // (60 * 60 * keep_weights_every_n_hours))
# If we have already seen a weight file with the target age then discard this weight file
if (weight_age == prev_weight_age): os.remove(old_weight_path)
# Update the target age for discarding
prev_weight_age = weight_age
# Save current weights
s_model.save_weights(os.path.join(weight_path, f'style_weights-{global_step:06d}.h5'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment