• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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 configs for identity."""
16import numpy as np
17import tensorflow.compat.v1 as tf
18from tensorflow.lite.testing.zip_test_utils import create_tensor_data
19from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests
20from tensorflow.lite.testing.zip_test_utils import register_make_test_function
21from tensorflow.python.ops import array_ops
22
23
24@register_make_test_function()
25def make_identity_tests(options):
26  """Make a set of tests to do identity."""
27
28  # Chose a set of parameters
29  test_parameters = [{
30      "input_shape": [[], [1], [3, 3]],
31      "op_to_use": [
32          "identity", "identity_n", "snapshot", "identity_n_with_2_inputs"
33      ],
34  }]
35
36  def build_graph(parameters):
37    """Make a set of tests to do identity."""
38
39    input_tensors = []
40    input_count = (2 if parameters["op_to_use"] == "identity_n_with_2_inputs"
41                   else 1)
42    input_tensors = [
43        tf.compat.v1.placeholder(
44            dtype=tf.float32, name="input", shape=parameters["input_shape"])
45        for _ in range(input_count)
46    ]
47
48    # We add the Multiply before Identity just as a walk-around to make the test
49    # pass when input_shape is scalar.
50    # During graph transformation, converter will replace the Identity op with
51    # Reshape when input has shape. However, currently converter can't
52    # distinguish between missing shape and scalar shape. As a result, when
53    # input has scalar shape, this conversion still fails.
54    inputs_doubled = [input_tensor * 2.0 for input_tensor in input_tensors]
55    if parameters["op_to_use"] == "identity":
56      identity_outputs = [tf.identity(inputs_doubled[0])]
57    elif parameters["op_to_use"] == "snapshot":
58      identity_outputs = [array_ops.snapshot(inputs_doubled[0])]
59    elif parameters["op_to_use"] in ("identity_n", "identity_n_with_2_inputs"):
60      identity_outputs = tf.identity_n(inputs_doubled)
61    return input_tensors, identity_outputs
62
63  def build_inputs(parameters, sess, inputs, outputs):
64    input_values = [
65        create_tensor_data(
66            np.float32, parameters["input_shape"], min_value=-4, max_value=10)
67        for _ in range(len(inputs))
68    ]
69
70    return input_values, sess.run(
71        outputs, feed_dict=dict(zip(inputs, input_values)))
72
73  make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
74