This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
discrimination.c
587 lines (476 loc) · 12.8 KB
/
discrimination.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
* Copyright 2002 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Niels Provos.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <jpeglib.h>
#include "config.h"
#include "common.h"
#include "extraction.h"
#include "discrimination.h"
struct cd_entry {
TAILQ_ENTRY(cd_entry) next;
char *filename;
int positive;
int npoints;
double *points;
};
struct cd_decision {
TAILQ_ENTRY(cd_decision) next;
char *name;
int npoints; /* data points for each sample */
double *b; /* projection values */
double k; /* decision boundary */
double where; /* parameter from testing */
double projpos;
double projneg;
TAILQ_HEAD(cd_list, cd_entry) positiveq;
int npositive;
struct cd_list negativeq;
int nnegative;
char *transform_name;
transform_t transform;
};
static TAILQ_HEAD(cdqueue, cd_decision) cdq;
#define CD_PERCENT (0.8)
void
cd_init(void)
{
TAILQ_INIT(&cdq);
}
void
cd_insert(struct cd_decision *cdd)
{
TAILQ_INSERT_TAIL(&cdq, cdd, next);
}
struct cd_decision *
cd_iterate(struct cd_decision *cdd)
{
if (cdd == NULL)
return (TAILQ_FIRST(&cdq));
return (TAILQ_NEXT(cdd, next));
}
struct cd_decision *
cd_new(void)
{
struct cd_decision *cdd;
if ((cdd = calloc(1, sizeof(struct cd_decision))) == NULL)
err(1, "calloc");
TAILQ_INIT(&cdd->positiveq);
TAILQ_INIT(&cdd->negativeq);
return (cdd);
}
#define MAX_NPOINTS 1024
/* Read a single line of data and return an array of doubles */
double *
cd_collect_data(char *data, int *pnpoints)
{
double points[MAX_NPOINTS], *ppoints;
char *p, *endp;
int n = 0;
while (n < MAX_NPOINTS && data != NULL && *data != '\0') {
p = strsep(&data, " ");
if (p == NULL)
return (NULL);
/* Ignore multiple spaces */
if (!strlen(p))
continue;
points[n] = strtod(p, &endp);
if (endp == NULL || *endp != '\0')
return (NULL);
n++;
}
if ((ppoints = malloc(n * sizeof(double))) == NULL)
return (NULL);
memcpy(ppoints, points, n * sizeof(double));
*pnpoints = n;
return (ppoints);
}
void
cd_make_entry(struct cd_decision *cdd, char *name, int positive,
double *points, int npoints)
{
struct cd_entry *entry;
if ((entry = calloc(1, sizeof(struct cd_entry))) == NULL)
err(1, "calloc");
if ((entry->filename = strdup(name)) == NULL)
err(1, "strdup");
entry->positive = positive;
entry->points = points;
entry->npoints = npoints;
if (positive) {
TAILQ_INSERT_TAIL(&cdd->positiveq, entry, next);
cdd->npositive++;
} else {
TAILQ_INSERT_TAIL(&cdd->negativeq, entry, next);
cdd->nnegative++;
}
}
int
cd_process_file(struct cd_decision *cdd, char *filename)
{
FILE *fin;
char line[2048], *p;
char *imagename, *type, *tfname, *tmp, *data;
double *points;
int linenr = 0, npoints = 0, tmpnpoints;
if ((fin = fopen(filename, "r")) == NULL)
err(1, "fopen: %s", filename);
if (cdd->npoints)
npoints = cdd->npoints;
while (fgetl(line, sizeof(line), fin) != NULL) {
linenr++;
p = line;
imagename = strsep(&p, ":");
tmp = strsep(&p, ":");
data = strsep(&p, ":");
if (tmp == NULL || data == NULL)
errx(1, "\"%s\": bad format in line %d",
filename, linenr);
type = strsep(&tmp, ",");
tfname = strsep(&tmp, ",");
if (type == NULL || tfname == NULL)
errx(1, "\"%s\": bad format in line %d",
filename, linenr);
if (cdd->transform_name != NULL &&
strcmp(cdd->transform_name, tfname))
errx(1, "\"%s\": transform name changed in line %d",
filename, linenr);
else {
cdd->transform = transform_lookup(tfname);
if (cdd->transform == NULL)
errx(1, "\"%s\": unknown transform in line %d",
filename, linenr);
if ((cdd->transform_name = strdup(tfname)) == NULL)
err(1, "strdup");
}
points = cd_collect_data(data, &tmpnpoints);
if (points == NULL)
errx(1, "\"%s\": bad data in line %d",
filename, linenr);
if (npoints && tmpnpoints != npoints)
errx(1, "\"%s\": require %d data points but got %d in line %d",
filename, npoints, tmpnpoints, linenr);
if (!npoints)
npoints = tmpnpoints;
cd_make_entry(cdd, imagename, atoi(type), points, npoints);
}
fclose(fin);
if (!cdd->npoints)
cdd->npoints = npoints;
return (0);
}
void
cd_meanest(struct cd_list *head, int howmany, double *mest, int mestsize)
{
int i;
struct cd_entry *tmp;
for (i = 0; i < mestsize; i++) {
int count = 0;
double mean = 0;
TAILQ_FOREACH(tmp, head, next) {
mean += tmp->points[i];
count++;
if (howmany && count >= howmany)
break;
}
mest[i] = mean / count;
}
}
/* Compute the estimator for the covariance matrix */
void
cd_covarest(struct cd_decision *cdd, int npositive, int nnegative,
double *mestpos, double *mestneg, double **covarest)
{
int i, j;
int npoints = cdd->npoints;
struct cd_entry *tmp;
for (i = 0; i < npoints; i++) {
for (j = 0; j < npoints; j++) {
double sum1, sum2;
int count1, count2;
sum1 = 0;
count1 = 0;
TAILQ_FOREACH(tmp, &cdd->positiveq, next) {
sum1 += (tmp->points[i] - mestpos[i]) *
(tmp->points[j] - mestpos[j]);
count1++;
if (npositive && count1 >= npositive)
break;
}
sum2 = 0;
count2 = 0;
TAILQ_FOREACH(tmp, &cdd->negativeq, next) {
sum1 += (tmp->points[i] - mestneg[i]) *
(tmp->points[j] - mestneg[j]);
count2++;
if (nnegative && count2 >= nnegative)
break;
}
covarest[i][j] = (sum1 + sum2) / (count1 + count2 - 2);
}
}
}
double
cd_project(double *a, double *b, int npoints)
{
int i;
double sum = 0;
for (i = 0; i < npoints; i++)
sum += *a++ * *b++;
return (sum);
}
void
cd_dump(FILE *fout, struct cd_decision *cdd)
{
int i;
fprintf(fout, "%s\n", cdd->name);
fprintf(fout, "%s\n", cdd->transform_name);
fprintf(fout, "%d", cdd->npoints);
for (i = 0; i < cdd->npoints; i++)
fprintf(fout, " %f", cdd->b[i]);
fprintf(fout, "\n");
fprintf(fout, "%f %f %f\n", cdd->projpos, cdd->projneg, cdd->k);
}
struct cd_decision *
cd_read(FILE *fin)
{
char line[1024], *p, *str;
struct cd_decision *cdd;
int n;
if ((cdd = calloc(1, sizeof(struct cd_decision))) == NULL)
return (NULL);
/* Read name */
if (fgetl(line, sizeof(line), fin) == NULL)
goto error;
if ((cdd->name = strdup(line)) == NULL)
goto error;
/* Read transform */
if (fgetl(line, sizeof(line), fin) == NULL)
goto error;
if ((cdd->transform_name = strdup(line)) == NULL)
goto error;
if ((cdd->transform = transform_lookup(cdd->transform_name)) == NULL) {
fprintf(stderr, "Unknown transforms \"%s\" for \"%s\"\n",
cdd->transform_name, cdd->name);
goto error;
}
/* Read projection */
if (fgetl(line, sizeof(line), fin) == NULL)
goto error;
p = line;
str = strsep(&p, " ");
if (str == NULL || p == NULL)
goto error;
cdd->npoints = atoi(str);
cdd->b = cd_collect_data(p, &n);
if (cdd->b == NULL || cdd->npoints != n) {
fprintf(stderr, "Format %s: projection data malformed\n",
cdd->name);
goto error;
}
/* Read projection */
if (fgetl(line, sizeof(line), fin) == NULL)
goto error;
n = sscanf(line, "%lf %lf %lf", &cdd->projpos, &cdd->projneg, &cdd->k);
if (n != 3) {
fprintf(stderr, "Format %s: boundary values malformed\n",
cdd->name);
}
fprintf(stderr, "Read detection for %s\n", cdd->name);
return (cdd);
error:
if (cdd->name)
free(cdd->name);
if (cdd->b)
free(cdd->b);
free (cdd);
return (NULL);
}
void
cd_compute(struct cd_decision *cdd, char *name, int test)
{
int i, j, npoints;
double *mestpos, *mestneg, **covarest;
int nnegative, npositive;
if (cdd->nnegative < 2 || cdd->npositive < 2)
errx(1, "Not enough data points for \"%s\"", name);
/* Free memory if in use before */
if (cdd->name) {
free(cdd->name);
cdd->name = NULL;
}
if (cdd->b) {
free(cdd->b);
cdd->b = NULL;
}
npoints = cdd->npoints;
mestpos = malloc(npoints * sizeof(double));
mestneg = malloc(npoints * sizeof(double));
covarest = malloc(npoints * sizeof(double *));
if (mestpos == NULL || mestneg == NULL || covarest == NULL)
err(1, "malloc");
if ((cdd->name = strdup(name)) == NULL)
err(1, "strdup");
cdd->b = malloc(npoints * sizeof(double *));
if (cdd->b == NULL)
err(1, "malloc");
for (i = 0; i < npoints; i++) {
covarest[i] = malloc(npoints * sizeof(double));
if (covarest[i] == NULL)
err(1, "malloc");
}
/*
* In the test case, we use 80% of the images to train our
* system, and the remaining 20% to test is accuracy.
* Otherwise, we want to use all images.
*/
if (test) {
npositive = cdd->npositive * CD_PERCENT;
nnegative = cdd->nnegative * CD_PERCENT;
} else {
npositive = cdd->npositive;
nnegative = cdd->nnegative;
}
cd_meanest(&cdd->positiveq, npositive, mestpos, npoints);
cd_meanest(&cdd->negativeq, nnegative, mestneg, npoints);
cd_covarest(cdd, npositive, nnegative, mestpos, mestneg, covarest);
matrix_invert(covarest, npoints);
for (i = 0; i < npoints; i++) {
double diff, sum = 0;
for (j = 0; j < npoints; j++) {
diff = mestpos[j] - mestneg[j];
sum += covarest[i][j] * diff;
}
cdd->b[i] = sum;
}
/* Free up memory */
for (i = 0; i < npoints; i++)
free(covarest[i]);
free(covarest);
cdd->projpos = cd_project(mestpos, cdd->b, npoints);
cdd->projneg = cd_project(mestneg, cdd->b, npoints);
cdd->k = (cdd->projpos + cdd->projneg) / 2;
cd_setboundary(cdd, cdd->where);
free(mestpos);
free(mestneg);
}
void
cd_setboundary(struct cd_decision *cdd, double where)
{
double k;
if (cdd->projpos > cdd->projneg)
k = (cdd->projpos - cdd->projneg) * where + cdd->projneg;
else
k = (cdd->projneg - cdd->projpos) * (1 - where) + cdd->projpos;
cdd->k = k;
}
int
cd_classify(struct cd_decision *cdd, double *points)
{
double val;
val = cd_project(points, cdd->b, cdd->npoints);
if (cdd->projpos > cdd->projneg) {
return (val > cdd->k);
} else {
return (val < cdd->k);
}
}
void
cd_test(struct cd_decision *cdd)
{
struct cd_entry *tmp;
int negcorrect, poscorrect;
int negfalse, posfalse;
int nnegative, npositive;
double where, fprate;
int saved = 0, count, allcount;
fprintf(stderr, "%6.4f %6.4f\n", 1.0, 1.0);
for (where = -1; where <= 2; where += 0.15) {
negcorrect = negfalse = 0;
poscorrect = posfalse = 0;
cd_setboundary(cdd, where);
npositive = cdd->npositive * CD_PERCENT;
nnegative = cdd->nnegative * CD_PERCENT;
allcount = 0;
count = 0;
TAILQ_FOREACH(tmp, &cdd->negativeq, next) {
count++;
if (count <= nnegative)
continue;
if (!cd_classify(cdd, tmp->points))
negcorrect++;
else
negfalse++;
allcount++;
}
count = 0;
TAILQ_FOREACH(tmp, &cdd->positiveq, next) {
count++;
if (count <= npositive)
continue;
if (cd_classify(cdd, tmp->points))
poscorrect++;
else
posfalse++;
allcount++;
}
fprate = (float)negfalse/(negfalse + negcorrect)*1.0;
fprintf(stderr, "%6.4f %6.4f - %6.5f\n", fprate,
(float)poscorrect/(posfalse + poscorrect)*1.0,
(float)(posfalse + negfalse)/allcount);
/* Find a false positive rate that is below 1% */
if (!saved && fprate < 1) {
saved = 1;
cdd->where = where;
}
}
/* Did not find a good false positive rate, use the middle */
if (!saved)
cdd->where = 0.5;
fprintf(stderr, "%6.4f %6.4f\n", 0.0, 0.0);
}
char *
cd_name(struct cd_decision *cdd)
{
return (cdd->name);
}
transform_t
cd_transform(struct cd_decision *cdd)
{
return (cdd->transform);
}