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 #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
21 namespace tflite {
22 namespace {
23
24 using ::testing::ElementsAreArray;
25
26 class ResizeBilinearOpModel : public SingleOpModel {
27 public:
ResizeBilinearOpModel(const TensorData & input,std::initializer_list<int> size_data={})28 ResizeBilinearOpModel(const TensorData& input,
29 std::initializer_list<int> size_data = {}) {
30 bool const_size = size_data.size() != 0;
31 input_ = AddInput(input);
32 if (const_size) {
33 size_ = AddConstInput(TensorType_INT32, size_data, {2});
34 } else {
35 size_ = AddInput({TensorType_INT32, {2}});
36 }
37 output_ = AddOutput(TensorType_FLOAT32); // Always float.
38 SetBuiltinOp(BuiltinOperator_RESIZE_BILINEAR,
39 BuiltinOptions_ResizeBilinearOptions,
40 CreateResizeBilinearOptions(builder_).Union());
41 if (const_size) {
42 BuildInterpreter({GetShape(input_)});
43 } else {
44 BuildInterpreter({GetShape(input_), GetShape(size_)});
45 }
46 }
47
SetInput(std::initializer_list<float> data)48 void SetInput(std::initializer_list<float> data) {
49 PopulateTensor(input_, data);
50 }
SetSize(std::initializer_list<int> data)51 void SetSize(std::initializer_list<int> data) { PopulateTensor(size_, data); }
52
GetOutput()53 std::vector<float> GetOutput() { return ExtractVector<float>(output_); }
54
55 private:
56 int input_;
57 int size_;
58 int output_;
59 };
60
TEST(ResizeBilinearOpTest,HorizontalResize)61 TEST(ResizeBilinearOpTest, HorizontalResize) {
62 ResizeBilinearOpModel m({TensorType_FLOAT32, {1, 1, 2, 1}});
63 m.SetInput({3, 6});
64 m.SetSize({1, 3});
65 m.Invoke();
66 EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear({3, 5, 6})));
67
68 ResizeBilinearOpModel const_m({TensorType_FLOAT32, {1, 1, 2, 1}}, {1, 3});
69 const_m.SetInput({3, 6});
70 const_m.Invoke();
71 EXPECT_THAT(const_m.GetOutput(), ElementsAreArray(ArrayFloatNear({3, 5, 6})));
72 }
73
TEST(ResizeBilinearOpTest,VerticalResize)74 TEST(ResizeBilinearOpTest, VerticalResize) {
75 ResizeBilinearOpModel m({TensorType_FLOAT32, {1, 2, 1, 1}});
76 m.SetInput({3, 9});
77 m.SetSize({3, 1});
78 m.Invoke();
79 EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear({3, 7, 9})));
80
81 ResizeBilinearOpModel const_m({TensorType_FLOAT32, {1, 2, 1, 1}}, {3, 1});
82 const_m.SetInput({3, 9});
83 const_m.Invoke();
84 EXPECT_THAT(const_m.GetOutput(), ElementsAreArray(ArrayFloatNear({3, 7, 9})));
85 }
86
TEST(ResizeBilinearOpTest,TwoDimensionalResize)87 TEST(ResizeBilinearOpTest, TwoDimensionalResize) {
88 ResizeBilinearOpModel m({TensorType_FLOAT32, {1, 2, 2, 1}});
89 m.SetInput({
90 3, 6, //
91 9, 12 //
92 });
93 m.SetSize({3, 3});
94 m.Invoke();
95 EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear({
96 3, 5, 6, //
97 7, 9, 10, //
98 9, 11, 12, //
99 })));
100
101 ResizeBilinearOpModel const_m({TensorType_FLOAT32, {1, 2, 2, 1}}, {3, 3});
102 const_m.SetInput({
103 3, 6, //
104 9, 12 //
105 });
106 const_m.Invoke();
107 EXPECT_THAT(const_m.GetOutput(), ElementsAreArray(ArrayFloatNear({
108 3, 5, 6, //
109 7, 9, 10, //
110 9, 11, 12, //
111 })));
112 }
113
TEST(ResizeBilinearOpTest,TwoDimensionalResizeWithTwoBatches)114 TEST(ResizeBilinearOpTest, TwoDimensionalResizeWithTwoBatches) {
115 ResizeBilinearOpModel m({TensorType_FLOAT32, {2, 2, 2, 1}});
116 m.SetInput({
117 3, 6, //
118 9, 12, //
119 4, 10, //
120 10, 16 //
121 });
122 m.SetSize({3, 3});
123 m.Invoke();
124 EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear({
125 3, 5, 6, //
126 7, 9, 10, //
127 9, 11, 12, //
128 4, 8, 10, //
129 8, 12, 14, //
130 10, 14, 16, //
131 })));
132
133 ResizeBilinearOpModel const_m({TensorType_FLOAT32, {2, 2, 2, 1}}, {3, 3});
134 const_m.SetInput({
135 3, 6, //
136 9, 12, //
137 4, 10, //
138 10, 16 //
139 });
140 const_m.Invoke();
141 EXPECT_THAT(const_m.GetOutput(), ElementsAreArray(ArrayFloatNear({
142 3, 5, 6, //
143 7, 9, 10, //
144 9, 11, 12, //
145 4, 8, 10, //
146 8, 12, 14, //
147 10, 14, 16, //
148 })));
149 }
150
TEST(ResizeBilinearOpTest,ThreeDimensionalResize)151 TEST(ResizeBilinearOpTest, ThreeDimensionalResize) {
152 ResizeBilinearOpModel m({TensorType_FLOAT32, {1, 2, 2, 2}});
153 m.SetInput({
154 3, 4, 6, 10, //
155 9, 10, 12, 16, //
156 });
157 m.SetSize({3, 3});
158 m.Invoke();
159 EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear({
160 3, 4, 5, 8, 6, 10, //
161 7, 8, 9, 12, 10, 14, //
162 9, 10, 11, 14, 12, 16, //
163 })));
164
165 ResizeBilinearOpModel const_m({TensorType_FLOAT32, {1, 2, 2, 2}}, {3, 3});
166 const_m.SetInput({
167 3, 4, 6, 10, //
168 9, 10, 12, 16, //
169 });
170 const_m.Invoke();
171 EXPECT_THAT(const_m.GetOutput(), ElementsAreArray(ArrayFloatNear({
172 3, 4, 5, 8, 6, 10, //
173 7, 8, 9, 12, 10, 14, //
174 9, 10, 11, 14, 12, 16, //
175 })));
176 }
177
178 } // namespace
179 } // namespace tflite
180
main(int argc,char ** argv)181 int main(int argc, char** argv) {
182 ::tflite::LogToStderr();
183 ::testing::InitGoogleTest(&argc, argv);
184 return RUN_ALL_TESTS();
185 }
186