27#ifndef TMVA_CNN_CONVLAYER
28#define TMVA_CNN_CONVLAYER
62 TConvParams(
size_t _batchSize,
size_t _inputDepth,
size_t _inputHeight,
size_t _inputWidth,
size_t _numberFilters,
63 size_t _filterHeight,
size_t _filterWidth,
size_t _strideRows,
size_t _strideCols,
64 size_t _paddingHeight,
size_t _paddingWidth)
74template <
typename Architecture_t>
77 using Tensor_t =
typename Architecture_t::Tensor_t;
78 using Matrix_t =
typename Architecture_t::Matrix_t;
79 using Scalar_t =
typename Architecture_t::Scalar_t;
94 static size_t calculateDimension(
size_t imgDim,
size_t fltDim,
size_t padding,
size_t stride);
100 static size_t calculateNLocalViews(
size_t inputHeight,
size_t filterHeight,
size_t paddingHeight,
size_t strideRows,
101 size_t inputWidth,
size_t filterWidth,
size_t paddingWidth,
size_t strideCols);
140 TConvLayer(
size_t BatchSize,
size_t InputDepth,
size_t InputHeight,
size_t InputWidth,
size_t Depth,
EInitialization Init,
141 size_t FilterHeight,
size_t FilterWidth,
size_t StrideRows,
size_t StrideCols,
size_t PaddingHeight,
221template <
typename Architecture_t>
223 size_t depth,
EInitialization init,
size_t filterHeight,
size_t filterWidth,
224 size_t strideRows,
size_t strideCols,
size_t paddingHeight,
size_t paddingWidth,
227 :
VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth, depth,
228 calculateDimension(inputHeight, filterHeight, paddingHeight, strideRows),
229 calculateDimension(inputWidth, filterWidth, paddingWidth, strideCols),
230 1, depth, calculateNLocalViewPixels(inputDepth, filterHeight, filterWidth),
231 1, depth, 1, batchSize, depth,
232 calculateNLocalViews(inputHeight, filterHeight, paddingHeight, strideRows,
233 inputWidth, filterWidth, paddingWidth, strideCols),
235 fFilterDepth(inputDepth), fFilterHeight(filterHeight), fFilterWidth(filterWidth), fStrideRows(strideRows),
236 fStrideCols(strideCols), fNLocalViewPixels(calculateNLocalViewPixels(inputDepth, filterHeight, filterWidth)),
237 fNLocalViews(calculateNLocalViews(inputHeight, filterHeight, paddingHeight, strideRows,
238 inputWidth, filterWidth, paddingWidth, strideCols)),
239 fDropoutProbability(dropoutProbability), fPaddingHeight(paddingHeight), fPaddingWidth(paddingWidth),
240 fInputActivation(), fF(
f), fReg(reg), fWeightDecay(
weightDecay)
256template <
typename Architecture_t>
258 :
VGeneralLayer<Architecture_t>(layer), fFilterDepth(layer->GetFilterDepth()),
259 fFilterHeight(layer->GetFilterHeight()), fFilterWidth(layer->GetFilterWidth()),
260 fStrideRows(layer->GetStrideRows()), fStrideCols(layer->GetStrideCols()),
261 fNLocalViewPixels(layer->GetNLocalViewPixels()), fNLocalViews(layer->GetNLocalViews()),
262 fDropoutProbability(layer->GetDropoutProbability()), fPaddingHeight(layer->GetPaddingHeight()),
263 fPaddingWidth(layer->GetPaddingWidth()),
264 fInputActivation( layer->GetInputActivation().GetShape() ),
265 fF(layer->GetActivationFunction()),
266 fReg(layer->GetRegularization()), fWeightDecay(layer->GetWeightDecay()),
267 fForwardTensor( layer->GetForwardMatrices().GetShape() )
275template <
typename Architecture_t>
277 :
VGeneralLayer<Architecture_t>(convLayer), fFilterDepth(convLayer.fFilterDepth),
278 fFilterHeight(convLayer.fFilterHeight), fFilterWidth(convLayer.fFilterWidth), fStrideRows(convLayer.fStrideRows),
279 fStrideCols(convLayer.fStrideCols), fNLocalViewPixels(convLayer.fNLocalViewPixels),
280 fNLocalViews(convLayer.fNLocalViews), fDropoutProbability(convLayer.fDropoutProbability),
281 fPaddingHeight(convLayer.fPaddingHeight), fPaddingWidth(convLayer.fPaddingWidth),
282 fInputActivation( convLayer.GetInputActivation().GetShape() ),
284 fReg(convLayer.fReg), fWeightDecay(convLayer.fWeightDecay),
285 fForwardTensor( convLayer.GetForwardMatrices().GetShape() )
293template <
typename Architecture_t>
298 ReleaseDescriptors();
310template <
typename Architecture_t>
313 TConvParams params(this->GetBatchSize(), this->GetInputDepth(), this->GetInputHeight(), this->GetInputWidth(),
314 this->GetDepth(), this->GetFilterHeight(), this->GetFilterWidth(),
315 this->GetStrideRows(), this->GetStrideCols(), this->GetPaddingHeight(), this->GetPaddingWidth());
318 Architecture_t::ConvLayerForward(this->GetOutput(), this->GetInputActivation(), input, this->GetWeightsAt(0),
319 this->GetBiasesAt(0), params, this->GetActivationFunction(),
325template <
typename Architecture_t>
327 const Tensor_t &activations_backward) ->
void
331 Architecture_t::ConvLayerBackward(
332 gradients_backward, this->GetWeightGradientsAt(0), this->GetBiasGradientsAt(0), this->GetInputActivation(),
333 this->GetActivationGradients(), this->GetWeightsAt(0), activations_backward, this->GetOutput(),
334 this->GetActivationFunction(),
337 this->GetBatchSize(), this->GetInputHeight(), this->GetInputWidth(), this->GetDepth(),
338 this->GetHeight(), this->GetWidth(), this->GetFilterDepth(), this->GetFilterHeight(),
339 this->GetFilterWidth(), this->GetNLocalViews());
341 addRegularizationGradients<Architecture_t>(this->GetWeightGradientsAt(0), this->GetWeightsAt(0),
342 this->GetWeightDecay(), this->GetRegularization());
346template <
typename Architecture_t>
349 std::cout <<
" CONV LAYER: \t";
350 std::cout <<
"( W = " << this->GetWidth() <<
" , ";
351 std::cout <<
" H = " << this->GetHeight() <<
" , ";
352 std::cout <<
" D = " << this->GetDepth() <<
" ) ";
354 std::cout <<
"\t Filter ( W = " << this->GetFilterWidth() <<
" , ";
355 std::cout <<
" H = " << this->GetFilterHeight() <<
" ) ";
357 if (this->GetOutput().GetSize() > 0) {
358 std::cout <<
"\tOutput = ( " << this->GetOutput().GetFirstSize() <<
" , "
359 << this->GetOutput().GetCSize() <<
" , " << this->GetOutput().GetHSize() <<
" , " << this->GetOutput().GetWSize()
362 std::vector<std::string> activationNames = {
"Identity",
"Relu",
"Sigmoid",
"Tanh",
"SymmRelu",
"SoftSign",
"Gauss" };
363 std::cout <<
"\t Activation Function = ";
364 std::cout << activationNames[ static_cast<int>(fF) ] << std::endl;
368template <
typename Architecture_t>
382 int activationFunction =
static_cast<int>(
this -> GetActivationFunction());
387 this->WriteMatrixToXML(layerxml,
"Weights",
this -> GetWeightsAt(0));
388 this->WriteMatrixToXML(layerxml,
"Biases",
this -> GetBiasesAt(0));
392template <
typename Architecture_t>
397 this->ReadMatrixXML(parent,
"Weights",
this -> GetWeightsAt(0));
398 this->ReadMatrixXML(parent,
"Biases",
this -> GetBiasesAt(0));
401template <
typename Architecture_t>
404 size_t temp = imgDim - fltDim + 2 * padding;
405 if (temp % stride || temp + stride <= 0) {
406 Fatal(
"calculateDimension",
"Not compatible hyper parameters for layer - (imageDim, filterDim, padding, stride) "
407 "%zu, %zu, %zu, %zu", imgDim, fltDim, padding, stride);
409 return temp / stride + 1;
412template <
typename Architecture_t>
414 size_t strideRows,
size_t inputWidth,
size_t filterWidth,
415 size_t paddingWidth,
size_t strideCols)
417 int height = calculateDimension(inputHeight, filterHeight, paddingHeight, strideRows);
418 int width = calculateDimension(inputWidth, filterWidth, paddingWidth, strideCols);
420 return height *
width;
424template <
typename Architecture_t>
426 Architecture_t::InitializeConvDescriptors(fDescriptors,
this);
429template <
typename Architecture_t>
431 Architecture_t::ReleaseConvDescriptors(fDescriptors);
435template <
typename Architecture_t>
437 TConvParams params(this->GetBatchSize(), this->GetInputDepth(), this->GetInputHeight(), this->GetInputWidth(),
438 this->GetDepth(), this->GetFilterHeight(), this->GetFilterWidth(),
439 this->GetStrideRows(), this->GetStrideCols(), this->GetPaddingHeight(), this->GetPaddingWidth());
441 Architecture_t::InitializeConvWorkspace(fWorkspace, fDescriptors, params,
this);
444template <
typename Architecture_t>
446 Architecture_t::FreeConvWorkspace(fWorkspace);
include TDocParser_001 C image html pict1_TDocParser_001 png width
void Fatal(const char *location, const char *msgfmt,...)
Use this function in case of a fatal error. It will abort the program.
size_t fNLocalViews
The number of local views in one image.
static 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.
const Tensor_t & GetForwardMatrices() const
size_t GetNLocalViewPixels() const
Tensor_t fInputActivation
First output of this layer after conv, before activation.
const TDescriptors * GetDescriptors() const
typename Architecture_t::ActivationDescriptor_t HelperDescriptor_t
size_t GetStrideRows() const
void Backward(Tensor_t &gradients_backward, const Tensor_t &activations_backward)
Compute weight, bias and activation gradients.
typename Architecture_t::Tensor_t Tensor_t
size_t fPaddingWidth
The number of zero layers left and right of the input.
Scalar_t GetWeightDecay() const
Scalar_t fWeightDecay
The weight decay.
Tensor_t & GetInputActivation()
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 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.
static size_t calculateDimension(size_t imgDim, size_t fltDim, size_t padding, size_t stride)
Tensor_t fForwardTensor
Cache tensor used for speeding-up the forward pass.
TDescriptors * GetDescriptors()
void ReleaseDescriptors()
typename Architecture_t::ConvolutionDescriptor_t LayerDescriptor_t
size_t fFilterDepth
The depth of the filter.
size_t GetPaddingWidth() const
size_t fNLocalViewPixels
The number of pixels in one local image view.
typename Architecture_t::AlgorithmForward_t AlgorithmForward_t
Scalar_t fDropoutProbability
Probability that an input is active.
static size_t calculateNLocalViewPixels(size_t depth, size_t height, size_t width)
size_t fStrideCols
The number of column pixels to slid the filter each step.
typename Architecture_t::AlgorithmDataType_t AlgorithmDataType_t
virtual ~TConvLayer()
Destructor.
Tensor_t & GetForwardMatrices()
typename Architecture_t::AlgorithmBackward_t AlgorithmBackward_t
size_t fStrideRows
The number of row pixels to slid the filter each step.
ERegularization fReg
The regularization method.
const TWorkspace * GetWorkspace() const
const Matrix_t & GetInputActivationAt(size_t i) const
typename Architecture_t::Matrix_t Matrix_t
EActivationFunction GetActivationFunction() const
TDescriptors * fDescriptors
Keeps the convolution, activations and filter descriptors.
size_t fPaddingHeight
The number of zero layers added top and bottom of the input.
Matrix_t & GetInputActivationAt(size_t i)
TWorkspace * GetWorkspace()
size_t fFilterHeight
The height of the filter.
size_t GetStrideCols() const
typename Architecture_t::FilterDescriptor_t WeightsDescriptor_t
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
const Tensor_t & GetInputActivation() const
void InitializeWorkspace()
typename Architecture_t::ReduceTensorDescriptor_t ReduceTensorDescriptor_t
void Forward(Tensor_t &input, bool applyDropout=false)
Computes activation of the layer for the given input.
size_t GetNLocalViews() const
typename Architecture_t::AlgorithmHelper_t AlgorithmHelper_t
Scalar_t GetDropoutProbability() const
typename Architecture_t::Scalar_t Scalar_t
void Print() const
Prints the info about the layer.
ERegularization GetRegularization() const
void InitializeDescriptors()
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).
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=nullptr)
create new child element for parent node
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
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.
create variable transformations
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.