-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNNImage.js
217 lines (184 loc) · 7.01 KB
/
CNNImage.js
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
class CNNImage {
/**
* @param {CNNChannel[]} images
*/
constructor(images, width, height, depth) {
if (images.length != depth) {
console.error("CNNImage Error: Pixels array depth doesnot match depth value");
return;
}
/**
* @type {CNNChannel[]}
*/
this.images = images;
this.width = width;
this.height = height;
this.depth = depth;
}
drawImage(x, y) {
for (let i = 0; i < this.images.length; i++) {
this.images[i].drawChannel(x + i * this.width, y);
}
}
flatten() {
let flattened = [];
for (let i = 0; i < this.images.length; i++) {
for (let j = 0; j < this.images[i].pixels.length; j++) {
flattened.push(this.images[i].pixels[j] / 255);
}
}
return flattened;
}
map(f) {
for (let i = 0; i < this.images.length; i++) {
this.images[i].mapPixels(f);
}
}
calcPartialError(errors) {
let partialErrorChannels = [];
for (let i = 0; i < errors.length; i++) {
partialErrorChannels.push(this.images[i].calcPartialError(errors[i]));
}
let partialErrorImage = new CNNImage(partialErrorChannels, this.width, this.height, errors.length);
return partialErrorImage;
}
clipImage(min, max) {
for (let i = 0; i < this.depth; i++) {
this.images[i].clipValues(min, max);
}
}
/**
*
* @param {CNNImage} img
* @param {CNNKernel} allKernels
* @param {CNNImage} biases
* @returns
*/
static applyConvolution(img, allKernels, biases) {
if (allKernels[0].depth != img.depth) {
console.error("Image and Kernels are not compatible!");
}
let outputs = [];
for (let i = 0; i < allKernels.length; i++) {
let kernel = allKernels[i];
let bias;
if (biases) {
bias = biases.images[i];
}
let kernelOutput = new CNNChannel([], img.width, img.height);
kernelOutput.initToZeros();
for (let j = 0; j < img.depth; j++) {
let feature = img.images[j];
let filterMatrix = kernel.kernels[j];
let convolvedFeature = CNNChannel.convolveImage(feature, filterMatrix);
kernelOutput.addChannel(convolvedFeature);
// convolvedFeature.drawChannel(j * 28, 40);
}
if (bias) {
kernelOutput.addChannel(bias);
}
outputs.push(kernelOutput);
}
let outputImage = new CNNImage(outputs, img.width, img.height, allKernels.length);
return outputImage;
}
/**
* @param {CNNImage} img Image to pool
* @param {avgPoolSize} avgPoolSize pool size
*/
static avgPool(img, avgPoolSize) {
let newWidth = (img.width + (avgPoolSize - (img.width % avgPoolSize))) / avgPoolSize;
let newHeight = (img.height + (avgPoolSize - (img.height % avgPoolSize))) / avgPoolSize;
let pooledChannels = [];
for (let i = 0; i < img.images.length; i++) {
let pooled = new CNNChannel([], newWidth, newHeight);
for (let y = 0; y < newHeight * avgPoolSize; y += avgPoolSize) {
for (let x = 0; x < newWidth * avgPoolSize; x += avgPoolSize) {
let avgVal = 0;
for (let ny = y; ny < y + avgPoolSize; ny++) {
for (let nx = x; nx < x + avgPoolSize; nx++) {
avgVal += img.images[i].getFromPixel(nx, ny);
}
}
avgVal /= avgPoolSize ** 2;
pooled.pixels.push(avgVal);
}
}
pooledChannels.push(pooled);
}
let pooledImage = new CNNImage(pooledChannels, newWidth, newHeight, img.depth);
return pooledImage;
}
static unpool(img, poolSize, unpooledWidth, unpooledHeight) {
let unpooledChannels = [];
for (let i = 0; i < img.images.length; i++) {
unpooledChannels.push(CNNChannel.unpool(img.images[i], poolSize, unpooledWidth, unpooledHeight));
}
let unpooledImage = new CNNImage(unpooledChannels, unpooledWidth, unpooledHeight, img.images.length);
return unpooledImage;
}
static map(img, f) {
let mappedChannels = [];
for (let i = 0; i < img.depth; i++) {
mappedChannels.push(CNNChannel.map(img.images[i], f));
}
let mappedImage = new CNNImage(mappedChannels, img.width, img.height, img.depth);
return mappedImage;
}
static mult(img1, img2) {
if (img1.depth != img2.depth) {
console.error("Incompatible images to multiply!");
return;
}
let multipliedChannels = [];
for (let i = 0; i < img1.depth; i++) {
multipliedChannels.push(CNNChannel.mult(img1.images[i], img2.images[i]));
}
let multipliedImg = new CNNImage(multipliedChannels, img1.width, img2.height, img1.depth);
return multipliedImg;
}
/**
*
* @param {CNNImage} errorImg
* @param {CNNImage} inputImg
*/
static calcKernelDeltas(errorImg, inputImg, kernelSize) {
let deltas = [];
for (let i = 0; i < errorImg.depth; i++) {
let error = errorImg.images[i];
// let partialErrorChannels = [];
// for (let i = 0; i < inputImg.depth; i++) {
// partialErrorChannels[i] = new CNNChannel([], inputImg.width, inputImg.height);
// }
// for (let i = 0; i < inputImg.images[0].pixels.length; i++) {
// let sum = 0;
// for (let j = 0; j < inputImg.depth; j++) {
// sum += inputImg.images[j].pixels[i];
// }
// if (sum == 0) {
// sum = 1;
// }
// for (let j = 0; j < inputImg.depth; j++) {
// partialErrorChannels[j].pixels.push(inputImg.images[j].pixels[i] * error.pixels[i] / sum);
// }
// }
// let partialErrorImage = new CNNImage(partialErrorChannels, inputImg.width, inputImg.height, inputImg.depth);
//partialErrorImage.drawImage(30 * i, 70);
deltas.push([]);
for (let j = 0; j < inputImg.depth; j++) {
let ip = inputImg.images[j];
// let e = partialErrorImage.images[j];
let delta = CNNChannel.calcKernelDeltas(error, ip, kernelSize);
deltas[i].push(delta);
}
}
return deltas;
}
static deserialize(imageObj) {
let channels = [];
for (let i = 0; i < imageObj.images.length; i++) {
channels.push(CNNChannel.deserialize(imageObj.images[i]));
}
return new CNNImage(channels, imageObj.width, imageObj.height, imageObj.depth);
}
}