Clean checkpoint renames pth w/ SHA hash in a torch hub compatible way

pull/53/head
Ross Wightman 5 years ago
parent a39cc43374
commit 3b8f63084d

@ -2,6 +2,7 @@ import torch
import argparse import argparse
import os import os
import hashlib import hashlib
import shutil
from collections import OrderedDict from collections import OrderedDict
parser = argparse.ArgumentParser(description='PyTorch ImageNet Validation') parser = argparse.ArgumentParser(description='PyTorch ImageNet Validation')
@ -30,11 +31,10 @@ def main():
state_dict_key = 'state_dict_ema' if args.use_ema else 'state_dict' state_dict_key = 'state_dict_ema' if args.use_ema else 'state_dict'
if state_dict_key in checkpoint: if state_dict_key in checkpoint:
state_dict = checkpoint[state_dict_key] state_dict = checkpoint[state_dict_key]
else:
print("Error: No state_dict found in checkpoint {}.".format(args.checkpoint))
exit(1)
else: else:
state_dict = checkpoint state_dict = checkpoint
else:
assert False
for k, v in state_dict.items(): for k, v in state_dict.items():
name = k[7:] if k.startswith('module') else k name = k[7:] if k.startswith('module') else k
new_state_dict[name] = v new_state_dict[name] = v
@ -43,7 +43,11 @@ def main():
torch.save(new_state_dict, args.output) torch.save(new_state_dict, args.output)
with open(args.output, 'rb') as f: with open(args.output, 'rb') as f:
sha_hash = hashlib.sha256(f.read()).hexdigest() sha_hash = hashlib.sha256(f.read()).hexdigest()
print("=> Saved state_dict to '{}, SHA256: {}'".format(args.output, sha_hash))
checkpoint_base = os.path.splitext(args.checkpoint)[0]
final_filename = '-'.join([checkpoint_base, sha_hash[:8]]) + '.pth'
shutil.move(args.output, final_filename)
print("=> Saved state_dict to '{}, SHA256: {}'".format(final_filename, sha_hash))
else: else:
print("Error: Checkpoint ({}) doesn't exist".format(args.checkpoint)) print("Error: Checkpoint ({}) doesn't exist".format(args.checkpoint))

Loading…
Cancel
Save