1/* Copyright 2018 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 BaseMeanOpModel : public SingleOpModel { 27 public: 28 void SetAxis(std::initializer_list<int> data) { PopulateTensor(axis_, data); } 29 30 template <class T> 31 void SetInput(std::initializer_list<T> data) { 32 PopulateTensor(input_, data); 33 } 34 35 template <class T> 36 std::vector<T> GetOutput() { 37 return ExtractVector<T>(output_); 38 } 39 40 std::vector<int> GetOutputShape() { return GetTensorShape(output_); } 41 42 protected: 43 int input_; 44 int axis_; 45 int output_; 46}; 47 48// Model for the tests case where axis is a const tensor. 49class MeanOpConstModel : public BaseMeanOpModel { 50 public: 51 MeanOpConstModel(const TensorData& input, const TensorData& output, 52 std::initializer_list<int> axis_shape, 53 std::initializer_list<int> axis, bool keep_dims) { 54 input_ = AddInput(input); 55 axis_ = AddConstInput(TensorType_INT32, axis, axis_shape); 56 output_ = AddOutput(output); 57 SetBuiltinOp(BuiltinOperator_MEAN, BuiltinOptions_MeanOptions, 58 CreateMeanOptions(builder_, keep_dims).Union()); 59 BuildInterpreter({GetShape(input_)}); 60 } 61}; 62 63// Model for the tests case where axis is a dynamic tensor. 64class MeanOpDynamicModel : public BaseMeanOpModel { 65 public: 66 MeanOpDynamicModel(const TensorData& input, const TensorData& output, 67 const TensorData& axis, bool keep_dims) { 68 input_ = AddInput(input); 69 axis_ = AddInput(axis); 70 output_ = AddOutput(output); 71 SetBuiltinOp(BuiltinOperator_MEAN, BuiltinOptions_MeanOptions, 72 CreateMeanOptions(builder_, keep_dims).Union()); 73 BuildInterpreter({GetShape(input_)}); 74 } 75}; 76 77TEST(ConstMeanOpTest, NotKeepDims) { 78 std::initializer_list<float> data = { 79 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 80 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0}; 81 MeanOpConstModel m({TensorType_FLOAT32, {4, 3, 2}}, {TensorType_FLOAT32, {2}}, 82 {4}, {1, 0, -3, -3}, false); 83 m.SetInput(data); 84 m.Invoke(); 85 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2})); 86 EXPECT_THAT(m.GetOutput<float>(), ElementsAreArray(ArrayFloatNear({12, 13}))); 87} 88 89TEST(ConstMeanOpTest, KeepDims) { 90 std::initializer_list<float> data = { 91 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 92 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0}; 93 MeanOpConstModel m({TensorType_FLOAT32, {4, 3, 2}}, {TensorType_FLOAT32, {3}}, 94 {2}, {0, 2}, true); 95 m.SetInput(data); 96 m.Invoke(); 97 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({1, 3, 1})); 98 EXPECT_THAT(m.GetOutput<float>(), 99 ElementsAreArray(ArrayFloatNear({10.5, 12.5, 14.5}))); 100} 101 102TEST(DynamicMeanOpTest, NotKeepDims) { 103 std::initializer_list<float> data = { 104 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 105 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0}; 106 MeanOpDynamicModel m({TensorType_FLOAT32, {4, 3, 2}}, 107 {TensorType_FLOAT32, {2}}, {TensorType_INT32, {4}}, 108 false); 109 std::initializer_list<int> axis = {1, 0, -3, -3}; 110 m.SetAxis(axis); 111 m.SetInput(data); 112 m.Invoke(); 113 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({2})); 114 EXPECT_THAT(m.GetOutput<float>(), ElementsAreArray(ArrayFloatNear({12, 13}))); 115} 116 117TEST(DynamicMeanOpTest, KeepDims) { 118 std::initializer_list<float> data = { 119 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 120 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0}; 121 MeanOpDynamicModel m({TensorType_FLOAT32, {4, 3, 2}}, 122 {TensorType_FLOAT32, {3}}, {TensorType_INT32, {2}}, 123 true); 124 std::initializer_list<int> axis = {0, 2}; 125 m.SetAxis(axis); 126 m.SetInput(data); 127 m.Invoke(); 128 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({1, 3, 1})); 129 EXPECT_THAT(m.GetOutput<float>(), 130 ElementsAreArray(ArrayFloatNear({10.5, 12.5, 14.5}))); 131} 132 133} // namespace 134} // namespace tflite 135 136int main(int argc, char** argv) { 137 ::tflite::LogToStderr(); 138 ::testing::InitGoogleTest(&argc, argv); 139 return RUN_ALL_TESTS(); 140} 141