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 "tensorflow/core/kernels/image/sampling_kernels.h"
17
18 #include "tensorflow/core/platform/test.h"
19
20 namespace tensorflow {
21 namespace functor {
22 namespace {
23
24 class KernelsTest : public ::testing::Test {
25 protected:
26 template <typename KernelType>
TestKernelValues(const KernelType & kernel,const std::vector<float> & x,const std::vector<float> & expected) const27 void TestKernelValues(const KernelType& kernel, const std::vector<float>& x,
28 const std::vector<float>& expected) const {
29 ASSERT_EQ(x.size(), expected.size());
30 for (int i = 0; i < x.size(); ++i) {
31 constexpr float kTolerance = 1e-3;
32 EXPECT_NEAR(kernel(x[i]), expected[i], kTolerance);
33 EXPECT_NEAR(kernel(-x[i]), expected[i], kTolerance);
34 }
35 }
36 };
37
TEST_F(KernelsTest,TestKernelValues)38 TEST_F(KernelsTest, TestKernelValues) {
39 // Tests kernel values against a set of known golden values
40 TestKernelValues(CreateLanczos1Kernel(), {0.0f, 0.5f, 1.0f, 1.5},
41 {1.0f, 0.4052f, 0.0f, 0.0f});
42 TestKernelValues(CreateLanczos3Kernel(), {0.0f, 0.5f, 1.0f, 1.5f, 2.5f, 3.5},
43 {1.0f, 0.6079f, 0.0f, -0.1351f, 0.0243f, 0.0f});
44 TestKernelValues(
45 CreateLanczos5Kernel(), {0.0f, 0.5f, 1.0f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5},
46 {1.0f, 0.6262f, 0.0f, -0.1822f, 0.0810569f, -0.0334f, 0.0077f, 0.0f});
47 TestKernelValues(CreateGaussianKernel(), {0.0f, 0.5f, 1.0f, 1.5},
48 {1.0f, 0.6065f, 0.1353f, 0.0f});
49
50 TestKernelValues(CreateBoxKernel(), {0.0f, 0.25f, 0.5f, 1.0f},
51 {1.0f, 1.0f, 0.5f, 0.0f});
52 TestKernelValues(CreateTriangleKernel(), {0.0f, 0.5f, 1.0f},
53 {1.0f, 0.5f, 0.0f});
54
55 TestKernelValues(CreateKeysCubicKernel(), {0.0f, 0.5f, 1.0f, 1.5f, 2.5},
56 {1.0f, 0.5625f, 0.0f, -0.0625f, 0.0f});
57 TestKernelValues(CreateMitchellCubicKernel(), {0.0f, 0.5f, 1.0f, 1.5f, 2.5},
58 {0.8889f, 0.5347f, 0.0556f, -0.0347f, 0.0f});
59 }
60
TEST(SamplingKernelTypeFromStringTest,Works)61 TEST(SamplingKernelTypeFromStringTest, Works) {
62 EXPECT_EQ(SamplingKernelTypeFromString("lanczos1"), Lanczos1Kernel);
63 EXPECT_EQ(SamplingKernelTypeFromString("lanczos3"), Lanczos3Kernel);
64 EXPECT_EQ(SamplingKernelTypeFromString("lanczos5"), Lanczos5Kernel);
65 EXPECT_EQ(SamplingKernelTypeFromString("gaussian"), GaussianKernel);
66 EXPECT_EQ(SamplingKernelTypeFromString("box"), BoxKernel);
67 EXPECT_EQ(SamplingKernelTypeFromString("triangle"), TriangleKernel);
68 EXPECT_EQ(SamplingKernelTypeFromString("mitchellcubic"), MitchellCubicKernel);
69 EXPECT_EQ(SamplingKernelTypeFromString("keyscubic"), KeysCubicKernel);
70 EXPECT_EQ(SamplingKernelTypeFromString("not a kernel"),
71 SamplingKernelTypeEnd);
72 }
73
74 } // namespace
75 } // namespace functor
76 } // namespace tensorflow
77