IDF release/v3.3 (#3339)

* IDF release/v3.3 46b12a560

* fix build

* IDF release/v3.3 367c3c09c
This commit is contained in:
Me No Dev
2020-01-20 22:07:04 +02:00
committed by GitHub
parent 307b1368dd
commit 1977370e6f
282 changed files with 13004 additions and 4377 deletions

View File

@ -4,8 +4,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#if CONFIG_SPIRAM_SUPPORT
#include "freertos/FreeRTOS.h"
#endif
#ifndef max
#define max(x, y) (((x) < (y)) ? (y) : (x))
#endif
#ifndef min
#define min(x, y) (((x) < (y)) ? (x) : (y))
#endif
typedef float fptp_t;
typedef uint8_t uc_t;
@ -19,30 +32,37 @@ typedef enum
{
PADDING_VALID = 0,
PADDING_SAME = 1,
PADDING_SAME_DONT_FREE_INPUT = 2,
PADDING_SAME_MXNET = 3,
} dl_padding_type;
typedef enum
{
DL_POOLING_MAX = 0,
DL_POOLING_AVG = 1,
} dl_pooling_type;
/*
* Matrix for 3d
* @Warning: the sequence of variables is fixed, cannot be modified, otherwise there will be errors in esp_dsp_dot_float
*/
typedef struct
{
int w; /*!< Width */
int h; /*!< Height */
int c; /*!< Channel */
int n; /*!< Number of filter, input and output must be 1 */
int stride; /*!< Step between lines */
fptp_t *item; /*!< Data */
int w; /*!< Width */
int h; /*!< Height */
int c; /*!< Channel */
int n; /*!< Number of filter, input and output must be 1 */
int stride; /*!< Step between lines */
fptp_t *item; /*!< Data */
} dl_matrix3d_t;
typedef struct
{
int w; /*!< Width */
int h; /*!< Height */
int c; /*!< Channel */
int n; /*!< Number of filter, input and output must be 1 */
int stride; /*!< Step between lines */
uc_t *item; /*!< Data */
int w; /*!< Width */
int h; /*!< Height */
int c; /*!< Channel */
int n; /*!< Number of filter, input and output must be 1 */
int stride; /*!< Step between lines */
uc_t *item; /*!< Data */
} dl_matrix3du_t;
typedef struct
@ -52,6 +72,51 @@ typedef struct
dl_padding_type padding;
} dl_matrix3d_mobilenet_config_t;
/*
* @brief Allocate a zero-initialized space. Must use 'dl_lib_free' to free the memory.
*
* @param cnt Count of units.
* @param size Size of unit.
* @param align Align of memory. If not required, set 0.
* @return Pointer of allocated memory. Null for failed.
*/
static inline void *dl_lib_calloc(int cnt, int size, int align)
{
int total_size = cnt * size + align + sizeof(void *);
void *res = malloc(total_size);
if (NULL == res)
{
#if CONFIG_SPIRAM_SUPPORT
res = heap_caps_malloc(total_size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
}
if (NULL == res)
{
printf("Item psram alloc failed. Size: %d x %d\n", cnt, size);
#else
printf("Item alloc failed. Size: %d x %d\n", cnt, size);
#endif
return NULL;
}
bzero(res, total_size);
void **data = (void **)res + 1;
void **aligned;
if (align)
aligned = (void **)(((size_t)data + (align - 1)) & -align);
else
aligned = data;
aligned[-1] = res;
return (void *)aligned;
}
static inline void dl_lib_free(void *d)
{
if (NULL == d)
return;
free(((void **)d)[-1]);
}
/*
* @brief Allocate a 3D matrix with float items, the access sequence is NHWC
*
@ -61,7 +126,31 @@ typedef struct
* @param c Channel of matrix3d
* @return 3d matrix
*/
dl_matrix3d_t *dl_matrix3d_alloc(int n, int w, int h, int c);
static inline dl_matrix3d_t *dl_matrix3d_alloc(int n, int w, int h, int c)
{
dl_matrix3d_t *r = (dl_matrix3d_t *)dl_lib_calloc(1, sizeof(dl_matrix3d_t), 0);
if (NULL == r)
{
printf("internal r failed.\n");
return NULL;
}
fptp_t *items = (fptp_t *)dl_lib_calloc(n * w * h * c, sizeof(fptp_t), 0);
if (NULL == items)
{
printf("matrix3d item alloc failed.\n");
dl_lib_free(r);
return NULL;
}
r->w = w;
r->h = h;
r->c = c;
r->n = n;
r->stride = w * c;
r->item = items;
return r;
}
/*
* @brief Allocate a 3D matrix with 8-bits items, the access sequence is NHWC
@ -72,21 +161,68 @@ dl_matrix3d_t *dl_matrix3d_alloc(int n, int w, int h, int c);
* @param c Channel of matrix3d
* @return 3d matrix
*/
dl_matrix3du_t *dl_matrix3du_alloc(int n, int w, int h, int c);
static inline dl_matrix3du_t *dl_matrix3du_alloc(int n, int w, int h, int c)
{
dl_matrix3du_t *r = (dl_matrix3du_t *)dl_lib_calloc(1, sizeof(dl_matrix3du_t), 0);
if (NULL == r)
{
printf("internal r failed.\n");
return NULL;
}
uc_t *items = (uc_t *)dl_lib_calloc(n * w * h * c, sizeof(uc_t), 0);
if (NULL == items)
{
printf("matrix3du item alloc failed.\n");
dl_lib_free(r);
return NULL;
}
r->w = w;
r->h = h;
r->c = c;
r->n = n;
r->stride = w * c;
r->item = items;
return r;
}
/*
* @brief Free a matrix3d
*
* @param m matrix3d with float items
*/
void dl_matrix3d_free(dl_matrix3d_t *m);
static inline void dl_matrix3d_free(dl_matrix3d_t *m)
{
if (NULL == m)
return;
if (NULL == m->item)
{
dl_lib_free(m);
return;
}
dl_lib_free(m->item);
dl_lib_free(m);
}
/*
* @brief Free a matrix3d
*
* @param m matrix3d with 8-bits items
*/
void dl_matrix3du_free(dl_matrix3du_t *m);
static inline void dl_matrix3du_free(dl_matrix3du_t *m)
{
if (NULL == m)
return;
if (NULL == m->item)
{
dl_lib_free(m);
return;
}
dl_lib_free(m->item);
dl_lib_free(m);
}
/*
* @brief Dot product with a vector and matrix
@ -138,6 +274,15 @@ void dl_matrix3du_slice_copy(dl_matrix3du_t *dst,
int w,
int h);
/**
* @brief Transform a sliced matrix block from nhwc to nchw, the block needs to be memory continous.
*
* @param out The destination sliced matrix in nchw
* @param in The source sliced matrix in nhwc
*/
void dl_matrix3d_sliced_transform_nchw(dl_matrix3d_t *out,
dl_matrix3d_t *in);
/**
* @brief Do a general CNN layer pass, dimension is (number, width, height, channel)
*
@ -159,20 +304,6 @@ dl_matrix3d_t *dl_matrix3d_conv(dl_matrix3d_t *in,
int padding,
int mode);
/**
* @brief Do a general CNN layer pass, dimension is (number, width, height, channel)
*
* @param in Input matrix3d
* @param filter Weights of the neurons
* @param bias Bias for the CNN layer
* @param stride_x The step length of the convolution window in x(width) direction
* @param stride_y The step length of the convolution window in y(height) direction
* @param padding One of VALID or SAME
* @param mode Do convolution using C implement or xtensa implement, 0 or 1, with respect
* If ESP_PLATFORM is not defined, this value is not used. Default is 0
* @return The result of CNN layer
*/
/**
* @brief Do a global average pooling layer pass, dimension is (number, width, height, channel)
*
@ -182,6 +313,25 @@ dl_matrix3d_t *dl_matrix3d_conv(dl_matrix3d_t *in,
*/
dl_matrix3d_t *dl_matrix3d_global_pool(dl_matrix3d_t *in);
/**
* @brief Calculate pooling layer of a feature map
*
* @param in Input matrix, size (1, w, h, c)
* @param f_w Window width
* @param f_h Window height
* @param stride_x Stride in horizontal direction
* @param stride_y Stride in vertical direction
* @param padding Padding type: PADDING_VALID and PADDING_SAME
* @param pooling_type Pooling type: DL_POOLING_MAX and POOLING_AVG
* @return Resulting matrix, size (1, w', h', c)
*/
dl_matrix3d_t *dl_matrix3d_pooling(dl_matrix3d_t *in,
int f_w,
int f_h,
int stride_x,
int stride_y,
dl_padding_type padding,
dl_pooling_type pooling_type);
/**
* @brief Do a batch normalization operation, update the input matrix3d: input = input * scale + offset
*
@ -414,6 +564,13 @@ dl_matrix3d_t *dl_matrix3duf_conv_common(dl_matrix3du_t *in,
int stride_y,
dl_padding_type padding);
dl_matrix3d_t *dl_matrix3dff_conv_common(dl_matrix3d_t *in,
dl_matrix3d_t *filter,
dl_matrix3d_t *bias,
int stride_x,
int stride_y,
dl_padding_type padding);
//
// Depthwise 3x3
//

View File

@ -11,13 +11,13 @@ typedef int16_t qtp_t;
typedef struct
{
/******* fix start *******/
int w; /*!< Width */
int h; /*!< Height */
int c; /*!< Channel */
int n; /*!< Number of filter, input and output must be 1 */
int stride; /*!< Step between lines */
int exponent; /*!< Exponent for quantization */
qtp_t *item; /*!< Data */
int w; /*!< Width */
int h; /*!< Height */
int c; /*!< Channel */
int n; /*!< Number of filter, input and output must be 1 */
int stride; /*!< Step between lines */
int exponent; /*!< Exponent for quantization */
qtp_t *item; /*!< Data */
/******* fix end *******/
} dl_matrix3dq_t;
@ -38,23 +38,22 @@ typedef struct
*/
typedef enum
{
DL_C_IMPL = 0, /*!< ANSI C */
DL_XTENSA_IMPL = 1 /*!< Handwrite xtensa instruction */
DL_C_IMPL = 0, /*!< ANSI C */
DL_XTENSA_IMPL = 1 /*!< Handwrite xtensa instruction */
} dl_conv_mode;
/**
* Configuration of mobilenet operation
*/
typedef struct
{
int stride_x; /*!< Strides of width */
int stride_y; /*!< Strides of height */
dl_padding_type padding; /*!< Padding type */
dl_conv_mode mode; /*!< Implementation mode */
int dilate_exponent; /*!< Exponent of dilation filter */
int depthwise_exponent; /*!< Exponent of depthwise filter */
int compress_exponent; /*!< Exponent of compress filter */
int stride_x; /*!< Strides of width */
int stride_y; /*!< Strides of height */
dl_padding_type padding; /*!< Padding type */
dl_conv_mode mode; /*!< Implementation mode */
int dilate_exponent; /*!< Exponent of dilation filter */
int depthwise_exponent; /*!< Exponent of depthwise filter */
int compress_exponent; /*!< Exponent of compress filter */
} dl_matrix3dq_mobilenet_config_t;
//
@ -71,14 +70,52 @@ typedef struct
* @param e Exponent of matrix data
* @return 3d quantized matrix
*/
dl_matrix3dq_t *dl_matrix3dq_alloc(int n, int w, int h, int c, int e);
static inline dl_matrix3dq_t *dl_matrix3dq_alloc(int n, int w, int h, int c, int e)
{
dl_matrix3dq_t *r = (dl_matrix3dq_t *)dl_lib_calloc(1, sizeof(dl_matrix3dq_t), 0);
if (NULL == r)
{
printf("dl_matrix3dq alloc failed.\n");
return NULL;
}
qtp_t *items = (qtp_t *)dl_lib_calloc(n * w * h * c, sizeof(qtp_t), 16);
if (NULL == items)
{
printf("matrix3dq item alloc failed.\n");
dl_lib_free(r);
return NULL;
}
r->w = w;
r->h = h;
r->c = c;
r->n = n;
r->exponent = e;
r->stride = w * c;
r->item = items;
return r;
}
/*
* @brief Free a 3d quantized matrix
*
* @param m 3d quantised matrix
*/
void dl_matrix3dq_free(dl_matrix3dq_t *m);
static inline void dl_matrix3dq_free(dl_matrix3dq_t *m)
{
if (NULL == m)
return;
if (NULL == m->item)
{
dl_lib_free(m);
return;
}
dl_lib_free(m->item);
dl_lib_free(m);
}
/**
* @brief Copy a range of items from an existing matrix to a preallocated matrix
@ -92,6 +129,15 @@ void dl_matrix3dq_free(dl_matrix3dq_t *m);
*/
void dl_matrix3dq_slice_copy(dl_matrix3dq_t *dst, dl_matrix3dq_t *src, int x, int y, int w, int h);
/**
* @brief Transform a sliced matrix block from nhwc to nchw, the block needs to be memory continous.
*
* @param out The destination sliced matrix in nchw
* @param in The source sliced matrix in nhwc
*/
void dl_matrix3dq_sliced_transform_nchw(dl_matrix3dq_t *out,
dl_matrix3dq_t *in);
/**
* @brief Transform a fixed point matrix to a float point matrix
*
@ -245,7 +291,8 @@ dl_matrix3dq_t *dl_matrix3dq_concat_8(dl_matrix3dq_t *in_1,
void dl_matrix3dqq_conv_1x1(dl_matrix3dq_t *out,
dl_matrix3dq_t *in,
dl_matrix3dq_t *filter,
dl_conv_mode mode);
dl_conv_mode mode,
char *name);
/**
* @brief Do 1x1 convolution with a quantized matrix, with relu activation
@ -258,7 +305,8 @@ void dl_matrix3dqq_conv_1x1(dl_matrix3dq_t *out,
void dl_matrix3dqq_conv_1x1_with_relu(dl_matrix3dq_t *out,
dl_matrix3dq_t *in,
dl_matrix3dq_t *filter,
dl_conv_mode mode);
dl_conv_mode mode,
char *name);
/**
* @brief Do 1x1 convolution with a quantized matrix, with bias adding
@ -290,13 +338,25 @@ void dl_matrix3dqq_conv_1x1_with_bias_relu(dl_matrix3dq_t *out,
dl_matrix3dq_t *in,
dl_matrix3dq_t *filter,
dl_matrix3dq_t *bias,
dl_conv_mode mode);
dl_conv_mode mode,
char *name);
/**
* @brief
*
* @param out
* @param in
* @param filter
* @param prelu
* @param mode
* @param name
*/
void dl_matrix3dqq_conv_1x1_with_prelu(dl_matrix3dq_t *out,
dl_matrix3dq_t *in,
dl_matrix3dq_t *filter,
dl_matrix3dq_t *prelu,
dl_conv_mode mode);
dl_conv_mode mode,
char *name);
/**
* @brief Do 1x1 convolution with an 8-bit fixed point matrix
@ -309,7 +369,8 @@ void dl_matrix3dqq_conv_1x1_with_prelu(dl_matrix3dq_t *out,
void dl_matrix3duq_conv_1x1(dl_matrix3dq_t *out,
dl_matrix3du_t *in,
dl_matrix3dq_t *filter,
dl_conv_mode mode);
dl_conv_mode mode,
char *name);
/**
* @brief Do 1x1 convolution with an 8-bit fixed point matrix, with bias adding
@ -324,66 +385,47 @@ void dl_matrix3duq_conv_1x1_with_bias(dl_matrix3dq_t *out,
dl_matrix3du_t *in,
dl_matrix3dq_t *filter,
dl_matrix3dq_t *bias,
dl_conv_mode mode);
dl_conv_mode mode,
char *name);
//
// Conv 3x3
//
/**
* @brief Do 3x3 convolution basic operation with a quantized matrix
*
* @param out Preallocated quantized matrix
* @param in Input matrix, size (1, w, h, c)
* @param filter 3x3 filter, size (n, 3, 3, c)
* @param stride_x Stride of width
* @param stride_y Stride of height
*/
void dl_matrix3dqq_conv_3x3_op(dl_matrix3dq_t *out,
dl_matrix3dq_t *in,
dl_matrix3dq_t *filter,
int stride_x,
int stride_y);
/**
* @brief Do 3x3 convolution with a quantized matrix
*
* @param in Input matrix, size (1, w, h, c)
* @param filter 3x3 filter, size (n, 3, 3, c)
* @param stride_x Stride of width
* @param stride_y Stride of height
* @param padding Padding type, 0: valid, 1: same
* @param exponent Exponent for resulting matrix
* @return Resulting quantized matrix
*
* @param input Input matrix, size (1, w, h, c)
* @param filter 3x3 filter, size (n, 3, 3, c)
* @param stride_x Stride of width
* @param stride_y Stride of height
* @param padding Padding type, 0: valid, 1: same
* @param exponent Exponent for resulting matrix
* @param name
* @return dl_matrix3dq_t* Resulting quantized matrix
*/
dl_matrix3dq_t *dl_matrix3dqq_conv_3x3(dl_matrix3dq_t *in,
dl_matrix3dq_t *dl_matrix3dqq_conv_3x3(dl_matrix3dq_t *input,
dl_matrix3dq_t *filter,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent);
int exponent,
char *name);
/**
* @brief Do 3x3 convolution with a quantized matrix, with bias adding
*
* @param in Input matrix, size (1, w, h, c)
* @param filter 3x3 filter, size (n, 3, 3, c)
* @param bias Bias, size (1, 1, 1, n)
* @param stride_x Stride of width
* @param stride_y Stride of height
* @param padding Padding type, 0: valid, 1: same
* @param exponent Exponent for resulting matrix
* @return Resulting quantized matrix
*
* @param input Input matrix, size (1, w, h, c)
* @param filter 3x3 filter, size (n, 3, 3, c)
* @param bias Bias, size (1, 1, 1, n)
* @param stride_x Stride of width
* @param stride_y Stride of height
* @param padding
* @param exponent Exponent for resulting matrix
* @param name
* @return dl_matrix3dq_t* Resulting quantized matrix
*/
dl_matrix3dq_t *dl_matrix3dqq_conv_3x3_with_bias(dl_matrix3dq_t *in,
dl_matrix3dq_t *f,
dl_matrix3dq_t *bias,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent,
int relu);
dl_matrix3dq_t *dl_matrix3duq_conv_3x3_with_bias(dl_matrix3du_t *in,
dl_matrix3dq_t *dl_matrix3dqq_conv_3x3_with_bias(dl_matrix3dq_t *input,
dl_matrix3dq_t *filter,
dl_matrix3dq_t *bias,
int stride_x,
@ -392,7 +434,65 @@ dl_matrix3dq_t *dl_matrix3duq_conv_3x3_with_bias(dl_matrix3du_t *in,
int exponent,
char *name);
dl_matrix3dq_t *dl_matrix3duq_conv_3x3_with_bias_prelu(dl_matrix3du_t *in,
/**
* @brief Do 3x3 convolution with a quantized matrix, with bias adding, relu activation
*
* @param input Input matrix, size (1, w, h, c)
* @param filter 3x3 filter, size (n, 3, 3, c)
* @param bias Bias, size (1, 1, 1, n)
* @param stride_x Stride of width
* @param stride_y Stride of height
* @param padding
* @param exponent Exponent for resulting matrix
* @param name
* @return dl_matrix3dq_t* Resulting quantized matrix
*/
dl_matrix3dq_t *dl_matrix3dqq_conv_3x3_with_bias_relu(dl_matrix3dq_t *input,
dl_matrix3dq_t *filter,
dl_matrix3dq_t *bias,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent,
char *name);
/**
* @brief
*
* @param input
* @param filter
* @param bias
* @param stride_x
* @param stride_y
* @param padding
* @param exponent
* @param name
* @return dl_matrix3dq_t*
*/
dl_matrix3dq_t *dl_matrix3duq_conv_3x3_with_bias(dl_matrix3du_t *input,
dl_matrix3dq_t *filter,
dl_matrix3dq_t *bias,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent,
char *name);
/**
* @brief
*
* @param input
* @param filter
* @param bias
* @param prelu
* @param stride_x
* @param stride_y
* @param padding
* @param exponent
* @param name
* @return dl_matrix3dq_t*
*/
dl_matrix3dq_t *dl_matrix3duq_conv_3x3_with_bias_prelu(dl_matrix3du_t *input,
dl_matrix3dq_t *filter,
dl_matrix3dq_t *bias,
dl_matrix3dq_t *prelu,
@ -402,6 +502,17 @@ dl_matrix3dq_t *dl_matrix3duq_conv_3x3_with_bias_prelu(dl_matrix3du_t *in,
int exponent,
char *name);
dl_matrix3dq_t *dl_matrix3dqq_conv_3x3_with_bias_prelu(dl_matrix3dq_t *input,
dl_matrix3dq_t *filter,
dl_matrix3dq_t *bias,
dl_matrix3dq_t *prelu,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent,
char *name);
//
// Conv common
//
@ -469,7 +580,8 @@ dl_matrix3dq_t *dl_matrix3duq_depthwise_conv_3x3(dl_matrix3du_t *in,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent);
int exponent,
char *name);
/**
* @brief Do 3x3 depthwise convolution with a quantized matrix
@ -487,7 +599,8 @@ dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3(dl_matrix3dq_t *in,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent);
int exponent,
char *name);
#if CONFIG_DEVELOPING_CODE
dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3_2(dl_matrix3dq_t *in,
@ -495,14 +608,16 @@ dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3_2(dl_matrix3dq_t *in,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent);
int exponent,
char *name);
dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3_3(dl_matrix3dq_t *in,
dl_matrix3dq_t *filter,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent);
int exponent,
char *name);
#endif
/**
@ -519,13 +634,14 @@ dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3_3(dl_matrix3dq_t *in,
* @return Resulting quantized matrix
*/
dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3_with_bias(dl_matrix3dq_t *in,
dl_matrix3dq_t *f,
dl_matrix3dq_t *bias,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent,
int relu);
dl_matrix3dq_t *f,
dl_matrix3dq_t *bias,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent,
int relu,
char *name);
/**
* @brief Do 3x3 depthwise convolution with a quantized matrix, with bias adding and stride 1
@ -539,11 +655,12 @@ dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3_with_bias(dl_matrix3dq_t *in,
* @return Resulting quantized matrix
*/
dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3s1_with_bias(dl_matrix3dq_t *in,
dl_matrix3dq_t *f,
dl_matrix3dq_t *bias,
dl_padding_type padding,
int exponent,
int relu);
dl_matrix3dq_t *f,
dl_matrix3dq_t *bias,
dl_padding_type padding,
int exponent,
int relu,
char *name);
dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3_with_prelu(dl_matrix3dq_t *in,
dl_matrix3dq_t *filter,
@ -551,9 +668,25 @@ dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3_with_prelu(dl_matrix3dq_t *in,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent);
int exponent,
char *name);
dl_matrix3dq_t *dl_matrix3dqq_depthwise_conv_3x3_with_bias_prelu(dl_matrix3dq_t *in,
dl_matrix3dq_t *f,
dl_matrix3dq_t *bias,
dl_matrix3dq_t *prelu,
int stride_x,
int stride_y,
dl_padding_type padding,
int exponent,
char *name);
dl_matrix3dq_t *dl_matrix3dqq_global_depthwise_conv_with_bias(dl_matrix3dq_t *in,
dl_matrix3dq_t *filter,
dl_matrix3dq_t *bias,
int exponent,
char *name);
//
// Depthwise Common
//
@ -594,11 +727,13 @@ void dl_matrix3dqq_dot_product(dl_matrix3dq_t *out,
* @param in Input matrix, size (1, 1, 1, w)
* @param filter Filter matrix, size (1, w, h, 1)
* @param mode Implementation mode
* @param name
*/
void dl_matrix3dqq_fc(dl_matrix3dq_t *out,
dl_matrix3dq_t *in,
dl_matrix3dq_t *filter,
dl_conv_mode mode);
dl_conv_mode mode,
char *name);
/**
* @brief Do fully connected layer forward, with bias adding
@ -695,6 +830,86 @@ dl_matrix3dq_t *dl_matrix3dqq_mobilefaceblock(dl_matrix3dq_t *in,
dl_conv_mode mode,
int shortcut);
dl_matrix3dq_t *dl_matrix3dqq_mobilefaceblock_prelu(dl_matrix3dq_t *in,
dl_matrix3dq_t *pw,
dl_matrix3dq_t *pw_bias,
dl_matrix3dq_t *pw_prelu,
dl_matrix3dq_t *dw,
dl_matrix3dq_t *dw_bias,
dl_matrix3dq_t *dw_prelu,
dl_matrix3dq_t *pw_linear,
dl_matrix3dq_t *pw_linear_bias,
int pw_exponent,
int dw_exponent,
int pw_linear_exponent,
int stride_x,
int stride_y,
dl_padding_type padding,
dl_conv_mode mode,
int shortcut);
dl_matrix3dq_t *dl_matrix3dqq_mobilefaceblock_prelu_split_2_2(dl_matrix3dq_t *in,
dl_matrix3dq_t *pw_1,
dl_matrix3dq_t *pw_2,
dl_matrix3dq_t *pw_bias,
dl_matrix3dq_t *pw_prelu,
dl_matrix3dq_t *dw,
dl_matrix3dq_t *dw_bias,
dl_matrix3dq_t *dw_prelu,
dl_matrix3dq_t *pw_linear_1,
dl_matrix3dq_t *pw_linear_2,
dl_matrix3dq_t *pw_linear_bias,
int pw_exponent,
int dw_exponent,
int pw_linear_exponent,
int stride_x,
int stride_y,
dl_padding_type padding,
dl_conv_mode mode,
int shortcut);
dl_matrix3dq_t *dl_matrix3dqq_mobilefaceblock_prelu_split_4_4(dl_matrix3dq_t *in,
dl_matrix3dq_t *pw_1,
dl_matrix3dq_t *pw_2,
dl_matrix3dq_t *pw_3,
dl_matrix3dq_t *pw_4,
dl_matrix3dq_t *pw_bias,
dl_matrix3dq_t *pw_prelu,
dl_matrix3dq_t *dw,
dl_matrix3dq_t *dw_bias,
dl_matrix3dq_t *dw_prelu,
dl_matrix3dq_t *pw_linear_1,
dl_matrix3dq_t *pw_linear_2,
dl_matrix3dq_t *pw_linear_3,
dl_matrix3dq_t *pw_linear_4,
dl_matrix3dq_t *pw_linear_bias,
int pw_exponent,
int dw_exponent,
int pw_linear_exponent,
int stride_x,
int stride_y,
dl_padding_type padding,
dl_conv_mode mode,
int shortcut);
dl_matrix3dq_t *dl_matrix3dqq_mobilefaceblock_prelu_split_1_2(dl_matrix3dq_t *in,
dl_matrix3dq_t *pw,
dl_matrix3dq_t *pw_bias,
dl_matrix3dq_t *pw_prelu,
dl_matrix3dq_t *dw,
dl_matrix3dq_t *dw_bias,
dl_matrix3dq_t *dw_prelu,
dl_matrix3dq_t *pw_linear_1,
dl_matrix3dq_t *pw_linear_2,
dl_matrix3dq_t *pw_linear_bias,
int pw_exponent,
int dw_exponent,
int pw_linear_exponent,
int stride_x,
int stride_y,
dl_padding_type padding,
dl_conv_mode mode,
int shortcut);
//
// Mobilenet
//
@ -749,23 +964,49 @@ dl_matrix3dq_t *dl_matrix3duq_mobilenet(dl_matrix3du_t *in,
// Padding
//
dl_error_type dl_matrix3dqq_padding(dl_matrix3dq_t **padded_in,
dl_matrix3dq_t **out,
dl_matrix3dq_t *in,
int out_c,
/**
* @brief
*
* @param padded_input
* @param output_height
* @param output_width
* @param input
* @param stride_x
* @param stride_y
* @param kernel_size
* @param padding_type
* @return dl_error_type
*/
dl_error_type dl_matrix3dqq_padding(dl_matrix3dq_t **padded_input,
int *output_height,
int *output_width,
dl_matrix3dq_t *input,
int stride_x,
int stride_y,
int padding,
int exponent);
int kernel_size,
dl_padding_type padding_type);
dl_error_type dl_matrix3duq_padding(dl_matrix3du_t **padded_in,
dl_matrix3dq_t **out,
dl_matrix3du_t *in,
int out_c,
/**
* @brief
*
* @param padded_input
* @param output_height
* @param output_width
* @param input
* @param stride_x
* @param stride_y
* @param kernel_size
* @param padding_type
* @return dl_error_type
*/
dl_error_type dl_matrix3duq_padding(dl_matrix3du_t **padded_input,
int *output_height,
int *output_width,
dl_matrix3du_t *input,
int stride_x,
int stride_y,
int padding,
int exponent);
int kernel_size,
dl_padding_type padding_type);
//
// Pooling
@ -777,3 +1018,23 @@ dl_error_type dl_matrix3duq_padding(dl_matrix3du_t **padded_in,
* @return Resulting matrix, size (1, 1, 1, c)
*/
dl_matrix3dq_t *dl_matrix3dq_global_pool(dl_matrix3dq_t *in);
/**
* @brief Calculate pooling layer of a feature map
*
* @param in Input matrix, size (1, w, h, c)
* @param f_w Window width
* @param f_h Window height
* @param stride_x Stride in horizontal direction
* @param stride_y Stride in vertical direction
* @param padding Padding type: PADDING_VALID and PADDING_SAME
* @param pooling_type Pooling type: DL_POOLING_MAX and POOLING_AVG
* @return Resulting matrix, size (1, w', h', c)
*/
dl_matrix3dq_t *dl_matrix3dq_pooling(dl_matrix3dq_t *in,
int f_w,
int f_h,
int stride_x,
int stride_y,
dl_padding_type padding,
dl_pooling_type pooling_type);

View File

@ -12,7 +12,7 @@ extern "C"
#define FACE_WIDTH 56
#define FACE_HEIGHT 56
#define FACE_ID_SIZE 512
#define FACE_REC_THRESHOLD 0.5
#define FACE_REC_THRESHOLD 0.55
#define LEFT_EYE_X 0
#define LEFT_EYE_Y 1
@ -20,6 +20,10 @@ extern "C"
#define RIGHT_EYE_Y 7
#define NOSE_X 4
#define NOSE_Y 5
#define LEFT_MOUTH_X 2
#define LEFT_MOUTH_Y 3
#define RIGHT_MOUTH_X 8
#define RIGHT_MOUTH_Y 9
#define EYE_DIST_SET 16.5f
#define NOSE_EYE_RATIO_THRES_MIN 0.49f
@ -81,13 +85,24 @@ extern "C"
* @return ESP_OK Input face is good for recognition
* @return ESP_FAIL Input face is not good for recognition
*/
int8_t align_face(box_array_t *onet_boxes,
int8_t align_face_rot(box_array_t *onet_boxes,
dl_matrix3du_t *src,
dl_matrix3du_t *dest);
int8_t align_face2(fptp_t *landmark,
dl_matrix3du_t *src,
dl_matrix3du_t *dest);
int8_t align_face_sim(box_array_t *onet_boxes,
dl_matrix3du_t *src,
dl_matrix3du_t *dest);
inline int8_t align_face(box_array_t *onet_boxes,
dl_matrix3du_t *src,
dl_matrix3du_t *dest)
{
return align_face_sim(onet_boxes, src, dest);
}
/**
* @brief Run the face recognition model to get the face feature

View File

@ -25,15 +25,6 @@ extern "C"
*/
dl_matrix3dq_t *frmn_q(dl_matrix3dq_t *in, dl_conv_mode mode);
/**
* @brief Forward the face recognition process with frmn2 model. Calculate in quantization.
*
* @param in Image matrix, rgb888 format, size is 56x56, normalized
* @param mode 0: C implement; 1: handwrite xtensa instruction implement
* @return Face ID feature vector, size is 512
*/
dl_matrix3dq_t *frmn2_q(dl_matrix3dq_t *in, dl_conv_mode mode);
/**
* @brief Forward the face recognition process with frmn2p model. Calculate in quantization.
*
@ -43,14 +34,14 @@ extern "C"
*/
dl_matrix3dq_t *frmn2p_q(dl_matrix3dq_t *in, dl_conv_mode mode);
/**
* @brief Forward the face recognition process with frmn2c model. Calculate in quantization.
*
* @param in Image matrix, rgb888 format, size is 56x56, normalized
* @param mode 0: C implement; 1: handwrite xtensa instruction implement
* @return Face ID feature vector, size is 512
*/
dl_matrix3dq_t *frmn2c_q(dl_matrix3dq_t *in, dl_conv_mode mode);
dl_matrix3dq_t *mfn56_42m_q(dl_matrix3dq_t *in, dl_conv_mode mode);
dl_matrix3dq_t *mfn56_72m_q(dl_matrix3dq_t *in, dl_conv_mode mode);
dl_matrix3dq_t *mfn56_112m_q(dl_matrix3dq_t *in, dl_conv_mode mode);
dl_matrix3dq_t *mfn56_156m_q(dl_matrix3dq_t *in, dl_conv_mode mode);
#if __cplusplus
}

View File

@ -35,9 +35,6 @@ extern "C"
#define DL_IMAGE_MIN(A, B) ((A) < (B) ? (A) : (B))
#define DL_IMAGE_MAX(A, B) ((A) < (B) ? (B) : (A))
#define IMAGE_WIDTH 320
#define IMAGE_HEIGHT 240
#define RGB565_MASK_RED 0xF800
#define RGB565_MASK_GREEN 0x07E0
#define RGB565_MASK_BLUE 0x001F
@ -93,7 +90,7 @@ extern "C"
*area = w * h;
}
static inline void image_calibrate_by_offset(image_list_t *image_list)
static inline void image_calibrate_by_offset(image_list_t *image_list, int image_height, int image_width)
{
for (image_box_t *head = image_list->head; head; head = head->next)
{
@ -102,16 +99,16 @@ extern "C"
head->box.box_p[0] = DL_IMAGE_MAX(0, head->box.box_p[0] + head->offset.box_p[0] * w);
head->box.box_p[1] = DL_IMAGE_MAX(0, head->box.box_p[1] + head->offset.box_p[1] * w);
head->box.box_p[2] += head->offset.box_p[2] * w;
if (head->box.box_p[2] > IMAGE_WIDTH)
if (head->box.box_p[2] > image_width)
{
head->box.box_p[2] = IMAGE_WIDTH - 1;
head->box.box_p[0] = IMAGE_WIDTH - w;
head->box.box_p[2] = image_width - 1;
head->box.box_p[0] = image_width - w;
}
head->box.box_p[3] += head->offset.box_p[3] * h;
if (head->box.box_p[3] > IMAGE_HEIGHT)
if (head->box.box_p[3] > image_height)
{
head->box.box_p[3] = IMAGE_HEIGHT - 1;
head->box.box_p[1] = IMAGE_HEIGHT - h;
head->box.box_p[3] = image_height - 1;
head->box.box_p[1] = image_height - h;
}
}
}
@ -154,8 +151,8 @@ extern "C"
int h = y2 - y1 + 1;
int l = DL_IMAGE_MAX(w, h);
box->box_p[0] = round(DL_IMAGE_MAX(0, x1) + 0.5 * (w - l));
box->box_p[1] = round(DL_IMAGE_MAX(0, y1) + 0.5 * (h - l));
box->box_p[0] = DL_IMAGE_MAX(round(DL_IMAGE_MAX(0, x1) + 0.5 * (w - l)), 0);
box->box_p[1] = DL_IMAGE_MAX(round(DL_IMAGE_MAX(0, y1) + 0.5 * (h - l)), 0);
box->box_p[2] = box->box_p[0] + l - 1;
if (box->box_p[2] > width)
@ -193,20 +190,30 @@ extern "C"
*
* @param score
* @param offset
* @param landmark
* @param width
* @param height
* @param p_net_size
* @param anchor_number
* @param anchors_size
* @param score_threshold
* @param scale
* @param stride
* @param resized_height_scale
* @param resized_width_scale
* @param do_regression
* @return image_list_t*
*/
image_list_t *image_get_valid_boxes(fptp_t *score,
fptp_t *offset,
fptp_t *landmark,
int width,
int height,
int p_net_size,
int anchor_number,
int *anchors_size,
fptp_t score_threshold,
fptp_t scale);
int stride,
fptp_t resized_height_scale,
fptp_t resized_width_scale,
bool do_regression);
/**
* @brief
*
@ -284,6 +291,7 @@ extern "C"
* @param count
*/
void transform_output_image(uint16_t *bmp, uint8_t *m, int count);
void transform_output_image_adjustable(uint16_t *bmp, uint8_t *m, int src_w, int src_h, int dst_w, int dst_h);
/**
* @brief
@ -305,6 +313,20 @@ extern "C"
void image_abs_diff(uint8_t *dst, uint8_t *src1, uint8_t *src2, int count);
void image_threshold(uint8_t *dst, uint8_t *src, int threshold, int value, int count, en_threshold_mode mode);
void image_erode(uint8_t *dst, uint8_t *src, int src_w, int src_h, int src_c);
typedef float matrixType;
typedef struct
{
int w;
int h;
matrixType **array;
} Matrix;
Matrix *matrix_alloc(int h, int w);
void matrix_free(Matrix *m);
Matrix *get_similarity_matrix(float *srcx, float *srcy, float *dstx, float *dsty, int num);
void warp_affine(dl_matrix3du_t *img, dl_matrix3du_t *crop, Matrix *M);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,91 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS products only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "dl_lib_matrix3d.h"
#include "dl_lib_matrix3dq.h"
#include "freertos/FreeRTOS.h"
typedef struct
{
int resized_height;
int resized_width;
fptp_t y_resize_scale;
fptp_t x_resize_scale;
int enabled_top_k;
fptp_t score_threshold;
fptp_t nms_threshold;
dl_conv_mode mode;
} lssh_config_t;
typedef struct
{
int *anchor_size;
int stride;
int boundary;
} lssh_module_config_t;
typedef struct
{
lssh_module_config_t *module_config;
int number;
} lssh_modules_config_t;
typedef struct
{
dl_matrix3d_t *category;
dl_matrix3d_t *box_offset;
dl_matrix3d_t *landmark_offset;
} lssh_module_result_t;
/**
* @brief
*
* @param value
*/
void lssh_module_result_free(lssh_module_result_t value);
/**
* @brief
*
* @param values
* @param length
*/
void lssh_module_results_free(lssh_module_result_t *values, int length);
/////////////////////////
//////sparse_mn_5_q//////
/////////////////////////
extern lssh_modules_config_t sparse_mn_5_modules_config;
lssh_module_result_t *sparse_mn_5_q_without_landmark(dl_matrix3du_t *image, bool free_image, int enabled_top_k, dl_conv_mode mode);
lssh_module_result_t *sparse_mn_5_q_with_landmark(dl_matrix3du_t *image, bool free_image, int enabled_top_k, dl_conv_mode mode);
#ifdef __cplusplus
}
#endif