-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathclean_checkpoint.py
39 lines (24 loc) · 1.08 KB
/
clean_checkpoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Copyright (C) 2022, Pyronear.
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
# Borrowed from https://github.com/frgfm/Holocron/blob/main/references/clean_checkpoint.py
import hashlib
import torch
def main(args):
checkpoint = torch.load(args.checkpoint, map_location="cpu")["model"]
torch.save(checkpoint, args.outfile, _use_new_zipfile_serialization=True)
with open(args.outfile, "rb") as f:
sha_hash = hashlib.sha256(f.read()).hexdigest()
print(f"Checkpoint saved to {args.outfile} with hash: {sha_hash[:8]}")
def parse_args():
import argparse
parser = argparse.ArgumentParser(
description="Training checkpoint cleanup", formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("checkpoint", type=str, help="path to the training checkpoint")
parser.add_argument("outfile", type=str, help="model")
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
main(args)