forked from open-mmlab/mmdetection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdo_swa.py
52 lines (38 loc) · 1.3 KB
/
do_swa.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
50
51
52
import torch
import argparse
from pathlib import Path
def do_swa(checkpoint):
skip = []
K = len(checkpoint)
swa = None
for k in range(K):
state_dict = torch.load(checkpoint[k], map_location=lambda storage, loc: storage)['state_dict']
if K==1:
return state_dict
if swa is None:
swa = state_dict
else:
for k, v in state_dict.items():
if any(s in k for s in skip): continue
swa[k] += v
for k, v in swa.items():
if any(s in k for s in skip): continue
try:
swa[k] /= K
except:
swa[k] //= K
return swa
def main(workdir):
workdir = Path(workdir)
checkpoint_paths = list(workdir.glob("epoch_*.pth"))
checkpoint_paths = list(map(lambda x: str(x), checkpoint_paths))
swa_model = do_swa(checkpoint_paths)
state_dict = torch.load(checkpoint_paths[-1], map_location=lambda storage, loc: storage)
state_dict['state_dict'] = swa_model
save_path = workdir.joinpath("swa_last.pth")
torch.save(state_dict, save_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Do SWA')
parser.add_argument('--workdir', help='work directory',)
args = parser.parse_args()
main(args.workdir)