1 /* Copyright 2015 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 <functional> 17 #include <memory> 18 19 #include "tensorflow/core/framework/allocator.h" 20 #include "tensorflow/core/framework/fake_input.h" 21 #include "tensorflow/core/framework/node_def_builder.h" 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/framework/summary.pb.h" 24 #include "tensorflow/core/framework/tensor.h" 25 #include "tensorflow/core/framework/types.h" 26 #include "tensorflow/core/kernels/ops_testutil.h" 27 #include "tensorflow/core/kernels/ops_util.h" 28 #include "tensorflow/core/lib/core/status_test_util.h" 29 #include "tensorflow/core/lib/histogram/histogram.h" 30 #include "tensorflow/core/lib/strings/strcat.h" 31 #include "tensorflow/core/platform/env.h" 32 #include "tensorflow/core/platform/logging.h" 33 #include "tensorflow/core/platform/protobuf.h" 34 #include "tensorflow/core/platform/test.h" 35 36 namespace tensorflow { 37 namespace { 38 39 static void EXPECT_SummaryMatches(const Summary& actual, 40 const string& expected_str) { 41 Summary expected; 42 CHECK(protobuf::TextFormat::ParseFromString(expected_str, &expected)); 43 EXPECT_EQ(expected.DebugString(), actual.DebugString()); 44 } 45 46 // -------------------------------------------------------------------------- 47 // SummaryImageOp 48 // -------------------------------------------------------------------------- 49 class SummaryImageOpTest : public OpsTestBase { 50 protected: 51 void MakeOp(int max_images) { 52 TF_ASSERT_OK(NodeDefBuilder("myop", "ImageSummary") 53 .Input(FakeInput()) 54 .Input(FakeInput()) 55 .Attr("max_images", max_images) 56 .Finalize(node_def())); 57 TF_ASSERT_OK(InitOp()); 58 } 59 60 void CheckAndRemoveEncodedImages(Summary* summary) { 61 for (int i = 0; i < summary->value_size(); ++i) { 62 Summary::Value* value = summary->mutable_value(i); 63 ASSERT_TRUE(value->has_image()) << "No image for value: " << value->tag(); 64 ASSERT_FALSE(value->image().encoded_image_string().empty()) 65 << "No encoded_image_string for value: " << value->tag(); 66 if (VLOG_IS_ON(2)) { 67 // When LOGGING, output the images to disk for manual inspection. 68 TF_CHECK_OK(WriteStringToFile( 69 Env::Default(), strings::StrCat("/tmp/", value->tag(), ".png"), 70 value->image().encoded_image_string())); 71 } 72 value->mutable_image()->clear_encoded_image_string(); 73 } 74 } 75 }; 76 77 TEST_F(SummaryImageOpTest, ThreeGrayImagesOutOfFive4dInput) { 78 MakeOp(3 /* max images */); 79 80 // Feed and run 81 AddInputFromArray<string>(TensorShape({}), {"tag"}); 82 AddInputFromArray<float>(TensorShape({5, 2, 1, 1}), 83 {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}); 84 TF_ASSERT_OK(RunOpKernel()); 85 86 // Check the output size. 87 Tensor* out_tensor = GetOutput(0); 88 ASSERT_EQ(0, out_tensor->dims()); 89 Summary summary; 90 ParseProtoUnlimited(&summary, out_tensor->scalar<string>()()); 91 92 CheckAndRemoveEncodedImages(&summary); 93 EXPECT_SummaryMatches(summary, R"( 94 value { tag: 'tag/image/0' image { width: 1 height: 2 colorspace: 1} } 95 value { tag: 'tag/image/1' image { width: 1 height: 2 colorspace: 1} } 96 value { tag: 'tag/image/2' image { width: 1 height: 2 colorspace: 1} } 97 )"); 98 } 99 100 TEST_F(SummaryImageOpTest, OneGrayImage4dInput) { 101 MakeOp(1 /* max images */); 102 103 // Feed and run 104 AddInputFromArray<string>(TensorShape({}), {"tag"}); 105 AddInputFromArray<float>(TensorShape({5 /*batch*/, 2, 1, 1 /*depth*/}), 106 {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}); 107 TF_ASSERT_OK(RunOpKernel()); 108 109 // Check the output size. 110 Tensor* out_tensor = GetOutput(0); 111 ASSERT_EQ(0, out_tensor->dims()); 112 Summary summary; 113 ParseProtoUnlimited(&summary, out_tensor->scalar<string>()()); 114 115 CheckAndRemoveEncodedImages(&summary); 116 EXPECT_SummaryMatches(summary, R"( 117 value { tag: 'tag/image' image { width: 1 height: 2 colorspace: 1} })"); 118 } 119 120 TEST_F(SummaryImageOpTest, OneColorImage4dInput) { 121 MakeOp(1 /* max images */); 122 123 // Feed and run 124 AddInputFromArray<string>(TensorShape({}), {"tag"}); 125 AddInputFromArray<float>( 126 TensorShape({1 /*batch*/, 5 /*rows*/, 2 /*columns*/, 3 /*depth*/}), 127 { 128 /* r0, c0, RGB */ 1.0f, 0.1f, 0.2f, 129 /* r0, c1, RGB */ 1.0f, 0.3f, 0.4f, 130 /* r1, c0, RGB */ 0.0f, 1.0f, 0.0f, 131 /* r1, c1, RGB */ 0.0f, 1.0f, 0.0f, 132 /* r2, c0, RGB */ 0.0f, 0.0f, 1.0f, 133 /* r2, c1, RGB */ 0.0f, 0.0f, 1.0f, 134 /* r3, c0, RGB */ 1.0f, 1.0f, 0.0f, 135 /* r3, c1, RGB */ 1.0f, 0.0f, 1.0f, 136 /* r4, c0, RGB */ 1.0f, 1.0f, 0.0f, 137 /* r4, c1, RGB */ 1.0f, 0.0f, 1.0f, 138 }); 139 TF_ASSERT_OK(RunOpKernel()); 140 141 // Check the output size. 142 Tensor* out_tensor = GetOutput(0); 143 ASSERT_EQ(0, out_tensor->dims()); 144 Summary summary; 145 ParseProtoUnlimited(&summary, out_tensor->scalar<string>()()); 146 147 CheckAndRemoveEncodedImages(&summary); 148 EXPECT_SummaryMatches(summary, R"( 149 value { tag: 'tag/image' image { width: 2 height: 5 colorspace: 3} })"); 150 } 151 152 } // namespace 153 } // namespace tensorflow 154