1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#include <gtest/gtest.h>
16#include "tensorflow/contrib/lite/interpreter.h"
17#include "tensorflow/contrib/lite/kernels/register.h"
18#include "tensorflow/contrib/lite/kernels/test_util.h"
19#include "tensorflow/contrib/lite/model.h"
20
21namespace tflite {
22namespace {
23
24using ::testing::ElementsAreArray;
25
26class LocalResponseNormOpModel : public SingleOpModel {
27 public:
28  LocalResponseNormOpModel(std::initializer_list<int> input_shape, int radius,
29                           float bias, float alpha, float beta) {
30    input_ = AddInput(TensorType_FLOAT32);
31    output_ = AddOutput(TensorType_FLOAT32);
32    SetBuiltinOp(BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION,
33                 BuiltinOptions_LocalResponseNormalizationOptions,
34                 CreateLocalResponseNormalizationOptions(builder_, radius, bias,
35                                                         alpha, beta)
36                     .Union());
37    BuildInterpreter({input_shape});
38  }
39
40  void SetInput(std::initializer_list<float> data) {
41    PopulateTensor(input_, data);
42  }
43
44  std::vector<float> GetOutput() { return ExtractVector<float>(output_); }
45
46 private:
47  int input_;
48  int output_;
49};
50
51TEST(LocalResponseNormOpTest, SameAsL2Norm) {
52  LocalResponseNormOpModel m({1, 1, 1, 6}, /*radius=*/20, /*bias=*/0.0,
53                             /*alpha=*/1.0, /*beta=*/0.5);
54  m.SetInput({-1.1, 0.6, 0.7, 1.2, -0.7, 0.1});
55  m.Invoke();
56  // The result is every input divided by 2.
57  EXPECT_THAT(
58      m.GetOutput(),
59      ElementsAreArray(ArrayFloatNear({-0.55, 0.3, 0.35, 0.6, -0.35, 0.05})));
60}
61
62TEST(LocalResponseNormOpTest, WithAlpha) {
63  LocalResponseNormOpModel m({1, 1, 1, 6}, /*radius=*/20, /*bias=*/0.0,
64                             /*alpha=*/4.0, /*beta=*/0.5);
65  m.SetInput({-1.1, 0.6, 0.7, 1.2, -0.7, 0.1});
66  m.Invoke();
67  // The result is every input divided by 3.
68  EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear(
69                                 {-0.275, 0.15, 0.175, 0.3, -0.175, 0.025})));
70}
71
72TEST(LocalResponseNormOpTest, WithBias) {
73  LocalResponseNormOpModel m({1, 1, 1, 6}, /*radius=*/20, /*bias=*/9.0,
74                             /*alpha=*/4.0, /*beta=*/0.5);
75  m.SetInput({-1.1, 0.6, 0.7, 1.2, -0.7, 0.1});
76  m.Invoke();
77  // The result is every input divided by 5.
78  EXPECT_THAT(
79      m.GetOutput(),
80      ElementsAreArray(ArrayFloatNear({-0.22, 0.12, 0.14, 0.24, -0.14, 0.02})));
81}
82
83TEST(LocalResponseNormOpTest, SmallRadius) {
84  LocalResponseNormOpModel m({1, 1, 1, 6}, /*radius=*/2, /*bias=*/9.0,
85                             /*alpha=*/4.0, /*beta=*/0.5);
86  m.SetInput({-1.1, 0.6, 0.7, 1.2, -0.7, 0.1});
87  m.Invoke();
88  EXPECT_THAT(
89      m.GetOutput(),
90      ElementsAreArray(ArrayFloatNear(
91          {-0.264926, 0.125109, 0.140112, 0.267261, -0.161788, 0.0244266})));
92}
93
94}  // namespace
95}  // namespace tflite
96
97int main(int argc, char** argv) {
98  ::tflite::LogToStderr();
99  ::testing::InitGoogleTest(&argc, argv);
100  return RUN_ALL_TESTS();
101}
102