#include "facedet.h" #include "gemm.h" #include "nn.h" #include #include #include #include #include enum { OP_CONV = 1, OP_MAXPOOL, OP_RESIZE2X, OP_ADD }; typedef struct { int op, group, k, s, pad, relu, widx; int cin, ih, iw, cout, oh, ow; size_t in0_off, in1_off, out_off; } facedet_op; typedef struct { const char *name; size_t off; int c, h, w, sigmoid; } facedet_map_info; #ifndef FACEDET_MODEL_HEADER #define FACEDET_MODEL_HEADER "model_data.h" #endif #include FACEDET_MODEL_HEADER #ifdef FACEDET_REFINER_HEADER #include FACEDET_REFINER_HEADER _Static_assert(REFINER_OP_COUNT == MODEL_OP_COUNT, "Refiner operation count mismatch"); _Static_assert(REFINER_MAP_COUNT == MODEL_MAP_COUNT, "Refiner map count mismatch"); _Static_assert(REFINER_ARENA_BYTES == MODEL_ARENA_BYTES, "Refiner arena mismatch"); #endif struct facedet { int width, height, has_result; unsigned char *arena; facedet_op ops[MODEL_OP_COUNT]; facedet_map_info maps[MODEL_MAP_COUNT]; float *packs[MODEL_OP_COUNT]; float *im2col, *bscratch; const float *const *weights; int is_refiner; }; static facedet *create_model(int width, int height, const facedet_op *ops, const facedet_map_info *maps, const float *const *weights) { if (width < 32 || height < 32 || width > 4096 || height > 4096 || width % 32 || height % 32) return NULL; size_t area = (size_t)(width / 32) * (height / 32); if (area > SIZE_MAX / MODEL_ARENA_BYTES) return NULL; facedet *m = calloc(1, sizeof *m); if (!m) return NULL; m->width = width; m->height = height; m->weights = weights; m->arena = malloc(MODEL_ARENA_BYTES * area); if (!m->arena) goto fail; memset(m->arena, 0, (size_t)width * height * 3 * sizeof(float)); size_t max_col = 0, max_k = 0; for (int i = 0; i < MODEL_OP_COUNT; i++) { facedet_op op = ops[i]; /* All spatial dimensions scale from the 32x32 plan. Scaling every * arena slot by the same area preserves its capacity and lifetime. */ op.ih *= height / 32; op.iw *= width / 32; op.oh *= height / 32; op.ow *= width / 32; op.in0_off *= area; op.in1_off *= area; op.out_off *= area; m->ops[i] = op; if (op.op == OP_CONV && op.group == 1) { int k = op.cin * op.k * op.k; m->packs[i] = malloc(facedet_gemm_pack_a_bytes(op.cout, k)); if (!m->packs[i]) goto fail; facedet_gemm_pack_a(weights[op.widx], op.cout, k, m->packs[i]); if ((size_t)k > max_k) max_k = (size_t)k; size_t col = op.k == 3 ? (size_t)k * op.oh * op.ow : 0; if (col > max_col) max_col = col; } } if (max_col > SIZE_MAX / sizeof(float)) goto fail; m->im2col = malloc(max_col * sizeof(float)); m->bscratch = malloc(max_k * 12 * sizeof(float)); if (!m->im2col || !m->bscratch) goto fail; for (int i = 0; i < MODEL_MAP_COUNT; i++) { m->maps[i] = maps[i]; m->maps[i].off *= area; m->maps[i].h *= height / 32; m->maps[i].w *= width / 32; } return m; fail: facedet_free(m); return NULL; } facedet *facedet_create(int width, int height) { return create_model(width, height, model_ops, model_maps, model_weights); } facedet *facedet_create_refiner(void) { #ifdef FACEDET_REFINER_HEADER facedet *m = create_model(160, 160, refiner_ops, refiner_maps, refiner_weights); if (m) m->is_refiner = 1; return m; #else return NULL; #endif } void facedet_free(facedet *m) { if (!m) return; for (int i = 0; i < MODEL_OP_COUNT; i++) free(m->packs[i]); free(m->im2col); free(m->bscratch); free(m->arena); free(m); } float *facedet_input(facedet *m) { if (!m) return NULL; m->has_result = 0; return (float *)m->arena; } int facedet_set_rgb(facedet *m, const unsigned char *rgb, int width, int height, size_t row_bytes) { if (!m || !rgb || width < 1 || height < 1 || width > m->width || height > m->height || row_bytes < (size_t)width * 3 || (height > 1 && row_bytes > (SIZE_MAX - (size_t)width * 3) / (size_t)(height - 1))) return -1; float *input = facedet_input(m); size_t plane = (size_t)m->width * m->height; memset(input, 0, plane * 3 * sizeof(float)); for (int y = 0; y < height; y++) { const unsigned char *row = rgb + (size_t)y * row_bytes; for (int x = 0; x < width; x++) for (int c = 0; c < 3; c++) input[(size_t)c * plane + (size_t)y * m->width + x] = row[x * 3 + 2 - c]; } return 0; } int facedet_run(facedet *m) { if (!m) return -1; m->has_result = 0; const float *input = (const float *)m->arena; for (size_t i = 0; i < (size_t)m->width * m->height * 3; i++) if (!isfinite(input[i]) || input[i] < 0.0f || input[i] > 255.0f) return -1; unsigned char *a = m->arena; for (int i = 0; i < MODEL_OP_COUNT; i++) { facedet_op op = m->ops[i]; const float *in0 = (const float *)(a + op.in0_off); float *out = (float *)(a + op.out_off); switch (op.op) { case OP_CONV: { size_t n = (size_t)op.oh * op.ow; const float *bias = m->weights[op.widx + 1]; if (op.group == 1 && op.k == 1 && op.s == 1) { facedet_gemm(m->packs[i], op.cout, op.cin, in0, (size_t)op.ih * op.iw, (int)n, bias, op.relu, out, n, m->bscratch); } else if (op.group == 1 && op.k == 3) { facedet_nn_im2col3(in0, op.cin, op.ih, op.iw, op.s, m->im2col, op.oh, op.ow); facedet_gemm(m->packs[i], op.cout, op.cin * 9, m->im2col, n, (int)n, bias, op.relu, out, n, m->bscratch); } else if (op.group == op.cin && op.group == op.cout && op.k == 3 && op.s == 1) { facedet_nn_dwconv3(in0, op.cin, op.ih, op.iw, m->weights[op.widx], bias, op.relu, out); } else { facedet_nn_conv2d(in0, op.cin, op.ih, op.iw, m->weights[op.widx], bias, op.cout, op.k, op.s, op.pad, op.group, op.relu, out, op.oh, op.ow); } break; } case OP_MAXPOOL: facedet_nn_maxpool2(in0, op.cin, op.ih, op.iw, out); break; case OP_RESIZE2X: facedet_nn_resize2x(in0, op.cin, op.ih, op.iw, out); break; case OP_ADD: facedet_nn_add(in0, (const float *)(a + op.in1_off), out, (size_t)op.cin * op.ih * op.iw); break; } } m->has_result = 1; return 0; } const float *facedet_map(const facedet *m, const char *name, int *channels, int *height, int *width) { if (!m || !name || !m->has_result) return NULL; for (int i = 0; i < MODEL_MAP_COUNT; i++) { const facedet_map_info *mm = &m->maps[i]; if (strcmp(mm->name, name) == 0) { if (channels) *channels = mm->c; if (height) *height = mm->h; if (width) *width = mm->w; return (const float *)(m->arena + mm->off); } } return NULL; } static float sample_rgb(const unsigned char *rgb, int width, int height, size_t row_bytes, double x, double y, int channel) { if (x <= -1 || x >= width || y <= -1 || y >= height) return 0; int x0 = (int)floor(x), y0 = (int)floor(y); double dx = x - x0, dy = y - y0, value = 0; for (int j = 0; j < 2; j++) { int yy = y0 + j; if (yy < 0 || yy >= height) continue; for (int i = 0; i < 2; i++) { int xx = x0 + i; if (xx >= 0 && xx < width) value += rgb[(size_t)yy * row_bytes + (size_t)xx * 3 + channel] * (i ? dx : 1 - dx) * (j ? dy : 1 - dy); } } return (float)value; } int facedet_refine(facedet *m, const unsigned char *rgb, int width, int height, size_t row_bytes, facedet_detection *faces, int count) { if (!m || !m->is_refiner || !rgb || width < 1 || height < 1 || width > 4096 || height > 4096 || count < 0 || (!faces && count) || row_bytes < (size_t)width * 3 || (height > 1 && row_bytes > (SIZE_MAX - (size_t)width * 3) / (size_t)(height - 1))) return -1; if (!count) return 0; if ((size_t)count > SIZE_MAX / (10 * sizeof(float))) return -1; float (*points)[5][2] = malloc((size_t)count * sizeof *points); if (!points) return -1; for (int i = 0; i < count; i++) { const facedet_detection *face = &faces[i]; if (!isfinite(face->x) || !isfinite(face->y) || !isfinite(face->w) || !isfinite(face->h) || face->w <= 0 || face->h <= 0) goto fail; double extent = fmax(face->w, face->h) * 1.3; double ox = face->x + (double)face->w / 2 - extent / 2; double oy = face->y + (double)face->h / 2 - extent / 2; float *input = facedet_input(m); for (int y = 0; y < 160; y++) for (int x = 0; x < 160; x++) for (int c = 0; c < 3; c++) input[(size_t)c * 160 * 160 + y * 160 + x] = sample_rgb(rgb, width, height, row_bytes, ox + (x + .5) * extent / 160 - .5, oy + (y + .5) * extent / 160 - .5, 2 - c); if (facedet_run(m)) goto fail; int channels, h, w; /* The landmark readout is the fixed center cell, independent of objectness. */ const float *map = facedet_map(m, "kps_8", &channels, &h, &w); if (!map || channels != 10 || h != 20 || w != 20) goto fail; for (int k = 0; k < 5; k++) { double px = ox + (map[(2 * k) * 400 + 210] * 8.0 + 80.5) * extent / 160 - .5; double py = oy + (map[(2 * k + 1) * 400 + 210] * 8.0 + 80.5) * extent / 160 - .5; if (!isfinite(px) || !isfinite(py) || fabs(px) > FLT_MAX || fabs(py) > FLT_MAX) goto fail; points[i][k][0] = (float)px; points[i][k][1] = (float)py; } } for (int i = 0; i < count; i++) memcpy(faces[i].lmk, points[i], sizeof faces[i].lmk); free(points); return 0; fail: free(points); return -1; } static float iou_xywh(const facedet_detection *a, const facedet_detection *b) { float x0 = a->x > b->x ? a->x : b->x; float y0 = a->y > b->y ? a->y : b->y; float x1 = (a->x + a->w) < (b->x + b->w) ? a->x + a->w : b->x + b->w; float y1 = (a->y + a->h) < (b->y + b->h) ? a->y + a->h : b->y + b->h; float iw = x1 - x0 > 0.0f ? x1 - x0 : 0.0f; float ih = y1 - y0 > 0.0f ? y1 - y0 : 0.0f; float inter = iw * ih; float uni = a->w * a->h + b->w * b->h - inter; return uni > 0.0f ? inter / uni : 0.0f; } static int det_cmp(const void *pa, const void *pb) { const facedet_detection *a = pa, *b = pb; if (a->score != b->score) return a->score > b->score ? -1 : 1; if (a->y != b->y) return a->y < b->y ? -1 : 1; return a->x < b->x ? -1 : a->x > b->x; } int facedet_decode(const facedet *m, float score_thresh, float nms_iou, facedet_detection *out, int cap) { if (!m || m->is_refiner || !m->has_result || (!out && cap > 0) || cap < 0 || !isfinite(score_thresh) || score_thresh < 0.0f || score_thresh > 1.0f || !isfinite(nms_iou) || nms_iou < 0.0f || nms_iou > 1.0f) return -1; if (cap == 0) return 0; static const int strides[3] = {8, 16, 32}; static const char *names[3][4] = { {"cls_8", "obj_8", "bbox_8", "kps_8"}, {"cls_16", "obj_16", "bbox_16", "kps_16"}, {"cls_32", "obj_32", "bbox_32", "kps_32"}, }; size_t count = 0; for (int lv = 0; lv < 3; lv++) count += (size_t)(m->height / strides[lv]) * (m->width / strides[lv]); facedet_detection *cand = malloc(sizeof(*cand) * count); if (!cand) return -1; int n = 0; for (int lv = 0; lv < 3; lv++) { int H, W, s = strides[lv]; const float *cls = facedet_map(m, names[lv][0], NULL, &H, &W); const float *obj = facedet_map(m, names[lv][1], NULL, NULL, NULL); const float *bbox = facedet_map(m, names[lv][2], NULL, NULL, NULL); const float *kps = facedet_map(m, names[lv][3], NULL, NULL, NULL); size_t hw = (size_t)H * W; for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { size_t i = (size_t)y * W + x; float sc = sqrtf(facedet_nn_sigmoid(cls[i]) * facedet_nn_sigmoid(obj[i])); if (!isfinite(sc) || sc < score_thresh) continue; facedet_detection *d = &cand[n]; float cx = (x + bbox[i]) * s; float cy = (y + bbox[hw + i]) * s; d->w = expf(bbox[2 * hw + i]) * s; d->h = expf(bbox[3 * hw + i]) * s; d->x = cx - d->w / 2.0f; d->y = cy - d->h / 2.0f; int valid = isfinite(d->x) && isfinite(d->y) && isfinite(d->w * d->h) && d->w > 0.0f && d->h > 0.0f; for (int k = 0; k < 5; k++) { d->lmk[k][0] = (x + kps[(size_t)(2 * k) * hw + i]) * s; d->lmk[k][1] = (y + kps[(size_t)(2 * k + 1) * hw + i]) * s; valid &= isfinite(d->lmk[k][0]) && isfinite(d->lmk[k][1]); } d->score = sc; n += valid; } } } qsort(cand, (size_t)n, sizeof(*cand), det_cmp); int kept = 0; for (int i = 0; i < n && kept < cap; i++) { int keep = 1; for (int j = 0; j < kept; j++) if (iou_xywh(&cand[i], &out[j]) > nms_iou) { keep = 0; break; } if (keep) out[kept++] = cand[i]; } free(cand); return kept; }