• 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 
16 #include "tensorflow/lite/tools/accuracy/ilsvrc/inception_preprocessing.h"
17 
18 #include <memory>
19 
20 #include "tensorflow/cc/framework/scope.h"
21 #include "tensorflow/cc/ops/standard_ops.h"
22 #include "tensorflow/core/framework/tensor.h"
23 #include "tensorflow/core/graph/graph_def_builder.h"
24 #include "tensorflow/core/lib/core/errors.h"
25 #include "tensorflow/core/platform/logging.h"
26 #include "tensorflow/core/platform/types.h"
27 #include "tensorflow/core/public/session.h"
28 
29 namespace tensorflow {
30 namespace metrics {
31 
32 namespace {
CentralCropImage(const Scope & s,const tensorflow::Output & decoded_image,double crop_fraction,tensorflow::Output * cropped_image)33 void CentralCropImage(const Scope& s, const tensorflow::Output& decoded_image,
34                       double crop_fraction, tensorflow::Output* cropped_image) {
35   auto image_dims = ops::Slice(s, ops::Shape(s, decoded_image), {0}, {2});
36   auto height_width = ops::Cast(s, image_dims, DT_DOUBLE);
37   auto cropped_begin = ops::Div(
38       s, ops::Sub(s, height_width, ops::Mul(s, height_width, crop_fraction)),
39       2.0);
40   auto bbox_begin = ops::Cast(s, cropped_begin, DT_INT32);
41   auto bbox_size = ops::Sub(s, image_dims, ops::Mul(s, bbox_begin, 2));
42   auto slice_begin = ops::Concat(s, {bbox_begin, Input({0})}, 0);
43   auto slice_size = ops::Concat(s, {bbox_size, {-1}}, 0);
44   *cropped_image = ops::Slice(s, decoded_image, slice_begin, slice_size);
45 }
46 
47 }  // namespace
48 
AddToGraph(const Scope & scope,const Input & input)49 void InceptionPreprocessingStage::AddToGraph(const Scope& scope,
50                                              const Input& input) {
51   if (!scope.ok()) return;
52   Scope s = scope.WithOpName(name());
53   ops::DecodeJpeg::Attrs attrs;
54   attrs.channels_ = 3;
55   auto decoded_jpeg = ops::DecodeJpeg(s, input, attrs);
56   tensorflow::Output cropped_image;
57   CentralCropImage(s, decoded_jpeg, params_.cropping_fraction, &cropped_image);
58   auto dims_expander = ops::ExpandDims(s, cropped_image, 0);
59   auto resized_image =
60       ops::ResizeBilinear(s.WithOpName("resize"), dims_expander,
61                           ops::Const(s, {image_height_, image_width_}));
62 
63   ::tensorflow::Output preprocessed_image = resized_image;
64 
65   if (!params_.input_means.empty()) {
66     preprocessed_image =
67         ops::Sub(s.WithOpName("sub"), preprocessed_image,
68                  {params_.input_means[0], params_.input_means[1],
69                   params_.input_means[2]});
70   }
71 
72   if (std::abs(params_.scale) > 1e-7f) {
73     auto squeezed_image = ops::Squeeze(s, preprocessed_image);
74     preprocessed_image = ops::Div(s, squeezed_image, {params_.scale});
75     preprocessed_image = ops::ExpandDims(s, preprocessed_image, {0});
76   }
77 
78   // Cast the output from float to output datatype.
79   if (output_datatype_ != DT_FLOAT) {
80     preprocessed_image =
81         ops::Cast(s.WithOpName("cast"), preprocessed_image, output_datatype_);
82   }
83 
84   this->stage_output_ =
85       ops::Identity(s.WithOpName(output_name()), preprocessed_image);
86 }
87 
88 }  // namespace metrics
89 }  // namespace tensorflow
90