• 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 mirror_pad."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import numpy as np
21import tensorflow.compat.v1 as tf
22from tensorflow.lite.testing.zip_test_utils import create_tensor_data
23from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests
24from tensorflow.lite.testing.zip_test_utils import register_make_test_function
25
26
27@register_make_test_function()
28def make_mirror_pad_tests(options):
29  """Make a set of tests to do mirror_pad."""
30
31  test_parameters = [
32      {
33          "input_shape": [[2, 3]],
34          "padding_matrix": [[[1, 1], [2, 1]]],
35          "mode": ["REFLECT"],
36          "type": ["const"],
37          "fully_quantize": [True, False],
38      },
39      {
40          "input_shape": [[2, 3]],
41          "padding_matrix": [[[1, 1], [1, 1]]],
42          "mode": ["REFLECT"],
43          "type": ["const"],
44          "fully_quantize": [False],
45      },
46      {
47          "input_shape": [[2, 3]],
48          "padding_matrix": [[[1, 1], [2, 1]]],
49          "mode": ["SYMMETRIC"],
50          "type": ["placeholder"],
51          "fully_quantize": [False],
52      },
53      {
54          "input_shape": [[2, 3]],
55          "padding_matrix": [[[1, 1], [2, 1]]],
56          "mode": ["REFLECT"],
57          "type": ["placeholder"],
58          "fully_quantize": [False],
59      },
60      {
61          "input_shape": [[3]],
62          "padding_matrix": [[[0, 2]]],
63          "mode": ["SYMMETRIC"],
64          "type": ["placeholder"],
65          "fully_quantize": [False],
66      },
67      {
68          "input_shape": [[3]],
69          "padding_matrix": [[[0, 2]]],
70          "mode": ["SYMMETRIC"],
71          "type": ["const"],
72          "fully_quantize": [False],
73      },
74      {
75          "input_shape": [[3]],
76          "padding_matrix": [[[0, 2]]],
77          "mode": ["REFLECT"],
78          "type": ["const"],
79          "fully_quantize": [False, True],
80      },
81      {
82          "input_shape": [[3, 2, 4, 5]],
83          "padding_matrix": [[[1, 1], [2, 2], [1, 1], [1, 1]]],
84          "mode": ["SYMMETRIC"],
85          "type": ["placeholder"],
86          "fully_quantize": [False],
87      },
88  ]
89
90  def build_graph(parameters):
91    """Build the graph for the test case."""
92
93    input_tensor = tf.compat.v1.placeholder(
94        dtype=tf.float32, name="input", shape=parameters["input_shape"])
95    if parameters["type"] != "const" and not parameters["fully_quantize"]:
96      padding_matrix = tf.compat.v1.placeholder(
97          dtype=tf.int32,
98          name="padding",
99          shape=[len(parameters["input_shape"]), 2])
100      input_tensors = [input_tensor, padding_matrix]
101    else:
102      padding_matrix = tf.constant(np.array(parameters["padding_matrix"]))
103      input_tensors = [input_tensor]
104    output = tf.pad(
105        input_tensor, paddings=padding_matrix, mode=parameters["mode"])
106
107    return input_tensors, [output]
108
109  def build_inputs(parameters, sess, inputs, outputs):
110    if not parameters["fully_quantize"]:
111      input_values = [create_tensor_data(tf.float32, parameters["input_shape"])]
112    else:
113      input_values = [
114          create_tensor_data(
115              tf.float32, parameters["input_shape"], min_value=-1, max_value=1)
116      ]
117    if parameters["type"] != "const":
118      input_values.append(np.array(parameters["padding_matrix"]))
119    return input_values, sess.run(
120        outputs, feed_dict=dict(zip(inputs, input_values)))
121
122  make_zip_of_tests(options, test_parameters, build_graph, build_inputs)
123