-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconvert_patch_embed.py
executable file
·67 lines (55 loc) · 1.99 KB
/
convert_patch_embed.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Script to resize a model's patch embedding layer using PI-resizing (Flexivit).
"""
from argparse import ArgumentParser
import torch
from flexivit_pytorch import pi_resize_patch_embed
if __name__ == "__main__":
parser = ArgumentParser(
description="Script to resize a model's patch embedding layer using PI-resizing (Flexivit)."
)
parser.add_argument(
"--input", "-i", type=str, required=True, help="Path to model weights"
)
parser.add_argument(
"--output",
"-o",
type=str,
default="converted_weights.pt",
help="Path of output converted weights",
)
parser.add_argument(
"--name",
"-n",
type=str,
default="patch_embed.proj.weight",
help="Name of patch embedding layer parameter. Should be the weights of a Conv2d layer",
)
parser.add_argument(
"--patch-size",
"-ps",
nargs="+",
required=True,
help="Output height and width of patch embedding layer",
)
args = parser.parse_args()
checkpoint = torch.load(args.input, map_location="cpu")
if "state_dict" in checkpoint.keys():
new_state_dict = checkpoint["state_dict"]
else:
new_state_dict = checkpoint
print(f"Resizing patch embedding layer from {args.input}")
print(f"Original patch embedding layer shape: {new_state_dict[args.name].shape}")
if len(args.patch_size) == 1:
shape = (int(args.patch_size[0]), int(args.patch_size[0]))
else:
shape = (int(args.patch_size[0]), int(args.patch_size[1]))
new_state_dict[args.name] = pi_resize_patch_embed(new_state_dict[args.name], shape)
if "state_dict" in checkpoint.keys():
checkpoint["state_dict"] = new_state_dict
else:
checkpoint = new_state_dict
with open(args.output, "wb") as f:
torch.save(checkpoint, f)
print(f"New patch embedding layer shape: {new_state_dict[args.name].shape}")
print(f"Converted weights saved to {args.output}")