-
Notifications
You must be signed in to change notification settings - Fork 195
/
hidtopgm.c
71 lines (61 loc) · 1.41 KB
/
hidtopgm.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
/*
hidtopgm - scale hidden-unit weights to pixel values
*/
#include <stdio.h>
#include <math.h>
#include "pgmimage.h"
#include "backprop.h"
#define MAXROWS 256
#define MAXCOLS 256
double tmpimg;
main(argc, argv)
int argc;
char *argv[];
{
BPNN *net;
IMAGE *img;
int nr, nc, i, j, k, h, pxl;
double maxwt, minwt, range;
if (argc < 6) {
fprintf(stderr, "usage: %s net-file image-file x y hidden-unit-num\n",
argv[0]);
exit(1);
}
if ((net = bpnn_read(argv[1])) == NULL) {
fprintf(stderr, "%s: can't read net-file '%s'\n", argv[0], argv[1]);
exit(1);
}
nc = atoi(argv[3]);
nr = atoi(argv[4]);
h = atoi(argv[5]);
if ((img = img_creat(argv[2], nr, nc)) == NULL) {
fprintf(stderr, "%s: can't create image-file '%s'\n", argv[0], argv[2]);
exit(1);
}
/* first get min and max wts */
k = 0;
maxwt = -1e6;
minwt = 1e6;
for (i = 0; i < nr; i++) {
for (j = 0; j < nc; j++) {
if (net->input_weights[k][h] > maxwt)
maxwt = net->input_weights[k][h];
if (net->input_weights[k][h] < minwt)
minwt = net->input_weights[k][h];
k++;
}
}
range = maxwt - minwt;
/* now scale values */
k = 0;
for (i = 0; i < nr; i++) {
for (j = 0; j < nc; j++) {
tmpimg = net->input_weights[k][h];
pxl = ((tmpimg-minwt)/range) * 255.0;
img_setpixel(img, i, j, pxl);
k++;
}
}
img_write(img, argv[2]);
exit(0);
}