|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT license. |
| 3 | +""" tf2onnx mapping functions for onnx ml domain. """ |
| 4 | +from tf2onnx import constants |
| 5 | +from tf2onnx.handler import tf_op |
| 6 | + |
| 7 | + |
| 8 | +# pylint: disable=unused-argument,missing-docstring,unnecessary-pass |
| 9 | + |
| 10 | +@tf_op("HashTableV2") |
| 11 | +class HashTable: |
| 12 | + @classmethod |
| 13 | + def version_8(cls, ctx, node, **kwargs): |
| 14 | + """ HashTable will be removed """ |
| 15 | + pass |
| 16 | + |
| 17 | + |
| 18 | +@tf_op("LookupTableFindV2") |
| 19 | +class LookupTableFind: |
| 20 | + @classmethod |
| 21 | + def version_8(cls, ctx, node, **kwargs): |
| 22 | + """ convert lookup to category mapper """ |
| 23 | + table_node = node.inputs[0] |
| 24 | + file_path = table_node.get_attr_value("shared_name")[11:-6] |
| 25 | + cats_int64s = [] |
| 26 | + cats_strings = [] |
| 27 | + with open(file_path, 'r') as f: |
| 28 | + for i, s in enumerate(f.readlines()): |
| 29 | + cats_int64s.append(i) |
| 30 | + cats_strings.append(s.strip()) |
| 31 | + node_name = node.name |
| 32 | + node_inputs = node.input |
| 33 | + node_outputs = node.output |
| 34 | + ctx.remove_node(node.name) |
| 35 | + new_node = ctx.make_node("CategoryMapper", domain=constants.AI_ONNX_ML_DOMAIN, |
| 36 | + name=node_name, inputs=node_inputs[1: 2], outputs=node_outputs, |
| 37 | + attr={'cats_int64s': cats_int64s, 'cats_strings': cats_strings}) |
| 38 | + ctx.set_shape(new_node.name + ":0", [-1]) |
| 39 | + customer_nodes = ctx.find_output_consumers(table_node.output[0]) |
| 40 | + if len(customer_nodes) == 0: |
| 41 | + ctx.remove_node(table_node.name) |
0 commit comments