aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/kernels/summary_audio_op_test.cc
blob: 1b957c548b64c92024b539d07e60e66bd6a9b808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include <functional>
#include <memory>

#include "tensorflow/core/framework/allocator.h"
#include "tensorflow/core/framework/fake_input.h"
#include "tensorflow/core/framework/node_def_builder.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/summary.pb.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/kernels/ops_testutil.h"
#include "tensorflow/core/kernels/ops_util.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/lib/histogram/histogram.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/protobuf.h"
#include "tensorflow/core/platform/test.h"

namespace tensorflow {
namespace {

static void EXPECT_SummaryMatches(const Summary& actual,
                                  const string& expected_str) {
  Summary expected;
  CHECK(protobuf::TextFormat::ParseFromString(expected_str, &expected));
  EXPECT_EQ(expected.DebugString(), actual.DebugString());
}

// --------------------------------------------------------------------------
// SummaryAudioOp
// --------------------------------------------------------------------------
class SummaryAudioOpTest : public OpsTestBase {
 protected:
  void MakeOp(const int max_outputs) {
    TF_ASSERT_OK(NodeDefBuilder("myop", "AudioSummaryV2")
                     .Input(FakeInput())
                     .Input(FakeInput())
                     .Input(FakeInput())
                     .Attr("max_outputs", max_outputs)
                     .Finalize(node_def()));
    TF_ASSERT_OK(InitOp());
  }

  void CheckAndRemoveEncodedAudio(Summary* summary) {
    for (int i = 0; i < summary->value_size(); ++i) {
      Summary::Value* value = summary->mutable_value(i);
      ASSERT_TRUE(value->has_audio()) << "No audio for value: " << value->tag();
      ASSERT_FALSE(value->audio().encoded_audio_string().empty())
          << "No encoded_audio_string for value: " << value->tag();
      if (VLOG_IS_ON(2)) {
        // When LOGGING, output the audio to disk for manual inspection.
        TF_CHECK_OK(WriteStringToFile(
            Env::Default(), strings::StrCat("/tmp/", value->tag(), ".wav"),
            value->audio().encoded_audio_string()));
      }
      value->mutable_audio()->clear_encoded_audio_string();
    }
  }
};

TEST_F(SummaryAudioOpTest, Basic3D) {
  const float kSampleRate = 44100.0f;
  const int kMaxOutputs = 3;
  MakeOp(kMaxOutputs);

  // Feed and run
  AddInputFromArray<string>(TensorShape({}), {"tag"});
  AddInputFromArray<float>(TensorShape({4, 2, 2}),
                           {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                            0.0, 0.0, 0.0, 0.0, 0.0, 0.0});
  AddInputFromArray<float>(TensorShape({}), {kSampleRate});

  TF_ASSERT_OK(RunOpKernel());

  // Check the output size.
  Tensor* out_tensor = GetOutput(0);
  ASSERT_EQ(0, out_tensor->dims());
  Summary summary;
  ParseProtoUnlimited(&summary, out_tensor->scalar<string>()());

  CheckAndRemoveEncodedAudio(&summary);
  EXPECT_SummaryMatches(summary, R"(
    value { tag: 'tag/audio/0'
            audio { content_type: "audio/wav" sample_rate: 44100 num_channels: 2
                    length_frames: 2 } }
    value { tag: 'tag/audio/1'
            audio { content_type: "audio/wav" sample_rate: 44100 num_channels: 2
                    length_frames: 2 } }
    value { tag: 'tag/audio/2'
            audio { content_type: "audio/wav" sample_rate: 44100 num_channels: 2
                    length_frames: 2 } }
  )");
}

TEST_F(SummaryAudioOpTest, Basic2D) {
  const float kSampleRate = 44100.0f;
  const int kMaxOutputs = 3;
  MakeOp(kMaxOutputs);

  // Feed and run
  AddInputFromArray<string>(TensorShape({}), {"tag"});
  AddInputFromArray<float>(TensorShape({4, 4}),
                           {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                            0.0, 0.0, 0.0, 0.0, 0.0, 0.0});
  AddInputFromArray<float>(TensorShape({}), {kSampleRate});

  TF_ASSERT_OK(RunOpKernel());

  // Check the output size.
  Tensor* out_tensor = GetOutput(0);
  ASSERT_EQ(0, out_tensor->dims());
  Summary summary;
  ParseProtoUnlimited(&summary, out_tensor->scalar<string>()());

  CheckAndRemoveEncodedAudio(&summary);
  EXPECT_SummaryMatches(summary, R"(
    value { tag: 'tag/audio/0'
            audio { content_type: "audio/wav" sample_rate: 44100 num_channels: 1
                    length_frames: 4 } }
    value { tag: 'tag/audio/1'
            audio { content_type: "audio/wav" sample_rate: 44100 num_channels: 1
                    length_frames: 4 } }
    value { tag: 'tag/audio/2'
            audio { content_type: "audio/wav" sample_rate: 44100 num_channels: 1
                    length_frames: 4 } }
  )");
}

}  // namespace
}  // namespace tensorflow