Apache Singa
A General Distributed Deep Learning Library
transformer.h
1 
19 #ifndef SINGA_IO_TRANSFORMER_H_
20 #define SINGA_IO_TRANSFORMER_H_
21 
22 #include <vector>
23 #include <string>
24 #include "singa/core/tensor.h"
25 #include "singa/proto/io.pb.h"
26 #include "singa/proto/model.pb.h"
27 
28 namespace singa {
29 
31 class Transformer {
32  public:
33  Transformer() {}
34  virtual ~Transformer() {}
35 
36  virtual void Setup(const TransformerConf& conf) {}
37 
38  virtual Tensor Apply(int flag, Tensor& input) = 0;
39 };
40 
42  public:
43  void Setup(const TransformerConf& conf) override {
44  featurewise_center_ = conf.featurewise_center();
45  featurewise_std_norm_ = conf.featurewise_std_norm();
46  resize_height_ = conf.resize_height();
47  resize_width_ = conf.resize_width();
48  rescale_ = conf.rescale();
49  horizontal_mirror_ = conf.horizontal_mirror();
50  image_dim_order_ = conf.image_dim_order();
51 
53  if (conf.crop_shape_size() == 2)
54  crop_shape_ = {conf.crop_shape(0), conf.crop_shape(1)};
55  }
56 
57  Tensor Apply(int flag, Tensor& input) override;
58 
59  const bool featurewise_center() const { return featurewise_center_; }
60  const bool featurewise_std_norm() const { return featurewise_std_norm_; }
61  const bool horizontal_mirror() const { return horizontal_mirror_; }
62  const int resize_height() const { return resize_height_; }
63  const int resize_width() const { return resize_width_; }
64  const float rescale() const { return rescale_; }
65  const Shape crop_shape() const { return crop_shape_; }
66  const string image_dim_order() const { return image_dim_order_; }
67 
68  private:
69  bool featurewise_center_ = false;
70  bool featurewise_std_norm_ = false;
71  bool horizontal_mirror_ = false;
72  int resize_height_ = 0;
73  int resize_width_ = 0;
74  float rescale_ = 0.f;
75  Shape crop_shape_ = {};
76  std::string image_dim_order_ = "CHW";
77 };
78 
79 #ifdef USE_OPENCV
80 Tensor resize(Tensor& input, const size_t resize_height,
81  const size_t resize_width, const string& image_dim_order);
82 #endif
83 Tensor crop(Tensor& input, const size_t crop_height,
84  const size_t crop_width, const size_t crop_h_offset,
85  const size_t crop_w_offset, const string& image_dim_order);
86 Tensor mirror(Tensor& input, const bool horizontal_mirror,
87  const bool vertical_mirror, const string& image_dim_order);
88 } // namespace singa
89 #endif // SINGA_IO_TRANSFORMER_H_
Definition: transformer.h:41
void Setup(const TransformerConf &conf) override
Definition: transformer.h:43
A Tensor instance is a multi-dimensional array resident on a Device (default device is the host CPU)...
Definition: tensor.h:56
Base apply class that does data transformations in pre-processing stage.
Definition: transformer.h:31
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48