preprocess_op.h 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

17
#include <glog/logging.h>
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <yaml-cpp/yaml.h>

#include <vector>
#include <string>
#include <utility>
#include <memory>
#include <unordered_map>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

namespace PaddleDetection {

// Object for storing all preprocessed data
class ImageBlob {
 public:
35 36
  // image width and height
  std::vector<float> im_shape_;
37 38
  // Buffer for image data after preprocessing
  std::vector<float> im_data_;
39 40
  // input image width, height
  std::vector<int> input_shape_;
41
  // Evaluation image width and height
42 43 44
  //std::vector<float>  eval_im_size_f_;
  // Scale factor for image size to origin image size
  std::vector<float> scale_factor_;
45 46 47 48 49
};

// Abstraction of preprocessing opration class
class PreprocessOp {
 public:
50
  virtual void Init(const YAML::Node& item, const std::vector<int> image_shape) = 0;
51 52 53
  virtual void Run(cv::Mat* im, ImageBlob* data) = 0;
};

54 55 56 57 58 59
class InitInfo : public PreprocessOp{
 public:
  virtual void Init(const YAML::Node& item, const std::vector<int> image_shape) {}
  virtual void Run(cv::Mat* im, ImageBlob* data);
};

60 61
class Normalize : public PreprocessOp {
 public:
62
  virtual void Init(const YAML::Node& item, const std::vector<int> image_shape) {
63 64 65 66 67 68 69 70 71 72 73
    mean_ = item["mean"].as<std::vector<float>>();
    scale_ = item["std"].as<std::vector<float>>();
    is_scale_ = item["is_scale"].as<bool>();
  }

  virtual void Run(cv::Mat* im, ImageBlob* data);

 private:
  // CHW or HWC
  std::vector<float> mean_;
  std::vector<float> scale_;
74
  bool is_scale_;
75 76 77 78
};

class Permute : public PreprocessOp {
 public:
79
  virtual void Init(const YAML::Node& item, const std::vector<int> image_shape) {}
80 81 82 83 84 85
  virtual void Run(cv::Mat* im, ImageBlob* data);

};

class Resize : public PreprocessOp {
 public:
86
  virtual void Init(const YAML::Node& item, const std::vector<int> image_shape) {
87
    interp_ = item["interp"].as<int>();
88 89 90 91 92 93 94
    //max_size_ = item["target_size"].as<int>();
    keep_ratio_ = item["keep_ratio"].as<bool>();
    target_size_ = item["target_size"].as<std::vector<int>>();
    if (item["keep_ratio"]) {
      input_shape_ = image_shape;
    }
 }
95 96 97 98 99 100 101 102

  // Compute best resize scale for x-dimension, y-dimension
  std::pair<float, float> GenerateScale(const cv::Mat& im);

  virtual void Run(cv::Mat* im, ImageBlob* data);

 private:
  int interp_;
103 104 105
  bool keep_ratio_;
  std::vector<int> target_size_;
  std::vector<int> input_shape_;
106 107 108 109 110
};

// Models with FPN need input shape % stride == 0
class PadStride : public PreprocessOp {
 public:
111
  virtual void Init(const YAML::Node& item, const std::vector<int> image_shape) {
112 113 114 115 116 117 118 119 120 121 122
    stride_ = item["stride"].as<int>();
  }

  virtual void Run(cv::Mat* im, ImageBlob* data);

 private:
  int stride_;
};

class Preprocessor {
 public:
123 124 125
  void Init(const YAML::Node& config_node, const std::vector<int> image_shape) {
    // initialize image info at first
    ops_["InitInfo"] = std::make_shared<InitInfo>();
126 127
    for (const auto& item : config_node) {
      auto op_name = item["type"].as<std::string>();
128

129
      ops_[op_name] = CreateOp(op_name);
130
      ops_[op_name]->Init(item, image_shape);
131 132 133 134
    }
  }

  std::shared_ptr<PreprocessOp> CreateOp(const std::string& name) {
135
    if (name == "ResizeOp") {
136
      return std::make_shared<Resize>();
137
    } else if (name == "PermuteOp") {
138
      return std::make_shared<Permute>();
139
    } else if (name == "NormalizeImageOp") {
140
      return std::make_shared<Normalize>();
141
    } else if (name == "PadBatchOp") {
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
      return std::make_shared<PadStride>();
    }
    return nullptr;
  }

  void Run(cv::Mat* im, ImageBlob* data);

 public:
  static const std::vector<std::string> RUN_ORDER;

 private:
  std::unordered_map<std::string, std::shared_ptr<PreprocessOp>> ops_;
};

}  // namespace PaddleDetection
157