1 # Copyright (c) 2019 Guo Yejun
2 #
3 # This file is part of FFmpeg.
4 #
5 # FFmpeg is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # FFmpeg is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with FFmpeg; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 # ==============================================================================
19
20 import tensorflow as tf
21 import numpy as np
22 import sys, struct
23 import convert_header as header
24
25 __all__ = [
'convert_from_tensorflow']
26
30 IOTYPE_INTERMEDIATE = IOTYPE_INPUT | IOTYPE_OUTPUT
41 Operand.index = Operand.index + 1
42 self.
iotype2str = {Operand.IOTYPE_INPUT:
'in', Operand.IOTYPE_OUTPUT:
'out', Operand.IOTYPE_INTERMEDIATE:
'inout'}
43 self.
dtype2str = {Operand.DTYPE_FLOAT:
'DT_FLOAT', Operand.DTYPE_UINT8:
'DT_UINT8'}
44
47 if iotype == Operand.IOTYPE_INPUT:
49
51 return "{}: (name: {}, iotype: {}, dtype: {}, dims: {}, used_count: {})".
format(self.
index,
54
56 return self.
index < other.index
57
59 def __init__(self, graph_def, nodes, outfile, dump4tb):
76 self.
op2code = {
'Conv2D':1,
'DepthToSpace':2,
'MirrorPad':3,
'Maximum':4,
77 'MathBinary':5, 'MathUnary':6, 'AvgPool':7, 'MatMul':8}
78 self.
mathbin2code = {
'Sub':0,
'Add':1,
'Mul':2,
'RealDiv':3,
'Minimum':4,
'FloorMod':5}
79 self.
mathun2code = {
'Abs':0,
'Sin':1,
'Cos':2,
'Tan':3,
'Asin':4,
80 'Acos':5, 'Atan':6, 'Sinh':7, 'Cosh':8, 'Tanh':9, 'Asinh':10,
81 'Acosh':11, 'Atanh':12, 'Ceil':13, 'Floor':14, 'Round':15,
82 'Exp':16}
85
86
90 dtype = node.attr['dtype'].type
91 if dtype == 0:
92 dtype = node.attr['T'].type
93 dims = [-1,-1,-1,-1]
94 if 'shape' in node.attr:
95 dims[0] = node.attr['shape'].shape.dim[0].size
96 dims[1] = node.attr['shape'].shape.dim[1].size
97 dims[2] = node.attr['shape'].shape.dim[2].size
98 dims[3] = node.attr['shape'].shape.dim[3].size
99 operand =
Operand(name, dtype, dims)
103
104
106 graph = tf.get_default_graph()
107 tf.import_graph_def(self.
graph_def, name=
"")
108 tf.summary.FileWriter('/tmp/graph', graph)
109 print(
'graph saved, run "tensorboard --logdir=/tmp/graph" to see it')
110
111
115
118 else:
119 dnode = None
120
121 # the BiasAdd name is possible be changed into the output name,
122 # if activation is None, and BiasAdd.next is the last op which is Identity
123 if conv2d_scope_name +
'/BiasAdd' in self.
edges:
124 anode = self.
edges[conv2d_scope_name +
'/BiasAdd'][0]
126 anode = None
127 else:
128 anode = None
129 return knode, bnode, dnode, anode
130
131
135 # the BiasAdd name is possible be changed into the output name,
136 # if activation is None, and BiasAdd.next is the last op which is Identity
137 anode = None
138 if bnode:
139 if dense_scope_name +
'/BiasAdd' in self.
edges:
140 anode = self.
edges[dense_scope_name +
'/BiasAdd'][0]
142 anode = None
143 else:
144 anode = None
145 return knode, bnode, anode
146
147
149 assert(node.op == 'Conv2D')
152
153 scope_name = TFConverter.get_scope_name(node.name)
154 #knode for kernel, bnode for bias, dnode for dilation, anode for activation
156
157 if dnode is not None:
158 dilation = struct.unpack('i', dnode.attr['value'].tensor.tensor_content[0:4])[0]
159 else:
160 dilation = 1
161
162 if anode is not None:
163 activation = anode.op
164 else:
165 activation = 'None'
166
167 padding = node.attr['padding'].s.decode("utf-8")
168 # conv2d with dilation > 1 generates tens of nodes, not easy to parse them, so use this tricky method.
171 padding = 'SAME'
173
174 ktensor = knode.attr['value'].tensor
175 filter_height = ktensor.tensor_shape.dim[0].size
176 filter_width = ktensor.tensor_shape.dim[1].size
177 in_channels = ktensor.tensor_shape.dim[2].size
178 out_channels = ktensor.tensor_shape.dim[3].size
179 kernel = np.frombuffer(ktensor.tensor_content, dtype=np.float32)
180 kernel = kernel.reshape(filter_height, filter_width, in_channels, out_channels)
181 kernel = np.transpose(kernel, [3, 0, 1, 2])
182
183 has_bias = 1
184 np.array([self.
op2code[node.op], dilation, padding, self.
conv_activations[activation], in_channels, out_channels, filter_height, has_bias], dtype=np.uint32).tofile(f)
185 kernel.tofile(f)
186
187 btensor = bnode.attr['value'].tensor
188 if btensor.tensor_shape.dim[0].size == 1:
189 bias = struct.pack("f", btensor.float_val[0])
190 else:
191 bias = btensor.tensor_content
192 f.write(bias)
193
195 input_operand_index = self.
add_operand(input_name, Operand.IOTYPE_INPUT)
196
197 if anode is not None:
198 output_operand_index = self.
add_operand(anode.name, Operand.IOTYPE_OUTPUT)
199 else:
200 output_operand_index = self.
add_operand(self.
edges[bnode.name][0].name, Operand.IOTYPE_OUTPUT)
201 np.array([input_operand_index, output_operand_index], dtype=np.uint32).tofile(f)
202
204 assert(node.op == 'MatMul')
207
208 scope_name = TFConverter.get_scope_name(node.name)
209 #knode for kernel, bnode for bias, anode for activation
211
212 if bnode is not None:
213 has_bias = 1
214 btensor = bnode.attr['value'].tensor
215 if btensor.tensor_shape.dim[0].size == 1:
216 bias = struct.pack("f", btensor.float_val[0])
217 else:
218 bias = btensor.tensor_content
219 else:
220 has_bias = 0
221
222 if anode is not None:
223 activation = anode.op
224 else:
225 activation = 'None'
226
227 ktensor = knode.attr['value'].tensor
228 in_channels = ktensor.tensor_shape.dim[0].size
229 out_channels = ktensor.tensor_shape.dim[1].size
230 if in_channels * out_channels == 1:
231 kernel = np.float32(ktensor.float_val[0])
232 else:
233 kernel = np.frombuffer(ktensor.tensor_content, dtype=np.float32)
234 kernel = kernel.reshape(in_channels, out_channels)
235 kernel = np.transpose(kernel, [1, 0])
236
237 np.array([self.
op2code[node.op], self.
conv_activations[activation], in_channels, out_channels, has_bias], dtype=np.uint32).tofile(f)
238 kernel.tofile(f)
239 if has_bias:
240 f.write(bias)
241
243 input_operand_index = self.
add_operand(input_name, Operand.IOTYPE_INPUT)
244
245 if anode is not None:
246 output_operand_index = self.
add_operand(anode.name, Operand.IOTYPE_OUTPUT)
247 else:
248 if bnode is not None:
249 output_operand_index = self.
add_operand(self.
edges[bnode.name][0].name, Operand.IOTYPE_OUTPUT)
250 else:
251 output_operand_index = self.
add_operand(self.
edges[scope_name+
'/concat_1'][0].name, Operand.IOTYPE_OUTPUT)
252 np.array([input_operand_index, output_operand_index], dtype=np.uint32).tofile(f)
253
254
256 assert(node.op == 'Conv2D')
259
262 if node0.op == 'Const':
263 knode = node0
264 input_name = node.input[1]
265 else:
266 knode = node1
267 input_name = node.input[0]
268
269 ktensor = knode.attr['value'].tensor
270 filter_height = ktensor.tensor_shape.dim[0].size
271 filter_width = ktensor.tensor_shape.dim[1].size
272 in_channels = ktensor.tensor_shape.dim[2].size
273 out_channels = ktensor.tensor_shape.dim[3].size
274 if filter_height * filter_width * in_channels * out_channels == 1:
275 kernel = np.float32(ktensor.float_val[0])
276 else:
277 kernel = np.frombuffer(ktensor.tensor_content, dtype=np.float32)
278 kernel = kernel.reshape(filter_height, filter_width, in_channels, out_channels)
279 kernel = np.transpose(kernel, [3, 0, 1, 2])
280
281 has_bias = 0
282 dilation = 1
283 padding = node.attr['padding'].s.decode("utf-8")
285 in_channels, out_channels, filter_height, has_bias], dtype=np.uint32).tofile(f)
286 kernel.tofile(f)
287
288 input_operand_index = self.
add_operand(input_name, Operand.IOTYPE_INPUT)
289 output_operand_index = self.
add_operand(node.name, Operand.IOTYPE_OUTPUT)
290 np.array([input_operand_index, output_operand_index], dtype=np.uint32).tofile(f)
291
292
294 assert(node.op == 'DepthToSpace')
296 block_size = node.attr['block_size'].i
297 np.array([self.
op2code[node.op], block_size], dtype=np.uint32).tofile(f)
299 input_operand_index = self.
add_operand(node.input[0], Operand.IOTYPE_INPUT)
300 output_operand_index = self.
add_operand(node.name, Operand.IOTYPE_OUTPUT)
301 np.array([input_operand_index, output_operand_index], dtype=np.uint32).tofile(f)
302
303
305 assert(node.op == 'MirrorPad')
307 mode = node.attr['mode'].s
309 np.array([self.
op2code[node.op], mode], dtype=np.uint32).tofile(f)
312 paddings = pnode.attr['value'].tensor.tensor_content
313 f.write(paddings)
315 input_operand_index = self.
add_operand(node.input[0], Operand.IOTYPE_INPUT)
316 output_operand_index = self.
add_operand(node.name, Operand.IOTYPE_OUTPUT)
317 np.array([input_operand_index, output_operand_index], dtype=np.uint32).tofile(f)
318
319
321 assert(node.op == 'Maximum')
324 y = ynode.attr['value'].tensor.float_val[0]
325 np.array([self.
op2code[node.op]], dtype=np.uint32).tofile(f)
326 np.array([y], dtype=np.float32).tofile(f)
328 input_operand_index = self.
add_operand(node.input[0], Operand.IOTYPE_INPUT)
329 output_operand_index = self.
add_operand(node.name, Operand.IOTYPE_OUTPUT)
330 np.array([input_operand_index, output_operand_index], dtype=np.uint32).tofile(f)
331
332
338 np.array([self.
op2code[
'MathBinary'], self.
mathbin2code[node.op]], dtype=np.uint32).tofile(f)
339 if i0_node.op == 'Const':
340 scalar = i0_node.attr['value'].tensor.float_val[0]
341 np.array([1], dtype=np.uint32).tofile(f) # broadcast: 1
342 np.array([scalar], dtype=np.float32).tofile(f)
343 np.array([0], dtype=np.uint32).tofile(f) # broadcast: 0
344 input_operand_index = self.
add_operand(i1_node.name, Operand.IOTYPE_INPUT)
345 np.array([input_operand_index], dtype=np.uint32).tofile(f)
346 elif i1_node.op == 'Const':
347 scalar = i1_node.attr['value'].tensor.float_val[0]
348 np.array([0], dtype=np.uint32).tofile(f)
349 input_operand_index = self.
add_operand(i0_node.name, Operand.IOTYPE_INPUT)
350 np.array([input_operand_index], dtype=np.uint32).tofile(f)
351 np.array([1], dtype=np.uint32).tofile(f)
352 np.array([scalar], dtype=np.float32).tofile(f)
353 else:
354 np.array([0], dtype=np.uint32).tofile(f)
355 input_operand_index = self.
add_operand(i0_node.name, Operand.IOTYPE_INPUT)
356 np.array([input_operand_index], dtype=np.uint32).tofile(f)
357 np.array([0], dtype=np.uint32).tofile(f)
358 input_operand_index = self.
add_operand(i1_node.name, Operand.IOTYPE_INPUT)
359 np.array([input_operand_index], dtype=np.uint32).tofile(f)
360 output_operand_index = self.
add_operand(node.name, Operand.IOTYPE_OUTPUT)
361 np.array([output_operand_index], dtype=np.uint32).tofile(f)
362
363
368 np.array([self.
op2code[
'MathUnary'], self.
mathun2code[node.op]], dtype=np.uint32).tofile(f)
369 input_operand_index = self.
add_operand(i0_node.name, Operand.IOTYPE_INPUT)
370 np.array([input_operand_index], dtype=np.uint32).tofile(f)
371 output_operand_index = self.
add_operand(node.name, Operand.IOTYPE_OUTPUT)
372 np.array([output_operand_index],dtype=np.uint32).tofile(f)
373
374
376 assert(node.op == 'AvgPool')
380 strides = node.attr['strides']
381
382 # Tensorflow do not support pooling strides in batch dimension and
383 # current native NN do not support pooling strides in channel dimension, added assert() here.
384 assert(strides.list.i[1]==strides.list.i[2])
385 assert(strides.list.i[0]==1)
386 assert(strides.list.i[3]==1)
387 strides = strides.list.i[1]
388 filter_node = node.attr['ksize']
389 input_name = node.input[0]
390
391 # Tensorflow do not support pooling ksize in batch dimension and channel dimension.
392 assert(filter_node.list.i[0]==1)
393 assert(filter_node.list.i[3]==1)
394 filter_height = filter_node.list.i[1]
395 filter_width = filter_node.list.i[2]
396
397 padding = node.attr['padding'].s.decode("utf-8")
399 dtype=np.uint32).tofile(f)
400
401 input_operand_index = self.
add_operand(input_name, Operand.IOTYPE_INPUT)
402 output_operand_index = self.
add_operand(node.name, Operand.IOTYPE_OUTPUT)
403 np.array([input_operand_index, output_operand_index],dtype=np.uint32).tofile(f)
404
405
407 for node
in self.
nodes:
409 continue
410
411 # conv2d with dilation generates very complex nodes, so handle it in special
413 if node.op == 'Conv2D':
415 continue
417 if node.op == 'MatMul':
419 continue
420
421
422 if node.op == 'Conv2D':
424 continue
427 if TFConverter.get_scope_name(input_name)!=TFConverter.get_scope_name(node.name):
428 continue
429 if node.op == 'AvgPool':
431 elif node.op == 'DepthToSpace':
433 elif node.op == 'MirrorPad':
435 elif node.op == 'Maximum':
441
442
445 for operand in operands:
446 #print('{}'.format(operand))
447 np.array([operand.index,
len(operand.name)], dtype=np.uint32).tofile(f)
448 f.write(operand.name.encode('utf-8'))
449 np.array([operand.iotype, operand.dtype], dtype=np.uint32).tofile(f)
450 np.array(operand.dims, dtype=np.uint32).tofile(f)
451
452
454 with open(self.
outfile,
'wb')
as f:
455 f.write(header.str.encode('utf-8'))
456 np.array([header.major, header.minor], dtype=np.uint32).tofile(f)
460
461
463 for node
in self.
nodes:
465
466
468 used_names = []
469 for node
in self.
nodes:
470 for input in node.input:
471 used_names.append(input)
472
473 for node
in self.
nodes:
474 if node.name not in used_names:
476
477
480 id_nodes = []
481 id_dict = {}
482 for node
in self.
nodes:
483 if node.op == 'Identity':
484 name = node.name
485 input = node.input[0]
486 id_nodes.append(node)
487 # do not change the output name
493 else:
494 id_dict[name] = input
495
496 for idnode in id_nodes:
497 self.
nodes.remove(idnode)
498
499 for node
in self.
nodes:
500 for i
in range(
len(node.input)):
501 input = node.input[i]
502 if input in id_dict:
503 node.input[i] = id_dict[input]
504
505
507 for node
in self.
nodes:
508 for input in node.input:
509 if input
in self.
edges:
511 else:
512 self.
edges[input] = [node]
513
514
515 @staticmethod
517 index = name.rfind('/')
518 if index == -1:
519 return ""
520 return name[0:index]
521
522
524 inner_scope = TFConverter.get_scope_name(name)
525 if inner_scope == "":
526 return False;
528 index = inner_scope.find(scope)
529 if index == 0:
530 return True
531 return False
532
533
535 inner_scope = TFConverter.get_scope_name(name)
536 if inner_scope == "":
537 return False;
539 index = inner_scope.find(scope)
540 if index == 0:
541 return True
542 return False
543
545 # mostly, conv2d/dense is a sub block in graph, get the scope name
546 for node
in self.
nodes:
547 if node.op == 'Conv2D':
548 scope = TFConverter.get_scope_name(node.name)
549 # for the case tf.nn.conv2d is called directly
550 if scope == '':
551 continue
552 # for the case tf.nn.conv2d is called within a scope
554 continue
556 elif node.op == 'MatMul':
557 scope = TFConverter.get_scope_name(node.name)
558 # for the case tf.nn.dense is called directly
559 if scope == '':
560 continue
561 # for the case tf.nn.dense is called within a scope
563 continue
565
566 # get the input name to the conv2d/dense sub block
567 for node
in self.
nodes:
568 scope = TFConverter.get_scope_name(node.name)
570 if node.op == 'Conv2D' or node.op == 'Shape':
571 for inp in node.input:
572 if TFConverter.get_scope_name(inp) != scope:
575 if node.op == 'MatMul' or node.op == 'Shape':
576 for inp in node.input:
577 if TFConverter.get_scope_name(inp) != scope:
580 if node.op == 'Transpose':
581 for inp in node.input:
582 if TFConverter.get_scope_name(inp).find(scope)<0 and TFConverter.get_scope_name(inp).find(scope.split('/')[0])<0:
584
585
592
595
597
598
600 with open(infile, 'rb') as f:
601 # read the file in .proto format
602 graph_def = tf.GraphDef()
603 graph_def.ParseFromString(f.read())
604 nodes = graph_def.node
605
606 converter =
TFConverter(graph_def, nodes, outfile, dump4tb)
607 converter.run()