1 /* Copyright 2018 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 using ::testing::IsEmpty; 26 27 class BaseSqueezeOpModel : public SingleOpModel { 28 public: 29 BaseSqueezeOpModel(const TensorData& input, const TensorData& output, 30 std::initializer_list<int> axis) { 31 input_ = AddInput(input); 32 output_ = AddOutput(output); 33 SetBuiltinOp( 34 BuiltinOperator_SQUEEZE, BuiltinOptions_SqueezeOptions, 35 CreateSqueezeOptions(builder_, builder_.CreateVector<int>(axis)) 36 .Union()); 37 BuildInterpreter({GetShape(input_)}); 38 } 39 40 int input() { return input_; } 41 42 protected: 43 int input_; 44 int output_; 45 }; 46 47 class FloatSqueezeOpModel : public BaseSqueezeOpModel { 48 public: 49 using BaseSqueezeOpModel::BaseSqueezeOpModel; 50 51 void SetInput(std::initializer_list<float> data) { 52 PopulateTensor(input_, data); 53 } 54 55 std::vector<float> GetOutput() { return ExtractVector<float>(output_); } 56 std::vector<int> GetOutputShape() { return GetTensorShape(output_); } 57 }; 58 59 TEST(FloatSqueezeOpTest, SqueezeAll) { 60 std::initializer_list<float> data = { 61 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 62 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0}; 63 FloatSqueezeOpModel m({TensorType_FLOAT32, {1, 24, 1}}, 64 {TensorType_FLOAT32, {24}}, {}); 65 m.SetInput(data); 66 m.Invoke(); 67 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({24})); 68 EXPECT_THAT( 69 m.GetOutput(), 70 ElementsAreArray({1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 71 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 72 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0})); 73 } 74 75 TEST(FloatSqueezeOpTest, SqueezeSelectedAxis) { 76 std::initializer_list<float> data = { 77 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 78 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0}; 79 FloatSqueezeOpModel m({TensorType_FLOAT32, {1, 24, 1}}, 80 {TensorType_FLOAT32, {24}}, {2}); 81 m.SetInput(data); 82 m.Invoke(); 83 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({1, 24})); 84 EXPECT_THAT( 85 m.GetOutput(), 86 ElementsAreArray({1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 87 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 88 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0})); 89 } 90 91 TEST(FloatSqueezeOpTest, SqueezeNegativeAxis) { 92 std::initializer_list<float> data = { 93 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 94 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0}; 95 FloatSqueezeOpModel m({TensorType_FLOAT32, {1, 24, 1}}, 96 {TensorType_FLOAT32, {24}}, {-1, 0}); 97 m.SetInput(data); 98 m.Invoke(); 99 EXPECT_THAT(m.GetOutputShape(), ElementsAreArray({24})); 100 EXPECT_THAT( 101 m.GetOutput(), 102 ElementsAreArray({1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 103 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 104 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0})); 105 } 106 107 TEST(FloatSqueezeOpTest, SqueezeAllDims) { 108 std::initializer_list<float> data = {3.85}; 109 FloatSqueezeOpModel m({TensorType_FLOAT32, {1, 1, 1, 1, 1, 1, 1}}, 110 {TensorType_FLOAT32, {1}}, {}); 111 m.SetInput(data); 112 m.Invoke(); 113 EXPECT_THAT(m.GetOutputShape(), IsEmpty()); 114 EXPECT_THAT(m.GetOutput(), ElementsAreArray({3.85})); 115 } 116 117 } // namespace 118 } // namespace tflite 119 120 int main(int argc, char** argv) { 121 ::tflite::LogToStderr(); 122 ::testing::InitGoogleTest(&argc, argv); 123 return RUN_ALL_TESTS(); 124 } 125