-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn.js
More file actions
412 lines (399 loc) · 10.2 KB
/
cnn.js
File metadata and controls
412 lines (399 loc) · 10.2 KB
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
const { Deep, DeepPart } = require('./deep');
const { Matrix, Vector, IMGData, randomer } = require('./math/index');
class Tensor{
static Zero(m, n, length){
return Array.from({ length }, () => Matrix.Zero(n, m));
}
}
class CNN extends Deep{
/**
*
*
* @static
* @param { IMGData } img
* @param { number | 0 } x
* @param { number | 0 } y
* @param { number | 0 } [w = 3]
* @param { number | 0 } [h = 3]
* @returns { Matrix }
* @memberof CNN
*/
static getRegion(img, x, y, w = 3, h = 3){
return Matrix.Zero(w, h).map(
(_, i, j) => img.px(y + j - 1, x + i - 1)
);
}
}
class Filter{
/**
* Cria uma instância Filter.
* @param { IMGData | number[] | null } [map = null] Matriz de filtro
* @param { number } [size = 3] Tamanho da matriz, se não fornecida nehuma
*/
constructor(map = null, size = 3){
if(map) this.map = (
map instanceof IMGData && map.width === size && map.height === size
) ? map : new IMGData(map, size, size);
else{
this.map = new IMGData(null, size, size);
const { data } = this.map,
sm2 = size ** -2,
rand = randomer(-sm2, sm2);
for(var i in data) data[i] = rand();
}
}
/**
* Convoluciona um bloco de imagem
*
* @param { IMGData } { data, width, height } Imagem
* @param { number } [x = 0] X
* @param { number } [y = 0] Y
* @returns { Matrix }
*/
convolve(img, x = 0, y = 0){
let r = this.map.mapC((v, c) => v * img.px(c[0] + x, c[1] + y));
return r.sum();
}
/**
* Saves a Filter data
*
* @returns { IMGData } The filter's map
* @memberof Filter
*/
save(){ return this.map; }
/**
* Loads a Filter data
*
* @static
* @param { IMGData } data The data
* @returns { Filter } The loaded Filter
* @memberof Filter
*/
static load(data){
return new Filter(data, data.width);
}
}
class Layer extends DeepPart{
/**
* Cria uma instância de Layer.
* @param { number | 0 } [nFilters = 8] Número de Filtros
* @param { number | 0 } [fSize = 3] Tamanho dos Filtros
* @param { Filter[] } [filters = null] Filtros
* @memberof Layer
*/
constructor(nFilters = 8, fSize = 3, filters = null){
super();
this.nFilters = nFilters | 0;
this.fSize = fSize | 0;
if(filters){
this.filters = filters.filter(f => f instanceof Filter);
this.nFilters = this.filters.length;
}else{
this.filters = Array.from(
{ length: this.nSize },
() => new Filter(null, this.fSize)
);
}
}
*iterateRegions({ width, height }){
var i, j;
width -= 2; height -= 2;
for(i = 0; i < height; i++){
for(j = 0; j < width; j++) yield [j, i, width * i + j];
}
}
/**
* Avanço da camada
*
* @param { IMGData } img Imagem
* @returns { IMGData }
*/
forward(img){
this.cache_input = img;
var { filters, nFilters: n } = this,
output = new IMGData(null, img.width - 2, img.height - 2, n),
filter, sums,
convolve = (img, x, y) => f => f.convolve(img, x, y);
for(var [x, y] of this.iterateRegions(img)){
sums = filters.map(convolve(img, x, y));
for(filter = n - 1; filter >= 0; filter--)
output.pxSet(sums[filter], x, y, filter);
}
return output;
}
/**
* Retorno da camada
*
* @param { IMGData } grad_L_out
* @param { number } lr
* @returns
*/
backprop(grad_L_out, lr){
var { nFilters: n, cache_input: img, fSize, filters } = this,
grad_L_filters = Tensor.Zero(fSize, fSize, n),
f, mult;
for(var [x, y] of this.iterateRegions(img)){
for(f = 0; f < n; f++){
let tmp1 = grad_L_out[f].e(x + 1, y + 1),
tmp2 = CNN.getRegion(img, x, y, fSize, fSize);
mult = tmp2.x(tmp1);
grad_L_filters[f] = grad_L_filters[f].add(mult);
}
}
filters.forEach((filter, f) => {
var grad = grad_L_filters[f].elements;
filter.map.alterRegion((v, _i, c) => v - lr * grad[c[0]][c[1]], true);
});
return null;
}
/**
* Saves a Layer Filters
*
* @returns { IMGData[] } The Filters data
* @memberof Layer
*/
save(){
return this.filters.map(f => f.save());
}
/**
* Loads a Layer's Filters
*
* @static
* @param { IMGData[] } data The Filters data
* @returns { Layer } The loaded Layer
* @memberof Layer
*/
static load(data){
return new Layer(data.length, data[0].width, data.map(d => new Filter(d, d.width)));
}
}
class Pool extends DeepPart{
/**
* Cria uma instância de Pool.
* @param { number } [size = 2] Taxa de redução
*/
constructor(size = 2, fn = 'max'){
super();
this.size = size | 0;
this.pool_type = fn;
if(fn in Pool.FN) this.pool_fn = Pool.FN[fn];
else{
this.pool_fn = Pool.FN.max;
this.pool_type = 'max';
}
}
pool(img, ...coords){
var { size } = this,
pos = coords.slice(),
arr = [];
for(var i = 0; i < size; i++){
arr = arr.concat(img.slice(pos, size));
pos[1]++;
}
//arr = arr.concat(data.slice(start, start + size));
return this.pool_fn(arr);
}
*iterateRegions({ width, height }){
var i, j, { size } = this;
width -= width % size;
height -= height % size;
for(i = 0; i < height; i += size){
for(j = 0; j < width; j += size) yield [j, i, (width >> 1) * i + j];
}
}
/**
* Avanço da camada PoolMax
*
* @param {*} img
* @returns { IMGData }
* @memberof Pool
*/
forward(img){
//console.log(`\t[Pool] Input Length: ${img.length}`);
this.cache_input = img;
var { size } = this, { width, height, channels } = img,
//output = [],
output = new IMGData(null, width / size | 0, height / size | 0, channels),
//maxes,
ch, pool;//,
//pool = (x, y) => img => this.pool(img, x, y);
//width -= width % size; height -= height % size;
//width /= size; height /= size;
//console.log(`\t\t(forward)\tDimensions: ${width}x${height}`)
for(var [x, y] of this.iterateRegions(img)){
//maxes = img.map(pool(x, y));
//for(max = img.length - 1; max >= 0; max--){
// if(!output[max]) output[max] = [];
// output[max][i >> 1] = maxes[max];
//}
for(ch = channels - 1; ch >= 0; ch--){
pool = this.pool(img, x, y, ch);
output.pxSet(pool, x, y, ch);
}
}
//output = output.map(data => ({ width, height, data }));
//output.width = width;
//output.height = height;
return output;
}
/**
* Retorno da camada PoolMax
*
* @param { Vector } grad_L_out Gradiente
* @returns { IMGData } Gradiente
* @memberof Pool
*/
backprop(grad_L_out){
var { cache_input: img, size } = this,
{ width, height } = img,
grad_L_input = Tensor.Zero(width, height, 8),
max, i1, x1, y1, f,
pool = (x, y) => img => this.pool(img, x, y);
// Para cada região
for(var [x, y] in this.iterateRegions(img)){
max = img.map(pool(x, y));
// Para cada filtro
for(f = img.length - 1; f >= 0; f--){
var { data } = img[f],
m = max[f],
g1 = grad_L_input[f],
g2 = grad_L_out.elements[f];
// Para cada (x_1, y_1) entre (x, y) e (x + size, y + size)
for(y1 = y; y1 < y + size; y1++){
for(x1 = x; x1 < x + size; x1++){
i1 = y1 * width + x1;
if(data[i1] === m)
g1.elements[y1][x1] = g2[(width >> 1) * y + x >> 1];
}
}
}
}
return grad_L_input;
}
save(){
return { size: this.size, };
}
static load(){}
}
Pool.FN = {
max: x => Math.max(...x),
min: x => Math.min(...x)
};
/**
* Camada de classificação Softmax
*
* @class Softmax
* @extends { DeepPart }
*/
class Softmax extends DeepPart{
/**
* Cria uma instância de Softmax.
* @param { number | 0 } inputLength
* @param { number | 0 } outputLength
* @memberof Softmax
*/
/**
* Cria uma instância de Softmax.
* @param { Matrix } weights
* @param { Vector } biases
* @memberof Softmax
*/
constructor(inputLength, outputLength){
super();
if(typeof inputLength === 'object' && typeof outputLength === 'object'){
this.weights = inputLength;
this.biases = outputLength;
this.inputLength = this.weights.cols;
this.nodes = this.biases.dimensions;
}else{
this.inputLength = inputLength | 0;
this.nodes = outputLength | 0;
this.weights = Matrix.Random(outputLength, inputLength).x(inputLength ** -1);
this.biases = Vector.Zero(outputLength);
//console.log(`\t[Softmax] Weight Dimensions: ${nodes}x${inputLength}`);
}
}
/**
* Avanço da camada Softmax
*
* @param { IMGData } img
* @returns { Vector } Probabilidades por classe
* @memberof Softmax
*/
forward(img){
img = new Vector(img.data);
var { weights, biases } = this;
//let [ rows, cols ] = weights.dimensions;
//console.log(`\t[Softmax] Input Length: ${input.dimensions}`);
var exp = weights.x(img).add(biases).map(v => Math.exp(v)),
S = Vector.sum(exp);
this.cache_input = img;
this.cache_exp = exp;
this.cache_sum = S;
//console.log('weights:', weights.dimensions);
return exp.x(S ** -1);
}
/**
* Retorno da camada Softmax
*
* @param {*} grad_L_out
* @param { number } lr Taxa de aprendizagem
* @param { number | 0 } label Resultado correto
* @returns { Vector } Gradiente
* @memberof Softmax
*/
backprop(grad_L_out, lr, label){
var i = label + 1,//grad_L_out.indexOf(g),
g = grad_L_out.e(i),
{
cache_exp: exp,
cache_sum: S,
cache_input: grad_t_w
} = this,
exp_i = exp.e(i);
var grad_out_t = exp.x(- exp_i / (S ** 2));
grad_out_t.elements[i - 1] = exp_i * (S - exp_i) / (S ** 2);
var grad_t_inputs = this.weights.transpose(),
//grad_t_b = 1,
grad_L_t = grad_out_t.x(g).toMatrix('col'),
grad_L_w = grad_L_t.x(
grad_t_w.toMatrix('row')//.transpose()
),
grad_L_b = grad_L_t,//.x(grad_t_b)
grad_L_inputs = grad_t_inputs.x(grad_L_t);
//console.log(grad_L_w);
this.weights = this.weights.sub(grad_L_w.x(lr));
this.biases = this.biases.sub(grad_L_b.x(lr));
//console.log(this.weights);
return grad_L_inputs;
/*
* weights -= lr * grad_L_w
? grad_L_w = grad_t_w @ grad_L_t
* :grad_t_w:
* grad_L_t = g * grad_out_t
*
? grad_L_w = :grad_t_w: @ (g * grad_out_t)
*
* #weights = #grad_L_w
? = #(:grad_t_w: @ (g * grad_out_t))
? = #(:grad_t_w: @ grad_out_t)
* = 10 x 1352
* #grad_out_t = #grad_L_t = 10
* #grad_t_w = 1352
*/
}
save(){
const { weights, biases } = this;
return { weights, biases };
}
static load(data){
return new Softmax(data.weights, data.biases);
}
}
CNN.Filter = Filter;
CNN.Layer = Layer;
CNN.PoolMax = Pool;
CNN.Softmax = Softmax;
module.exports = {
CNN, Filter, Layer, Pool, Softmax
};