-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
77 lines (57 loc) · 1.21 KB
/
main.c
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
#define ArrayCount(x) (sizeof((x))/sizeof((x)[0]))
#include <stdio.h>
#include <stdint.h>
#if RIOT
# include "ztimer.h"
# include "benchmark.h"
#else
# define BENCHMARK_FUNC(name, runs, func) func
#endif
static char *labels[] =
{
#include "labels.h"
};
#include "model.h"
#ifdef EMLEARN
# define LIBNAME "EMLEARN"
# define Predict(f) model_predict((float *)f, 0)
typedef uint32_t ml_uint;
#elif defined MICROMLGEN
# define LIBNAME "MICROMLGEN"
# define Predict(f) predict((float *)f)
typedef uint32_t ml_uint;
#elif defined M2CGEN
# define LIBNAME "M2CGEN"
#include <float.h>
typedef uint64_t ml_uint;
static unsigned Predict(ml_uint *features)
{
double output[ArrayCount(labels)];
score((double *)features, output);
double max = -DBL_MIN;
unsigned index = 0;
for (int i = 0; i < ArrayCount(output); ++i)
{
double val = output[i];
if (val > max)
{
max = val;
index = i;
}
}
return index;
}
#else
# error "ML library not defined"
#endif
static ml_uint features[] =
{
#include "features.h"
};
int main(void)
{
unsigned index;
printf("[" LIBNAME "] ");
BENCHMARK_FUNC("Prediction", 5, index = Predict(features));
printf("Predicted label is %s\n", labels[index]);
}