27#ifndef TMVA_CNN_CONVLAYER
28#define TMVA_CNN_CONVLAYER
42template <
typename Architecture_t>
45 using Matrix_t =
typename Architecture_t::Matrix_t;
46 using Scalar_t =
typename Architecture_t::Scalar_t;
50 size_t calculateDimension(
size_t imgDim,
size_t fltDim,
size_t padding,
size_t stride);
56 size_t calculateNLocalViews(
size_t inputHeight,
size_t filterHeight,
size_t paddingHeight,
size_t strideRows,
57 size_t inputWidth,
size_t filterWidth,
size_t paddingWidth,
size_t strideCols);
89 size_t FilterHeight,
size_t FilterWidth,
size_t StrideRows,
size_t StrideCols,
size_t PaddingHeight,
107 void Forward(std::vector<Matrix_t> &input,
bool applyDropout =
false);
113 void Backward(std::vector<Matrix_t> &gradients_backward,
const std::vector<Matrix_t> &activations_backward,
114 std::vector<Matrix_t> &inp1, std::vector<Matrix_t> &inp2);
173 TConvParams(
size_t _batchSize,
size_t _inputDepth,
size_t _inputHeight,
size_t _inputWidth,
size_t _numberFilters,
174 size_t _filterHeight,
size_t _filterWidth,
size_t _strideRows,
size_t _strideCols,
175 size_t _paddingHeight,
size_t _paddingWidth)
188template <
typename Architecture_t>
191 size_t strideRows,
size_t strideCols,
size_t paddingHeight,
size_t paddingWidth,
194 :
VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth, depth,
195 calculateDimension(inputHeight, filterHeight, paddingHeight, strideRows),
196 calculateDimension(inputWidth, filterWidth, paddingWidth, strideCols),
197 1, depth, calculateNLocalViewPixels(inputDepth, filterHeight, filterWidth),
198 1, depth, 1, batchSize, depth,
199 calculateNLocalViews(inputHeight, filterHeight, paddingHeight, strideRows,
200 inputWidth, filterWidth, paddingWidth, strideCols),
202 fFilterDepth(inputDepth), fFilterHeight(filterHeight), fFilterWidth(filterWidth), fStrideRows(strideRows),
203 fStrideCols(strideCols), fNLocalViewPixels(calculateNLocalViewPixels(inputDepth, filterHeight, filterWidth)),
204 fNLocalViews(calculateNLocalViews(inputHeight, filterHeight, paddingHeight, strideRows,
205 inputWidth, filterWidth, paddingWidth, strideCols)),
206 fDropoutProbability(dropoutProbability), fPaddingHeight(paddingHeight), fPaddingWidth(paddingWidth),
207 fDerivatives(), fF(
f), fReg(reg), fWeightDecay(
weightDecay)
214 for (
size_t i = 0; i < batchSize; i++) {
222template <
typename Architecture_t>
224 :
VGeneralLayer<Architecture_t>(layer), fFilterDepth(layer->GetFilterDepth()),
225 fFilterHeight(layer->GetFilterHeight()), fFilterWidth(layer->GetFilterWidth()),
226 fStrideRows(layer->GetStrideRows()), fStrideCols(layer->GetStrideCols()),
227 fNLocalViewPixels(layer->GetNLocalViewPixels()), fNLocalViews(layer->GetNLocalViews()),
228 fDropoutProbability(layer->GetDropoutProbability()), fPaddingHeight(layer->GetPaddingHeight()),
229 fPaddingWidth(layer->GetPaddingWidth()), fF(layer->GetActivationFunction()),
230 fReg(layer->GetRegularization()), fWeightDecay(layer->GetWeightDecay())
233 size_t outputNRows = 0;
234 size_t outputNCols = 0;
236 for (
size_t i = 0; i < outputNSlices; i++) {
245template <
typename Architecture_t>
247 :
VGeneralLayer<Architecture_t>(convLayer), fFilterDepth(convLayer.fFilterDepth),
248 fFilterHeight(convLayer.fFilterHeight), fFilterWidth(convLayer.fFilterWidth), fStrideRows(convLayer.fStrideRows),
249 fStrideCols(convLayer.fStrideCols), fNLocalViewPixels(convLayer.fNLocalViewPixels),
250 fNLocalViews(convLayer.fNLocalViews), fDropoutProbability(convLayer.fDropoutProbability),
251 fPaddingHeight(convLayer.fPaddingHeight), fPaddingWidth(convLayer.fPaddingWidth), fF(convLayer.fF),
252 fReg(convLayer.fReg), fWeightDecay(convLayer.fWeightDecay)
258 for (
size_t i = 0; i < outputNSlices; i++) {
265template <
typename Architecture_t>
271template <
typename Architecture_t>
274 TConvParams params(this->GetBatchSize(), this->GetInputDepth(), this->GetInputHeight(), this->GetInputWidth(),
275 this->GetDepth(), this->GetFilterHeight(), this->GetFilterWidth(),
276 this->GetStrideRows(), this->GetStrideCols(), this->GetPaddingHeight(), this->GetPaddingWidth());
279 Architecture_t::ConvLayerForward(this->GetOutput(), this->GetDerivatives(), input, this->GetWeightsAt(0),
280 this->GetBiasesAt(0), params, this->GetActivationFunction(),
281 this->GetForwardMatrices());
285 Matrix_t inputTr(this->GetNLocalViews(), this->GetNLocalViewPixels());
287 std::vector<int> vIndices(inputTr.GetNrows() * inputTr.GetNcols() );
289 Architecture_t::Im2colIndices(vIndices, input[0], this->GetNLocalViews(), this->GetInputHeight(), this->GetInputWidth(), this->GetFilterHeight(),
290 this->GetFilterWidth(), this->GetStrideRows(), this->GetStrideCols(),
291 this->GetPaddingHeight(), this->GetPaddingWidth());
293 for (
size_t i = 0; i < this->GetBatchSize(); i++) {
295 if (applyDropout && (this->GetDropoutProbability() != 1.0)) {
296 Architecture_t::Dropout(input[i], this->GetDropoutProbability());
304 Architecture_t::Im2colFast(inputTr, input[i], vIndices);
323 Architecture_t::MultiplyTranspose(this->GetOutputAt(i), this->GetWeightsAt(0), inputTr);
324 Architecture_t::AddConvBiases(this->GetOutputAt(i), this->GetBiasesAt(0));
326 evaluateDerivative<Architecture_t>(this->GetDerivativesAt(i), fF, this->GetOutputAt(i));
327 evaluate<Architecture_t>(this->GetOutputAt(i), fF);
333template <
typename Architecture_t>
335 const std::vector<Matrix_t> &activations_backward,
336 std::vector<Matrix_t> & , std::vector<Matrix_t> &
339 Architecture_t::ConvLayerBackward(
340 gradients_backward, this->GetWeightGradientsAt(0), this->GetBiasGradientsAt(0), this->GetDerivatives(),
341 this->GetActivationGradients(), this->GetWeightsAt(0), activations_backward, this->GetBatchSize(),
342 this->GetInputHeight(), this->GetInputWidth(), this->GetDepth(), this->GetHeight(), this->GetWidth(),
343 this->GetFilterDepth(), this->GetFilterHeight(), this->GetFilterWidth(), this->GetNLocalViews());
345 addRegularizationGradients<Architecture_t>(this->GetWeightGradientsAt(0), this->GetWeightsAt(0),
346 this->GetWeightDecay(), this->GetRegularization());
350template <
typename Architecture_t>
353 std::cout <<
" CONV LAYER: \t";
354 std::cout <<
"( W = " << this->GetWidth() <<
" , ";
355 std::cout <<
" H = " << this->GetHeight() <<
" , ";
356 std::cout <<
" D = " << this->GetDepth() <<
" ) ";
358 std::cout <<
"\t Filter ( W = " << this->GetFilterWidth() <<
" , ";
359 std::cout <<
" H = " << this->GetFilterHeight() <<
" ) ";
361 if (this->GetOutput().size() > 0) {
362 std::cout <<
"\tOutput = ( " << this->GetOutput().size() <<
" , " << this->GetOutput()[0].GetNrows() <<
" , " << this->GetOutput()[0].GetNcols() <<
" ) ";
364 std::vector<std::string> activationNames = {
"Identity",
"Relu",
"Sigmoid",
"Tanh",
"SymmRelu",
"SoftSign",
"Gauss" };
365 std::cout <<
"\t Activation Function = ";
366 std::cout << activationNames[ static_cast<int>(fF) ] << std::endl;
370template <
typename Architecture_t>
384 int activationFunction =
static_cast<int>(
this -> GetActivationFunction());
389 this->WriteMatrixToXML(layerxml,
"Weights",
this -> GetWeightsAt(0));
390 this->WriteMatrixToXML(layerxml,
"Biases",
this -> GetBiasesAt(0));
395template <
typename Architecture_t>
400 this->ReadMatrixXML(parent,
"Weights",
this -> GetWeightsAt(0));
401 this->ReadMatrixXML(parent,
"Biases",
this -> GetBiasesAt(0));
404template <
typename Architecture_t>
407 size_t temp = imgDim - fltDim + 2 * padding;
408 if (temp % stride || temp + stride <= 0) {
409 Fatal(
"calculateDimension",
"Not compatible hyper parameters for layer - (imageDim, filterDim, padding, stride) "
410 "%zu, %zu, %zu, %zu", imgDim, fltDim, padding, stride);
412 return temp / stride + 1;
415template <
typename Architecture_t>
417 size_t strideRows,
size_t inputWidth,
size_t filterWidth,
418 size_t paddingWidth,
size_t strideCols)
420 int height = calculateDimension(inputHeight, filterHeight, paddingHeight, strideRows);
421 int width = calculateDimension(inputWidth, filterWidth, paddingWidth, strideCols);
423 return height *
width;
include TDocParser_001 C image html pict1_TDocParser_001 png width
void Fatal(const char *location, const char *msgfmt,...)
size_t fNLocalViews
The number of local views in one image.
void Forward(std::vector< Matrix_t > &input, bool applyDropout=false)
Computes activation of the layer for the given input.
size_t calculateNLocalViews(size_t inputHeight, size_t filterHeight, size_t paddingHeight, size_t strideRows, size_t inputWidth, size_t filterWidth, size_t paddingWidth, size_t strideCols)
virtual void ReadWeightsFromXML(void *parent)
Read the information and the weights about the layer from XML node.
size_t GetNLocalViewPixels() const
const std::vector< Matrix_t > & GetDerivatives() const
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)
Compute weight, bias and activation gradients.
size_t GetStrideRows() const
std::vector< Matrix_t > fDerivatives
First fDerivatives of the activations of this layer.
size_t fPaddingWidth
The number of zero layers left and right of the input.
Scalar_t GetWeightDecay() const
Scalar_t fWeightDecay
The weight decay.
size_t fFilterWidth
The width of the filter.
std::vector< int > fBackwardIndices
Vector of indices used for a fast Im2Col in backward pass.
size_t calculateNLocalViewPixels(size_t depth, size_t height, size_t width)
size_t GetFilterWidth() const
TConvLayer(size_t BatchSize, size_t InputDepth, size_t InputHeight, size_t InputWidth, size_t Depth, EInitialization Init, size_t FilterHeight, size_t FilterWidth, size_t StrideRows, size_t StrideCols, size_t PaddingHeight, size_t PaddingWidth, Scalar_t DropoutProbability, EActivationFunction f, ERegularization Reg, Scalar_t WeightDecay)
Constructor.
size_t calculateDimension(size_t imgDim, size_t fltDim, size_t padding, size_t stride)
std::vector< Matrix_t > fForwardMatrices
Vector of matrices used for speeding-up the forward pass.
size_t fFilterDepth
The depth of the filter.
size_t GetPaddingWidth() const
size_t fNLocalViewPixels
The number of pixels in one local image view.
const Matrix_t & GetDerivativesAt(size_t i) const
Scalar_t fDropoutProbability
Probability that an input is active.
size_t fStrideCols
The number of column pixels to slid the filter each step.
const std::vector< Matrix_t > & GetForwardMatrices() const
size_t fStrideRows
The number of row pixels to slid the filter each step.
ERegularization fReg
The regularization method.
typename Architecture_t::Matrix_t Matrix_t
EActivationFunction GetActivationFunction() const
std::vector< Matrix_t > & GetForwardMatrices()
size_t fPaddingHeight
The number of zero layers added top and bottom of the input.
size_t fFilterHeight
The height of the filter.
size_t GetStrideCols() const
virtual void AddWeightsXMLTo(void *parent)
Writes the information and the weights about the layer in an XML node.
EActivationFunction fF
Activation function of the layer.
size_t GetFilterDepth() const
Getters.
size_t GetPaddingHeight() const
size_t GetFilterHeight() const
size_t GetNLocalViews() const
Matrix_t & GetDerivativesAt(size_t i)
Scalar_t GetDropoutProbability() const
typename Architecture_t::Scalar_t Scalar_t
void Print() const
Prints the info about the layer.
ERegularization GetRegularization() const
std::vector< Matrix_t > & GetDerivatives()
Generic General Layer class.
static TString Itoa(Int_t value, Int_t base)
Converts an Int_t to a TString with respect to the base specified (2-36).
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
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=0)
create new child element for parent node
void Init(TClassEdit::TInterpreterLookupHelper *helper)
struct TMVA::DNN::CNN::TConvParams TConvParams
double weightDecay(double error, ItWeight itWeight, ItWeight itWeightEnd, double factorWeightDecay, EnumRegularization eRegularization)
compute the weight decay for regularization (L1 or L2)
ERegularization
Enum representing the regularization type applied for a given layer.
EActivationFunction
Enum that represents layer activation functions.
UInt_t Depth(const Node< T > *node)
Abstract ClassifierFactory template that handles arbitrary types.
size_t strideRows
The number of row pixels to slid the filter each step.
size_t filterHeight
The height of the filter.
size_t inputHeight
The height of the previous layer or input.
size_t batchSize
Batch size used for training and evaluation.
size_t paddingWidth
The number of zero layers left and right of the input.
size_t filterWidth
The width of the filter.
size_t paddingHeight
The number of zero layers added top and bottom of the input.
size_t inputWidth
The width of the previous layer or input.
TConvParams(size_t _batchSize, size_t _inputDepth, size_t _inputHeight, size_t _inputWidth, size_t _numberFilters, size_t _filterHeight, size_t _filterWidth, size_t _strideRows, size_t _strideCols, size_t _paddingHeight, size_t _paddingWidth)
size_t numberFilters
The number of the filters, which is equal to the output's depth.
size_t inputDepth
The depth of the previous layer or input.
size_t strideCols
The number of column pixels to slid the filter each step.