27#ifndef TMVA_CNN_CONVLAYER
28#define TMVA_CNN_CONVLAYER
61 TConvParams(
size_t _batchSize,
size_t _inputDepth,
size_t _inputHeight,
size_t _inputWidth,
size_t _numberFilters,
62 size_t _filterHeight,
size_t _filterWidth,
size_t _strideRows,
size_t _strideCols,
63 size_t _paddingHeight,
size_t _paddingWidth)
73template <
typename Architecture_t>
76 using Tensor_t =
typename Architecture_t::Tensor_t;
77 using Matrix_t =
typename Architecture_t::Matrix_t;
78 using Scalar_t =
typename Architecture_t::Scalar_t;
93 static size_t calculateDimension(
size_t imgDim,
size_t fltDim,
size_t padding,
size_t stride);
99 static size_t calculateNLocalViews(
size_t inputHeight,
size_t filterHeight,
size_t paddingHeight,
size_t strideRows,
100 size_t inputWidth,
size_t filterWidth,
size_t paddingWidth,
size_t strideCols);
140 size_t FilterHeight,
size_t FilterWidth,
size_t StrideRows,
size_t StrideCols,
size_t PaddingHeight,
220template <
typename Architecture_t>
223 size_t strideRows,
size_t strideCols,
size_t paddingHeight,
size_t paddingWidth,
226 :
VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth, depth,
227 calculateDimension(inputHeight, filterHeight, paddingHeight, strideRows),
228 calculateDimension(inputWidth, filterWidth, paddingWidth, strideCols),
229 1, depth, calculateNLocalViewPixels(inputDepth, filterHeight, filterWidth),
230 1, depth, 1, batchSize, depth,
231 calculateNLocalViews(inputHeight, filterHeight, paddingHeight, strideRows,
232 inputWidth, filterWidth, paddingWidth, strideCols),
234 fFilterDepth(inputDepth), fFilterHeight(filterHeight), fFilterWidth(filterWidth), fStrideRows(strideRows),
235 fStrideCols(strideCols), fNLocalViewPixels(calculateNLocalViewPixels(inputDepth, filterHeight, filterWidth)),
236 fNLocalViews(calculateNLocalViews(inputHeight, filterHeight, paddingHeight, strideRows,
237 inputWidth, filterWidth, paddingWidth, strideCols)),
238 fDropoutProbability(dropoutProbability), fPaddingHeight(paddingHeight), fPaddingWidth(paddingWidth),
239 fInputActivation(), fF(
f), fReg(reg), fWeightDecay(
weightDecay)
255template <
typename Architecture_t>
257 :
VGeneralLayer<Architecture_t>(layer), fFilterDepth(layer->GetFilterDepth()),
258 fFilterHeight(layer->GetFilterHeight()), fFilterWidth(layer->GetFilterWidth()),
259 fStrideRows(layer->GetStrideRows()), fStrideCols(layer->GetStrideCols()),
260 fNLocalViewPixels(layer->GetNLocalViewPixels()), fNLocalViews(layer->GetNLocalViews()),
261 fDropoutProbability(layer->GetDropoutProbability()), fPaddingHeight(layer->GetPaddingHeight()),
262 fPaddingWidth(layer->GetPaddingWidth()),
263 fInputActivation( layer->GetInputActivation().GetShape() ),
264 fF(layer->GetActivationFunction()),
265 fReg(layer->GetRegularization()), fWeightDecay(layer->GetWeightDecay()),
266 fForwardTensor( layer->GetForwardMatrices().GetShape() )
274template <
typename Architecture_t>
276 :
VGeneralLayer<Architecture_t>(convLayer), fFilterDepth(convLayer.fFilterDepth),
277 fFilterHeight(convLayer.fFilterHeight), fFilterWidth(convLayer.fFilterWidth), fStrideRows(convLayer.fStrideRows),
278 fStrideCols(convLayer.fStrideCols), fNLocalViewPixels(convLayer.fNLocalViewPixels),
279 fNLocalViews(convLayer.fNLocalViews), fDropoutProbability(convLayer.fDropoutProbability),
280 fPaddingHeight(convLayer.fPaddingHeight), fPaddingWidth(convLayer.fPaddingWidth),
281 fInputActivation( convLayer.GetInputActivation().GetShape() ),
283 fReg(convLayer.fReg), fWeightDecay(convLayer.fWeightDecay),
284 fForwardTensor( convLayer.GetForwardMatrices().GetShape() )
292template <
typename Architecture_t>
297 ReleaseDescriptors();
309template <
typename Architecture_t>
312 TConvParams params(this->GetBatchSize(), this->GetInputDepth(), this->GetInputHeight(), this->GetInputWidth(),
313 this->GetDepth(), this->GetFilterHeight(), this->GetFilterWidth(),
314 this->GetStrideRows(), this->GetStrideCols(), this->GetPaddingHeight(), this->GetPaddingWidth());
317 Architecture_t::ConvLayerForward(this->GetOutput(), this->GetInputActivation(), input, this->GetWeightsAt(0),
318 this->GetBiasesAt(0), params, this->GetActivationFunction(),
324template <
typename Architecture_t>
326 const Tensor_t &activations_backward) ->
void
330 Architecture_t::ConvLayerBackward(
331 gradients_backward, this->GetWeightGradientsAt(0), this->GetBiasGradientsAt(0), this->GetInputActivation(),
332 this->GetActivationGradients(), this->GetWeightsAt(0), activations_backward, this->GetOutput(),
333 this->GetActivationFunction(),
336 this->GetBatchSize(), this->GetInputHeight(), this->GetInputWidth(), this->GetDepth(),
337 this->GetHeight(), this->GetWidth(), this->GetFilterDepth(), this->GetFilterHeight(),
338 this->GetFilterWidth(), this->GetNLocalViews());
340 addRegularizationGradients<Architecture_t>(this->GetWeightGradientsAt(0), this->GetWeightsAt(0),
341 this->GetWeightDecay(), this->GetRegularization());
345template <
typename Architecture_t>
348 std::cout <<
" CONV LAYER: \t";
349 std::cout <<
"( W = " << this->GetWidth() <<
" , ";
350 std::cout <<
" H = " << this->GetHeight() <<
" , ";
351 std::cout <<
" D = " << this->GetDepth() <<
" ) ";
353 std::cout <<
"\t Filter ( W = " << this->GetFilterWidth() <<
" , ";
354 std::cout <<
" H = " << this->GetFilterHeight() <<
" ) ";
356 if (this->GetOutput().GetSize() > 0) {
357 std::cout <<
"\tOutput = ( " << this->GetOutput().GetFirstSize() <<
" , "
358 << this->GetOutput().GetCSize() <<
" , " << this->GetOutput().GetHSize() <<
" , " << this->GetOutput().GetWSize()
361 std::vector<std::string> activationNames = {
"Identity",
"Relu",
"Sigmoid",
"Tanh",
"SymmRelu",
"SoftSign",
"Gauss" };
362 std::cout <<
"\t Activation Function = ";
363 std::cout << activationNames[ static_cast<int>(fF) ] << std::endl;
367template <
typename Architecture_t>
381 int activationFunction =
static_cast<int>(
this -> GetActivationFunction());
386 this->WriteMatrixToXML(layerxml,
"Weights",
this -> GetWeightsAt(0));
387 this->WriteMatrixToXML(layerxml,
"Biases",
this -> GetBiasesAt(0));
391template <
typename Architecture_t>
396 this->ReadMatrixXML(parent,
"Weights",
this -> GetWeightsAt(0));
397 this->ReadMatrixXML(parent,
"Biases",
this -> GetBiasesAt(0));
400template <
typename Architecture_t>
403 size_t temp = imgDim - fltDim + 2 * padding;
404 if (temp % stride || temp + stride <= 0) {
405 Fatal(
"calculateDimension",
"Not compatible hyper parameters for layer - (imageDim, filterDim, padding, stride) "
406 "%zu, %zu, %zu, %zu", imgDim, fltDim, padding, stride);
408 return temp / stride + 1;
411template <
typename Architecture_t>
413 size_t strideRows,
size_t inputWidth,
size_t filterWidth,
414 size_t paddingWidth,
size_t strideCols)
416 int height = calculateDimension(inputHeight, filterHeight, paddingHeight, strideRows);
417 int width = calculateDimension(inputWidth, filterWidth, paddingWidth, strideCols);
419 return height *
width;
423template <
typename Architecture_t>
425 Architecture_t::InitializeConvDescriptors(fDescriptors,
this);
428template <
typename Architecture_t>
430 Architecture_t::ReleaseConvDescriptors(fDescriptors);
434template <
typename Architecture_t>
436 TConvParams params(this->GetBatchSize(), this->GetInputDepth(), this->GetInputHeight(), this->GetInputWidth(),
437 this->GetDepth(), this->GetFilterHeight(), this->GetFilterWidth(),
438 this->GetStrideRows(), this->GetStrideCols(), this->GetPaddingHeight(), this->GetPaddingWidth());
440 Architecture_t::InitializeConvWorkspace(fWorkspace, fDescriptors, params,
this);
443template <
typename Architecture_t>
445 Architecture_t::FreeConvWorkspace(fWorkspace,
this);
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.
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
EvaluateInfo init(std::vector< RooRealProxy > parameters, std::vector< ArrayWrapper * > wrappers, std::vector< double * > arrays, size_t begin, size_t batchSize)
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)
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.