Home | History | Annotate | Download | only in kernels
      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 #define EIGEN_USE_THREADS
     17 
     18 #include <vector>
     19 
     20 #include "tensorflow/cc/client/client_session.h"
     21 #include "tensorflow/cc/ops/array_ops.h"
     22 #include "tensorflow/core/framework/tensor_testutil.h"
     23 
     24 namespace tensorflow {
     25 namespace ops {
     26 namespace {
     27 
     28 void ReferenceImpl(const quint8* inp, float inp_min, float inp_max,
     29                    const TensorShape& shape, float var_eps, float* out) {
     30   int N = shape.dim_size(0);
     31   int H = shape.dim_size(1);
     32   int W = shape.dim_size(2);
     33   int C = shape.dim_size(3);
     34 
     35   int total = N * H * W * C;
     36   float inp_scale = (inp_max - inp_min) / 255.0f;
     37   std::unique_ptr<float[]> dequantized(new float[total]);
     38 
     39   for (int i = 0; i < total; ++i) {
     40     dequantized[i] = inp_min + inp_scale * static_cast<float>(inp[i]);
     41   }
     42 
     43   std::unique_ptr<float[]> inp_mean(new float[N * C]);
     44   std::unique_ptr<float[]> inp_var(new float[N * C]);
     45 
     46   float img_size = static_cast<float>(H) * static_cast<float>(W);
     47 
     48   // Compute mean
     49   for (int n = 0; n < N; ++n) {
     50     for (int c = 0; c < C; ++c) {
     51       float sum = 0.0;
     52       for (int i = 0; i < H * W; ++i) {
     53         sum += dequantized[n * H * W * C + i * C + c];
     54       }
     55       inp_mean[n * C + c] = sum / img_size;
     56     }
     57   }
     58 
     59   // Compute var
     60   for (int n = 0; n < N; ++n) {
     61     for (int c = 0; c < C; ++c) {
     62       float sum = 0.0;
     63       for (int i = 0; i < H * W; ++i) {
     64         float tmp =
     65             dequantized[n * H * W * C + i * C + c] - inp_mean[n * C + c];
     66         sum += tmp * tmp;
     67       }
     68       inp_var[n * C + c] = sum / img_size;
     69     }
     70   }
     71 
     72   for (int n = 0; n < N; ++n) {
     73     for (int c = 0; c < C; ++c) {
     74       for (int i = 0; i < H * W; ++i) {
     75         out[n * H * W * C + i * C + c] =
     76             (dequantized[n * H * W * C + i * C + c] - inp_mean[n * C + c]) /
     77             std::sqrt(inp_var[n * C + c] + var_eps);
     78       }
     79     }
     80   }
     81 }
     82 
     83 void Expect(const Tensor& input, float x_min, float x_max,
     84             bool output_range_given, float give_y_min, float given_y_max) {
     85   Scope root = Scope::NewRootScope();
     86 
     87   auto input_ph = Placeholder(root, DT_QUINT8);
     88 
     89   const float variance_eps = 1e-5;
     90   auto instance_norm = QuantizedInstanceNorm(
     91       root, input_ph, x_min, x_max,
     92       QuantizedInstanceNorm::Attrs().VarianceEpsilon(variance_eps));
     93 
     94   Status s = root.status();
     95   EXPECT_TRUE(s.ok());
     96 
     97   ClientSession session(root);
     98   std::vector<Tensor> outputs;
     99 
    100   s = session.Run({{input_ph, input}},
    101                   {instance_norm.y, instance_norm.y_min, instance_norm.y_max},
    102                   &outputs);
    103 
    104   EXPECT_TRUE(s.ok());
    105   Tensor expected(DT_FLOAT, input.shape());
    106 
    107   ReferenceImpl(input.flat<quint8>().data(), x_min, x_max, input.shape(),
    108                 variance_eps, expected.flat<float>().data());
    109 
    110   auto out = outputs[0].flat<quint8>();
    111 
    112   float out_min = outputs[1].flat<float>()(0);
    113   float out_max = outputs[2].flat<float>()(0);
    114   float out_scale = (out_max - out_min) / 255.0f;
    115 
    116   Eigen::Tensor<float, 0, Eigen::RowMajor> max_diff =
    117       (expected.flat<float>() - (out_min + out_scale * out.cast<float>()))
    118           .abs()
    119           .maximum();
    120   EXPECT_LE(max_diff(), 0.1);
    121   LOG(INFO) << "max diff " << max_diff();
    122 }
    123 
    124 void TestBasic() {
    125   Tensor input_tensor(DT_QUINT8, {1, 4, 4, 32});
    126   auto input = input_tensor.flat<quint8>();
    127   // Random input
    128   input = input.random(Eigen::internal::UniformRandomGenerator<quint8>());
    129 
    130   Expect(input_tensor, 0.0f, 1.0f, false, 0.0f, 0.0f);
    131 }
    132 
    133 void TestZeroInput() {
    134   Tensor input_tensor(DT_QUINT8, {1, 4, 4, 32});
    135   auto input = input_tensor.flat<quint8>();
    136   // Zero input, but input min > 0. Tests that output min and max should be
    137   // properly separated.
    138   input = input.setConstant(0);
    139 
    140   Expect(input_tensor, 2.0f, 3.0f, false, 0.0f, 0.0f);
    141 }
    142 
    143 void TestMaxInput() {
    144   Tensor input_tensor(DT_QUINT8, {1, 1, 2, 16});
    145   auto input = input_tensor.flat<quint8>();
    146   // Inputs are all FLT_MAX / (number of inputs).
    147   input = input.setConstant(255);
    148 
    149   Expect(input_tensor, 0.0f,
    150          std::numeric_limits<float>::max() / static_cast<float>(2 * 16), false,
    151          0.0f, 0.0f);
    152 }
    153 
    154 void TestOutputRangeGiven() {
    155   Tensor input_tensor(DT_QUINT8, {1, 4, 4, 32});
    156   auto input = input_tensor.flat<quint8>();
    157   input = input.random(Eigen::internal::UniformRandomGenerator<quint8>());
    158 
    159   Expect(input_tensor, -10.0f, 10.0f, true, -1.0f, 1.0f);
    160 }
    161 
    162 void TestClamp() {
    163   Tensor input_tensor(DT_QUINT8, {1, 4, 4, 32});
    164   auto input = input_tensor.flat<quint8>();
    165   input = input.random(Eigen::internal::UniformRandomGenerator<quint8>());
    166 
    167   // Tests that negative outputs are clamped at 0.0, as the output range is
    168   // given to be (0.0, 1.0).
    169   Expect(input_tensor, -10.0f, 10.0f, true, 0.0f, 1.0f);
    170 }
    171 
    172 }  // namespace
    173 }  // namespace ops
    174 }  // namespace tensorflow
    175 
    176 #define RUN_TEST(t) \
    177   TEST(QuantizedInstanceNormTest, t) { tensorflow::ops::t(); }
    178 
    179 RUN_TEST(TestBasic);
    180 RUN_TEST(TestZeroInput);
    181 RUN_TEST(TestMaxInput);
    182 RUN_TEST(TestOutputRangeGiven);
    183 RUN_TEST(TestClamp);
    184 
    185 int main(int argc, char** argv) {
    186   // On Linux, add: FLAGS_logtostderr = true;
    187   ::testing::InitGoogleTest(&argc, argv);
    188   return RUN_ALL_TESTS();
    189 }
    190