20 import tensorflow
as tf
23 import convert_header
as header
25 __all__ = [
'convert_from_tensorflow']
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'}
47 if iotype == Operand.IOTYPE_INPUT:
51 return "{}: (name: {}, iotype: {}, dtype: {}, dims: {}, used_count: {})".
format(self.
index,
56 return self.
index < other.index
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,
90 dtype = node.attr[
'dtype'].type
92 dtype = node.attr[
'T'].type
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)
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')
123 if conv2d_scope_name +
'/BiasAdd' in self.
edges:
124 anode = self.
edges[conv2d_scope_name +
'/BiasAdd'][0]
129 return knode, bnode, dnode, anode
139 if dense_scope_name +
'/BiasAdd' in self.
edges:
140 anode = self.
edges[dense_scope_name +
'/BiasAdd'][0]
145 return knode, bnode, anode
149 assert(node.op ==
'Conv2D')
153 scope_name = TFConverter.get_scope_name(node.name)
157 if dnode
is not None:
158 dilation = struct.unpack(
'i', dnode.attr[
'value'].tensor.tensor_content[0:4])[0]
162 if anode
is not None:
163 activation = anode.op
167 padding = node.attr[
'padding'].s.decode(
"utf-8")
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])
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)
187 btensor = bnode.attr[
'value'].tensor
188 if btensor.tensor_shape.dim[0].size == 1:
189 bias = struct.pack(
"f", btensor.float_val[0])
191 bias = btensor.tensor_content
195 input_operand_index = self.
add_operand(input_name, Operand.IOTYPE_INPUT)
197 if anode
is not None:
198 output_operand_index = self.
add_operand(anode.name, Operand.IOTYPE_OUTPUT)
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)
204 assert(node.op ==
'MatMul')
208 scope_name = TFConverter.get_scope_name(node.name)
212 if bnode
is not None:
214 btensor = bnode.attr[
'value'].tensor
215 if btensor.tensor_shape.dim[0].size == 1:
216 bias = struct.pack(
"f", btensor.float_val[0])
218 bias = btensor.tensor_content
222 if anode
is not None:
223 activation = anode.op
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])
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])
237 np.array([self.
op2code[node.op], self.
conv_activations[activation], in_channels, out_channels, has_bias], dtype=np.uint32).tofile(f)
243 input_operand_index = self.
add_operand(input_name, Operand.IOTYPE_INPUT)
245 if anode
is not None:
246 output_operand_index = self.
add_operand(anode.name, Operand.IOTYPE_OUTPUT)
248 if bnode
is not None:
249 output_operand_index = self.
add_operand(self.
edges[bnode.name][0].name, Operand.IOTYPE_OUTPUT)
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)
256 assert(node.op ==
'Conv2D')
262 if node0.op ==
'Const':
264 input_name = node.input[1]
267 input_name = node.input[0]
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])
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])
283 padding = node.attr[
'padding'].s.decode(
"utf-8")
285 in_channels, out_channels, filter_height, has_bias], dtype=np.uint32).tofile(f)
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)
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)
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
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)
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)
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)
342 np.array([scalar], dtype=np.float32).tofile(f)
343 np.array([0], dtype=np.uint32).tofile(f)
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)
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)
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)
376 assert(node.op ==
'AvgPool')
380 strides = node.attr[
'strides']
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]
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]
397 padding = node.attr[
'padding'].s.decode(
"utf-8")
399 dtype=np.uint32).tofile(f)
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)
407 for node
in self.
nodes:
413 if node.op ==
'Conv2D':
417 if node.op ==
'MatMul':
422 if node.op ==
'Conv2D':
427 if TFConverter.get_scope_name(input_name)!=TFConverter.get_scope_name(node.name):
429 if node.op ==
'AvgPool':
431 elif node.op ==
'DepthToSpace':
433 elif node.op ==
'MirrorPad':
435 elif node.op ==
'Maximum':
445 for operand
in operands:
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)
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)
463 for node
in self.
nodes:
469 for node
in self.
nodes:
470 for input
in node.input:
471 used_names.append(input)
473 for node
in self.
nodes:
474 if node.name
not in used_names:
482 for node
in self.
nodes:
483 if node.op ==
'Identity':
485 input = node.input[0]
486 id_nodes.append(node)
494 id_dict[name] = input
496 for idnode
in id_nodes:
497 self.
nodes.remove(idnode)
499 for node
in self.
nodes:
500 for i
in range(
len(node.input)):
501 input = node.input[i]
503 node.input[i] = id_dict[input]
507 for node
in self.
nodes:
508 for input
in node.input:
509 if input
in self.
edges:
512 self.
edges[input] = [node]
517 index = name.rfind(
'/')
524 inner_scope = TFConverter.get_scope_name(name)
525 if inner_scope ==
"":
528 index = inner_scope.find(scope)
535 inner_scope = TFConverter.get_scope_name(name)
536 if inner_scope ==
"":
539 index = inner_scope.find(scope)
546 for node
in self.
nodes:
547 if node.op ==
'Conv2D':
548 scope = TFConverter.get_scope_name(node.name)
556 elif node.op ==
'MatMul':
557 scope = TFConverter.get_scope_name(node.name)
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:
600 with open(infile,
'rb')
as f:
602 graph_def = tf.GraphDef()
603 graph_def.ParseFromString(f.read())
604 nodes = graph_def.node
606 converter =
TFConverter(graph_def, nodes, outfile, dump4tb)