-
Notifications
You must be signed in to change notification settings - Fork 0
/
guntrain.c
165 lines (132 loc) · 4.19 KB
/
guntrain.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include "netcreat.h"
#include "net_structs.h"
#include "netpass.h"
#include "netfile.h"
#include "net_errno.h"
#include "ipldefs.h"
#include "iplimage.h"
#include "edge_detect.h"
#include "net_def.h"
#define NEURO_PATH "/home/user/NeuroNet/gun_neuro.data"
#define GUNS_PATH "/home/user/NeuroNet/guns"
#define CNTGUNS 156
#define SAMPLE_SIZE 1000
#define ETA 0.01
struct sample {
double *data;
double target;
};
static void shufflearr(int *pathidx, int len)
{
int n1, n2, tmp;
n1 = rand() % (len);
n2 = rand() % (len);
while (n1 == n2)
n2 = rand() % (len);
tmp = *(pathidx + n1);
*(pathidx + n1) = *(pathidx + n2);
*(pathidx + n2) = tmp;
}
static double *getdata(struct IplImage *img)
{
int x, y;
double *data;
data = (double *)malloc(sizeof(double) * img->width * img->height);
for (y = 0; y < img->height; y++)
for (x = 0; x < img->width; x++) {
unsigned char px;
px = img->data[img->nchans * (y * img->width + x)];
/*g = img->data[img->nchans * (y * img->width + x) + 1];
b = img->data[img->nchans * (y * img->width + x) + 2];
max = (r > g)? r : g;
max = (b > max)? b : max;*/
data[y * img->width + x] = (double)px / 255.0 * 2.0 - 1.0;
}
return data;
}
int main(int argc, char** argv)
{
int nl = 2;
int nn[] = {100, 1};
double *out;
struct sample *examples;
int *idxes;
int i;
char name[256];
struct IplImage *img;
examples = (struct sample *)malloc(sizeof(struct sample) * CNTGUNS);
idxes = (int *)malloc(sizeof(int) * CNTGUNS);
for (i = 0; i < CNTGUNS; i++) {
bzero(name, 256);
sprintf(name, "%s/%d.png", GUNS_PATH, i);
if ((img = ipl_readimg(name, IPL_GRAY_MODE)) == NULL) {
fprintf(stderr, "error reading image: %s\n", name);
return 1;
}
(examples + i)->data = getdata(img);
(examples + i)->target = 1.0;
*(idxes + i) = i;
ipl_freeimg(&img);
}
/*for(i = 0; i < TOTAL; i++)
printf("tar1 = %lf tar2 = %lf\n", *((examples + i)->target), *((examples + i)->target + 1));
getchar();*/
/*gun_net = netcreat(nl, nn, SAMPLE_SIZE);
nettofile(gun_net, NEURO_PATH);
getchar();*/
gun_net = malloc(sizeof(struct neuronet));
if (netfromfile(gun_net, NEURO_PATH) == -1) {
fprintf(stderr, "Can not open file: %s: %s\n", strerror(errno), NEURO_PATH);
goto exit_failure;
}
// int isguncor, isgunincor;
// int notguncor, notgunincor;
double error;
do { //while(error > 0.018/*isguncor != CNTGUNS || notguncor != CNTNOTGUNS*/) {
//isguncor = isgunincor = notguncor = notgunincor = 0;
error = 0;
for (i = 0; i < CNTGUNS; i++) {
int idx;
double isgun_val;
idx = *(idxes + i);
out = netfpass(gun_net, (examples + idx)->data);
isgun_val = *(out + gun_net->total_nn - 1);
//error += abs(((*((examples + idx)->target) == 1)? isgun_val : isnotgun_val) - *((examples + idx)->target));
error += fabs((examples + idx)->target - isgun_val);
//printf("idx = %d tar1 = %lf tar2 = %lf\n", idx,*((examples + idx)->target), *((examples + idx)->target + 1));
/*if (*((examples + idx)->target) == 1.0 && isgun_val >= isnotgun_val)
isguncor++;
else if (*((examples + idx)->target) == 1.0 && isgun_val < isnotgun_val)
notgunincor++;
else if (*((examples + idx)->target) == 0.0 && isnotgun_val >= isgun_val)
notguncor++;
else if (*((examples + idx)->target) == 0.0 && isnotgun_val < isgun_val)
isgunincor++;
*/
netbpass(gun_net, (examples + idx)->data, out, &((examples + idx)->target), ETA);
//getchar();
free(out);
/*w = net->w;
for (i = 0; i < net->nl; i++) {
for(j = 0; j < net->nn[i]; j++) {
for(k = 0; k < net->nw[i]; k++)
printf("%lf|", *w++);
printf(" ");
}
printf("\n");
}*/
}
shufflearr(idxes, CNTGUNS);
printf("error = %lf\n", error / CNTGUNS);
//printf("isguncor = %d isgunincor = %d notguncor = %d notgunincor = %d\n", isguncor, isgunincor, notguncor, notgunincor);
nettofile(gun_net, NEURO_PATH);
} while(error / CNTGUNS);
return 0;
exit_failure:
return -1;
}