Functions to accelerate conversion in Synet Framework. More...
Functions | |
| SIMD_API void | SimdSynetConvert32fTo8u (const float *src, size_t batch, size_t channels, size_t height, size_t width, SimdTensorFormatType format, const float *scale, const float *shift, uint8_t *dst, SimdSynetCompatibilityType compatibility) |
| Converts an FP32 tensor to a UINT8 tensor using per-channel scale and shift. More... | |
| SIMD_API void | SimdSynetConvert8uTo32f (const uint8_t *src, size_t batch, size_t channels, size_t height, size_t width, SimdTensorFormatType format, const float *scale, const float *shift, float *dst, SimdSynetCompatibilityType compatibility) |
| Converts a UINT8 tensor to an FP32 tensor using per-channel scale and shift. More... | |
| SIMD_API void | SimdSynetSetInput (const uint8_t *src, size_t width, size_t height, size_t stride, SimdPixelFormatType srcFormat, const float *lower, const float *upper, float *dst, size_t channels, SimdTensorFormatType dstFormat) |
| Converts an 8-bit image to normalized FP32 neural-network input tensor. More... | |
| template<template< class > class A> | |
| SIMD_INLINE void | SynetSetInput (const View< A > &src, const float *lower, const float *upper, float *dst, size_t channels, SimdTensorFormatType format, bool isRgb=false) |
| Sets image to the input of neural network of Synet Framework. More... | |
Detailed Description
Functions to accelerate conversion in Synet Framework.
Function Documentation
◆ SimdSynetConvert32fTo8u()
| void SimdSynetConvert32fTo8u | ( | const float * | src, |
| size_t | batch, | ||
| size_t | channels, | ||
| size_t | height, | ||
| size_t | width, | ||
| SimdTensorFormatType | format, | ||
| const float * | scale, | ||
| const float * | shift, | ||
| uint8_t * | dst, | ||
| SimdSynetCompatibilityType | compatibility | ||
| ) |
Converts an FP32 tensor to a UINT8 tensor using per-channel scale and shift.
Algorithm's details (example for NCHW tensor format):
upper = isNarrowed(compatibility) ? 180 : 255;
for(b = 0; b < batch; ++b)
for(c = 0; c < channels; ++c)
for(h = 0; h < height; ++h)
for(w = 0; w < width; ++w)
{
offs = ((b*channels + c)*height + h)*width + w;
dst[offs] = restrict(round(src[offs]*scale[c] + shift[c]), 0, upper);
}
For NHWC tensor format the same calculation uses offset ((b*height + h)*width + w)*channels + c.
- Note
- This function is used in Synet Framework.
- Parameters
-
[in] src - a pointer to the FP32 input tensor. [in] batch - a batch size of input and output tensors. [in] channels - a number of channels in input and output tensors. [in] height - a height of input and output tensors. [in] width - a width of input and output tensors. [in] format - a format of input and output tensors. Can be NCHW or NHWC. [in] scale - a pointer to the 32-bit float array with per-channel scale coefficients. [in] shift - a pointer to the 32-bit float array with per-channel shift coefficients. [out] dst - a pointer to the UINT8 output tensor. [in] compatibility - calculation compatibility flags. When narrowed 8-bit mode is active, output is limited to [0, 180], otherwise to [0, 255].
◆ SimdSynetConvert8uTo32f()
| void SimdSynetConvert8uTo32f | ( | const uint8_t * | src, |
| size_t | batch, | ||
| size_t | channels, | ||
| size_t | height, | ||
| size_t | width, | ||
| SimdTensorFormatType | format, | ||
| const float * | scale, | ||
| const float * | shift, | ||
| float * | dst, | ||
| SimdSynetCompatibilityType | compatibility | ||
| ) |
Converts a UINT8 tensor to an FP32 tensor using per-channel scale and shift.
Algorithm's details (example for NCHW tensor format):
for(b = 0; b < batch; ++b)
for(c = 0; c < channels; ++c)
for(h = 0; h < height; ++h)
for(w = 0; w < width; ++w)
{
offs = ((b*channels + c)*height + h)*width + w;
dst[offs] = src[offs]*scale[c] + shift[c];
}
For NHWC tensor format the same calculation uses offset ((b*height + h)*width + w)*channels + c.
- Note
- This function is used in Synet Framework.
- Parameters
-
[in] src - a pointer to the UINT8 input tensor. [in] batch - a batch size of input and output tensors. [in] channels - a number of channels in input and output tensors. [in] height - a height of input and output tensors. [in] width - a width of input and output tensors. [in] format - a format of input and output tensors. Can be NCHW or NHWC. [in] scale - a pointer to the 32-bit float array with per-channel scale coefficients. [in] shift - a pointer to the 32-bit float array with per-channel shift coefficients. [out] dst - a pointer to the FP32 output tensor. [in] compatibility - calculation compatibility flags.
◆ SimdSynetSetInput()
| void SimdSynetSetInput | ( | const uint8_t * | src, |
| size_t | width, | ||
| size_t | height, | ||
| size_t | stride, | ||
| SimdPixelFormatType | srcFormat, | ||
| const float * | lower, | ||
| const float * | upper, | ||
| float * | dst, | ||
| size_t | channels, | ||
| SimdTensorFormatType | dstFormat | ||
| ) |
Converts an 8-bit image to normalized FP32 neural-network input tensor.
Algorithm's details (example for BGRA pixel format and NCHW tensor format):
for(c = 0; c < channels; ++c)
for(y = 0; y < height; ++y)
for(x = 0; x < width; ++x)
dst[(c*height + y)*width + x] = src[stride*y + x*4 + c]*(upper[c] - lower[c])/255 + lower[c];
Each output value is mapped from [0, 255] to [lower[c], upper[c]]. Note that there are following relationships:
upper[c] = (1 - mean[c]) / std[c]; lower[c] = - mean[c] / std[c];
Also this algorithm assumes that channel order of output tensor is BGR. In case of RGB channel order you need to change parameter srcFormat: SimdPixelFormatBgr24 <-> SimdPixelFormatRgb24, SimdPixelFormatBgra32 <-> SimdPixelFormatRgba32. The actual pixel data of the input image does not need to be changed.
- Note
- This function has a C++ wrappers: Simd::SynetSetInput(const View & src, const float * lower, const float * upper, float * dst, size_t channels, SimdTensorFormatType format, bool isRgb = false).
- Parameters
-
[in] src - a pointer to pixels data of input image. [in] width - a width of input image and output image tensor. [in] height - a height of input image and output image tensor. [in] stride - a row size of input image. [in] srcFormat - a pixel format of input image. There are supported following pixel formats: SimdPixelFormatGray8, SimdPixelFormatBgr24, SimdPixelFormatBgra32, SimdPixelFormatRgb24, SimdPixelFormatRgba32. [in] lower - a pointer to lower bounds of output tensor values. The size of the array must be equal to channels. [in] upper - a pointer to upper bounds of output tensor values. The size of the array must be equal to channels. [out] dst - a pointer to the output 32-bit float image tensor. [in] channels - a number of channels in the output image tensor. It can be 1 or 3. [in] dstFormat - a format of output image tensor. There are supported following tensor formats: SimdTensorFormatNchw, SimdTensorFormatNhwc.
◆ SynetSetInput()
| void SynetSetInput | ( | const View< A > & | src, |
| const float * | lower, | ||
| const float * | upper, | ||
| float * | dst, | ||
| size_t | channels, | ||
| SimdTensorFormatType | format, | ||
| bool | isRgb = false |
||
| ) |
Sets image to the input of neural network of Synet Framework.
Algorithm's details (example for BGRA pixel format and NCHW tensor format):
for(c = 0; c < channels; ++c)
for(y = 0; y < src.height; ++y)
for(x = 0; x < src.width; ++x)
dst[(c*height + y)*width + x] = src.data[src.stride*y + src.width*4 + c]*(upper[c] - lower[c])/255 + lower[c];
Note that there are following relationships:
upper[c] = (1 - mean[c]) / std[c]; lower[c] = - mean[c] / std[c];
- Note
- This function is a C++ wrapper for function SimdSynetSetInput.
- Parameters
-
[in] src - an input image.There are supported following image formats: View<A>::Gray8, View<A>::Bgr24, View<A>::Bgra32, View<A>::Rgb24, View<A>::Rgba32. [in] lower - a pointer to the array with lower bound of values of the output tensor. The size of the array have to correspond number of channels in the output image tensor. [in] upper - a pointer to the array with upper bound of values of the output tensor. The size of the array have to correspond number of channels in the output image tensor. [out] dst - a pointer to the output 32-bit float image tensor. [in] channels - a number of channels in the output image tensor. It can be 1 or 3. [in] format - a format of output image tensor. There are supported following tensor formats: SimdTensorFormatNchw, SimdTensorFormatNhwc. [in] isRgb - is channel order of output tensor is RGB or BGR. Its default value is false.