-
Notifications
You must be signed in to change notification settings - Fork 7
/
convert.py
259 lines (228 loc) · 8.11 KB
/
convert.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import argparse
import tensorflow as tf
import timm
import torch
torch.set_grad_enabled(False)
import os
from utils import helpers
from vit.deit_models import ViTDistilled
from vit.layers import mha
from vit.model_configs import base_config
from vit.vit_models import ViTClassifier
TF_MODEL_ROOT = "gs://deit-tf"
def parse_args():
parser = argparse.ArgumentParser(
description="Conversion of the PyTorch pre-trained DeiT weights to TensorFlow."
)
parser.add_argument(
"-m",
"--model-name",
default="deit_tiny_patch16_224",
type=str,
choices=[
"deit_base_distilled_patch16_224",
"deit_base_distilled_patch16_384",
"deit_base_patch16_224",
"deit_base_patch16_384",
"deit_small_distilled_patch16_224",
"deit_small_patch16_224",
"deit_tiny_distilled_patch16_224",
"deit_tiny_patch16_224",
],
help="Name of the DeiT model variant.",
)
parser.add_argument(
"-r",
"--resolution",
default=224,
type=int,
choices=[224, 384],
help="Image resolution.",
)
parser.add_argument(
"-p",
"--patch-size",
default=16,
type=int,
help="Patch size.",
)
parser.add_argument(
"-pd",
"--projection-dim",
default=192,
type=int,
help="Patch projection dimension.",
)
parser.add_argument(
"-nl",
"--num-layers",
default=12,
type=int,
help="Number of layers denoting depth.",
)
parser.add_argument(
"-nh",
"--num-heads",
default=3,
type=int,
help="Number of attention heads.",
)
parser.add_argument(
"-pl",
"--pre-logits",
action="store_true",
help="If we don't need the classification outputs.",
)
return parser.parse_args()
def main(args):
if args.pre_logits:
print(f"Converting {args.model_name} for feature extraction...")
else:
print(f"Converting {args.model_name}...")
print("Instantiating PyTorch model...")
pt_model = timm.create_model(
model_name=args.model_name, num_classes=1000, pretrained=True
)
if "distilled" in args.model_name:
assert (
"dist_token" in pt_model.state_dict()
), "Distillation token must be present for models trained with distillation."
pt_model.eval()
print("Instantiating TF model...")
model_cls = (
ViTDistilled if "distilled" in args.model_name else ViTClassifier
)
tf_model_config = base_config.get_config(**vars(args))
tf_model = model_cls(tf_model_config)
dummy_inputs = tf.ones((2, args.resolution, args.resolution, 3))
_ = tf_model(dummy_inputs)[0]
if not args.pre_logits:
assert tf_model.count_params() == sum(
p.numel() for p in pt_model.parameters()
)
# Load the PT params.
pt_model_dict = pt_model.state_dict()
pt_model_dict = {k: pt_model_dict[k].numpy() for k in pt_model_dict}
print("Beginning parameter porting process...")
# Projection layers.
tf_model.layers[0].layers[0] = helpers.modify_tf_block(
tf_model.layers[0].layers[0],
pt_model_dict["patch_embed.proj.weight"],
pt_model_dict["patch_embed.proj.bias"],
)
# Positional embedding.
tf_model.positional_embedding.assign(
tf.Variable(pt_model_dict["pos_embed"])
)
# CLS and (optional) Distillation tokens.
# Distillation token won't be present in the models trained without distillation.
tf_model.cls_token.assign(tf.Variable(pt_model_dict["cls_token"]))
if "distilled" in args.model_name:
tf_model.dist_token.assign(tf.Variable(pt_model_dict["dist_token"]))
# Layer norm layers.
ln_idx = -3 if "distilled" in args.model_name else -2
tf_model.layers[ln_idx] = helpers.modify_tf_block(
tf_model.layers[ln_idx],
pt_model_dict["norm.weight"],
pt_model_dict["norm.bias"],
)
# Head layers.
if not args.pre_logits:
head_layer = tf_model.get_layer("classification_head")
head_layer_idx = -2 if "distilled" in args.model_name else -1
tf_model.layers[head_layer_idx] = helpers.modify_tf_block(
head_layer,
pt_model_dict["head.weight"],
pt_model_dict["head.bias"],
)
if "distilled" in args.model_name:
head_dist_layer = tf_model.get_layer("distillation_head")
tf_model.layers[-1] = helpers.modify_tf_block(
head_dist_layer,
pt_model_dict["head_dist.weight"],
pt_model_dict["head_dist.bias"],
)
# Transformer blocks.
idx = 0
for outer_layer in tf_model.layers:
if (
isinstance(outer_layer, tf.keras.Model)
and outer_layer.name != "projection"
):
tf_block = tf_model.get_layer(outer_layer.name)
pt_block_name = f"blocks.{idx}"
# LayerNorm layers.
layer_norm_idx = 1
for layer in tf_block.layers:
if isinstance(layer, tf.keras.layers.LayerNormalization):
layer_norm_pt_prefix = (
f"{pt_block_name}.norm{layer_norm_idx}"
)
layer.gamma.assign(
tf.Variable(
pt_model_dict[f"{layer_norm_pt_prefix}.weight"]
)
)
layer.beta.assign(
tf.Variable(
pt_model_dict[f"{layer_norm_pt_prefix}.bias"]
)
)
layer_norm_idx += 1
# FFN layers.
ffn_layer_idx = 1
for layer in tf_block.layers:
if isinstance(layer, tf.keras.layers.Dense):
dense_layer_pt_prefix = (
f"{pt_block_name}.mlp.fc{ffn_layer_idx}"
)
layer = helpers.modify_tf_block(
layer,
pt_model_dict[f"{dense_layer_pt_prefix}.weight"],
pt_model_dict[f"{dense_layer_pt_prefix}.bias"],
)
ffn_layer_idx += 1
# Attention layer.
for layer in tf_block.layers:
(q_w, k_w, v_w), (q_b, k_b, v_b) = helpers.get_tf_qkv(
f"{pt_block_name}.attn",
pt_model_dict,
tf_model_config,
)
if isinstance(layer, mha.TFViTAttention):
# Key
layer.self_attention.key = helpers.modify_tf_block(
layer.self_attention.key,
k_w,
k_b,
is_attn=True,
)
# Query
layer.self_attention.query = helpers.modify_tf_block(
layer.self_attention.query,
q_w,
q_b,
is_attn=True,
)
# Value
layer.self_attention.value = helpers.modify_tf_block(
layer.self_attention.value,
v_w,
v_b,
is_attn=True,
)
# Final dense projection
layer.dense_output.dense = helpers.modify_tf_block(
layer.dense_output.dense,
pt_model_dict[f"{pt_block_name}.attn.proj.weight"],
pt_model_dict[f"{pt_block_name}.attn.proj.bias"],
)
idx += 1
print("Porting successful, serializing TensorFlow model...")
save_path = os.path.join(TF_MODEL_ROOT, args.model_name)
save_path = f"{save_path}_fe" if args.pre_logits else save_path
tf_model.save(save_path)
print(f"TensorFlow model serialized to: {save_path}...")
if __name__ == "__main__":
args = parse_args()
main(args)