1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Test utilities for tf.signal.""" 16 17from tensorflow.core.protobuf import config_pb2 18from tensorflow.lite.python import interpreter 19from tensorflow.lite.python import lite 20from tensorflow.python.eager import def_function 21from tensorflow.python.grappler import tf_optimizer 22from tensorflow.python.training import saver 23 24 25def grappler_optimize(graph, fetches=None, config_proto=None): 26 """Tries to optimize the provided graph using grappler. 27 28 Args: 29 graph: A `tf.Graph` instance containing the graph to optimize. 30 fetches: An optional list of `Tensor`s to fetch (i.e. not optimize away). 31 Grappler uses the 'train_op' collection to look for fetches, so if not 32 provided this collection should be non-empty. 33 config_proto: An optional `tf.compat.v1.ConfigProto` to use when rewriting 34 the graph. 35 36 Returns: 37 A `tf.compat.v1.GraphDef` containing the rewritten graph. 38 """ 39 if config_proto is None: 40 config_proto = config_pb2.ConfigProto() 41 config_proto.graph_options.rewrite_options.min_graph_nodes = -1 42 if fetches is not None: 43 for fetch in fetches: 44 graph.add_to_collection('train_op', fetch) 45 metagraph = saver.export_meta_graph(graph_def=graph.as_graph_def()) 46 return tf_optimizer.OptimizeGraph(config_proto, metagraph) 47 48 49def tflite_convert(fn, input_templates): 50 """Converts the provided fn to tf.lite model. 51 52 Args: 53 fn: A callable that expects a list of inputs like input_templates that 54 returns a tensor or structure of tensors. 55 input_templates: A list of Tensors, ndarrays or TensorSpecs describing the 56 inputs that fn expects. The actual values of the Tensors or ndarrays are 57 unused. 58 59 Returns: 60 The serialized tf.lite model. 61 """ 62 fn = def_function.function(fn) 63 concrete_func = fn.get_concrete_function(*input_templates) 64 converter = lite.TFLiteConverterV2([concrete_func]) 65 return converter.convert() 66 67 68def evaluate_tflite_model(tflite_model, input_ndarrays): 69 """Evaluates the provided tf.lite model with the given input ndarrays. 70 71 Args: 72 tflite_model: bytes. The serialized tf.lite model. 73 input_ndarrays: A list of NumPy arrays to feed as input to the model. 74 75 Returns: 76 A list of ndarrays produced by the model. 77 78 Raises: 79 ValueError: If the number of input arrays does not match the number of 80 inputs the model expects. 81 """ 82 the_interpreter = interpreter.Interpreter(model_content=tflite_model) 83 the_interpreter.allocate_tensors() 84 85 input_details = the_interpreter.get_input_details() 86 output_details = the_interpreter.get_output_details() 87 88 if len(input_details) != len(input_ndarrays): 89 raise ValueError('Wrong number of inputs: provided=%s, ' 90 'input_details=%s output_details=%s' % ( 91 input_ndarrays, input_details, output_details)) 92 for input_tensor, data in zip(input_details, input_ndarrays): 93 the_interpreter.set_tensor(input_tensor['index'], data) 94 the_interpreter.invoke() 95 return [the_interpreter.get_tensor(details['index']) 96 for details in output_details] 97