File size: 5,577 Bytes
c170a54 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | // Adapted from Shiqi Yu's libfacedetection (3-clause BSD) for FaceDetector (yunet_n, box-only).
#include "facedetectcnn.h"
#include <cstring>
#define NUM_CONV_LAYER 53
extern ConvInfoStruct param_pConvInfo[NUM_CONV_LAYER];
Filters<float> g_pFilters[NUM_CONV_LAYER];
bool param_initialized = false;
void init_parameters()
{
for (int i = 0; i < NUM_CONV_LAYER; i++)
g_pFilters[i] = param_pConvInfo[i];
}
std::vector<FaceRect> objectdetect_cnn(const unsigned char* rgbImageData, int width, int height, int step)
{
if (!param_initialized)
{
init_parameters();
param_initialized = true;
}
auto fx = setDataFrom3x3S2P1to1x1S1P0FromImage(rgbImageData, width, height, 3, step);
/***************CONV0*********************/
fx = convolution(fx, g_pFilters[0]);
fx = convolutionDP(fx, g_pFilters[1], g_pFilters[2]);
fx = maxpooling2x2S2(fx);
/***************CONV1*********************/
fx = convolution4layerUnit(fx, g_pFilters[3], g_pFilters[4], g_pFilters[5], g_pFilters[6]);
/***************CONV2*********************/
fx = convolution4layerUnit(fx, g_pFilters[7], g_pFilters[8], g_pFilters[9], g_pFilters[10]);
/***************CONV3*********************/
fx = maxpooling2x2S2(fx);
auto fb1 = convolution4layerUnit(fx, g_pFilters[11], g_pFilters[12], g_pFilters[13], g_pFilters[14]);
/***************CONV4*********************/
fx = maxpooling2x2S2(fb1);
auto fb2 = convolution4layerUnit(fx, g_pFilters[15], g_pFilters[16], g_pFilters[17], g_pFilters[18]);
/***************CONV5*********************/
fx = maxpooling2x2S2(fb2);
auto fb3 = convolution4layerUnit(fx, g_pFilters[19], g_pFilters[20], g_pFilters[21], g_pFilters[22]);
// yunet_n: shared_stacked_convs=1, use_kps=False
CDataBlob<float> pred_reg[3], pred_cls[3], pred_obj[3];
/***************branch5*********************/
fb3 = convolutionDP(fb3, g_pFilters[27], g_pFilters[28]);
auto sh3 = convolutionDP(fb3, g_pFilters[33], g_pFilters[34]);
pred_cls[2] = convolutionDP(sh3, g_pFilters[39], g_pFilters[40], false);
pred_reg[2] = convolutionDP(sh3, g_pFilters[45], g_pFilters[46], false);
pred_obj[2] = convolutionDP(sh3, g_pFilters[51], g_pFilters[52], false);
/*****************add5*********************/
fb2 = elementAdd(upsampleX2(fb3), fb2);
/*****************branch4*********************/
fb2 = convolutionDP(fb2, g_pFilters[25], g_pFilters[26]);
auto sh2 = convolutionDP(fb2, g_pFilters[31], g_pFilters[32]);
pred_cls[1] = convolutionDP(sh2, g_pFilters[37], g_pFilters[38], false);
pred_reg[1] = convolutionDP(sh2, g_pFilters[43], g_pFilters[44], false);
pred_obj[1] = convolutionDP(sh2, g_pFilters[49], g_pFilters[50], false);
/*****************add4*********************/
fb1 = elementAdd(upsampleX2(fb2), fb1);
/***************branch3*********************/
fb1 = convolutionDP(fb1, g_pFilters[23], g_pFilters[24]);
auto sh1 = convolutionDP(fb1, g_pFilters[29], g_pFilters[30]);
pred_cls[0] = convolutionDP(sh1, g_pFilters[35], g_pFilters[36], false);
pred_reg[0] = convolutionDP(sh1, g_pFilters[41], g_pFilters[42], false);
pred_obj[0] = convolutionDP(sh1, g_pFilters[47], g_pFilters[48], false);
/***************PRIORBOX*********************/
auto prior3 = meshgrid(fb1.cols, fb1.rows, 8);
auto prior4 = meshgrid(fb2.cols, fb2.rows, 16);
auto prior5 = meshgrid(fb3.cols, fb3.rows, 32);
bbox_decode(pred_reg[0], prior3, 8);
bbox_decode(pred_reg[1], prior4, 16);
bbox_decode(pred_reg[2], prior5, 32);
auto cls = concat3(blob2vector(pred_cls[0]), blob2vector(pred_cls[1]), blob2vector(pred_cls[2]));
auto reg = concat3(blob2vector(pred_reg[0]), blob2vector(pred_reg[1]), blob2vector(pred_reg[2]));
auto obj = concat3(blob2vector(pred_obj[0]), blob2vector(pred_obj[1]), blob2vector(pred_obj[2]));
// Box-only model (use_kps=False): supply zeroed kps vector to keep upstream detection_output ABI intact.
CDataBlob<float> kps(1, 1, obj.channels * 10);
if (kps.data)
std::memset(kps.data, 0, size_t(kps.cols) * size_t(kps.rows) * size_t(kps.channelStep));
sigmoid(cls);
sigmoid(obj);
// Threshold: 0.45f on sqrt(cls*obj) (~0.2025 on cls*obj), NMS IoU 0.45f
return detection_output(cls, reg, kps, obj, 0.45f, 0.45f, 1000, 512);
}
int* facedetect_cnn(unsigned char* result_buffer,
unsigned char* rgb_image_data, int width, int height, int step)
{
if (!result_buffer)
{
fprintf(stderr, "%s: null buffer memory.\n", __FUNCTION__);
return NULL;
}
result_buffer[0] = 0;
result_buffer[1] = 0;
result_buffer[2] = 0;
result_buffer[3] = 0;
std::vector<FaceRect> faces = objectdetect_cnn(rgb_image_data, width, height, step);
int num_faces = (int)faces.size();
num_faces = MIN(num_faces, FACEDETECTION_RESULT_MAX_FACES);
int* pCount = (int*)result_buffer;
pCount[0] = num_faces;
for (int i = 0; i < num_faces; i++)
{
short* p = ((short*)(result_buffer + 4)) + FACEDETECTION_RESULT_STRIDE_SHORTS * size_t(i);
p[0] = (short)(faces[i].score * 100);
p[1] = (short)faces[i].x;
p[2] = (short)faces[i].y;
p[3] = (short)faces[i].w;
p[4] = (short)faces[i].h;
for (int lmidx = 0; lmidx < 10; lmidx++)
p[5 + lmidx] = 0;
}
return pCount;
}
|