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