• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <vector>
17 
18 #include <gtest/gtest.h>
19 #include "flatbuffers/flexbuffers.h"  // from @flatbuffers
20 #include "tensorflow/lite/interpreter.h"
21 #include "tensorflow/lite/kernels/test_util.h"
22 #include "tensorflow/lite/schema/schema_generated.h"
23 
24 namespace tflite {
25 namespace ops {
26 namespace custom {
27 
28 TfLiteRegistration* Register_AUDIO_SPECTROGRAM();
29 
30 namespace {
31 
32 using ::testing::ElementsAre;
33 using ::testing::ElementsAreArray;
34 
35 class BaseAudioSpectrogramOpModel : public SingleOpModel {
36  public:
BaseAudioSpectrogramOpModel(const TensorData & input1,const TensorData & output,int window_size,int stride,bool magnitude_squared)37   BaseAudioSpectrogramOpModel(const TensorData& input1,
38                               const TensorData& output, int window_size,
39                               int stride, bool magnitude_squared) {
40     input1_ = AddInput(input1);
41     output_ = AddOutput(output);
42 
43     flexbuffers::Builder fbb;
44     fbb.Map([&]() {
45       fbb.Int("window_size", window_size);
46       fbb.Int("stride", stride);
47       fbb.Bool("magnitude_squared", magnitude_squared);
48     });
49     fbb.Finish();
50     SetCustomOp("AudioSpectrogram", fbb.GetBuffer(),
51                 Register_AUDIO_SPECTROGRAM);
52     BuildInterpreter({GetShape(input1_)});
53   }
54 
input1()55   int input1() { return input1_; }
GetOutput()56   std::vector<float> GetOutput() { return ExtractVector<float>(output_); }
GetOutputShape()57   std::vector<int> GetOutputShape() { return GetTensorShape(output_); }
58 
59  protected:
60   int input1_;
61   int output_;
62 };
63 
TEST(BaseAudioSpectrogramOpModel,NonSquaredTest)64 TEST(BaseAudioSpectrogramOpModel, NonSquaredTest) {
65   BaseAudioSpectrogramOpModel m({TensorType_FLOAT32, {8, 1}},
66                                 {TensorType_FLOAT32, {}}, 8, 1, false);
67   m.PopulateTensor<float>(m.input1(),
68                           {-1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f});
69 
70   ASSERT_EQ(m.Invoke(), kTfLiteOk);
71 
72   std::vector<int> output_shape = m.GetOutputShape();
73   EXPECT_EQ(3, output_shape.size());
74   EXPECT_THAT(output_shape, ElementsAre(1, 1, 5));
75 
76   EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear(
77                                  {0.0f, 1.0f, 2.0f, 1.0f, 0.0f}, 1e-3)));
78 }
79 
TEST(SpectrogramOpTest,SquaredTest)80 TEST(SpectrogramOpTest, SquaredTest) {
81   BaseAudioSpectrogramOpModel m({TensorType_FLOAT32, {8, 1}},
82                                 {TensorType_FLOAT32, {}}, 8, 1, true);
83   m.PopulateTensor<float>(m.input1(),
84                           {-1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f});
85 
86   ASSERT_EQ(m.Invoke(), kTfLiteOk);
87 
88   std::vector<int> output_shape = m.GetOutputShape();
89   EXPECT_EQ(3, output_shape.size());
90   EXPECT_THAT(output_shape, ElementsAre(1, 1, 5));
91 
92   EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear(
93                                  {0.f, 1.f, 4.f, 1.f, 0.f}, 1e-3)));
94 }
95 
TEST(SpectrogramOpTest,StrideTest)96 TEST(SpectrogramOpTest, StrideTest) {
97   BaseAudioSpectrogramOpModel m({TensorType_FLOAT32, {10, 1}},
98                                 {TensorType_FLOAT32, {}}, 8, 2, true);
99   m.PopulateTensor<float>(m.input1(), {-1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f,
100                                        1.0f, 0.0f, 1.0f, 0.0f});
101 
102   ASSERT_EQ(m.Invoke(), kTfLiteOk);
103 
104   std::vector<int> output_shape = m.GetOutputShape();
105   EXPECT_THAT(output_shape, ElementsAre(1, 2, 5));
106   EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear(
107                                  {0, 1, 4, 1, 0, 1, 2, 1, 2, 1}, 1e-3)));
108 }
109 
110 }  // namespace
111 }  // namespace custom
112 }  // namespace ops
113 }  // namespace tflite
114