Logo ROOT   6.14/05
Reference Guide
MaxPoolLayer.h
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Vladimir Ilievski
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : TMaxPoolLayer *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Max Pool Deep Neural Network Layer *
12  * *
13  * Authors (alphabetical): *
14  * Vladimir Ilievski <ilievski.vladimir@live.com> - CERN, Switzerland *
15  * *
16  * Copyright (c) 2005-2015: *
17  * CERN, Switzerland *
18  * U. of Victoria, Canada *
19  * MPI-K Heidelberg, Germany *
20  * U. of Bonn, Germany *
21  * *
22  * Redistribution and use in source and binary forms, with or without *
23  * modification, are permitted according to the terms listed in LICENSE *
24  * (http://tmva.sourceforge.net/LICENSE) *
25  **********************************************************************************/
26 
27 #ifndef MAXPOOLLAYER_H_
28 #define MAXPOOLLAYER_H_
29 
30 #include "TMatrix.h"
31 
32 #include "TMVA/DNN/GeneralLayer.h"
33 #include "TMVA/DNN/Functions.h"
34 
35 #include <iostream>
36 
37 namespace TMVA {
38 namespace DNN {
39 namespace CNN {
40 
41 /** \class TMaxPoolLayer
42 
43  Generic Max Pooling Layer class.
44 
45  This generic Max Pooling Layer Class represents a pooling layer of
46  a CNN. It inherits all of the properties of the generic virtual base class
47  VGeneralLayer. In addition to that, it contains a matrix of winning units.
48 
49  The height and width of the weights and biases is set to 0, since this
50  layer does not contain any weights.
51 
52  */
53 template <typename Architecture_t>
54 class TMaxPoolLayer : public VGeneralLayer<Architecture_t> {
55 public:
56  using Matrix_t = typename Architecture_t::Matrix_t;
57  using Scalar_t = typename Architecture_t::Scalar_t;
58 
59 private:
60  std::vector<Matrix_t> indexMatrix; ///< Matrix of indices for the backward pass.
61 
62  size_t fFrameHeight; ///< The height of the frame.
63  size_t fFrameWidth; ///< The width of the frame.
64 
65  size_t fStrideRows; ///< The number of row pixels to slid the filter each step.
66  size_t fStrideCols; ///< The number of column pixels to slid the filter each step.
67 
68  size_t fNLocalViewPixels; ///< The number of pixels in one local image view.
69  size_t fNLocalViews; ///< The number of local views in one image.
70 
71  Scalar_t fDropoutProbability; ///< Probability that an input is active.
72 
73 public:
74  /*! Constructor. */
75  TMaxPoolLayer(size_t BatchSize, size_t InputDepth, size_t InputHeight, size_t InputWidth, size_t Height,
76  size_t Width, size_t OutputNSlices, size_t OutputNRows, size_t OutputNCols, size_t FrameHeight,
77  size_t FrameWidth, size_t StrideRows, size_t StrideCols, Scalar_t DropoutProbability);
78 
79  /*! Copy the max pooling layer provided as a pointer */
81 
82  /*! Copy constructor. */
84 
85  /*! Destructor. */
87 
88  /*! Computes activation of the layer for the given input. The input
89  * must be in 3D tensor form with the different matrices corresponding to
90  * different events in the batch. It spatially downsamples the input
91  * matrices. */
92  void Forward(std::vector<Matrix_t> &input, bool applyDropout = false);
93 
94  /*! Depending on the winning units determined during the Forward pass,
95  * it only forwards the derivatives to the right units in the previous
96  * layer. Must only be called directly at the corresponding call
97  * to Forward(...). */
98  void Backward(std::vector<Matrix_t> &gradients_backward, const std::vector<Matrix_t> &activations_backward,
99  std::vector<Matrix_t> &inp1, std::vector<Matrix_t> &inp2);
100 
101  /*! Writes the information and the weights about the layer in an XML node. */
102  virtual void AddWeightsXMLTo(void *parent);
103 
104  /*! Read the information and the weights about the layer from XML node. */
105  virtual void ReadWeightsFromXML(void *parent);
106 
107 
108  /*! Prints the info about the layer. */
109  void Print() const;
110 
111  /*! Getters */
112  const std::vector<Matrix_t> &GetIndexMatrix() const { return indexMatrix; }
113  std::vector<Matrix_t> &GetIndexMatrix() { return indexMatrix; }
114 
115  size_t GetFrameHeight() const { return fFrameHeight; }
116  size_t GetFrameWidth() const { return fFrameWidth; }
117 
118  size_t GetStrideRows() const { return fStrideRows; }
119  size_t GetStrideCols() const { return fStrideCols; }
120 
121  size_t GetNLocalViewPixels() const { return fNLocalViewPixels; }
122  size_t GetNLocalViews() const { return fNLocalViews; }
123 
125 };
126 
127 //______________________________________________________________________________
128 template <typename Architecture_t>
129 TMaxPoolLayer<Architecture_t>::TMaxPoolLayer(size_t batchSize, size_t inputDepth, size_t inputHeight, size_t inputWidth,
130  size_t height, size_t width, size_t outputNSlices, size_t outputNRows,
131  size_t outputNCols, size_t frameHeight, size_t frameWidth,
132  size_t strideRows, size_t strideCols, Scalar_t dropoutProbability)
133  : VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth, inputDepth, height, width, 0, 0, 0,
134  0, 0, 0, outputNSlices, outputNRows, outputNCols, EInitialization::kZero),
135  indexMatrix(), fFrameHeight(frameHeight), fFrameWidth(frameWidth), fStrideRows(strideRows),
136  fStrideCols(strideCols), fNLocalViewPixels(inputDepth * frameHeight * frameWidth), fNLocalViews(height * width),
137  fDropoutProbability(dropoutProbability)
138 {
139  for (size_t i = 0; i < this->GetBatchSize(); i++) {
140  indexMatrix.emplace_back(this->GetDepth(), this->GetNLocalViews());
141  }
142 }
143 
144 //______________________________________________________________________________
145 template <typename Architecture_t>
147  : VGeneralLayer<Architecture_t>(layer), indexMatrix(), fFrameHeight(layer->GetFrameHeight()),
151 {
152  for (size_t i = 0; i < layer->GetBatchSize(); i++) {
153  indexMatrix.emplace_back(layer->GetDepth(), layer->GetNLocalViews());
154  }
155 }
156 
157 //______________________________________________________________________________
158 template <typename Architecture_t>
160  : VGeneralLayer<Architecture_t>(layer), indexMatrix(), fFrameHeight(layer.fFrameHeight),
164 {
165  for (size_t i = 0; i < layer.fBatchSize; i++) {
166  indexMatrix.emplace_back(layer.fDepth, layer.fNLocalViews);
167  }
168 }
169 
170 //______________________________________________________________________________
171 template <typename Architecture_t>
173 {
174 }
175 
176 //______________________________________________________________________________
177 template <typename Architecture_t>
178 auto TMaxPoolLayer<Architecture_t>::Forward(std::vector<Matrix_t> &input, bool applyDropout) -> void
179 {
180  for (size_t i = 0; i < this->GetBatchSize(); i++) {
181 
182  if (applyDropout && (this->GetDropoutProbability() != 1.0)) {
183  Architecture_t::Dropout(input[i], this->GetDropoutProbability());
184  }
185 
186  Architecture_t::Downsample(this->GetOutputAt(i), indexMatrix[i], input[i], this->GetInputHeight(),
187  this->GetInputWidth(), this->GetFrameHeight(), this->GetFrameWidth(),
188  this->GetStrideRows(), this->GetStrideCols());
189  }
190 }
191 
192 //______________________________________________________________________________
193 template <typename Architecture_t>
194 auto TMaxPoolLayer<Architecture_t>::Backward(std::vector<Matrix_t> &gradients_backward,
195  const std::vector<Matrix_t> & /*activations_backward*/,
196  std::vector<Matrix_t> & /*inp1*/, std::vector<Matrix_t> &
197  /*inp2*/) -> void
198 {
199  Architecture_t::MaxPoolLayerBackward(gradients_backward, this->GetActivationGradients(), indexMatrix,
200  this->GetBatchSize(), this->GetDepth(), this->GetNLocalViews());
201 }
202 
203 //______________________________________________________________________________
204 template <typename Architecture_t>
206 {
207  std::cout << " POOL Layer: \t";
208  std::cout << "( W = " << this->GetWidth() << " , ";
209  std::cout << " H = " << this->GetHeight() << " , ";
210  std::cout << " D = " << this->GetDepth() << " ) ";
211 
212  std::cout << "\t Frame ( W = " << this->GetFrameWidth() << " , ";
213  std::cout << " H = " << this->GetFrameHeight() << " ) ";
214 
215  if (this->GetOutput().size() > 0) {
216  std::cout << "\tOutput = ( " << this->GetOutput().size() << " , " << this->GetOutput()[0].GetNrows() << " , " << this->GetOutput()[0].GetNcols() << " ) ";
217  }
218  std::cout << std::endl;
219 }
220 
221 //______________________________________________________________________________
222 template <typename Architecture_t>
224 {
225  auto layerxml = gTools().xmlengine().NewChild(parent, 0, "MaxPoolLayer");
226 
227  // write maxpool layer info
228  gTools().xmlengine().NewAttr(layerxml, 0, "FrameHeight", gTools().StringFromInt(this->GetFrameHeight()));
229  gTools().xmlengine().NewAttr(layerxml, 0, "FrameWidth", gTools().StringFromInt(this->GetFrameWidth()));
230  gTools().xmlengine().NewAttr(layerxml, 0, "StrideRows", gTools().StringFromInt(this->GetStrideRows()));
231  gTools().xmlengine().NewAttr(layerxml, 0, "StrideCols", gTools().StringFromInt(this->GetStrideCols()));
232 
233 }
234 
235 //______________________________________________________________________________
236 template <typename Architecture_t>
238 {
239  // all info is read before - nothing to do
240 }
241 
242 } // namespace CNN
243 } // namespace DNN
244 } // namespace TMVA
245 
246 #endif
size_t GetDepth() const
Definition: GeneralLayer.h:144
size_t GetInputWidth() const
Definition: GeneralLayer.h:143
Generic General Layer class.
Definition: GeneralLayer.h:45
size_t fDepth
The depth of the layer.
Definition: GeneralLayer.h:56
TXMLEngine & xmlengine()
Definition: Tools.h:270
size_t fStrideRows
The number of row pixels to slid the filter each step.
Definition: MaxPoolLayer.h:65
image html pict1_TGaxis_012 png width
Define new text attributes for the label number "labNum".
Definition: TGaxis.cxx:2551
Generic Max Pooling Layer class.
Definition: MaxPoolLayer.h:54
size_t fNLocalViews
The number of local views in one image.
Definition: MaxPoolLayer.h:69
size_t fStrideCols
The number of column pixels to slid the filter each step.
Definition: MaxPoolLayer.h:66
EInitialization
Definition: Functions.h:70
Matrix_t & GetOutputAt(size_t i)
Definition: GeneralLayer.h:179
void Backward(std::vector< Matrix_t > &gradients_backward, const std::vector< Matrix_t > &activations_backward, std::vector< Matrix_t > &inp1, std::vector< Matrix_t > &inp2)
Depending on the winning units determined during the Forward pass, it only forwards the derivatives t...
Definition: MaxPoolLayer.h:194
size_t GetBatchSize() const
Getters.
Definition: GeneralLayer.h:140
size_t GetHeight() const
Definition: GeneralLayer.h:145
const std::vector< Matrix_t > & GetActivationGradients() const
Definition: GeneralLayer.h:176
virtual void ReadWeightsFromXML(void *parent)
Read the information and the weights about the layer from XML node.
Definition: MaxPoolLayer.h:237
const std::vector< Matrix_t > & GetIndexMatrix() const
Getters.
Definition: MaxPoolLayer.h:112
void Print() const
Prints the info about the layer.
Definition: MaxPoolLayer.h:205
size_t GetNLocalViewPixels() const
Definition: MaxPoolLayer.h:121
size_t fFrameHeight
The height of the frame.
Definition: MaxPoolLayer.h:62
typename Architecture_t::Scalar_t Scalar_t
Definition: MaxPoolLayer.h:57
virtual void AddWeightsXMLTo(void *parent)
Writes the information and the weights about the layer in an XML node.
Definition: MaxPoolLayer.h:223
size_t GetInputHeight() const
Definition: GeneralLayer.h:142
TMaxPoolLayer(size_t BatchSize, size_t InputDepth, size_t InputHeight, size_t InputWidth, size_t Height, size_t Width, size_t OutputNSlices, size_t OutputNRows, size_t OutputNCols, size_t FrameHeight, size_t FrameWidth, size_t StrideRows, size_t StrideCols, Scalar_t DropoutProbability)
Constructor.
Definition: MaxPoolLayer.h:129
Scalar_t fDropoutProbability
Probability that an input is active.
Definition: MaxPoolLayer.h:71
Tools & gTools()
size_t fNLocalViewPixels
The number of pixels in one local image view.
Definition: MaxPoolLayer.h:68
std::vector< Matrix_t > indexMatrix
Matrix of indices for the backward pass.
Definition: MaxPoolLayer.h:60
XMLAttrPointer_t NewAttr(XMLNodePointer_t xmlnode, XMLNsPointer_t, const char *name, const char *value)
creates new attribute for xmlnode, namespaces are not supported for attributes
Definition: TXMLEngine.cxx:578
size_t fBatchSize
Batch size used for training and evaluation.
Definition: GeneralLayer.h:50
typename Architecture_t::Matrix_t Matrix_t
Definition: MaxPoolLayer.h:56
Abstract ClassifierFactory template that handles arbitrary types.
std::vector< Matrix_t > & GetIndexMatrix()
Definition: MaxPoolLayer.h:113
void Forward(std::vector< Matrix_t > &input, bool applyDropout=false)
Computes activation of the layer for the given input.
Definition: MaxPoolLayer.h:178
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=0)
create new child element for parent node
Definition: TXMLEngine.cxx:707
const std::vector< Matrix_t > & GetOutput() const
Definition: GeneralLayer.h:173
size_t fFrameWidth
The width of the frame.
Definition: MaxPoolLayer.h:63
size_t GetWidth() const
Definition: GeneralLayer.h:146
Scalar_t GetDropoutProbability() const
Definition: MaxPoolLayer.h:124