• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 #include "tensorflow/lite/kernels/internal/test_util.h"
16 
17 #include <cmath>
18 #include <iterator>
19 
20 namespace tflite {
21 
22 // this is a copied from an internal function in propagate_fixed_sizes.cc
ComputeConvSizes(const RuntimeShape & input_shape,int output_depth,int filter_width,int filter_height,int stride,int dilation_width_factor,int dilation_height_factor,PaddingType padding_type,RuntimeShape * output_shape,int * pad_width,int * pad_height)23 bool ComputeConvSizes(const RuntimeShape& input_shape, int output_depth,
24                       int filter_width, int filter_height, int stride,
25                       int dilation_width_factor, int dilation_height_factor,
26                       PaddingType padding_type, RuntimeShape* output_shape,
27                       int* pad_width, int* pad_height) {
28   const int input_width = input_shape.Dims(2);
29   const int input_height = input_shape.Dims(1);
30   const int batch = input_shape.Dims(0);
31 
32   int dilated_filter_width = dilation_width_factor * (filter_width - 1) + 1;
33   int dilated_filter_height = dilation_height_factor * (filter_height - 1) + 1;
34 
35   int output_height = 0;
36   int output_width = 0;
37   if (padding_type == PaddingType::kValid) {
38     // Official TF is
39     // ceil((input_height - (dilated_filter_height - 1)) / stride),
40     // implemented as
41     // floor(
42     //   (input_height - (dilated_filter_height - 1) + (stride - 1)) / stride).
43     output_height = (input_height + stride - dilated_filter_height) / stride;
44     output_width = (input_width + stride - dilated_filter_width) / stride;
45   } else if (padding_type == PaddingType::kSame) {
46     output_height = (input_height + stride - 1) / stride;
47     output_width = (input_width + stride - 1) / stride;
48   } else {
49     return false;
50   }
51 
52   if (output_width <= 0 || output_height <= 0) {
53     return false;
54   }
55 
56   *pad_height = std::max(
57       0, ((output_height - 1) * stride + dilated_filter_height - input_height) /
58              2);
59   *pad_width = std::max(
60       0,
61       ((output_width - 1) * stride + dilated_filter_width - input_width) / 2);
62 
63   output_shape->BuildFrom({batch, output_height, output_width, output_depth});
64   return true;
65 }
66 
RandomEngine()67 std::mt19937& RandomEngine() {
68   static std::mt19937 engine;
69   return engine;
70 }
71 
UniformRandomInt(int min,int max)72 int UniformRandomInt(int min, int max) {
73   std::uniform_int_distribution<int> dist(min, max);
74   return dist(RandomEngine());
75 }
76 
UniformRandomFloat(float min,float max)77 float UniformRandomFloat(float min, float max) {
78   std::uniform_real_distribution<float> dist(min, max);
79   return dist(RandomEngine());
80 }
81 
ExponentialRandomPositiveInt(float percentile,int percentile_val,int max_val)82 int ExponentialRandomPositiveInt(float percentile, int percentile_val,
83                                  int max_val) {
84   const float lambda =
85       -std::log(1.f - percentile) / static_cast<float>(percentile_val);
86   std::exponential_distribution<float> dist(lambda);
87   float val;
88   do {
89     val = dist(RandomEngine());
90   } while (!val || !std::isfinite(val) || val > max_val);
91   return static_cast<int>(std::ceil(val));
92 }
93 
ExponentialRandomPositiveFloat(float percentile,float percentile_val,float max_val)94 float ExponentialRandomPositiveFloat(float percentile, float percentile_val,
95                                      float max_val) {
96   const float lambda =
97       -std::log(1.f - percentile) / static_cast<float>(percentile_val);
98   std::exponential_distribution<float> dist(lambda);
99   float val;
100   do {
101     val = dist(RandomEngine());
102   } while (!std::isfinite(val) || val > max_val);
103   return val;
104 }
105 
FillRandom(std::vector<float> * vec,float min,float max)106 void FillRandom(std::vector<float>* vec, float min, float max) {
107   std::uniform_real_distribution<float> dist(min, max);
108   // TODO(b/154540105): use std::ref to avoid copying the random engine.
109   auto gen = std::bind(dist, RandomEngine());
110   std::generate(std::begin(*vec), std::end(*vec), gen);
111 }
112 
113 }  // namespace tflite
114