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 depthwiseconv.""" 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_depthwiseconv_tests(options): 29 """Make a set of tests to do convolution.""" 30 31 # Tensorflow only supports equal strides 32 test_parameters = [ 33 { 34 "input_shape": [[1, 3, 4, 3], [1, 10, 10, 3]], 35 "filter_size": [[1, 1], [1, 2], [3, 3]], 36 "strides": [[1, 1, 1, 1], [1, 3, 3, 1]], 37 "dilations": [[1, 1, 1, 1], [1, 3, 2, 1], [1, 2, 2, 1]], 38 "channel_multiplier": [1, 2], 39 "rate": [[1, 1]], 40 "padding": ["SAME", "VALID"], 41 "data_format": ["NHWC"], 42 "constant_filter": [True, False], 43 "fully_quantize": [False], 44 "quant_16x8": [False] 45 }, 46 { 47 "input_shape": [[1, 3, 4, 3]], 48 "filter_size": [[1, 1]], 49 "strides": [[1, 1, 2, 1]], # TF needs [1, x, x, 1] 50 "dilations": [[1, 1, 1, 1], [1, 2, 2, 1]], 51 "channel_multiplier": [2], 52 "rate": [[2, 2]], # Only [1, 1] is supported 53 "padding": ["SAME"], 54 "data_format": ["NHWC"], 55 "constant_filter": [True, False], 56 "fully_quantize": [False], 57 "quant_16x8": [False] 58 }, 59 { 60 "input_shape": [[1, 3, 4, 3], [1, 10, 10, 3]], 61 "filter_size": [[1, 1], [1, 2], [3, 3]], 62 "strides": [[1, 1, 1, 1], [1, 3, 3, 1]], 63 "dilations": [[1, 1, 1, 1], [1, 3, 2, 1], [1, 2, 2, 1]], 64 "channel_multiplier": [1, 2], 65 "rate": [[1, 1]], 66 "padding": ["SAME", "VALID"], 67 "data_format": ["NHWC"], 68 "constant_filter": [True], 69 "fully_quantize": [True], 70 "quant_16x8": [False] 71 }, 72 { 73 "input_shape": [[1, 3, 4, 3]], 74 "filter_size": [[1, 2]], 75 "strides": [[1, 3, 3, 1]], 76 "dilations": [[1, 3, 2, 1]], 77 "channel_multiplier": [1], 78 "rate": [[1, 1]], 79 "padding": ["SAME", "VALID"], 80 "data_format": ["NHWC"], 81 "constant_filter": [True], 82 "fully_quantize": [True], 83 "quant_16x8": [True] 84 }, 85 ] 86 87 def get_tensor_shapes(parameters): 88 input_shape = parameters["input_shape"] 89 filter_size = parameters["filter_size"] 90 filter_shape = filter_size + [ 91 input_shape[3], parameters["channel_multiplier"] 92 ] 93 return [input_shape, filter_shape] 94 95 def build_graph(parameters): 96 """Build a depthwise conv graph given `parameters`.""" 97 input_shape, filter_shape = get_tensor_shapes(parameters) 98 input_tensor = tf.compat.v1.placeholder( 99 dtype=tf.float32, name="input", shape=input_shape) 100 101 # Get filter input either as a placeholder or constants. Also get a list of 102 # the input tensors that are represented as placeholders. 103 if parameters["constant_filter"]: 104 filter_input = create_tensor_data(np.float32, filter_shape) 105 input_tensors = [input_tensor] 106 else: 107 filter_input = tf.compat.v1.placeholder( 108 dtype=tf.float32, name="filter", shape=filter_shape) 109 input_tensors = [input_tensor, filter_input] 110 111 out = tf.nn.depthwise_conv2d( 112 input_tensor, 113 filter_input, 114 strides=parameters["strides"], 115 rate=parameters["rate"], 116 padding=parameters["padding"], 117 data_format=parameters["data_format"]) 118 return input_tensors, [out] 119 120 def build_inputs(parameters, sess, inputs, outputs): 121 # pylint: disable=g-doc-return-or-yield, g-doc-args 122 """Build list of input values. 123 124 It either contains 1 tensor (input) or 2 tensors (input, filter) based on 125 whether filter is constant or variable input. 126 """ 127 128 input_shape, filter_shape = get_tensor_shapes(parameters) 129 values = [ 130 create_tensor_data(np.float32, input_shape, min_value=-1, max_value=1) 131 ] 132 if not parameters["constant_filter"]: 133 values.append( 134 create_tensor_data( 135 np.float32, filter_shape, min_value=-1, max_value=1)) 136 return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) 137 138 make_zip_of_tests( 139 options, 140 test_parameters, 141 build_graph, 142 build_inputs, 143 expected_tf_failures=4) 144