This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
openvino2tensorflow.py
778 lines (677 loc) · 37.7 KB
/
openvino2tensorflow.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
'''
tensorflow==2.3.1
python3 openvino2tensorflow.py \
--model_path=openvino/448x448/FP32/Resnet34_3inputs_448x448_20200609.xml \
--output_saved_model=True \
--output_pb=True \
--output_weight_quant_tflite=True \
--output_float16_quant_tflite=True \
--output_no_quant_float32_tflite=True
python3 openvino2tensorflow.py \
--model_path=openvino/kitti_192x640/FP32/footprints_kitti_192x640.xml \
--output_saved_model=True \
--output_no_quant_float32_tflite=True
python3 openvino2tensorflow.py \
--model_path=openvino/dense_depth_nyu_480x640/FP32/dense_depth_nyu_480x640.xml \
--output_saved_model=True \
--output_no_quant_float32_tflite=True
python3 openvino2tensorflow.py \
--model_path=openvino/480x640/FP32/u2netp_480x640.xml \
--output_saved_model=True \
--output_no_quant_float32_tflite=True
python3 openvino2tensorflow.py \
--model_path=openvino/deeplabv3/FP32/deeplabv3.xml \
--output_saved_model=True \
--output_no_quant_float32_tflite=True
python3 openvino2tensorflow.py \
--model_path=openvino/efficientnet-b0-pytorch/FP32/efficientnet-b0-pytorch.xml \
--output_saved_model=True \
--output_pb=True \
--output_no_quant_float32_tflite=True \
--debug \
--debug_layer_number=5
python3 openvino2tensorflow.py \
--model_path=openvino/mobilenet-v2-pytorch/FP32/mobilenet-v2-pytorch.xml \
--output_saved_model=True \
--output_no_quant_float32_tflite=True \
--debug \
--debug_layer_number=258
python3 openvino2tensorflow.py \
--model_path=openvino/midasnet/FP32/midasnet.xml \
--output_saved_model=True \
--output_no_quant_float32_tflite=True
python3 openvino2tensorflow.py \
--model_path openvino/tf_efficientnet_lite3_256x256/FP32/tf_efficientnet_lite3.xml \
--output_saved_model=True \
--output_pb=True
'''
import os
import sys
import argparse
import struct
import numpy as np
import xml.etree.ElementTree as et
from openvino.inference_engine import IECore
import tensorflow as tf
from tensorflow.keras import Model, Input
from tensorflow.keras.layers import Conv2D, DepthwiseConv2D, Add, ReLU, PReLU, MaxPool2D, AveragePooling2D, Reshape, Concatenate, Conv2DTranspose, Layer
from tensorflow.keras.initializers import Constant
from tensorflow.keras.backend import shape, clip
from tensorflow.keras.activations import tanh, elu, sigmoid, swish, softmax
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
import numpy as np
import sys
import tensorflow_datasets as tfds
def convert(model,
model_output_path,
output_saved_model,
output_h5,
output_pb,
output_no_quant_float32_tflite,
output_weight_quant_tflite,
output_float16_quant_tflite,
debug,
debug_layer_number):
# for unpacking binary buffer
format_config = { 'FP32': ['f', 4],
'FP16': ['e', 2],
'I64' : ['q', 8],
'I32' : ['i', 4],
'I16' : ['h', 2],
'I8' : ['b', 1],
'U8' : ['B', 1]}
# vino: u8, u16, u32, u64, i8, i16, i32, i64, f16, f32, bf16, boolean
# tf : uint8, uint16, uint32, uint64, int8, int16, int32, int64, float16, float32, float64, bfloat16
# type conversion table
cast_type_ov_tf = { 'u8' : tf.uint8,
'u16' : tf.uint16,
'u32' : tf.uint32,
'u64' : tf.uint64,
'i8' : tf.int8,
'i16' : tf.int16,
'i32' : tf.int32,
'i64' : tf.int64,
'f16' : tf.float16,
'f32' : tf.float32,
'bf16': tf.bfloat16}
# pad type conversion table
pad_type_ov_tf = { 'constant' : 'CONSTANT',
'reflect' : 'REFLECT',
'symmetric': 'SYMMETRIC',
'edge' : 'REFLECT'}
# Read IR weight data
with open(model+'.bin', 'rb') as f:
binWeight = f.read()
# Parse IR XML file,
tree = et.parse(model+'.xml')
root = tree.getroot()
edges = root.find('edges')
layers = root.find('layers')
tf_layers_dict = {}
tf_edges = {}
tf_inputs = []
tf_outputs = []
transpose_squeeze_skip = False
# edges
for edge in edges:
to_layer = int(edge.attrib['to-layer'])
from_layer = int(edge.attrib['from-layer'])
tf_edges.setdefault(to_layer, []).append(from_layer)
# layers
for idx, layer in enumerate(layers):
layer_id = int(layer.attrib['id'])
layer_name = layer.attrib['name'].replace('.', '_').replace('/', '_')
data = layer.find('data')
### Parameter
if layer.attrib['type'] == 'Parameter':
if not data is None and 'shape' in data.attrib:
shape_str = data.attrib['shape'].split(',')
shape = [int(s) for s in shape_str]
tf_layers_dict[layer_id] = Input(shape=(shape[2], shape[3], shape[1]), batch_size=shape[0], name=layer_name)
tf_inputs.append(tf_layers_dict[layer_id])
### Const
elif layer.attrib['type'] == 'Const':
if not data is None:
if 'offset' in data.attrib and 'size' in data.attrib:
offset = int(data.attrib['offset'])
size = int(data.attrib['size'])
shape_str = '1' if data.attrib['shape'] == '' else data.attrib['shape'].split(',')
shape = [int(s) for s in shape_str]
blobBin = binWeight[offset:offset+size]
prec = layer.find('output').find('port').attrib['precision']
formatstring = '<' + format_config[prec][0] * (len(blobBin)//format_config[prec][1])
decodedwgt = np.array(list(struct.unpack(formatstring, blobBin))).reshape(shape)
tf_layers_dict[layer_id] = decodedwgt
### Convolution
elif layer.attrib['type'] == 'Convolution':
# port0 = [int(sdim.text) for sdim in layer.find('input')[0]]
port1 = [int(sdim.text) for sdim in layer.find('input')[1]]
filters = int(port1[0])
kernel_size = [int(port1[2]), int(port1[3])]
strides = [int(s) for s in data.attrib['strides'].split(',')]
pads_begin = sum([int(s) for s in data.attrib['pads_begin'].split(',')])
pads_end = sum([int(s) for s in data.attrib['pads_end'].split(',')])
padding = ''
if (pads_begin + pads_end) == 0:
if 'auto_pad' in data.attrib:
if data.attrib['auto_pad'] == 'same_upper' or data.attrib['auto_pad'] == 'same_lower':
padding = 'same'
else:
padding = 'valid'
else:
padding = 'valid'
else:
padding = 'same'
dilations = [int(s) for s in data.attrib['dilations'].split(',')]
tf_layers_dict[layer_id] = Conv2D(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding=padding,
dilation_rate=dilations,
use_bias=False,
kernel_initializer=Constant(tf_layers_dict[tf_edges[layer_id][1]].transpose(2,3,1,0)))(tf_layers_dict[tf_edges[layer_id][0]])
### Add
elif layer.attrib['type'] == 'Add':
# 'Fused_Add_' == BiasAdd
if len(tf_edges[layer_id]) == 2 and (type(tf_layers_dict[tf_edges[layer_id][1]]) == np.ndarray):
# Biasadd
edge_id0 = tf_edges[layer_id][0]
edge_id1 = tf_edges[layer_id][1]
tf_layers_dict[layer_id] = tf.math.add(tf_layers_dict[edge_id0], tf_layers_dict[edge_id1].flatten())
else:
# Add
tf_layers_dict[layer_id] = Add()([tf_layers_dict[from_layer_id].transpose(0,2,3,1) if type(tf_layers_dict[from_layer_id]) == np.ndarray else tf_layers_dict[from_layer_id] for from_layer_id in tf_edges[layer_id]])
### ReLU
elif layer.attrib['type'] == 'ReLU':
tf_layers_dict[layer_id] = ReLU()(tf_layers_dict[tf_edges[layer_id][0]])
### PReLU
elif layer.attrib['type'] == 'PReLU':
tf_layers_dict[layer_id] = PReLU(alpha_initializer=Constant(tf_layers_dict[tf_edges[layer_id][1]]))(tf_layers_dict[tf_edges[layer_id][0]])
### Clamp
elif layer.attrib['type'] == 'Clamp':
cmin = float(data.attrib['min'])
cmax = float(data.attrib['max'])
if cmin == 0.0 and cmax == 6.0:
# ReLU6
tf_layers_dict[layer_id] = tf.nn.relu6(tf_layers_dict[tf_edges[layer_id][0]])
else:
# Other
tf_layers_dict[layer_id] = clip(tf_layers_dict[tf_edges[layer_id][0]], min_value=cmin, max_value=cmax)
### Tan
elif layer.attrib['type'] == 'Tan':
tf_layers_dict[layer_id] = tf.math.tan(tf_layers_dict[tf_edges[layer_id][0]])
### Tanh
elif layer.attrib['type'] == 'Tanh':
tf_layers_dict[layer_id] = tanh(tf_layers_dict[tf_edges[layer_id][0]])
### Elu
elif layer.attrib['type'] == 'Elu':
alpha = float(data.attrib['alpha'])
tf_layers_dict[layer_id] = elu(tf_layers_dict[tf_edges[layer_id][0]], alpha=alpha)
### Sigmoid
elif layer.attrib['type'] == 'Sigmoid':
tf_layers_dict[layer_id] = sigmoid(tf_layers_dict[tf_edges[layer_id][0]])
### Swish
elif layer.attrib['type'] == 'Swish':
tf_layers_dict[layer_id] = swish(tf_layers_dict[tf_edges[layer_id][0]])
### MaxPool
elif layer.attrib['type'] == 'MaxPool':
kernel_size = [int(s) for s in data.attrib['kernel'].split(',')]
strides = [int(s) for s in data.attrib['strides'].split(',')]
pads_begin = sum([int(s) for s in data.attrib['pads_begin'].split(',')])
pads_end = sum([int(s) for s in data.attrib['pads_end'].split(',')])
padding = ''
if (pads_begin + pads_end) == 0:
if 'auto_pad' in data.attrib:
if data.attrib['auto_pad'] == 'same_upper' or data.attrib['auto_pad'] == 'same_lower':
padding = 'SAME'
else:
padding = 'VALID'
else:
padding = 'VALID'
else:
padding = 'SAME'
tf_layers_dict[layer_id] = tf.nn.max_pool(tf_layers_dict[tf_edges[layer_id][0]], ksize=kernel_size, strides=strides, padding=padding)
### AvgPool
elif layer.attrib['type'] == 'AvgPool':
kernel_size = [int(s) for s in data.attrib['kernel'].split(',')]
strides = [int(s) for s in data.attrib['strides'].split(',')]
# exclude_pad = data.attrib['exclude-pad']
pads_begin = sum([int(s) for s in data.attrib['pads_begin'].split(',')])
pads_end = sum([int(s) for s in data.attrib['pads_end'].split(',')])
padding = ''
if (pads_begin + pads_end) == 0:
if 'auto_pad' in data.attrib:
if data.attrib['auto_pad'] == 'same_upper' or data.attrib['auto_pad'] == 'same_lower':
padding = 'same'
else:
padding = 'valid'
else:
padding = 'valid'
else:
padding = 'same'
tf_layers_dict[layer_id] = AveragePooling2D(pool_size=kernel_size, strides=strides, padding=padding)(tf_layers_dict[tf_edges[layer_id][0]])
### GroupConvolution
elif layer.attrib['type'] == 'GroupConvolution':
port0 = [int(sdim.text) for sdim in layer.find('input')[0]]
port1 = [int(sdim.text) for sdim in layer.find('input')[1]]
depth_multiplier = 1
kernel_size = [int(port1[3]), int(port1[4])]
strides = [int(s) for s in data.attrib['strides'].split(',')]
pads_begin = sum([int(s) for s in data.attrib['pads_begin'].split(',')])
pads_end = sum([int(s) for s in data.attrib['pads_end'].split(',')])
padding = ''
if (pads_begin + pads_end) == 0:
if 'auto_pad' in data.attrib:
if data.attrib['auto_pad'] == 'same_upper' or data.attrib['auto_pad'] == 'same_lower':
padding = 'same'
else:
padding = 'valid'
else:
padding = 'valid'
else:
padding = 'same'
dilations = [int(s) for s in data.attrib['dilations'].split(',')]
if int(port1[1]) > 1:
# Conv2D with groups
filters = int(port0[1])
groups = int(port1[0])
if len(port1) == 5:
tf_layers_dict[layer_id] = Conv2D(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding=padding,
dilation_rate=dilations,
groups=groups,
use_bias=False,
kernel_initializer=Constant(tf_layers_dict[tf_edges[layer_id][1]].transpose(3,4,1,2,0)))(tf_layers_dict[tf_edges[layer_id][0]])
else:
tf_layers_dict[layer_id] = Conv2D(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding=padding,
dilation_rate=dilations,
groups=groups,
use_bias=False,
kernel_initializer=Constant(tf_layers_dict[tf_edges[layer_id][1]].transpose(2,3,1,0)))(tf_layers_dict[tf_edges[layer_id][0]])
else:
# DepthwiseConv2D
tf_layers_dict[layer_id] = DepthwiseConv2D(kernel_size=kernel_size,
strides=strides,
padding=padding,
depth_multiplier=depth_multiplier,
dilation_rate=dilations,
use_bias=False,
depthwise_initializer=Constant(tf_layers_dict[tf_edges[layer_id][1]].transpose(3,4,1,2,0)))(tf_layers_dict[tf_edges[layer_id][0]])
### ConvolutionBackpropData
elif layer.attrib['type'] == 'ConvolutionBackpropData':
# port0 = [int(sdim.text) for sdim in layer.find('input')[0]]
port1 = [int(sdim.text) for sdim in layer.find('input')[1]]
filters = int(port1[0])
kernel_size = [int(port1[2]), int(port1[3])]
strides = [int(s) for s in data.attrib['strides'].split(',')]
pads_begin = sum([int(s) for s in data.attrib['pads_begin'].split(',')])
pads_end = sum([int(s) for s in data.attrib['pads_end'].split(',')])
padding = ''
if (pads_begin + pads_end) == 0:
if 'auto_pad' in data.attrib:
if data.attrib['auto_pad'] == 'same_upper' or data.attrib['auto_pad'] == 'same_lower':
padding = 'same'
else:
padding = 'valid'
else:
padding = 'valid'
else:
padding = 'same'
dilations = [int(s) for s in data.attrib['dilations'].split(',')]
tf_layers_dict[layer_id] = Conv2DTranspose(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding=padding,
dilation_rate=dilations,
use_bias=False,
kernel_initializer=Constant(tf_layers_dict[tf_edges[layer_id][1]].transpose(2,3,1,0)))(tf_layers_dict[tf_edges[layer_id][0]])
### Concat
elif layer.attrib['type'] == 'Concat':
tf_layers_dict[layer_id] = Concatenate()([tf_layers_dict[from_layer_id] for from_layer_id in tf_edges[layer_id]])
### Multiply
elif layer.attrib['type'] == 'Multiply':
if len(tf_edges[layer_id]) == 2 and (type(tf_layers_dict[tf_edges[layer_id][1]]) == np.ndarray):
if tf_layers_dict[tf_edges[layer_id][1]].ndim == 4:
# 4D - NCHW->NHWC
tf_layers_dict[layer_id] = tf.math.multiply(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]].transpose(0,2,3,1).astype(np.float32))
else:
# unknown
tf_layers_dict[layer_id] = tf.math.multiply(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]])
elif len(tf_edges[layer_id]) == 2 and (type(tf_layers_dict[tf_edges[layer_id][0]]) == np.ndarray):
if tf_layers_dict[tf_edges[layer_id][0]].ndim == 4:
# 4D - NCHW->NHWC
tf_layers_dict[layer_id] = tf.math.multiply(tf_layers_dict[tf_edges[layer_id][0]].transpose(0,2,3,1).astype(np.float32), tf_layers_dict[tf_edges[layer_id][1]])
else:
# unknown
tf_layers_dict[layer_id] = tf.math.multiply(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]])
else:
# unknown
tf_layers_dict[layer_id] = tf.math.multiply(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]])
### Interpolate
elif layer.attrib['type'] == 'Interpolate':
mode = data.attrib['mode']
antialias = False if int(data.attrib['antialias']) == 0 else True
out_port0 = [int(sdim.text) for sdim in layer.find('output')[0]]
out_height = int(out_port0[2])
out_width = int(out_port0[3])
if mode == 'linear':
tf_layers_dict[layer_id] = tf.image.resize(tf_layers_dict[tf_edges[layer_id][0]], [out_height, out_width], method='bilinear', antialias=antialias)
elif mode == 'nearest':
tf_layers_dict[layer_id] = tf.image.resize(tf_layers_dict[tf_edges[layer_id][0]], [out_height, out_width], method='nearest', antialias=antialias)
else:
print('The Interpolate - {} is not yet implemented.'.format(mode))
sys.exit(-1)
### ShapeOf
elif layer.attrib['type'] == 'ShapeOf':
tf_layers_dict[layer_id] = tf.keras.backend.shape(tf_layers_dict[tf_edges[layer_id][0]])
### Convert
elif layer.attrib['type'] == 'Convert':
# vino: u8, u16, u32, u64, i8, i16, i32, i64, f16, f32, bf16, boolean
# tf : uint8, uint16, uint32, uint64, int8, int16, int32, int64, float16, float32, float64, bfloat16
destination_type = data.attrib['destination_type']
tf_layers_dict[layer_id] = tf.cast(tf_layers_dict[tf_edges[layer_id][0]], cast_type_ov_tf[destination_type])
### StridedSlice
elif layer.attrib['type'] == 'StridedSlice':
begin_mask = int(data.attrib['begin_mask'])
end_mask = int(data.attrib['end_mask'])
ellipsis_mask = int(data.attrib['ellipsis_mask'])
new_axis_mask = int(data.attrib['new_axis_mask'])
shrink_axis_mask = int(data.attrib['shrink_axis_mask'])
begin = [-1] if int(tf_layers_dict[tf_edges[layer_id][1]]) == -1 else [int(tf_layers_dict[tf_edges[layer_id][1]]) - 1]
end = [-1] if int(tf_layers_dict[tf_edges[layer_id][2]]) == -1 else [int(tf_layers_dict[tf_edges[layer_id][2]]) - 1]
strides = [int(tf_layers_dict[tf_edges[layer_id][3]])]
tf_layers_dict[layer_id] = tf.strided_slice(tf_layers_dict[tf_edges[layer_id][0]],
begin=begin,
end=end,
strides=strides,
begin_mask=begin_mask,
end_mask=end_mask,
ellipsis_mask=ellipsis_mask,
new_axis_mask=new_axis_mask,
shrink_axis_mask=shrink_axis_mask)
### Pad
elif layer.attrib['type'] == 'Pad':
pad_mode = pad_type_ov_tf[data.attrib['pad_mode']]
pads_begin = tf_layers_dict[tf_edges[layer_id][1]] # [0,0,1,1]
pads_end = tf_layers_dict[tf_edges[layer_id][2]] # [0,0,1,1]
pad_b_top = 0 if (pads_end[0] == 0 and pads_begin[0] == 0) else (pads_end[0] - pads_begin[0] + 1)
pad_b_bottom = pad_b_top
pad_c_top = 0 if (pads_end[1] == 0 and pads_begin[1] == 0) else (pads_end[1] - pads_begin[1] + 1)
pad_c_bottom = pad_c_top
pad_top = 0 if (pads_end[2] == 0 and pads_begin[2] == 0) else (pads_end[2] - pads_begin[2] + 1)
pad_bottom = pad_top
pad_left = 0 if (pads_end[3] == 0 and pads_begin[3] == 0) else (pads_end[3] - pads_begin[3] + 1)
pad_right = pad_left
paddings = [[pad_b_top, pad_b_bottom], [pad_top, pad_bottom], [pad_left, pad_right], [pad_c_top, pad_c_bottom]]
pad_value = [0.0]
if 'pad_value' in data.attrib:
pad_value = [float(data.attrib['pad_value'])]
tf_layers_dict[layer_id] = tf.pad(tf_layers_dict[tf_edges[layer_id][0]], paddings, mode=pad_mode, constant_values=pad_value)
### TopK
elif layer.attrib['type'] == 'TopK':
# axis = int(data.attrib['axis'])
# index_element_type = data.attrib['index_element_type']
# mode = data.attrib['mode']
# sort = data.attrib['sort']
transpose_squeeze_skip = False
k = int(tf_layers_dict[tf_edges[layer_id][1]])
if k == 1:
tf_layers_dict[layer_id] = tf.math.argmax(tf_layers_dict[tf_edges[layer_id][0]], axis=-1, output_type=tf.dtypes.int32)
transpose_squeeze_skip = True
temp_final_output_layer = tf_layers_dict[layer_id]
else:
tf_layers_dict[layer_id] = tf.math.top_k(tf_layers_dict[tf_edges[layer_id][0]], k=int(tf_layers_dict[tf_edges[layer_id][1]]), sorted=True)
transpose_squeeze_skip = False
### Transpose -TODO
elif layer.attrib['type'] == 'Transpose':
if transpose_squeeze_skip and tf_layers_dict[tf_edges[layer_id][0]].name.split(':')[0] == 'ArgMax':
continue
else:
tf_layers_dict[layer_id] = tf.transpose(tf_layers_dict[tf_edges[layer_id][0]], perm=tf_layers_dict[tf_edges[layer_id][1]])
### Squeeze
elif layer.attrib['type'] == 'Squeeze':
if transpose_squeeze_skip:
continue
else:
axis = None
if len(tf_layers_dict[tf_edges[layer_id][1]]) == 1:
axis = int(tf_layers_dict[tf_edges[layer_id][1]])
if axis == 1:
axis = -1
elif axis >= 2:
axis -= 1
else:
for idx, part_axis in enumerate(tf_layers_dict[tf_edges[layer_id][1]]):
if part_axis == 1:
tf_layers_dict[tf_edges[layer_id][1]][idx] = 3
elif part_axis >= 2:
tf_layers_dict[tf_edges[layer_id][1]][idx] -= 1
axis = tf_layers_dict[tf_edges[layer_id][1]]
tf_layers_dict[layer_id] = tf.squeeze(tf_layers_dict[tf_edges[layer_id][0]], axis=axis)
transpose_squeeze_skip = False
### Gather - TODO
elif layer.attrib['type'] == 'Gather':
axis = int(tf_layers_dict[tf_edges[layer_id][2]])
indices = int(tf_layers_dict[tf_edges[layer_id][1]])
tf_layers_dict[layer_id] = [tf.cast(tf.gather(tf_layers_dict[tf_edges[layer_id][0]], indices, axis=axis), tf.int64)]
### ReduceMean, ReduceMax, ReduceMin, ReduceSum
elif layer.attrib['type'] == 'ReduceMean' or layer.attrib['type'] == 'ReduceMax' or layer.attrib['type'] == 'ReduceMin' or layer.attrib['type'] == 'ReduceSum':
keep_dims = True if data.attrib['keep_dims'] == "True" else False
axis1 = tf_layers_dict[tf_edges[layer_id][1]][0] - 1
axis2 = tf_layers_dict[tf_edges[layer_id][1]][1] - 1
if layer.attrib['type'] == 'ReduceMean':
tf_layers_dict[layer_id] = tf.math.reduce_mean(tf_layers_dict[tf_edges[layer_id][0]], axis=[axis1, axis2], keepdims=keep_dims)
elif layer.attrib['type'] == 'ReduceMax':
tf_layers_dict[layer_id] = tf.math.reduce_max(tf_layers_dict[tf_edges[layer_id][0]], axis=[axis1, axis2], keepdims=keep_dims)
elif layer.attrib['type'] == 'ReduceMin':
tf_layers_dict[layer_id] = tf.math.reduce_min(tf_layers_dict[tf_edges[layer_id][0]], axis=[axis1, axis2], keepdims=keep_dims)
elif layer.attrib['type'] == 'ReduceSum':
tf_layers_dict[layer_id] = tf.math.reduce_sum(tf_layers_dict[tf_edges[layer_id][0]], axis=[axis1, axis2], keepdims=keep_dims)
### MatMul
elif layer.attrib['type'] == 'MatMul':
if not data is None and 'a' in data.attrib:
transpose_a = True if int(data.attrib['a']) == 1 else False
if not data is None and 'b' in data.attrib:
transpose_b = True if int(data.attrib['b']) == 1 else False
if not data is None and 'transpose_a' in data.attrib:
transpose_a = True if int(data.attrib['transpose_a']) == 1 else False
if not data is None and 'transpose_b' in data.attrib:
transpose_b = True if int(data.attrib['transpose_b']) == 1 else False
tf_layers_dict[layer_id] = tf.linalg.matmul(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]],
transpose_a, transpose_b)
### Reshape -TODO
elif layer.attrib['type'] == 'Reshape':
tf_layers_dict[layer_id] = tf.reshape(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]])
### Range - TODO
elif layer.attrib['type'] == 'Range':
dtype = cast_type_ov_tf[data.attrib['output_type']]
tf_layers_dict[layer_id] = tf.range(tf_layers_dict[tf_edges[layer_id][0]][0],
tf_layers_dict[tf_edges[layer_id][1]],
delta=int(tf_layers_dict[tf_edges[layer_id][2]]),
dtype=dtype)
### Exp
elif layer.attrib['type'] == 'Exp':
tf_layers_dict[layer_id] = tf.math.exp(tf_layers_dict[tf_edges[layer_id][0]])
### Abs
elif layer.attrib['type'] == 'Abs':
tf_layers_dict[layer_id] = tf.math.abs(tf_layers_dict[tf_edges[layer_id][0]])
### SoftMax
elif layer.attrib['type'] == 'SoftMax':
axis = int(data.attrib['axis'])
tf_layers_dict[layer_id] = softmax(tf_layers_dict[tf_edges[layer_id][0]], axis=axis)
### Negative
elif layer.attrib['type'] == 'Negative':
tf_layers_dict[layer_id] = tf.math.negative(tf_layers_dict[tf_edges[layer_id][0]])
### Maximum
elif layer.attrib['type'] == 'Maximum':
# No broadcast
tf_layers_dict[layer_id] = tf.math.maximum(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]])
### Minimum
elif layer.attrib['type'] == 'Minimum':
# No broadcast
tf_layers_dict[layer_id] = tf.math.minimum(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]])
### Acos
elif layer.attrib['type'] == 'Acos':
tf_layers_dict[layer_id] = tf.math.acos(tf_layers_dict[tf_edges[layer_id][0]])
### Acosh
elif layer.attrib['type'] == 'Acosh':
tf_layers_dict[layer_id] = tf.math.acosh(tf_layers_dict[tf_edges[layer_id][0]])
### Asin
elif layer.attrib['type'] == 'Asin':
tf_layers_dict[layer_id] = tf.math.asin(tf_layers_dict[tf_edges[layer_id][0]])
### Asinh
elif layer.attrib['type'] == 'Asinh':
tf_layers_dict[layer_id] = tf.math.asinh(tf_layers_dict[tf_edges[layer_id][0]])
### Atan
elif layer.attrib['type'] == 'Atan':
tf_layers_dict[layer_id] = tf.math.atan(tf_layers_dict[tf_edges[layer_id][0]])
### Atanh
elif layer.attrib['type'] == 'Atanh':
tf_layers_dict[layer_id] = tf.math.atanh(tf_layers_dict[tf_edges[layer_id][0]])
### Ceiling
elif layer.attrib['type'] == 'Ceiling':
tf_layers_dict[layer_id] = tf.math.ceil(tf_layers_dict[tf_edges[layer_id][0]])
### Cos
elif layer.attrib['type'] == 'Cos':
tf_layers_dict[layer_id] = tf.math.cos(tf_layers_dict[tf_edges[layer_id][0]])
### Cosh
elif layer.attrib['type'] == 'Cosh':
tf_layers_dict[layer_id] = tf.math.cosh(tf_layers_dict[tf_edges[layer_id][0]])
### Divide
elif layer.attrib['type'] == 'Divide':
tf_layers_dict[layer_id] = tf.math.divide_no_nan(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]])
### Erf
elif layer.attrib['type'] == 'Erf':
tf_layers_dict[layer_id] = tf.math.erf(tf_layers_dict[tf_edges[layer_id][0]])
### Floor
elif layer.attrib['type'] == 'Floor':
tf_layers_dict[layer_id] = tf.math.floor(tf_layers_dict[tf_edges[layer_id][0]])
### FloorMod
elif layer.attrib['type'] == 'FloorMod':
tf_layers_dict[layer_id] = tf.math.floormod(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]])
### HSwish
elif layer.attrib['type'] == 'HSwish':
# Hard-Swish
tf_layers_dict[layer_id] = tf_layers_dict[tf_edges[layer_id][0]] * tf.nn.relu6(tf_layers_dict[tf_edges[layer_id][0]] + 3) * 0.16666667
### Log
elif layer.attrib['type'] == 'Log':
tf_layers_dict[layer_id] = tf.math.log(tf_layers_dict[tf_edges[layer_id][0]])
### Power
elif layer.attrib['type'] == 'Power':
# No broadcast
tf_layers_dict[layer_id] = tf.math.pow(tf_layers_dict[tf_edges[layer_id][0]], tf_layers_dict[tf_edges[layer_id][1]])
### Mish
elif layer.attrib['type'] == 'Mish':
tf_layers_dict[layer_id] = tf_layers_dict[tf_edges[layer_id][0]] * tf.math.tanh(tf.math.softplus(tf_layers_dict[tf_edges[layer_id][0]]))
### Selu
elif layer.attrib['type'] == 'Selu':
tf_layers_dict[layer_id] = tf.nn.selu(tf_layers_dict[tf_edges[layer_id][0]])
# ### Split - TODO
# elif layer.attrib['type'] == 'Split':
# num_splits = int(data.attrib['num_splits'])
# axis = int(tf_layers_dict[tf_edges[layer_id][1]])
# if axis == 1:
# axis = 3
# elif axis >= 2:
# axis -= 1
# tf_layers_dict[layer_id] = tf.split(tf_layers_dict[tf_edges[layer_id][0]], num_splits, axis=axis)
### Result
elif layer.attrib['type'] == 'Result':
try:
tf_layers_dict[layer_id] = tf.identity(tf_layers_dict[tf_edges[layer_id][0]], name=layer.attrib['name'].split('/')[0])
tf_outputs.append(tf_layers_dict[layer_id])
except:
tf_layers_dict[layer_id] = tf.identity(temp_final_output_layer, name=layer.attrib['name'].split('/')[0])
tf_outputs.append(tf_layers_dict[layer_id])
transpose_squeeze_skip = False
else:
print('The {} layer is not yet implemented.'.format(layer.attrib['type']))
sys.exit(-1)
if debug and idx > debug_layer_number:
tf_outputs.append(tf_layers_dict[layer_id])
break
model = Model(inputs=tf_inputs, outputs=tf_outputs)
model.summary()
if output_saved_model:
tf.saved_model.save(model, model_output_path)
# .h5 output
if output_h5:
model.save('{}/model.h5'.format(model_output_path))
# .pb output
if output_pb:
full_model = tf.function(lambda inputs: model(inputs))
full_model = full_model.get_concrete_function(inputs=[tf.TensorSpec(model_input.shape, model_input.dtype) for model_input in model.inputs])
frozen_func = convert_variables_to_constants_v2(full_model, lower_control_flow=False)
frozen_func.graph.as_graph_def()
tf.io.write_graph(graph_or_graph_def=frozen_func.graph,
logdir=".",
name='{}/model_float32.pb'.format(model_output_path),
as_text=False)
# No Quantization - Input/Output=float32
if output_no_quant_float32_tflite:
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('{}/model_float32.tflite'.format(model_output_path), 'wb') as w:
w.write(tflite_model)
print("tflite convert complete! - {}/model_float32.tflite".format(model_output_path))
# Weight Quantization - Input/Output=float32
if output_weight_quant_tflite:
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
tflite_model = converter.convert()
with open('{}/model_weight_quant.tflite'.format(model_output_path), 'wb') as w:
w.write(tflite_model)
print('Weight Quantization complete! - {}/model_weight_quant.tflite'.format(model_output_path))
# Float16 Quantization - Input/Output=float32
if output_float16_quant_tflite:
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.float16]
tflite_quant_model = converter.convert()
with open('{}/model_float16_quant.tflite'.format(model_output_path), 'wb') as w:
w.write(tflite_quant_model)
print('Float16 Quantization complete! - {}/model_float16_quant.tflite'.format(model_output_path))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--model_path', type=str, required=True, help='input IR model path (.xml)')
parser.add_argument('--model_output_path', type=str, default='saved_model', help='The output folder path of the converted model file')
parser.add_argument('--output_saved_model', type=bool, default=False, help='saved_model output switch')
parser.add_argument('--output_h5', type=bool, default=False, help='.h5 output switch')
parser.add_argument('--output_pb', type=bool, default=False, help='.pb output switch')
parser.add_argument('--output_no_quant_float32_tflite', type=bool, default=False, help='float32 tflite output switch')
parser.add_argument('--output_weight_quant_tflite', type=bool, default=False, help='weight quant tflite output switch')
parser.add_argument('--output_float16_quant_tflite', type=bool, default=False, help='float16 quant tflite output switch')
parser.add_argument('--debug', action='store_true', help='debug mode switch')
parser.add_argument('--debug_layer_number', type=int, default=0, help='The last layer number to output when debugging. Used only when --debug=True.')
args = parser.parse_args()
model, ext = os.path.splitext(args.model_path)
model_output_path = args.model_output_path.rstrip('/')
if ext != '.xml':
print('The specified model is not \'.xml\' file.')
sys.exit(-1)
output_saved_model = args.output_saved_model
output_h5 = args.output_h5
output_pb = args.output_pb
output_no_quant_float32_tflite = args.output_no_quant_float32_tflite
output_weight_quant_tflite = args.output_weight_quant_tflite
output_float16_quant_tflite = args.output_float16_quant_tflite
debug = args.debug
debug_layer_number = args.debug_layer_number
if not output_saved_model and \
not output_h5 and \
not output_pb and \
not output_no_quant_float32_tflite and \
not output_weight_quant_tflite and \
not output_float16_quant_tflite:
print('Set at least one of the output switches (output_*) to true.')
sys.exit(-1)
convert(model, model_output_path, output_saved_model, output_h5, output_pb,
output_no_quant_float32_tflite, output_weight_quant_tflite, output_float16_quant_tflite,
debug, debug_layer_number)
if __name__ == "__main__":
main()