40#include <json-glib/json-glib.h>
43static double max_abs_diff(
const float *a,
const float *b,
size_t count)
46 for(
size_t i = 0;
i < count;
i++)
48 const double d = fabs((
double)a[
i] - b[
i]);
49 if(
d > max_abs) max_abs =
d;
54static float *
read_f32(
const char *dir,
const char *
name,
size_t count)
57 snprintf(path,
sizeof(path),
"%s/%s", dir,
name);
58 FILE *
f = fopen(path,
"rb");
61 fprintf(stderr,
"cannot open %s\n", path);
64 float *buf = malloc(count *
sizeof(
float));
65 const size_t got = buf ? fread(buf,
sizeof(
float), count,
f) : 0;
69 fprintf(stderr,
"%s: expected %zu floats, got %zu\n", path, count, got);
83static int read_meta(
const char *dir,
const char *model_path,
int *is_xtrans)
86 snprintf(path,
sizeof(path),
"%s/fixture-meta.json", dir);
88 JsonParser *parser = json_parser_new();
90 if(!json_parser_load_from_file(parser, path, &
error))
92 fprintf(stderr,
"cannot read %s: %s\n", path,
error ?
error->message :
"?");
94 g_object_unref(parser);
97 JsonObject *root = json_node_get_object(json_parser_get_root(parser));
99 if(json_object_has_member(root,
"cfa"))
100 *is_xtrans = !g_strcmp0(json_object_get_string_member(root,
"cfa"),
"xtrans");
103 if(json_object_has_member(root,
"model_sha256"))
105 const char *want = json_object_get_string_member(root,
"model_sha256");
108 if(g_file_get_contents(model_path, &blob, &len, NULL))
110 gchar *got = g_compute_checksum_for_data(G_CHECKSUM_SHA256, (
const guchar *)blob, len);
111 if(g_strcmp0(got, want))
114 "FAIL: this fixture was generated from a different model.\n"
115 " fixture pins %s\n model is %s\n"
116 " Regenerate with scripts/make_fixture.py in the ansel-denoise repo.\n",
124 g_object_unref(parser);
128int main(
int argc,
char *argv[])
132 fprintf(stderr,
"usage: %s <model.anselnn> <fixture-dir> [N] [--xtrans]\n", argv[0]);
135 const int n = (argc > 3 && argv[3][0] !=
'-') ? atoi(argv[3]) : 96;
138 if(
read_meta(argv[2], argv[1], &is_xtrans))
return 1;
139 for(
int i = 3;
i < argc;
i++)
141 if(!strcmp(argv[
i],
"--xtrans")) is_xtrans = 1;
142 if(!strcmp(argv[
i],
"--bayer")) is_xtrans = 0;
149 fprintf(stderr,
"model load failed: %s\n", err);
156 const size_t plane = (size_t)
n *
n;
158 float *expected =
read_f32(argv[2],
"fixture-expected.f32", plane);
159 float *
out = calloc(plane,
sizeof(
float));
160 if(!
in || !expected || !
out)
return 2;
165 printf(
"fixture CFA: %s, superpixel bin %d\n", is_xtrans ?
"xtrans" :
"bayer", bin);
168 const int cn =
n / bin;
169 const size_t cplane = (size_t)cn * cn;
172 float *base =
read_f32(argv[2],
"fixture-base-planes.f32", plane * 5);
173 float *c_in_exp =
read_f32(argv[2],
"fixture-coarse-input.f32", cplane * c_in);
174 float *c_out_exp =
read_f32(argv[2],
"fixture-coarse-expected.f32", cplane * c_out);
175 float *
rgb = malloc(cplane * 3 *
sizeof(
float));
176 float *cnt = malloc(cplane * 3 *
sizeof(
float));
177 float *c_out_got = malloc(cplane * c_out *
sizeof(
float));
178 if(!base || !c_in_exp || !c_out_exp || !
rgb || !cnt || !c_out_got)
return 2;
183 printf(
"binning contract: max abs err %.3g (tolerance 1e-6)\n", bin_err);
186 fprintf(stderr,
"FAIL: binning contract\n");
193 fprintf(stderr,
"coarse stage apply failed\n");
196 const double c_err =
max_abs_diff(c_out_got, c_out_exp, cplane * c_out);
197 printf(
"coarse stage parity: max abs err %.3g (tolerance 2e-4)\n", c_err);
200 fprintf(stderr,
"FAIL: coarse stage parity\n");
207 float *fine_in = malloc(plane * fine_in_ch *
sizeof(
float));
208 float *e2e_out = malloc(plane *
sizeof(
float));
209 if(!fine_in || !e2e_out)
return 2;
210 memcpy(fine_in, base, plane * 5 *
sizeof(
float));
219 fprintf(stderr,
"fine stage apply failed\n");
222 const double e2e_err =
max_abs_diff(e2e_out, expected, plane);
223 printf(
"end-to-end parity: max abs err %.3g (tolerance 5e-4)\n", e2e_err);
226 fprintf(stderr,
"FAIL: end-to-end parity\n");
239 struct timespec t0, t1;
240 clock_gettime(CLOCK_MONOTONIC, &t0);
242 clock_gettime(CLOCK_MONOTONIC, &t1);
245 fprintf(stderr,
"dt_nn_unet_apply failed (%d)\n", rc);
248 const double ms = (t1.tv_sec - t0.tv_sec) * 1e3 + (t1.tv_nsec - t0.tv_nsec) / 1e6;
250 double max_abs = 0.0, sum_sq = 0.0;
252 for(
size_t i = 0;
i < plane;
i++)
254 const double d = fabs((
double)
out[
i] - expected[
i]);
262 const double rms = sqrt(sum_sq / plane);
263 const double tolerance = 2e-4;
264 printf(
"parity vs torch: max abs err %.3g (at %zu: %.6f vs %.6f), rms %.3g | %.1f ms for %dx%d\n", max_abs,
265 worst,
out[worst], expected[worst], rms, ms,
n,
n);
271 if(max_abs > tolerance)
273 fprintf(stderr,
"FAIL: max abs err %.3g > %.3g\n", max_abs, tolerance);
static void error(char *msg)
static dt_aligned_pixel_t rgb
const dt_colormatrix_t dt_aligned_pixel_t out
memcpy(roundtrip, origin, sizeof(roundtrip))
void dt_nn_model_free(dt_nn_model_t *m)
int dt_nn_model_in_channels(const dt_nn_model_t *m)
int dt_nn_model_coarse_in_channels(const dt_nn_model_t *m)
size_t dt_nn_unet_scratch_bytes(const dt_nn_model_t *m, int width, int height)
int dt_nn_unet_apply_stage(const dt_nn_model_t *m, int stage, const float *in, float *out, int width, int height, int apply_residual)
dt_nn_model_t * dt_nn_model_load(const char *path, char *err, size_t err_len)
__DT_CLONE_TARGETS__ void dt_nn_upsample_nearest(const float *in, int ch, int w, int h, int factor, float *out)
int dt_nn_model_coarse_out_channels(const dt_nn_model_t *m)
int dt_nn_model_alignment(const dt_nn_model_t *m)
int dt_nn_unet_apply(const dt_nn_model_t *m, const float *in, float *out, int width, int height)
__DT_CLONE_TARGETS__ void dt_nn_bin_planes(const float *planes, int pw, int ph, int bin, float *out_rgb, float *out_cnt)
int dt_nn_model_bin(const dt_nn_model_t *m, const int is_xtrans)
int dt_nn_model_out_channels(const dt_nn_model_t *m)
static float * read_f32(const char *dir, const char *name, size_t count)
static double max_abs_diff(const float *a, const float *b, size_t count)
static int read_meta(const char *dir, const char *model_path, int *is_xtrans)