forked from cedriclmenard/irislandmarks.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_test.py
387 lines (314 loc) · 13.9 KB
/
convert_test.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# # Convert TFLite model to PyTorch
#
# This uses the model **face_detection_front.tflite** from [MediaPipe](https://github.com/google/mediapipe/tree/master/mediapipe/models).
#
# Prerequisites:
#
# 1) Clone the MediaPipe repo:
#
# ```
# git clone https://github.com/google/mediapipe.git
# ```
#
# 2) Install **flatbuffers**:
#
# ```
# git clone https://github.com/google/flatbuffers.git
# cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
# make -j
#
# cd flatbuffers/python
# python setup.py install
# ```
#
# 3) Clone the TensorFlow repo. We only need this to get the FlatBuffers schema files (I guess you could just download [schema.fbs](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/schema/schema.fbs)).
#
# ```
# git clone https://github.com/tensorflow/tensorflow.git
# ```
#
# 4) Convert the schema files to Python files using **flatc**:
#
# ```
# ./flatbuffers/flatc --python tensorflow/tensorflow/lite/schema/schema.fbs
# ```
#
# Now we can use the Python FlatBuffer API to read the TFLite file!
# %%
# !git clone https://github.com/google/mediapipe.git
# !git clone https://github.com/google/flatbuffers.git
# !cd flatbuffers ; cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ; make -j
# !cd flatbuffers/python ; python setup.py install
# !git clone https://github.com/tensorflow/tensorflow.git
# !./flatbuffers/flatc --python tensorflow/tensorflow/lite/schema/schema.fbs
# Now restart this notebook
# %%
import os
import numpy as np
from collections import OrderedDict
# ## Get the weights from the TFLite file
# Load the TFLite model using the FlatBuffers library:
# %%
from tflite import Model
# taken from arcore pod
data = open("../mediapipe/mediapipe/models/iris_landmark.tflite", "rb").read()
model = Model.GetRootAsModel(data, 0)
# %%
subgraph = model.Subgraphs(0)
subgraph.Name()
# %%
def get_shape(tensor):
return [tensor.Shape(i) for i in range(tensor.ShapeLength())]
# List all the tensors in the graph:
# %%
for i in range(0, subgraph.TensorsLength()):
tensor = subgraph.Tensors(i)
print("%3d %30s %d %2d %s" % (i, tensor.Name(), tensor.Type(), tensor.Buffer(),
get_shape(subgraph.Tensors(i))))
# Make a look-up table that lets us get the tensor index based on the tensor name:
# %%
tensor_dict = {(subgraph.Tensors(i).Name().decode("utf8")): i
for i in range(subgraph.TensorsLength())}
# Grab only the tensors that represent weights and biases.
# %%
parameters = {}
for i in range(subgraph.TensorsLength()):
tensor = subgraph.Tensors(i)
if tensor.Buffer() > 0:
name = tensor.Name().decode("utf8")
parameters[name] = tensor.Buffer()
len(parameters)
# The buffers are simply arrays of bytes. As the docs say,
#
# > The data_buffer itself is an opaque container, with the assumption that the
# > target device is little-endian. In addition, all builtin operators assume
# > the memory is ordered such that if `shape` is [4, 3, 2], then index
# > [i, j, k] maps to `data_buffer[i*3*2 + j*2 + k]`.
#
# For weights and biases, we need to interpret every 4 bytes as being as float. On my machine, the native byte ordering is already little-endian so we don't need to do anything special for that.
# %%
def get_weights(tensor_name):
i = tensor_dict[tensor_name]
tensor = subgraph.Tensors(i)
buffer = tensor.Buffer()
shape = get_shape(tensor)
assert(tensor.Type() == 0) # FLOAT32
# tensor types are here: https://github.com/jackwish/tflite/blob/master/tflite/TensorType.py
W = model.Buffers(buffer).DataAsNumpy()
W = W.view(dtype=np.float32)
W = W.reshape(shape)
return W
# %%
W = get_weights("conv2d_1/Kernel")
b = get_weights("conv2d_1/Bias")
W.shape, b.shape
# Now we can get the weights for all the layers and copy them into our PyTorch model.
# ## Convert the weights to PyTorch format
# %%
import torch
import torch.nn as nn
from irislandmarks import IrisLandmarks
# %%
net = IrisLandmarks()
# %%
net
# %%
net(torch.randn(2,3,64,64))[0].shape
# Make a lookup table that maps the layer names between the two models. We're going to assume here that the tensors will be in the same order in both models. If not, we should get an error because shapes don't match.
# %%
probable_names = []
for i in range(0, subgraph.TensorsLength()):
tensor = subgraph.Tensors(i)
if tensor.Buffer() > 0 and tensor.Type() == 0:
probable_names.append(tensor.Name().decode("utf-8"))
probable_names[:5]
# %%
len(probable_names)
# %%
from pprint import pprint
# %%
pprint(list(zip(probable_names, net.state_dict())))
# %%
len(net.state_dict()), len(probable_names)
# %%
convert = {}
i = 0
for name, params in net.state_dict().items():
if i < 85:
convert[name] = probable_names[i]
i += 1
# %%
manual_mapping = {
'split_eye.0.convAct.1.weight': 'p_re_lu_21/Alpha',
'split_eye.0.dwConvConv.0.weight': 'depthwise_conv2d_10/Kernel',
'split_eye.0.dwConvConv.0.bias': 'depthwise_conv2d_10/Bias',
'split_eye.0.dwConvConv.1.weight': 'conv2d_22/Kernel',
'split_eye.0.dwConvConv.1.bias': 'conv2d_22/Bias',
'split_eye.0.act.weight': 'p_re_lu_22/Alpha',
'split_eye.1.convAct.0.weight': 'conv2d_23/Kernel',
'split_eye.1.convAct.0.bias': 'conv2d_23/Bias',
'split_eye.1.convAct.1.weight': 'p_re_lu_23/Alpha',
'split_eye.1.dwConvConv.0.weight': 'depthwise_conv2d_11/Kernel',
'split_eye.1.dwConvConv.0.bias': 'depthwise_conv2d_11/Bias',
'split_eye.1.dwConvConv.1.weight': 'conv2d_24/Kernel',
'split_eye.1.dwConvConv.1.bias': 'conv2d_24/Bias',
'split_eye.1.act.weight': 'p_re_lu_24/Alpha',
'split_eye.2.convAct.0.weight': 'conv2d_25/Kernel',
'split_eye.2.convAct.0.bias': 'conv2d_25/Bias',
'split_eye.2.convAct.1.weight': 'p_re_lu_25/Alpha',
'split_eye.2.dwConvConv.0.weight': 'depthwise_conv2d_12/Kernel',
'split_eye.2.dwConvConv.0.bias': 'depthwise_conv2d_12/Bias',
'split_eye.2.dwConvConv.1.weight': 'conv2d_26/Kernel',
'split_eye.2.dwConvConv.1.bias': 'conv2d_26/Bias',
'split_eye.2.act.weight': 'p_re_lu_26/Alpha',
'split_eye.3.convAct.0.weight': 'conv2d_27/Kernel',
'split_eye.3.convAct.0.bias': 'conv2d_27/Bias',
'split_eye.3.convAct.1.weight': 'p_re_lu_27/Alpha',
'split_eye.3.dwConvConv.0.weight': 'depthwise_conv2d_13/Kernel',
'split_eye.3.dwConvConv.0.bias': 'depthwise_conv2d_13/Bias',
'split_eye.3.dwConvConv.1.weight': 'conv2d_28/Kernel',
'split_eye.3.dwConvConv.1.bias': 'conv2d_28/Bias',
'split_eye.3.act.weight': 'p_re_lu_28/Alpha',
'split_eye.4.convAct.0.weight': 'conv2d_29/Kernel',
'split_eye.4.convAct.0.bias': 'conv2d_29/Bias',
'split_eye.4.convAct.1.weight': 'p_re_lu_29/Alpha',
'split_eye.4.dwConvConv.0.weight': 'depthwise_conv2d_14/Kernel',
'split_eye.4.dwConvConv.0.bias': 'depthwise_conv2d_14/Bias',
'split_eye.4.dwConvConv.1.weight': 'conv2d_30/Kernel',
'split_eye.4.dwConvConv.1.bias': 'conv2d_30/Bias',
'split_eye.4.act.weight': 'p_re_lu_30/Alpha',
'split_eye.5.convAct.0.weight': 'conv2d_31/Kernel',
'split_eye.5.convAct.0.bias': 'conv2d_31/Bias',
'split_eye.5.convAct.1.weight': 'p_re_lu_31/Alpha',
'split_eye.5.dwConvConv.0.weight': 'depthwise_conv2d_15/Kernel',
'split_eye.5.dwConvConv.0.bias': 'depthwise_conv2d_15/Bias',
'split_eye.5.dwConvConv.1.weight': 'conv2d_32/Kernel',
'split_eye.5.dwConvConv.1.bias': 'conv2d_32/Bias',
'split_eye.5.act.weight': 'p_re_lu_32/Alpha',
'split_eye.6.convAct.0.weight': 'conv2d_33/Kernel',
'split_eye.6.convAct.0.bias': 'conv2d_33/Bias',
'split_eye.6.convAct.1.weight': 'p_re_lu_33/Alpha',
'split_eye.6.dwConvConv.0.weight': 'depthwise_conv2d_16/Kernel',
'split_eye.6.dwConvConv.0.bias': 'depthwise_conv2d_16/Bias',
'split_eye.6.dwConvConv.1.weight': 'conv2d_34/Kernel',
'split_eye.6.dwConvConv.1.bias': 'conv2d_34/Bias',
'split_eye.6.act.weight': 'p_re_lu_34/Alpha',
'split_eye.7.convAct.0.weight': 'conv2d_35/Kernel',
'split_eye.7.convAct.0.bias': 'conv2d_35/Bias',
'split_eye.7.convAct.1.weight': 'p_re_lu_35/Alpha',
'split_eye.7.dwConvConv.0.weight': 'depthwise_conv2d_17/Kernel',
'split_eye.7.dwConvConv.0.bias': 'depthwise_conv2d_17/Bias',
'split_eye.7.dwConvConv.1.weight': 'conv2d_36/Kernel',
'split_eye.7.dwConvConv.1.bias': 'conv2d_36/Bias',
'split_eye.7.act.weight': 'p_re_lu_36/Alpha',
'split_eye.8.weight': 'conv_eyes_contours_and_brows/Kernel',
'split_eye.8.bias': 'conv_eyes_contours_and_brows/Bias',
'split_iris.0.convAct.0.weight': 'conv2d_37/Kernel',
'split_iris.0.convAct.0.bias': 'conv2d_37/Bias',
'split_iris.0.convAct.1.weight': 'p_re_lu_37/Alpha',
'split_iris.0.dwConvConv.0.weight': 'depthwise_conv2d_18/Kernel',
'split_iris.0.dwConvConv.0.bias': 'depthwise_conv2d_18/Bias',
'split_iris.0.dwConvConv.1.weight': 'conv2d_38/Kernel',
'split_iris.0.dwConvConv.1.bias': 'conv2d_38/Bias',
'split_iris.0.act.weight': 'p_re_lu_38/Alpha',
'split_iris.1.convAct.0.weight': 'conv2d_39/Kernel',
'split_iris.1.convAct.0.bias': 'conv2d_39/Bias',
'split_iris.1.convAct.1.weight': 'p_re_lu_39/Alpha',
'split_iris.1.dwConvConv.0.weight': 'depthwise_conv2d_19/Kernel',
'split_iris.1.dwConvConv.0.bias': 'depthwise_conv2d_19/Bias',
'split_iris.1.dwConvConv.1.weight': 'conv2d_40/Kernel',
'split_iris.1.dwConvConv.1.bias': 'conv2d_40/Bias',
'split_iris.1.act.weight': 'p_re_lu_40/Alpha',
'split_iris.2.convAct.0.weight': 'conv2d_41/Kernel',
'split_iris.2.convAct.0.bias': 'conv2d_41/Bias',
'split_iris.2.convAct.1.weight': 'p_re_lu_41/Alpha',
'split_iris.2.dwConvConv.0.weight': 'depthwise_conv2d_20/Kernel',
'split_iris.2.dwConvConv.0.bias': 'depthwise_conv2d_20/Bias',
'split_iris.2.dwConvConv.1.weight': 'conv2d_42/Kernel',
'split_iris.2.dwConvConv.1.bias': 'conv2d_42/Bias',
'split_iris.2.act.weight': 'p_re_lu_42/Alpha',
'split_iris.3.convAct.0.weight': 'conv2d_43/Kernel',
'split_iris.3.convAct.0.bias': 'conv2d_43/Bias',
'split_iris.3.convAct.1.weight': 'p_re_lu_43/Alpha',
'split_iris.3.dwConvConv.0.weight': 'depthwise_conv2d_21/Kernel',
'split_iris.3.dwConvConv.0.bias': 'depthwise_conv2d_21/Bias',
'split_iris.3.dwConvConv.1.weight': 'conv2d_44/Kernel',
'split_iris.3.dwConvConv.1.bias': 'conv2d_44/Bias',
'split_iris.3.act.weight': 'p_re_lu_44/Alpha',
'split_iris.4.convAct.0.weight': 'conv2d_45/Kernel',
'split_iris.4.convAct.0.bias': 'conv2d_45/Bias',
'split_iris.4.convAct.1.weight': 'p_re_lu_45/Alpha',
'split_iris.4.dwConvConv.0.weight': 'depthwise_conv2d_22/Kernel',
'split_iris.4.dwConvConv.0.bias': 'depthwise_conv2d_22/Bias',
'split_iris.4.dwConvConv.1.weight': 'conv2d_46/Kernel',
'split_iris.4.dwConvConv.1.bias': 'conv2d_46/Bias',
'split_iris.4.act.weight': 'p_re_lu_46/Alpha',
'split_iris.5.convAct.0.weight': 'conv2d_47/Kernel',
'split_iris.5.convAct.0.bias': 'conv2d_47/Bias',
'split_iris.5.convAct.1.weight': 'p_re_lu_47/Alpha',
'split_iris.5.dwConvConv.0.weight': 'depthwise_conv2d_23/Kernel',
'split_iris.5.dwConvConv.0.bias': 'depthwise_conv2d_23/Bias',
'split_iris.5.dwConvConv.1.weight': 'conv2d_48/Kernel',
'split_iris.5.dwConvConv.1.bias': 'conv2d_48/Bias',
'split_iris.5.act.weight': 'p_re_lu_48/Alpha',
'split_iris.6.convAct.0.weight': 'conv2d_49/Kernel',
'split_iris.6.convAct.0.bias': 'conv2d_49/Bias',
'split_iris.6.convAct.1.weight': 'p_re_lu_49/Alpha',
'split_iris.6.dwConvConv.0.weight': 'depthwise_conv2d_24/Kernel',
'split_iris.6.dwConvConv.0.bias': 'depthwise_conv2d_24/Bias',
'split_iris.6.dwConvConv.1.weight': 'conv2d_50/Kernel',
'split_iris.6.dwConvConv.1.bias': 'conv2d_50/Bias',
'split_iris.6.act.weight': 'p_re_lu_50/Alpha',
'split_iris.7.convAct.0.weight': 'conv2d_51/Kernel',
'split_iris.7.convAct.0.bias': 'conv2d_51/Bias',
'split_iris.7.convAct.1.weight': 'p_re_lu_51/Alpha',
'split_iris.7.dwConvConv.0.weight': 'depthwise_conv2d_25/Kernel',
'split_iris.7.dwConvConv.0.bias': 'depthwise_conv2d_25/Bias',
'split_iris.7.dwConvConv.1.weight': 'conv2d_52/Kernel',
'split_iris.7.dwConvConv.1.bias': 'conv2d_52/Bias',
'split_iris.7.act.weight': 'p_re_lu_52/Alpha',
'split_iris.8.weight': 'conv_iris/Kernel',
'split_iris.8.bias': 'conv_iris/Bias'
}
convert.update(manual_mapping)
# Copy the weights into the layers.
#
# Note that the ordering of the weights is different between PyTorch and TFLite, so we need to transpose them.
#
# Convolution weights:
#
# TFLite: (out_channels, kernel_height, kernel_width, in_channels)
# PyTorch: (out_channels, in_channels, kernel_height, kernel_width)
#
# Depthwise convolution weights:
#
# TFLite: (1, kernel_height, kernel_width, channels)
# PyTorch: (channels, 1, kernel_height, kernel_width)
#
# PReLU:
#
# TFLite: (1, 1, num_channels)
# PyTorch: (num_channels, )
#
# %%
new_state_dict = OrderedDict()
for dst, src in convert.items():
W = get_weights(src)
print(dst, src, W.shape, net.state_dict()[dst].shape)
if W.ndim == 4:
if W.shape[0] == 1: # no conv2d with out_channel == 1 in this net
W = W.transpose((3, 0, 1, 2)) # depthwise conv
else:
W = W.transpose((0, 3, 1, 2)) # regular conv
elif W.ndim == 3:
W = W.reshape(-1)
new_state_dict[dst] = torch.from_numpy(W)
# %%
net.load_state_dict(new_state_dict, strict=True)
# No errors? Then the conversion was successful!
# ## Save the checkpoint
# %%
torch.save(net.state_dict(), "irislandmarks.pth")
# %%