The SynetDeconvolution32f class is a C++ wrapper of FP32 deconvolution (transposed convolution). More...
#include <SimdSynet.hpp>
Public Member Functions | |
| SynetDeconvolution32f () | |
| virtual | ~SynetDeconvolution32f () |
| SIMD_INLINE void | Init (size_t batch, const SimdConvolutionParameters *conv, SimdSynetCompatibilityType compatibility=SimdSynetCompatibilityDefault) |
| SIMD_INLINE bool | Enable () const |
| SIMD_INLINE size_t | ExternalBufferSize () const |
| SIMD_INLINE size_t | InternalBufferSize () const |
| SIMD_INLINE const char * | Info () const |
| SIMD_INLINE void | SetParams (const float *weight, SimdBool *internal, const float *bias, const float *params) |
| SIMD_INLINE void | Forward (const float *src, float *buf, float *dst) |
| SIMD_INLINE void | Clear () |
Detailed Description
The SynetDeconvolution32f class is a C++ wrapper of FP32 deconvolution (transposed convolution).
The class wraps C API functions SimdSynetDeconvolution32fInit, SimdSynetDeconvolution32fExternalBufferSize, SimdSynetDeconvolution32fInternalBufferSize, SimdSynetDeconvolution32fInfo, SimdSynetDeconvolution32fSetParams and SimdSynetDeconvolution32fForward. It applies transposed convolution to each image in the batch, optionally adds bias and applies activation:
dst[:] = 0;
for(sc = 0; sc < srcC/group; ++sc)
for(sy = 0; sy < srcH; ++sy)
for(sx = 0; sx < srcW; ++sx)
for(ky = 0; ky < kernelY; ++ky)
for(kx = 0; kx < kernelX; ++kx)
dst[outputOffset] += src[inputOffset] * weight[weightOffset];
dst[outputOffset] = Activate(dst[outputOffset] + bias[dc], activation, params);
The exact offsets depend on tensor format, padding, dilation, stride and group. The current implementation supports FP32 source and destination tensors with matching NCHW format, or matching NHWC format when group is 1. The destination spatial size must match deconvolution parameters:
dstH = strideY*(srcH - 1) + dilationY*(kernelY - 1) + 1 - padY - padH dstW = strideX*(srcW - 1) + dilationX*(kernelX - 1) + 1 - padX - padW
Call Init() and SetParams() before Forward(). Use Enable() to check that a context was created. The context is released by Clear() or by the destructor.
Using example:
#include "Simd/SimdSynet.hpp"
int main()
{
const size_t batch = 1, srcC = 4, srcH = 3, srcW = 3, dstC = 4;
SimdConvolutionParameters conv = {};
conv.srcC = srcC;
conv.srcH = srcH;
conv.srcW = srcW;
conv.srcT = SimdTensorData32f;
conv.srcF = SimdTensorFormatNhwc;
conv.dstC = dstC;
conv.kernelY = 2;
conv.kernelX = 2;
conv.dilationY = 1;
conv.dilationX = 1;
conv.strideY = 2;
conv.strideX = 2;
conv.padY = 0;
conv.padX = 0;
conv.padH = 0;
conv.padW = 0;
conv.group = 1;
conv.activation = SimdConvolutionActivationIdentity;
conv.dstH = conv.strideY * (conv.srcH - 1) + conv.dilationY * (conv.kernelY - 1) + 1 - conv.padY - conv.padH;
conv.dstW = conv.strideX * (conv.srcW - 1) + conv.dilationX * (conv.kernelX - 1) + 1 - conv.padX - conv.padW;
conv.dstT = SimdTensorData32f;
conv.dstF = SimdTensorFormatNhwc;
std::vector<float> src(batch * srcH * srcW * srcC);
std::vector<float> weight(conv.kernelY * conv.kernelX * srcC * dstC / conv.group);
std::vector<float> bias(dstC, 0.0f);
std::vector<float> dst(batch * conv.dstH * conv.dstW * dstC, 0.0f);
for (size_t i = 0; i < src.size(); ++i)
src[i] = float(i) * 0.01f;
for (size_t i = 0; i < weight.size(); ++i)
weight[i] = float(i) * 0.02f;
Simd::SynetDeconvolution32f deconvolution;
deconvolution.Init(batch, &conv);
if (deconvolution.Enable())
{
deconvolution.SetParams(weight.data(), NULL, bias.data(), NULL);
deconvolution.Forward(src.data(), NULL, dst.data());
}
return 0;
}
Constructor & Destructor Documentation
◆ SynetDeconvolution32f()
Creates a new empty SynetDeconvolution32f class.
◆ ~SynetDeconvolution32f()
|
virtual |
SynetDeconvolution32f class destructor. Releases internal context.
Member Function Documentation
◆ Init()
| SIMD_INLINE void Init | ( | size_t | batch, |
| const SimdConvolutionParameters * | conv, | ||
| SimdSynetCompatibilityType | compatibility = SimdSynetCompatibilityDefault |
||
| ) |
Initializes (or re-initializes) an FP32 deconvolution context.
Creates an internal context with using of function SimdSynetDeconvolution32fInit. The context is recreated only if batch size, deconvolution parameters or compatibility flags were changed.
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution32fInit.
- Parameters
-
[in] batch - a batch size. [in] conv - a pointer to deconvolution parameters. Source and destination tensor types must be FP32. [in] compatibility - calculation compatibility flags.
◆ Enable()
| SIMD_INLINE bool Enable | ( | ) | const |
Checks that the internal deconvolution context was created.
- Returns
- true if the context exists and Forward() can be called.
◆ ExternalBufferSize()
| SIMD_INLINE size_t ExternalBufferSize | ( | ) | const |
Gets the size of caller-provided temporary buffer for FP32 deconvolution.
The returned value is a number of FP32 elements. It depends on the implementation selected during initialization and can be used when allocating the buf argument of Forward(). Some implementations return 1 when they do not need external temporary storage.
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution32fExternalBufferSize.
- Returns
- a number of FP32 elements required for external temporary buffer.
◆ InternalBufferSize()
| SIMD_INLINE size_t InternalBufferSize | ( | ) | const |
Gets the size of internal storage used by the deconvolution context.
The returned value is a number of FP32 elements. It reports internal temporary buffers and implementation-specific reordered weights, bias or activation parameters already allocated by the context.
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution32fInternalBufferSize.
- Returns
- a number of FP32 elements used by internal buffers.
◆ Info()
| SIMD_INLINE const char * Info | ( | ) | const |
Gets a short description of the selected FP32 deconvolution implementation.
The returned string contains the implementation extension and algorithm name, for example a GEMM-based or NHWC direct 2x2 variant. The returned pointer is owned by the context and remains valid until the next call of this function or until the context is released.
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution32fInfo.
- Returns
- a string with description of internal implementation. NULL if the context was not created.
◆ SetParams()
| SIMD_INLINE void SetParams | ( | const float * | weight, |
| SimdBool * | internal, | ||
| const float * | bias, | ||
| const float * | params | ||
| ) |
Sets weights, bias and activation parameters for FP32 deconvolution.
This function must be called before Forward(). The weight array contains FP32 deconvolution weights with kernelY*kernelX*srcC*dstC/group elements. Depending on the selected implementation, weights can be used directly or transformed and stored inside the context. If internal is not NULL, SimdTrue means the weights were copied/reordered into the context; SimdFalse means the original weight pointer can be used by later forward calls and must remain valid.
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution32fSetParams.
- Parameters
-
[in] weight - a pointer to FP32 deconvolution weights. [out] internal - a pointer to a flag receiving weight storage mode. Can be NULL. [in] bias - a pointer to FP32 bias array with dstC elements. Can be NULL. [in] params - a pointer to FP32 parameters of activation function (see SimdConvolutionActivationType). Can be NULL when activation does not require parameters.
◆ Forward()
| SIMD_INLINE void Forward | ( | const float * | src, |
| float * | buf, | ||
| float * | dst | ||
| ) |
Performs FP32 deconvolution forward propagation.
The function applies transposed convolution to each image in the batch, adds bias when it was set, and applies the activation stored in the context created by Init() and SetParams(). The buf argument can be NULL (it causes usage of internal buffer).
- Note
- This function is a C++ wrapper for function SimdSynetDeconvolution32fForward.
- Parameters
-
[in] src - a pointer to FP32 input tensor. [out] buf - a pointer to external temporary FP32 buffer. Can be NULL. [out] dst - a pointer to FP32 output tensor.
◆ Clear()
| SIMD_INLINE void Clear | ( | ) |
Releases internal context and clears stored deconvolution parameters.