aboutsummaryrefslogtreecommitdiff
path: root/facedet.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-10-21 17:47:57 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-10-21 17:47:57 +0200
commit8b8acffcf5f1e9e9ddb552b8ac125e50f68987ac (patch)
tree9b618133a468585087ff4b014b22029d40b24bc5 /facedet.c
parent2ce585cc96a3fffba65f1bafbfbc796c6370308f (diff)
downloadfacedet-master.tar.gz
facedet-master.zip
Rename to facedetHEADv0.1.0master
Diffstat (limited to '')
-rw-r--r--facedet.c (renamed from yunet.c)108
1 files changed, 54 insertions, 54 deletions
diff --git a/yunet.c b/facedet.c
index 4e38f71..33f72bd 100644
--- a/yunet.c
+++ b/facedet.c
@@ -1,4 +1,4 @@
-#include "yunet.h"
+#include "facedet.h"
#include "gemm.h"
#include "nn.h"
@@ -14,45 +14,45 @@ 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;
-} yunet_op;
+} facedet_op;
typedef struct {
const char *name;
size_t off;
int c, h, w, sigmoid;
-} yunet_map_info;
+} facedet_map_info;
-#ifndef YUNET_MODEL_HEADER
-#define YUNET_MODEL_HEADER "model_data.h"
+#ifndef FACEDET_MODEL_HEADER
+#define FACEDET_MODEL_HEADER "model_data.h"
#endif
-#include YUNET_MODEL_HEADER
-#ifdef YUNET_REFINER_HEADER
-#include YUNET_REFINER_HEADER
+#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 yunet {
+struct facedet {
int width, height, has_result;
unsigned char *arena;
- yunet_op ops[MODEL_OP_COUNT];
- yunet_map_info maps[MODEL_MAP_COUNT];
+ 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 yunet *create_model(int width, int height, const yunet_op *ops,
- const yunet_map_info *maps, const float *const *weights) {
+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;
- yunet *m = calloc(1, sizeof *m);
+ facedet *m = calloc(1, sizeof *m);
if (!m)
return NULL;
m->width = width;
@@ -64,7 +64,7 @@ static yunet *create_model(int width, int height, const yunet_op *ops,
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++) {
- yunet_op op = ops[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;
@@ -77,10 +77,10 @@ static yunet *create_model(int width, int height, const yunet_op *ops,
m->ops[i] = op;
if (op.op == OP_CONV && op.group == 1) {
int k = op.cin * op.k * op.k;
- m->packs[i] = malloc(yunet_gemm_pack_a_bytes(op.cout, k));
+ m->packs[i] = malloc(facedet_gemm_pack_a_bytes(op.cout, k));
if (!m->packs[i])
goto fail;
- yunet_gemm_pack_a(weights[op.widx], op.cout, k, m->packs[i]);
+ 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;
@@ -102,17 +102,17 @@ static yunet *create_model(int width, int height, const yunet_op *ops,
}
return m;
fail:
- yunet_free(m);
+ facedet_free(m);
return NULL;
}
-yunet *yunet_create(int width, int height) {
+facedet *facedet_create(int width, int height) {
return create_model(width, height, model_ops, model_maps, model_weights);
}
-yunet *yunet_create_refiner(void) {
-#ifdef YUNET_REFINER_HEADER
- yunet *m = create_model(160, 160, refiner_ops, refiner_maps, refiner_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;
@@ -121,7 +121,7 @@ yunet *yunet_create_refiner(void) {
#endif
}
-void yunet_free(yunet *m) {
+void facedet_free(facedet *m) {
if (!m)
return;
for (int i = 0; i < MODEL_OP_COUNT; i++)
@@ -132,19 +132,19 @@ void yunet_free(yunet *m) {
free(m);
}
-float *yunet_input(yunet *m) {
+float *facedet_input(facedet *m) {
if (!m)
return NULL;
m->has_result = 0;
return (float *)m->arena;
}
-int yunet_set_rgb(yunet *m, const unsigned char *rgb, int width, int height, size_t row_bytes) {
+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 = yunet_input(m);
+ 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++) {
@@ -156,7 +156,7 @@ int yunet_set_rgb(yunet *m, const unsigned char *rgb, int width, int height, siz
return 0;
}
-int yunet_run(yunet *m) {
+int facedet_run(facedet *m) {
if (!m)
return -1;
m->has_result = 0;
@@ -166,7 +166,7 @@ int yunet_run(yunet *m) {
return -1;
unsigned char *a = m->arena;
for (int i = 0; i < MODEL_OP_COUNT; i++) {
- yunet_op op = m->ops[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) {
@@ -174,29 +174,29 @@ int yunet_run(yunet *m) {
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) {
- yunet_gemm(m->packs[i], op.cout, op.cin, in0, (size_t)op.ih * op.iw, (int)n,
+ 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) {
- yunet_nn_im2col3(in0, op.cin, op.ih, op.iw, op.s, m->im2col, op.oh, op.ow);
- yunet_gemm(m->packs[i], op.cout, op.cin * 9, m->im2col, n, (int)n,
+ 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) {
- yunet_nn_dwconv3(in0, op.cin, op.ih, op.iw, m->weights[op.widx], bias,
+ facedet_nn_dwconv3(in0, op.cin, op.ih, op.iw, m->weights[op.widx], bias,
op.relu, out);
} else {
- yunet_nn_conv2d(in0, op.cin, op.ih, op.iw, m->weights[op.widx], bias,
+ 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:
- yunet_nn_maxpool2(in0, op.cin, op.ih, op.iw, out);
+ facedet_nn_maxpool2(in0, op.cin, op.ih, op.iw, out);
break;
case OP_RESIZE2X:
- yunet_nn_resize2x(in0, op.cin, op.ih, op.iw, out);
+ facedet_nn_resize2x(in0, op.cin, op.ih, op.iw, out);
break;
case OP_ADD:
- yunet_nn_add(in0, (const float *)(a + op.in1_off), out, (size_t)op.cin * op.ih * op.iw);
+ facedet_nn_add(in0, (const float *)(a + op.in1_off), out, (size_t)op.cin * op.ih * op.iw);
break;
}
}
@@ -204,11 +204,11 @@ int yunet_run(yunet *m) {
return 0;
}
-const float *yunet_map(const yunet *m, const char *name, int *channels, int *height, int *width) {
+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 yunet_map_info *mm = &m->maps[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;
@@ -239,8 +239,8 @@ static float sample_rgb(const unsigned char *rgb, int width, int height, size_t
return (float)value;
}
-int yunet_refine(yunet *m, const unsigned char *rgb, int width, int height,
- size_t row_bytes, yunet_detection *faces, int count) {
+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 ||
@@ -254,14 +254,14 @@ int yunet_refine(yunet *m, const unsigned char *rgb, int width, int height,
if (!points)
return -1;
for (int i = 0; i < count; i++) {
- const yunet_detection *face = &faces[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 = yunet_input(m);
+ 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++)
@@ -269,11 +269,11 @@ int yunet_refine(yunet *m, const unsigned char *rgb, int width, int height,
sample_rgb(rgb, width, height, row_bytes,
ox + (x + .5) * extent / 160 - .5,
oy + (y + .5) * extent / 160 - .5, 2 - c);
- if (yunet_run(m))
+ if (facedet_run(m))
goto fail;
int channels, h, w;
/* The landmark readout is the fixed center cell, independent of objectness. */
- const float *map = yunet_map(m, "kps_8", &channels, &h, &w);
+ 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++) {
@@ -294,7 +294,7 @@ fail:
return -1;
}
-static float iou_xywh(const yunet_detection *a, const yunet_detection *b) {
+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;
@@ -307,7 +307,7 @@ static float iou_xywh(const yunet_detection *a, const yunet_detection *b) {
}
static int det_cmp(const void *pa, const void *pb) {
- const yunet_detection *a = pa, *b = 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)
@@ -315,7 +315,7 @@ static int det_cmp(const void *pa, const void *pb) {
return a->x < b->x ? -1 : a->x > b->x;
}
-int yunet_decode(const yunet *m, float score_thresh, float nms_iou, yunet_detection *out, int cap) {
+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)
@@ -331,24 +331,24 @@ int yunet_decode(const yunet *m, float score_thresh, float nms_iou, yunet_detect
size_t count = 0;
for (int lv = 0; lv < 3; lv++)
count += (size_t)(m->height / strides[lv]) * (m->width / strides[lv]);
- yunet_detection *cand = malloc(sizeof(*cand) * count);
+ 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 = yunet_map(m, names[lv][0], NULL, &H, &W);
- const float *obj = yunet_map(m, names[lv][1], NULL, NULL, NULL);
- const float *bbox = yunet_map(m, names[lv][2], NULL, NULL, NULL);
- const float *kps = yunet_map(m, names[lv][3], NULL, NULL, NULL);
+ 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(yunet_nn_sigmoid(cls[i]) * yunet_nn_sigmoid(obj[i]));
+ float sc = sqrtf(facedet_nn_sigmoid(cls[i]) * facedet_nn_sigmoid(obj[i]));
if (!isfinite(sc) || sc < score_thresh)
continue;
- yunet_detection *d = &cand[n];
+ 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;