• 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 pool operators."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import tensorflow as tf
21from tensorflow.lite.testing.zip_test_utils import create_tensor_data
22from tensorflow.lite.testing.zip_test_utils import make_zip_of_tests
23from tensorflow.lite.testing.zip_test_utils import register_make_test_function
24
25
26def make_pool_tests(pool_op_in, allow_fully_quantize=False):
27  """Make a set of tests to do average pooling.
28
29  Args:
30    pool_op_in: TensorFlow pooling operation to test  i.e. `tf.nn.avg_pool2d`.
31    allow_fully_quantize: bool, whether fully_quantize is allowed.
32
33  Returns:
34    A function representing the true generator (after curried pool_op_in).
35  """
36
37  pool_op = pool_op_in
38
39  def f(options, expected_tf_failures=0):
40    """Actual function that generates examples.
41
42    Args:
43      options: An Options instance.
44      expected_tf_failures: number of expected tensorflow failures.
45    """
46
47    # Chose a set of parameters
48    test_parameters = [
49        {
50            "ksize": [[2, 1, 1, 2], [1, 1, 1, 1], [1, 1, 2, 1], [1, 10, 11, 1]],
51            "strides": [[2, 1, 1, 2], [1, 1, 1, 1], [1, 1, 2, 1],
52                        [1, 10, 11, 1]],
53            # TODO(aselle): should add a degenerate shape (e.g. [1, 0, 1, 1]).
54            "input_shape": [[], [1, 1, 1, 1], [1, 15, 14, 1], [3, 15, 14, 3]],
55            "padding": ["SAME", "VALID"],
56            "data_format": ["NHWC"],  # TODO(aselle): NCHW  would be good
57            "fully_quantize": [False],
58        },
59        {
60            "ksize": [[2, 1, 1, 2], [1, 1, 1, 1], [1, 1, 2, 1], [1, 10, 11, 1]],
61            "strides": [[2, 1, 1, 2], [1, 1, 1, 1], [1, 1, 2, 1],
62                        [1, 10, 11, 1]],
63            # TODO(aselle): should add a degenerate shape (e.g. [1, 0, 1, 1]).
64            "input_shape": [[], [1, 1, 1, 1], [1, 15, 14, 1], [3, 15, 14, 3]],
65            "padding": ["SAME", "VALID"],
66            "data_format": ["NHWC"],  # TODO(aselle): NCHW  would be good
67            "fully_quantize": [True],
68        }
69    ]
70    # test_parameters include fully_quantize option only when
71    # allow_fully_quantize is True.
72    if not allow_fully_quantize:
73      test_parameters = [
74          test_parameter for test_parameter in test_parameters
75          if True not in test_parameter["fully_quantize"]
76      ]
77
78    def build_graph(parameters):
79      input_tensor = tf.compat.v1.placeholder(
80          dtype=tf.float32, name="input", shape=parameters["input_shape"])
81      out = pool_op(
82          input_tensor,
83          ksize=parameters["ksize"],
84          strides=parameters["strides"],
85          data_format=parameters["data_format"],
86          padding=parameters["padding"])
87      return [input_tensor], [out]
88
89    def build_inputs(parameters, sess, inputs, outputs):
90      if allow_fully_quantize:
91        input_values = create_tensor_data(
92            tf.float32, parameters["input_shape"], min_value=-1, max_value=1)
93      else:
94        input_values = create_tensor_data(tf.float32, parameters["input_shape"])
95      return [input_values], sess.run(
96          outputs, feed_dict=dict(zip(inputs, [input_values])))
97
98    make_zip_of_tests(
99        options,
100        test_parameters,
101        build_graph,
102        build_inputs,
103        expected_tf_failures=expected_tf_failures)
104
105  return f
106
107
108def make_l2_pool(input_tensor, ksize, strides, padding, data_format):
109  """Given an input perform a sequence of TensorFlow ops to produce l2pool."""
110  return tf.sqrt(
111      tf.nn.avg_pool(
112          tf.square(input_tensor),
113          ksize=ksize,
114          strides=strides,
115          padding=padding,
116          data_format=data_format))
117
118
119@register_make_test_function()
120def make_l2_pool_tests(options):
121  make_pool_tests(make_l2_pool)(options, expected_tf_failures=80)
122
123
124@register_make_test_function()
125def make_avg_pool_tests(options):
126  make_pool_tests(
127      tf.nn.avg_pool, allow_fully_quantize=True)(
128          options, expected_tf_failures=160)
129
130
131@register_make_test_function()
132def make_max_pool_tests(options):
133  make_pool_tests(
134      tf.nn.max_pool, allow_fully_quantize=True)(
135          options, expected_tf_failures=160)
136