DaNNet
Public Member Functions | Private Attributes | List of all members
dnn::layer_conv Class Reference

A convolution layer class. More...

#include <dnn_layer_conv.h>

Inheritance diagram for dnn::layer_conv:
dnn::layer

Public Member Functions

 layer_conv (const arma::uword n_channels, const arma::uword n_kernel, const arma::uword n_pad=0, const arma::uword n_stride=1)
 Convolution layer constructor. More...
 
void init (void)
 Initialization of layer. More...
 
void upd_buf_size (arma::uword nmb)
 Updates the buffer sizes. More...
 
arma::Mat< DNN_Dtyperotate_180 (const arma::Mat< DNN_Dtype > &M)
 Rotates a matrix 180 degree. More...
 
arma::Cube< DNN_Dtyperotate_180 (const arma::Cube< DNN_Dtype > &C)
 Rotates each matrix in a cube 180 degree. More...
 
template<typename DNN_Dtype >
void im2col_nzp (DNN_Dtype *Dptr, const DNN_Dtype *Xptr, const arma::uword XRows, const arma::uword XCols, const arma::uword XDepth, const arma::uword KRows, const arma::uword KCols, const arma::uword SRows, const arma::uword SCols)
 Im2col without zero padding. More...
 
template<typename DNN_Dtype >
void im2col (DNN_Dtype *Dptr, const DNN_Dtype *Xptr, const arma::uword XRows, const arma::uword XCols, const arma::uword XDepth, const arma::uword KRows, const arma::uword KCols, const arma::uword PRows, const arma::uword PCols, const arma::uword SRows, const arma::uword SCols)
 Im2col with zero padding. More...
 
template<typename DNN_Dtype >
void im2row (DNN_Dtype *Dptr, const DNN_Dtype *Xptr, const unsigned int XRows, const unsigned int XCols, const unsigned int XDepth, const unsigned int KRows, const unsigned int KCols, const unsigned int PRows, const unsigned int PCols, const unsigned int SRows, const unsigned int SCols)
 Im2row with zero padding. More...
 
void disp (void)
 Display info about layer. More...
 
arma::uword get_nrof_params (void)
 Get info about number of trainable parameters in layer. More...
 
void prop (void)
 Forward propagation though layer. More...
 
void prop_mb (void)
 Forward mini batch propagation though layer. More...
 
void backprop (void)
 Backpropagation of mini batch propagation though layer. More...
 
void update (void)
 Updates the trainable parameters. More...
 
arma::Mat< DNN_Dtypeweights2img (arma::uword IR)
 Generate an image of the weights. More...
 
- Public Member Functions inherited from dnn::layer
 layer (void)
 Layer constructor. More...
 
 ~layer ()
 
virtual void enable_training (void)
 Enable training of layer. More...
 
virtual void disable_training (void)
 Disable training of layer. More...
 
virtual void enable_bias (void)
 Enable bias term in layer. More...
 
virtual void disable_bias (void)
 Disable bias term in layer. More...
 
virtual arma::Mat< DNN_Dtypeget_B (void)
 Get bias. More...
 
virtual arma::Cube< DNN_Dtypeget_W (void)
 Get weights. More...
 
virtual void set_B (arma::Mat< DNN_Dtype > &b)
 Set bias. More...
 
virtual void set_W (arma::Cube< DNN_Dtype > &w)
 Set weights. More...
 
virtual void set_phase (PHASE p)
 Set phase/state. More...
 
virtual void set_left (layer *lptr)
 Set pointer to left layer. More...
 
virtual void set_right (layer *rptr)
 Set pointer to right layer. More...
 
virtual void set_id (const std::string str)
 Set layer id string. More...
 
virtual std::string get_id (void)
 Get layer id string. More...
 
virtual void set_type (const std::string str)
 Set layer type string. More...
 
virtual std::string get_type (void)
 Get layer type string. More...
 
virtual void set_ix (const arma::uword n)
 Set layer index. More...
 
virtual arma::uword get_ix (void)
 Get layer index. More...
 
virtual void set_batch_size (const arma::uword n)
 Set mini batch size. More...
 
virtual arma::uword get_batch_size (void)
 Get mini batch size. More...
 
virtual void set_Y1 (arma::Mat< DNN_Dtype > &y)
 Set output buffer. More...
 
virtual void set_Y (arma::Mat< DNN_Dtype > &y)
 Set output buffer - mini batch. More...
 
virtual void set_Dleft (arma::Mat< DNN_Dtype > &d)
 Set error output buffer - mini batch. More...
 
virtual void set_rows_in (arma::uword r)
 Set input buffer row size. More...
 
virtual arma::uword get_nrof_rows (void)
 Get output buffer row size. More...
 
virtual void set_cols_in (arma::uword c)
 Set input buffer column size. More...
 
virtual arma::uword get_nrof_cols (void)
 Get output buffer column size. More...
 
virtual void set_channels_in (arma::uword l)
 Set input buffer channel/layer size. More...
 
virtual arma::uword get_nrof_channels (void)
 Get output buffer channel/layer size. More...
 
virtual void set_opt_alg (opt &alg_class)
 Set pointer to optimizer class. More...
 
virtual optget_opt_alg (void)
 Get pointer to optimizer class. More...
 
virtual arma::uword get_nrof_inputs (void)
 Get total number of layer inputs. More...
 
virtual arma::uword get_nrof_outputs (void)
 Get total number of layer outputs. More...
 
virtual arma::Mat< DNN_Dtypeget_Y1 (void)
 Get output buffer. More...
 
virtual arma::Mat< DNN_Dtype > * get_Y1_ptr (void)
 Get output buffer pointer. More...
 
virtual DNN_Dtypeget_Y1_memptr (void)
 Get output buffer memory pointer. More...
 
virtual arma::Mat< DNN_Dtypeget_Y (void)
 Get output buffer - mini batch. More...
 
virtual arma::Mat< DNN_Dtype > * get_Y_ptr (void)
 Get output buffer pointer - mini batch. More...
 
virtual DNN_Dtypeget_Y_memptr (void)
 Get output buffer memory pointer - mini batch. More...
 
virtual DNN_Dtypeget_Y_colptr (const arma::uword n)
 Get output buffer memory column pointer - mini batch. More...
 
virtual arma::Mat< DNN_Dtypeget_Dleft ()
 Get error buffer - mini batch. More...
 
virtual arma::Mat< DNN_Dtype > * get_Dleft_ptr (void)
 Get error buffer pointer - mini batch. More...
 
virtual DNN_Dtypeget_Dleft_memptr (void)
 Get error buffer memory pointer - mini batch. More...
 
virtual DNN_Dtypeget_Dleft_colptr (const arma::uword n)
 Get error buffer memory column pointer - mini batch. More...
 
virtual void save_layer_param (std::string name, PARAM_FORMAT f=PARAM_FORMAT::ASCII)
 Save layer weight and bias. More...
 
virtual void load_layer_param (std::string name, PARAM_FORMAT f=PARAM_FORMAT::AUTO)
 Load layer weight and bias. More...
 
template<typename T >
void init_weights (T &w, arma::uword fan_in, arma::uword fan_out, INIT_W_ALG alg, INIT_W_DIST dist)
 Initiate weights. More...
 
DNN_Dtype sigmoid (const DNN_Dtype x)
 Sigmoid function - scalar. More...
 
arma::Mat< DNN_Dtypesigmoid (const arma::Mat< DNN_Dtype > &x)
 Sigmoid function - matrix. More...
 
arma::Mat< DNN_Dtypesoftmax (const arma::Mat< DNN_Dtype > &x)
 Softmax function - matrix. More...
 

Private Attributes

arma::uword N_filter_size
 Conv. filter size. More...
 
arma::uword N_pad
 Padding. More...
 
arma::uword N_stride
 Stride. More...
 
arma::Cube< DNN_DtypeC
 Input expanded by im2col. More...
 
arma::Mat< DNN_DtypeCbp
 Input expanded by im2col. More...
 

Additional Inherited Members

- Protected Attributes inherited from dnn::layer
layerleft
 Pointer to previous layer. More...
 
layerright
 Pointer to next layer. More...
 
std::string id
 Layer id string. More...
 
std::string type
 Layer type string. More...
 
arma::uword layer_ix
 Layer index [0..]. More...
 
bool train_par
 Enable training. More...
 
bool add_bias
 Enable bias. More...
 
optopt_alg
 Pointer to optimizer. More...
 
PHASE phase
 Active state/phase. More...
 
arma::uword N_batch
 Mini batch size. More...
 
arma::uword N_rows_left
 Input rows. More...
 
arma::uword N_cols_left
 Input cols. More...
 
arma::uword N_channels_left
 Input channels, number of filters. More...
 
arma::uword N_rows_right
 Output rows. More...
 
arma::uword N_cols_right
 Output cols. More...
 
arma::uword N_channels_right
 Output channels, number of filters. More...
 
arma::uword N_left
 Total size left. More...
 
arma::uword N_right
 Total size right. More...
 
arma::Mat< DNN_DtypeY
 Output buffer mini batch [N_right,N_batch]. More...
 
arma::Mat< DNN_DtypeY1
 Output buffer [N_right,1]. More...
 
arma::Mat< DNN_DtypeDleft
 Error buffer [N_left,N_batch]. More...
 
arma::Cube< DNN_DtypeW
 Weights. More...
 
arma::Mat< DNN_DtypeB
 Bias. More...
 

Detailed Description

A convolution layer class.

A layer that convolves (actually correlates) the input layers with a stack of kernel filters. If the input has size [Ri,Ci,Li] and the filter kernel har size [K,K,LiLo] then the output will have size [Ro,Co,Lo] depending upon padding and stride parameters during the convolution.

Definition at line 31 of file dnn_layer_conv.h.

Constructor & Destructor Documentation

◆ layer_conv()

dnn::layer_conv::layer_conv ( const arma::uword  n_channels,
const arma::uword  n_kernel,
const arma::uword  n_pad = 0,
const arma::uword  n_stride = 1 
)
inline

Convolution layer constructor.

Parameters
[in]n_channelsNr of output channels/layers
[in]n_kernelFilter kernel size [KxK]
[in]n_padInput image padding
[in]n_strideStride (for row AND col)

Definition at line 49 of file dnn_layer_conv.h.

Member Function Documentation

◆ backprop()

void dnn::layer_conv::backprop ( void  )
inlinevirtual

Backpropagation of mini batch propagation though layer.

Performas a error backpropagation mini batch pass through layer

Reimplemented from dnn::layer.

Definition at line 363 of file dnn_layer_conv.h.

◆ disp()

void dnn::layer_conv::disp ( void  )
inlinevirtual

Display info about layer.

Reimplemented from dnn::layer.

Definition at line 288 of file dnn_layer_conv.h.

◆ get_nrof_params()

arma::uword dnn::layer_conv::get_nrof_params ( void  )
inlinevirtual

Get info about number of trainable parameters in layer.

Returns
Number of trainable parameters

Reimplemented from dnn::layer.

Definition at line 303 of file dnn_layer_conv.h.

◆ im2col()

template<typename DNN_Dtype >
void dnn::layer_conv::im2col ( DNN_Dtype Dptr,
const DNN_Dtype Xptr,
const arma::uword  XRows,
const arma::uword  XCols,
const arma::uword  XDepth,
const arma::uword  KRows,
const arma::uword  KCols,
const arma::uword  PRows,
const arma::uword  PCols,
const arma::uword  SRows,
const arma::uword  SCols 
)
inline

Im2col with zero padding.

Parameters
[in,out]Dptr,XptrPointers to destination and source data
[in]XRows,XColsNumber of rows/cols in source data
[in]XDepthNumber of channels in source data
[in]KRows,KColsNumber of rows/cols in kernel (filter)
[in]PRows,PColsPadding for row/col (on each side of image)
[in]SRows,SColsStride for row/col

im2col generates a matrix of size [XDepth*KRows*KCols,DRow*Dcol] where DCol=(XCol+2PCol-KCol)/SCol+1 and DRow=(XRow+2PRow-KRow)/SRow+1

Definition at line 183 of file dnn_layer_conv.h.

◆ im2col_nzp()

template<typename DNN_Dtype >
void dnn::layer_conv::im2col_nzp ( DNN_Dtype Dptr,
const DNN_Dtype Xptr,
const arma::uword  XRows,
const arma::uword  XCols,
const arma::uword  XDepth,
const arma::uword  KRows,
const arma::uword  KCols,
const arma::uword  SRows,
const arma::uword  SCols 
)
inline

Im2col without zero padding.

Parameters
[in,out]Dptr,XptrPointers to destination and source data
[in]XRows,XColsNumber of rows/cols in source data
[in]XDepthNumber of channels in source data
[in]KRows,KColsNumber of rows/cols in kernel (filter)
[in]SRows,SColsStride for row/col

im2col_nzp generates a matrix of size [XDepth*KRows*KCols,DRow*Dcol] where DCol=(XCol-KCol)/SCol+1 and DRow=(XRow-KRow)/SRow+1

Definition at line 141 of file dnn_layer_conv.h.

◆ im2row()

template<typename DNN_Dtype >
void dnn::layer_conv::im2row ( DNN_Dtype Dptr,
const DNN_Dtype Xptr,
const unsigned int  XRows,
const unsigned int  XCols,
const unsigned int  XDepth,
const unsigned int  KRows,
const unsigned int  KCols,
const unsigned int  PRows,
const unsigned int  PCols,
const unsigned int  SRows,
const unsigned int  SCols 
)
inline

Im2row with zero padding.

Parameters
[in,out]Dptr,XptrPointers to destination and source data
[in]XRows,XColsNumber of rows/cols in source data
[in]XDepthNumber of channels in source data
[in]KRows,KColsNumber of rows/cols in kernel (filter)
[in]PRows,PColsPadding for row/col (on each side of image)
[in]SRows,SColsStride for row/col

im2row generates a matrix of size [DRow*Dcol,XDepth*KRows*KCols] where DCol=(XCol+2PCol-KCol)/SCol+1 and DRow=(XRow+2PRow-KRow)/SRow+1

Definition at line 246 of file dnn_layer_conv.h.

◆ init()

void dnn::layer_conv::init ( void  )
inlinevirtual

Initialization of layer.

Calculates the output and internal buffer sizes and initializes the parameters

Reimplemented from dnn::layer.

Definition at line 66 of file dnn_layer_conv.h.

◆ prop()

void dnn::layer_conv::prop ( void  )
inlinevirtual

Forward propagation though layer.

Performas a forward pass through layer

Reimplemented from dnn::layer.

Definition at line 313 of file dnn_layer_conv.h.

◆ prop_mb()

void dnn::layer_conv::prop_mb ( void  )
inlinevirtual

Forward mini batch propagation though layer.

Performas a forward mini batch pass through layer

Reimplemented from dnn::layer.

Definition at line 337 of file dnn_layer_conv.h.

◆ rotate_180() [1/2]

arma::Mat<DNN_Dtype> dnn::layer_conv::rotate_180 ( const arma::Mat< DNN_Dtype > &  M)
inline

Rotates a matrix 180 degree.

Parameters
[in]MInput matrix
Returns
Rotated matrix

Rotates a matrix 180 degree (equals fliplr(flipud(M)) in Matlab)

Definition at line 106 of file dnn_layer_conv.h.

◆ rotate_180() [2/2]

arma::Cube<DNN_Dtype> dnn::layer_conv::rotate_180 ( const arma::Cube< DNN_Dtype > &  C)
inline

Rotates each matrix in a cube 180 degree.

Parameters
[in]CInput matrix
Returns
Cube with each slice rotated

Rotates each matrix 180 degree (equals flipud(fliplr(C)) in Matlab)

Definition at line 119 of file dnn_layer_conv.h.

◆ upd_buf_size()

void dnn::layer_conv::upd_buf_size ( arma::uword  nmb)
inlinevirtual

Updates the buffer sizes.

Parameters
[in]nmbNew mini batch buffer size

Updates and clears the buffer sizes, called when size of mini batch is changed

Reimplemented from dnn::layer.

Definition at line 93 of file dnn_layer_conv.h.

◆ update()

void dnn::layer_conv::update ( void  )
inlinevirtual

Updates the trainable parameters.

Calculates the gradients and update the trainiable parameters

Reimplemented from dnn::layer.

Definition at line 384 of file dnn_layer_conv.h.

◆ weights2img()

arma::Mat<DNN_Dtype> dnn::layer_conv::weights2img ( arma::uword  IR)
inline

Generate an image of the weights.

Parameters
[in]IRNumber of images per row
Returns
An image of the weights

Flattens weights from each layer into a concatenated image

Definition at line 423 of file dnn_layer_conv.h.

Member Data Documentation

◆ C

arma::Cube<DNN_Dtype> dnn::layer_conv::C
private

Input expanded by im2col.

Definition at line 38 of file dnn_layer_conv.h.

◆ Cbp

arma::Mat<DNN_Dtype> dnn::layer_conv::Cbp
private

Input expanded by im2col.

Definition at line 39 of file dnn_layer_conv.h.

◆ N_filter_size

arma::uword dnn::layer_conv::N_filter_size
private

Conv. filter size.

Definition at line 34 of file dnn_layer_conv.h.

◆ N_pad

arma::uword dnn::layer_conv::N_pad
private

Padding.

Definition at line 35 of file dnn_layer_conv.h.

◆ N_stride

arma::uword dnn::layer_conv::N_stride
private

Stride.

Definition at line 36 of file dnn_layer_conv.h.


The documentation for this class was generated from the following file: