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.""" 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_conv_to_depthwiseconv_with_shared_weights_tests(options): 29 """Make a test where 2 Conv ops shared the same constant weight tensor.""" 30 31 test_parameters = [{ 32 "input_shape": [[1, 10, 10, 1]], 33 "filter_shape": [[3, 3]], 34 "strides": [[1, 1, 1, 1]], 35 "dilations": [[1, 1, 1, 1]], 36 "padding": ["SAME"], 37 "data_format": ["NHWC"], 38 "channel_multiplier": [3], 39 }] 40 41 def get_tensor_shapes(parameters): 42 input_shape = parameters["input_shape"] 43 filter_size = parameters["filter_shape"] 44 filter_shape = filter_size + [ 45 input_shape[3], parameters["channel_multiplier"] 46 ] 47 return [input_shape, filter_shape] 48 49 def build_graph(parameters): 50 """Build a conv graph given `parameters`.""" 51 input_shape, filter_shape = get_tensor_shapes(parameters) 52 input_tensor = tf.compat.v1.placeholder( 53 dtype=tf.float32, name="input", shape=input_shape) 54 55 # Construct a constant weights tensor which will be used by both Conv2D. 56 filter_tensor = tf.constant( 57 create_tensor_data(np.float32, filter_shape), dtype=tf.float32) 58 input_tensors = [input_tensor] 59 60 # Construct 2 Conv2D operations which use exactly the same input and 61 # weights. 62 result1 = tf.nn.conv2d( 63 input_tensor, 64 filter_tensor, 65 strides=parameters["strides"], 66 dilations=parameters["dilations"], 67 padding=parameters["padding"], 68 data_format=parameters["data_format"]) 69 result2 = tf.nn.conv2d( 70 input_tensor, 71 filter_tensor, 72 strides=parameters["strides"], 73 dilations=parameters["dilations"], 74 padding=parameters["padding"], 75 data_format=parameters["data_format"]) 76 # Add the 2 results up. 77 out = result1 + result2 78 return input_tensors, [out] 79 80 def build_inputs(parameters, sess, inputs, outputs): 81 # Build list of input values either containing 1 tensor (input) or 2 tensors 82 # (input, filter) based on whether filter is constant or variable input. 83 input_shape, unused_filter_shape = get_tensor_shapes(parameters) 84 values = [create_tensor_data(np.float32, input_shape)] 85 return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) 86 87 make_zip_of_tests(options, test_parameters, build_graph, build_inputs) 88