1# Copyright 2017 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"""Tests for Local Response Normalization ops.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21import copy 22 23import numpy as np 24 25from tensorflow.compiler.tests import xla_test 26from tensorflow.python.framework import constant_op 27from tensorflow.python.framework import dtypes 28from tensorflow.python.framework import ops 29from tensorflow.python.ops import array_ops 30from tensorflow.python.ops import gen_nn_ops 31from tensorflow.python.ops import nn 32from tensorflow.python.platform import googletest 33 34CPU_DEVICE = "/job:localhost/replica:0/task:0/cpu:0" 35 36 37# Local response normalization tests. The forward tests are copied from 38# tensorflow/python/kernel_tests/lrn_op_test.py 39class LRNTest(xla_test.XLATestCase): 40 41 def _LRN(self, input_image, lrn_depth_radius=5, bias=1.0, alpha=1.0, 42 beta=0.5): 43 """Compute expected result.""" 44 output = copy.deepcopy(input_image) 45 batch_size = input_image.shape[0] 46 rows = input_image.shape[1] 47 cols = input_image.shape[2] 48 depth = input_image.shape[3] 49 for b in range(batch_size): 50 for r in range(rows): 51 for c in range(cols): 52 for d in range(depth): 53 begin = max(0, d - lrn_depth_radius) 54 end = min(depth, d + lrn_depth_radius + 1) 55 patch = input_image[b, r, c, begin:end] 56 output[b, r, c, d] /= ( 57 np.power(bias + alpha * np.sum(patch * patch), beta)) 58 return output 59 60 def _RunAndVerify(self, dtype): 61 with self.session(): 62 # random shape 63 shape = np.random.randint(1, 16, size=4) 64 # Make depth at least 2 to make it meaningful 65 shape[3] += 1 66 p = array_ops.placeholder(dtype, shape=shape) 67 # random depth_radius, bias, alpha, beta 68 lrn_depth_radius = np.random.randint(1, shape[3]) 69 bias = 1.0 + np.random.rand() 70 alpha = 2.0 * np.random.rand() 71 beta = 2.0 * np.random.rand() 72 with self.test_scope(): 73 lrn_t = nn.local_response_normalization( 74 p, 75 name="lrn", 76 depth_radius=lrn_depth_radius, 77 bias=bias, 78 alpha=alpha, 79 beta=beta) 80 params = {p: np.random.rand(*shape).astype("f")} 81 result = lrn_t.eval(feed_dict=params) 82 expected = self._LRN( 83 params[p], 84 lrn_depth_radius=lrn_depth_radius, 85 bias=bias, 86 alpha=alpha, 87 beta=beta) 88 err = np.amax(np.abs(result - expected)) 89 print("LRN error for bias ", bias, "alpha ", alpha, " beta ", beta, " is ", 90 err) 91 if dtype == dtypes.float32: 92 self.assertTrue(err < 1e-4) 93 else: 94 self.assertTrue(err < 1e-2) 95 self.assertShapeEqual(expected, lrn_t) 96 97 def testCompute(self): 98 for _ in range(2): 99 self._RunAndVerify(dtypes.float32) 100 101 def testLrnGrad(self): 102 # Test for LRNGrad that compares against the CPU implementation. 103 shape = [1, 2, 3, 4] 104 total_size = np.prod(shape) 105 in_image_vals = np.arange(1, total_size + 1, dtype=np.float32) 106 out_image_vals = np.arange(1, total_size + 1, dtype=np.float32) 107 out_grads_vals = np.arange(1, total_size + 1, dtype=np.float32) 108 depth_radius = np.random.randint(1, shape[3]) 109 bias = 1.0 + np.random.rand() 110 alpha = 1.0 * np.random.rand() 111 beta = 1.0 * np.random.rand() 112 113 with self.session(): 114 in_image = constant_op.constant(in_image_vals, shape=shape) 115 out_image = constant_op.constant(out_image_vals, shape=shape) 116 out_grads = constant_op.constant(out_grads_vals, shape=shape) 117 with ops.device(CPU_DEVICE): 118 expected = gen_nn_ops.lrn_grad(out_grads, in_image, out_image, 119 depth_radius, bias, alpha, beta) 120 with self.test_scope(): 121 actual = gen_nn_ops.lrn_grad(out_grads, in_image, out_image, 122 depth_radius, bias, alpha, beta) 123 expected_val = self.evaluate(expected) 124 actual_val = self.evaluate(actual) 125 self.assertAllClose(actual_val, expected_val, rtol=1e-3) 126 127 128if __name__ == "__main__": 129 googletest.main() 130