Simd Library Documentation.

Home | Release Notes | Download | Documentation | Issues | GitHub
GridSampleLayer functions

Functions to accelerate GridSampleLayer in Synet Framework. More...

Enumerations

enum  SimdGridSampleInterpType {
  SimdGridSampleInterpBilinear = 0 ,
  SimdGridSampleInterpNearest ,
  SimdGridSampleInterpBicubic
}
 
enum  SimdGridSamplePaddingType {
  SimdGridSamplePaddingZeros = 0 ,
  SimdGridSamplePaddingBorder ,
  SimdGridSamplePaddingReflect
}
 

Functions

SIMD_API void * SimdSynetGridSample2dInit (size_t batch, size_t channels, size_t srcH, size_t srcW, size_t dstH, size_t dstW, SimdTensorDataType type, SimdGridSampleInterpType interp, SimdGridSamplePaddingType padding, SimdBool align)
 Initializes an ONNX-compatible GridSample-2D context. More...
 
SIMD_API size_t SimdSynetGridSample2dInternalBufferSize (const void *context)
 Gets the size in bytes of internal storage used by a GridSample-2D context. More...
 
SIMD_API void SimdSynetGridSample2dForward (void *context, const uint8_t *src, const uint8_t *grd, uint8_t *dst)
 Performs GridSample-2D forward propagation. More...
 

Detailed Description

Functions to accelerate GridSampleLayer in Synet Framework.

Enumeration Type Documentation

◆ SimdGridSampleInterpType

Describes interpolation modes used by SimdSynetGridSample2dInit.

The grid tensor stores normalized (x, y) coordinates. During SimdSynetGridSample2dForward these modes define how source pixels around each denormalized coordinate are combined.

Enumerator
SimdGridSampleInterpBilinear 

Bilinear interpolation from the four neighboring source pixels.

SimdGridSampleInterpNearest 

Nearest-neighbor interpolation after rounding the denormalized coordinate.

SimdGridSampleInterpBicubic 

Bicubic interpolation from a 4x4 neighborhood.

◆ SimdGridSamplePaddingType

Describes padding modes used by SimdSynetGridSample2dInit.

Padding is applied when a denormalized grid coordinate falls outside the source image. The border used for this test depends on the align parameter of SimdSynetGridSample2dInit.

Enumerator
SimdGridSamplePaddingZeros 

Use zero for out-of-bound source samples.

SimdGridSamplePaddingBorder 

Clamp out-of-bound source samples to the nearest border pixel.

SimdGridSamplePaddingReflect 

Reflect out-of-bound source samples across the border before reading the source pixel.

Function Documentation

◆ SimdSynetGridSample2dInit()

void * SimdSynetGridSample2dInit ( size_t  batch,
size_t  channels,
size_t  srcH,
size_t  srcW,
size_t  dstH,
size_t  dstW,
SimdTensorDataType  type,
SimdGridSampleInterpType  interp,
SimdGridSamplePaddingType  padding,
SimdBool  align 
)

Initializes an ONNX-compatible GridSample-2D context.

The function creates a context for NCHW tensors. The input tensor shape is batch*channels*srcH*srcW, the grid tensor shape is batch*dstH*dstW*2 and the output tensor shape is batch*channels*dstH*dstW. The grid stores normalized coordinates in the range [-1, 1] as pairs (x, y). The current implementation supports FP32 source, grid and destination tensors.

Parameters
[in]batch- a batch size.
[in]channels- a number of channels in the input and output tensors.
[in]srcH- a height of input tensor.
[in]srcW- a width of input tensor.
[in]dstH- a height of output tensor.
[in]dstW- a width of output tensor.
[in]type- a type of input, grid and output tensor. Currently only FP32 is supported.
[in]interp- an interpolation type: bilinear, nearest or bicubic.
[in]padding- a padding type for out-of-bound grid coordinates.
[in]align- a flag corresponding to ONNX/PyTorch align_corners.
Returns
a pointer to grid sample 2D context. On error it returns NULL. It must be released with using of function SimdRelease. This pointer is used in functions SimdSynetGridSample2dInternalBufferSize, and SimdSynetGridSample2dForward.

◆ SimdSynetGridSample2dInternalBufferSize()

size_t SimdSynetGridSample2dInternalBufferSize ( const void *  context)

Gets the size in bytes of internal storage used by a GridSample-2D context.

The optimized bilinear/zero-padding FP32 implementation stores padded source rows, precomputed source indexes and interpolation coefficients internally. The reference implementation returns 0.

Parameters
[in]context- a pointer to grid sample 2D context. It must be created by function SimdSynetGridSample2dInit and released by function SimdRelease.
Returns
a number of bytes used by internal buffers.

◆ SimdSynetGridSample2dForward()

void SimdSynetGridSample2dForward ( void *  context,
const uint8_t *  src,
const uint8_t *  grd,
uint8_t *  dst 
)

Performs GridSample-2D forward propagation.

For every output pixel the function denormalizes grid coordinates to input image coordinates, applies the selected padding rule for out-of-bound coordinates and samples the input with nearest, bilinear or bicubic interpolation:

if(align)
    x = (gridX + 1) * (srcW - 1) / 2;
else
    x = ((gridX + 1) * srcW - 1) / 2;
y is computed in the same way from gridY and srcH.
dst[b, c, dy, dx] = Interpolate(src[b, c], x, y, interp, padding);
Parameters
[in]context- a pointer to grid sample 2D context. It must be created by function SimdSynetGridSample2dInit and released by function SimdRelease.
[in]src- a pointer to input tensor. Its shape is batch * channels * srcH * srcW.
[in]grd- a pointer to grid tensor. Its shape is batch * dstH * dstW * 2.
[out]dst- a pointer to output tensor. Its shape is batch * channels * dstH * dstW.