-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_weights.py
42 lines (34 loc) · 1.18 KB
/
convert_weights.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
import argparse
import os
import megengine
import numpy as np
import torch
from models.convnext import *
from models.torch_models import model_urls
def main(torch_name):
url = model_urls[torch_name]
torch_state_dict = torch.hub.load_state_dict_from_url(url=url, map_location="cpu", check_hash=True)['model']
new_dict = {}
# model = convnext_tiny()
s = model.state_dict()
for k, v in torch_state_dict.items():
data = v.numpy()
if len(data.shape) != len(s[k].shape) and not 'dwconv.weight' in k:
data = data.reshape(1, -1, 1, 1)
if 'dwconv.weight' in k:
data = np.expand_dims(data, 1)
new_dict[k] = data
# model.load_state_dict(new_dict)
os.makedirs('pretrained', exist_ok=True)
mge.save(new_dict, os.path.join('pretrained', torch_name + '.pkl'))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-m",
"--model",
type=str,
default='convnext_tiny_1k',
help=f"which model to convert from torch to megengine, default: convnext_tiny_1k, optional: {list(model_urls.keys())}",
)
args = parser.parse_args()
main(args.model)