Home | History | Annotate | Download | only in kernels
      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 
     16 #define EIGEN_USE_THREADS
     17 
     18 #include <functional>
     19 #include <memory>
     20 #include <vector>
     21 
     22 #include "tensorflow/cc/client/client_session.h"
     23 #include "tensorflow/cc/ops/audio_ops.h"
     24 #include "tensorflow/cc/ops/const_op.h"
     25 #include "tensorflow/cc/ops/math_ops.h"
     26 #include "tensorflow/core/framework/tensor_testutil.h"
     27 #include "tensorflow/core/framework/types.h"
     28 #include "tensorflow/core/framework/types.pb.h"
     29 #include "tensorflow/core/kernels/ops_util.h"
     30 #include "tensorflow/core/lib/core/status_test_util.h"
     31 #include "tensorflow/core/platform/test.h"
     32 
     33 namespace tensorflow {
     34 namespace ops {
     35 namespace {
     36 
     37 TEST(SpectrogramOpTest, SimpleTest) {
     38   Scope root = Scope::NewRootScope();
     39 
     40   Tensor audio_tensor(DT_FLOAT, TensorShape({8, 1}));
     41   test::FillValues<float>(&audio_tensor,
     42                           {-1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f});
     43 
     44   Output audio_const_op = Const(root.WithOpName("audio_const_op"),
     45                                 Input::Initializer(audio_tensor));
     46 
     47   AudioSpectrogram spectrogram_op =
     48       AudioSpectrogram(root.WithOpName("spectrogram_op"), audio_const_op, 8, 1);
     49 
     50   TF_ASSERT_OK(root.status());
     51 
     52   ClientSession session(root);
     53   std::vector<Tensor> outputs;
     54 
     55   TF_EXPECT_OK(session.Run(ClientSession::FeedType(),
     56                            {spectrogram_op.spectrogram}, &outputs));
     57 
     58   const Tensor& spectrogram_tensor = outputs[0];
     59 
     60   EXPECT_EQ(3, spectrogram_tensor.dims());
     61   EXPECT_EQ(5, spectrogram_tensor.dim_size(2));
     62   EXPECT_EQ(1, spectrogram_tensor.dim_size(1));
     63   EXPECT_EQ(1, spectrogram_tensor.dim_size(0));
     64 
     65   test::ExpectTensorNear<float>(
     66       spectrogram_tensor,
     67       test::AsTensor<float>({0, 1, 2, 1, 0}, TensorShape({1, 1, 5})), 1e-3);
     68 }
     69 
     70 TEST(SpectrogramOpTest, SquaredTest) {
     71   Scope root = Scope::NewRootScope();
     72 
     73   Tensor audio_tensor(DT_FLOAT, TensorShape({8, 1}));
     74   test::FillValues<float>(&audio_tensor,
     75                           {-1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f});
     76 
     77   Output audio_const_op = Const(root.WithOpName("audio_const_op"),
     78                                 Input::Initializer(audio_tensor));
     79 
     80   AudioSpectrogram spectrogram_op =
     81       AudioSpectrogram(root.WithOpName("spectrogram_op"), audio_const_op, 8, 1,
     82                        AudioSpectrogram::Attrs().MagnitudeSquared(true));
     83 
     84   TF_ASSERT_OK(root.status());
     85 
     86   ClientSession session(root);
     87   std::vector<Tensor> outputs;
     88 
     89   TF_EXPECT_OK(session.Run(ClientSession::FeedType(),
     90                            {spectrogram_op.spectrogram}, &outputs));
     91 
     92   const Tensor& spectrogram_tensor = outputs[0];
     93 
     94   EXPECT_EQ(3, spectrogram_tensor.dims());
     95   EXPECT_EQ(5, spectrogram_tensor.dim_size(2));
     96   EXPECT_EQ(1, spectrogram_tensor.dim_size(1));
     97   EXPECT_EQ(1, spectrogram_tensor.dim_size(0));
     98 
     99   test::ExpectTensorNear<float>(
    100       spectrogram_tensor,
    101       test::AsTensor<float>({0, 1, 4, 1, 0}, TensorShape({1, 1, 5})), 1e-3);
    102 }
    103 
    104 }  // namespace
    105 }  // namespace ops
    106 }  // namespace tensorflow
    107