• 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 conv with activations."""
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
27def make_conv_activation_tests(activation_op):
28  """Make a set of tests to do convolution with activation."""
29
30  def f(options):
31    """Actual function that generates examples."""
32    test_parameters = [
33        {
34            "input_shape": [[1, 3, 4, 3], [4, 6, 6, 1]],
35            "filter_shape": [[1, 1], [2, 3], [3, 3]],
36            "strides": [[1, 1, 1, 1], [1, 2, 3, 1]],
37            "dilations": [[1, 1, 1, 1], [1, 3, 2, 1], [1, 2, 2, 1]],
38            "padding": ["SAME", "VALID"],
39            "data_format": ["NHWC"],  # TODO(aselle): NCHW  would be good
40            "constant_filter": [True, False],
41            "channel_multiplier": [1, 2],
42            "fully_quantize": [False],
43            "quant_16x8": [False],
44            "dynamic_range_quantize": [False],
45        },
46        # TODO(b/134702301): The fully_quantize param is just ignored by the
47        # MLIR testing path now, resulting in duplicate tests. Either ignore
48        # these tests or handle it properly in the mlir_convert() function.
49        {
50            "input_shape": [[1, 3, 4, 3], [4, 6, 6, 1]],
51            "filter_shape": [[1, 1], [2, 3]],
52            "strides": [[1, 1, 1, 1], [1, 2, 3, 1]],
53            "dilations": [[1, 1, 1, 1], [1, 3, 2, 1]],
54            "padding": ["SAME", "VALID"],
55            "data_format": ["NHWC"],  # TODO(aselle): NCHW  would be good
56            "constant_filter": [True],
57            "channel_multiplier": [1, 2],
58            "fully_quantize": [True],
59            "quant_16x8": [False, True],
60            "dynamic_range_quantize": [False],
61        },
62        {
63            "input_shape": [[1, 3, 4, 3]],
64            "filter_shape": [[1, 1], [2, 3], [3, 3]],
65            "strides": [[1, 1, 1, 1], [1, 2, 3, 1]],
66            "dilations": [[1, 1, 1, 1]],
67            "padding": ["SAME", "VALID"],
68            "data_format": ["NHWC"],
69            "constant_filter": [True],
70            "channel_multiplier": [1, 2],
71            "fully_quantize": [False],
72            "quant_16x8": [False],
73            "dynamic_range_quantize": [True],
74        },
75    ]
76
77    def get_tensor_shapes(parameters):
78      input_shape = parameters["input_shape"]
79      filter_size = parameters["filter_shape"]
80      filter_shape = filter_size + [
81          input_shape[3], parameters["channel_multiplier"]
82      ]
83      return [input_shape, filter_shape]
84
85    def build_graph(parameters):
86      """Build a conv graph given `parameters`."""
87      input_shape, filter_shape = get_tensor_shapes(parameters)
88      input_tensor = tf.compat.v1.placeholder(
89          dtype=tf.float32, name="input", shape=input_shape)
90
91      # Get filter input either as a placeholder or constants. Also get a list
92      # of the input tensors that are represented as placeholders.
93      if parameters["constant_filter"]:
94        filter_input = create_tensor_data(
95            np.float32, filter_shape, min_value=-10, max_value=10)
96        input_tensors = [input_tensor]
97      else:
98        filter_input = tf.compat.v1.placeholder(
99            dtype=tf.float32, name="filter", shape=filter_shape)
100        input_tensors = [input_tensor, filter_input]
101
102      out = tf.nn.conv2d(
103          input_tensor,
104          filter_input,
105          strides=parameters["strides"],
106          dilations=parameters["dilations"],
107          padding=parameters["padding"],
108          data_format=parameters["data_format"])
109      out = activation_op(out)
110      return input_tensors, [out]
111
112    def build_inputs(parameters, sess, inputs, outputs):
113      """Build inputs for conv with activation."""
114
115      input_shape, filter_shape = get_tensor_shapes(parameters)
116      values = [
117          create_tensor_data(
118              np.float32, input_shape, min_value=-1, max_value=1)
119      ]
120      if not parameters["constant_filter"]:
121        values.append(create_tensor_data(np.float32, filter_shape))
122      return values, sess.run(outputs, feed_dict=dict(zip(inputs, values)))
123
124    make_zip_of_tests(
125        options,
126        test_parameters,
127        build_graph,
128        build_inputs,
129        expected_tf_failures=48)
130
131  return f
132
133
134@register_make_test_function()
135def make_conv_relu6_tests(options):
136  """Make a set of tests to do conv_relu6."""
137  return make_conv_activation_tests(tf.nn.relu6)(options)
138
139
140@register_make_test_function()
141def make_conv_relu_tests(options):
142  """Make a set of tests to do conv_relu."""
143  return make_conv_activation_tests(tf.nn.relu)(options)
144
145
146def relu1(input_tensor):
147  # Note that the following is not supported:
148  #   out = tf.maximum(-1.0, tf.minimum(input_tensor, 1.0))
149  out = tf.minimum(1.0, tf.maximum(input_tensor, -1.0))
150  return out
151
152
153@register_make_test_function()
154def make_conv_relu1_tests(options):
155  """Make a set of tests to do conv_relu1."""
156  return make_conv_activation_tests(relu1)(options)
157