|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +""" |
| 5 | +tensor |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import division |
| 9 | +from __future__ import print_function |
| 10 | +from __future__ import unicode_literals |
| 11 | + |
| 12 | +import logging |
| 13 | +import sys |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +from onnx import onnx_pb |
| 17 | +from onnx.onnx_pb import TensorProto |
| 18 | + |
| 19 | +from tf2onnx import constants, utils |
| 20 | +from tf2onnx.graph_builder import GraphBuilder |
| 21 | +from tf2onnx.handler import tf_op |
| 22 | +from tf2onnx.onnx_opset import nn, math |
| 23 | + |
| 24 | +logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +# pylint: disable=unused-argument,missing-docstring,unused-variable,pointless-string-statement,invalid-name |
| 28 | + |
| 29 | + |
| 30 | +@tf_op("FakeQuantWithMinMaxVars") |
| 31 | +class FakeQuantWithMinMaxVars: |
| 32 | + @classmethod |
| 33 | + def version_11(cls, ctx, node, **kwargs): |
| 34 | + # hack to make up for the missing onnx pack op |
| 35 | + import pprint |
| 36 | + pprint.pprint(node) |
| 37 | + amin = node.get_attr("min").i |
| 38 | + if axis < 0: |
| 39 | + axis += len(ctx.get_shape(node.input[0])) + 1 |
| 40 | + |
| 41 | + inputs = [] |
| 42 | + dtype = None |
| 43 | + # insert Unsqueeze on each input |
| 44 | + for i, n in enumerate(node.inputs): |
| 45 | + dtype = ctx.get_dtype(node.input[i]) |
| 46 | + shape = ctx.get_shape(node.input[i]) |
| 47 | + new_node = ctx.make_node("Unsqueeze", [node.input[i]], op_name_scope=node.name, attr={"axes": [axis]}, |
| 48 | + shapes=[shape], dtypes=[dtype]) |
| 49 | + output_name = new_node.output[0] |
| 50 | + node.input[i] = output_name |
| 51 | + inputs.append(output_name) |
| 52 | + |
| 53 | + shapes = node.output_shapes |
| 54 | + dtypes = node.output_dtypes |
| 55 | + ctx.remove_node(node.name) |
| 56 | + # concat all unqueezes |
| 57 | + concat = ctx.make_node("Concat", inputs, op_name_scope=node.name, attr={"axis": axis}, |
| 58 | + shapes=shapes, dtypes=dtypes) |
| 59 | + ctx.replace_all_inputs(ctx.get_nodes(), node.output[0], concat.output[0]) |
| 60 | + |
| 61 | + |
0 commit comments