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 space_to_batch_nd.""" 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_space_to_batch_nd_tests(options): 29 """Make a set of tests to do space_to_batch_nd.""" 30 31 # TODO(nupurgarg): Add test for uint8. 32 test_parameters = [ 33 { 34 "dtype": [tf.int32, tf.int64, tf.float32], 35 "input_shape": [[1, 2, 2, 3], [2, 2, 4, 1]], 36 "block_shape": [[1, 3], [2, 2]], 37 "paddings": [[[0, 0], [0, 0]], [[0, 0], [2, 0]], [[1, 1], [1, 1]]], 38 "constant_block_shape": [True, False], 39 "constant_paddings": [True, False], 40 }, 41 { 42 "dtype": [tf.float32], 43 "input_shape": [[2, 3, 7, 3]], 44 "block_shape": [[1, 3], [2, 2]], 45 "paddings": [[[0, 0], [2, 0]], [[1, 0], [1, 0]]], 46 "constant_block_shape": [True, False], 47 "constant_paddings": [True, False], 48 }, 49 # Non-4D use case: 1 bath dimension, 3 spatial dimensions, 2 others. 50 { 51 "dtype": [tf.float32], 52 "input_shape": [[1, 4, 4, 4, 1, 1]], 53 "block_shape": [[2, 2, 2]], 54 "paddings": [[[0, 0], [0, 0], [0, 0]]], 55 "constant_block_shape": [True, False], 56 "constant_paddings": [True, False], 57 }, 58 # 3D case. 59 { 60 "dtype": [tf.float32], 61 "input_shape": [[1, 4, 4]], 62 "block_shape": [[2]], 63 "paddings": [[[0, 0]]], 64 "constant_block_shape": [True, False], 65 "constant_paddings": [True, False], 66 }, 67 ] 68 69 def build_graph(parameters): 70 """Build a space_to_batch graph given `parameters`.""" 71 input_tensor = tf.compat.v1.placeholder( 72 dtype=parameters["dtype"], 73 name="input", 74 shape=parameters["input_shape"]) 75 input_tensors = [input_tensor] 76 77 # Get block_shape either as a const or as a placeholder (tensor). 78 if parameters["constant_block_shape"]: 79 block_shape = parameters["block_shape"] 80 else: 81 shape = [len(parameters["block_shape"])] 82 block_shape = tf.compat.v1.placeholder( 83 dtype=tf.int32, name="shape", shape=shape) 84 input_tensors.append(block_shape) 85 86 # Get paddings either as a const or as a placeholder (tensor). 87 if parameters["constant_paddings"]: 88 paddings = parameters["paddings"] 89 else: 90 shape = [len(parameters["paddings"]), 2] 91 paddings = tf.compat.v1.placeholder( 92 dtype=tf.int32, name="paddings", shape=shape) 93 input_tensors.append(paddings) 94 95 out = tf.space_to_batch_nd(input_tensor, block_shape, paddings) 96 return input_tensors, [out] 97 98 def build_inputs(parameters, sess, inputs, outputs): 99 values = [ 100 create_tensor_data(parameters["dtype"], parameters["input_shape"]) 101 ] 102 if not parameters["constant_block_shape"]: 103 values.append(np.array(parameters["block_shape"])) 104 if not parameters["constant_paddings"]: 105 values.append(np.array(parameters["paddings"])) 106 return values, sess.run(outputs, feed_dict=dict(zip(inputs, values))) 107 108 if options.use_experimental_converter: 109 # Remove unsupported dimension cases. Currently, kernel supports 3 and 4-D 110 # inputs. 111 test_parameters = [ 112 test_parameters[0], test_parameters[1], test_parameters[3] 113 ] 114 115 make_zip_of_tests( 116 options, 117 test_parameters, 118 build_graph, 119 build_inputs, 120 expected_tf_failures=56) 121