forked from cwlroda/falldetection_openpifpaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.py
49 lines (34 loc) · 1.28 KB
/
migrate.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
40
41
42
43
44
45
46
47
48
49
"""Migrate a model."""
import argparse
import logging
import torch
from . import network, __version__
def main():
parser = argparse.ArgumentParser(
prog='python3 -m openpifpaf.migrate',
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument('--version', action='version',
version='OpenPifPaf {version}'.format(version=__version__))
parser.add_argument('--debug', default=False, action='store_true')
parser.add_argument('--output')
network.cli(parser)
args = parser.parse_args()
network.configure(args)
if args.checkpoint is None:
raise Exception('checkpoint must be provided for old model to migrate from')
if args.output is None:
args.output = args.checkpoint + '.out.pkl'
logging.basicConfig(level=logging.INFO if not args.debug else logging.DEBUG)
# load old model
checkpoint = torch.load(args.checkpoint)
# create a new model
args.checkpoint = None
new_model, _ = network.factory_from_args(args)
# transfer state from old to new model
new_model.load_state_dict(checkpoint['model'].state_dict())
checkpoint['model'] = new_model
torch.save(checkpoint, args.output)
if __name__ == '__main__':
main()