-
Notifications
You must be signed in to change notification settings - Fork 213
/
main.js
487 lines (437 loc) · 19.3 KB
/
main.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
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
/**
* @license
* Copyright 2018 Reiichiro Nakano All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import 'babel-polyfill';
import * as tf from '@tensorflow/tfjs';
tf.ENV.set('WEBGL_PACK', false); // This needs to be done otherwise things run very slow v1.0.4
import links from './links';
/**
* Main application to start on window load
*/
class Main {
constructor() {
if (window.mobilecheck()) {
document.getElementById('mobile-warning').hidden = false;
}
this.fileSelect = document.getElementById('file-select');
// Initialize model selection
this.modelSelectStyle = document.getElementById('model-select-style');
this.modelSelectStyle.onchange = (evt) => {
if (evt.target.value === 'mobilenet') {
this.disableStylizeButtons();
this.loadMobileNetStyleModel().then(model => {
this.styleNet = model;
}).finally(() => this.enableStylizeButtons());
} else if (evt.target.value === 'inception') {
this.disableStylizeButtons();
this.loadInceptionStyleModel().then(model => {
this.styleNet = model;
}).finally(() => this.enableStylizeButtons());
}
}
this.modelSelectTransformer = document.getElementById('model-select-transformer');
this.modelSelectTransformer.onchange = (evt) => {
if (evt.target.value === 'original') {
this.disableStylizeButtons();
this.loadOriginalTransformerModel().then(model => {
this.transformNet = model;
}).finally(() => this.enableStylizeButtons());
} else if (evt.target.value === 'separable') {
this.disableStylizeButtons();
this.loadSeparableTransformerModel().then(model => {
this.transformNet = model;
}).finally(() => this.enableStylizeButtons());
}
}
this.initalizeWebcamVariables();
this.initializeStyleTransfer();
this.initializeCombineStyles();
Promise.all([
this.loadMobileNetStyleModel(),
this.loadSeparableTransformerModel(),
]).then(([styleNet, transformNet]) => {
console.log('Loaded styleNet');
this.styleNet = styleNet;
this.transformNet = transformNet;
this.enableStylizeButtons()
});
}
async loadMobileNetStyleModel() {
if (!this.mobileStyleNet) {
this.mobileStyleNet = await tf.loadGraphModel(
'saved_model_style_js/model.json');
}
return this.mobileStyleNet;
}
async loadInceptionStyleModel() {
if (!this.inceptionStyleNet) {
this.inceptionStyleNet = await tf.loadGraphModel(
'saved_model_style_inception_js/model.json');
}
return this.inceptionStyleNet;
}
async loadOriginalTransformerModel() {
if (!this.originalTransformNet) {
this.originalTransformNet = await tf.loadGraphModel(
'saved_model_transformer_js/model.json'
);
}
return this.originalTransformNet;
}
async loadSeparableTransformerModel() {
if (!this.separableTransformNet) {
this.separableTransformNet = await tf.loadGraphModel(
'saved_model_transformer_separable_js/model.json'
);
}
return this.separableTransformNet;
}
initalizeWebcamVariables() {
this.camModal = $('#cam-modal');
this.snapButton = document.getElementById('snap-button');
this.webcamVideoElement = document.getElementById('webcam-video');
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
this.camModal.on('hidden.bs.modal', () => {
this.stream.getTracks()[0].stop();
})
this.camModal.on('shown.bs.modal', () => {
navigator.getUserMedia(
{
video: true
},
(stream) => {
this.stream = stream;
this.webcamVideoElement.srcObject = stream;
this.webcamVideoElement.play();
},
(err) => {
console.error(err);
}
);
})
}
openModal(element) {
this.camModal.modal('show');
this.snapButton.onclick = () => {
const hiddenCanvas = document.getElementById('hidden-canvas');
const hiddenContext = hiddenCanvas.getContext('2d');
hiddenCanvas.width = this.webcamVideoElement.width;
hiddenCanvas.height = this.webcamVideoElement.height;
hiddenContext.drawImage(this.webcamVideoElement, 0, 0,
hiddenCanvas.width, hiddenCanvas.height);
const imageDataURL = hiddenCanvas.toDataURL('image/jpg');
element.src = imageDataURL;
this.camModal.modal('hide');
};
}
initializeStyleTransfer() {
// Initialize images
this.contentImg = document.getElementById('content-img');
this.contentImg.onerror = () => {
alert("Error loading " + this.contentImg.src + ".");
}
this.styleImg = document.getElementById('style-img');
this.styleImg.onerror = () => {
alert("Error loading " + this.styleImg.src + ".");
}
this.stylized = document.getElementById('stylized');
// Initialize images
this.contentImgSlider = document.getElementById('content-img-size');
this.connectImageAndSizeSlider(this.contentImg, this.contentImgSlider);
this.styleImgSlider = document.getElementById('style-img-size');
this.styleImgSquare = document.getElementById('style-img-square');
this.connectImageAndSizeSlider(this.styleImg, this.styleImgSlider, this.styleImgSquare);
this.styleRatio = 1.0
this.styleRatioSlider = document.getElementById('stylized-img-ratio');
this.styleRatioSlider.oninput = (evt) => {
this.styleRatio = evt.target.value/100.;
}
// Initialize buttons
this.styleButton = document.getElementById('style-button');
this.styleButton.onclick = () => {
this.disableStylizeButtons();
this.startStyling().finally(() => {
this.enableStylizeButtons();
});
};
this.randomizeButton = document.getElementById('randomize');
this.randomizeButton.onclick = () => {
this.styleRatioSlider.value = getRndInteger(0, 100);
this.contentImgSlider.value = getRndInteger(256, 400);
this.styleImgSlider.value = getRndInteger(100, 400);
this.styleRatioSlider.dispatchEvent(new Event("input"));
this.contentImgSlider.dispatchEvent(new Event("input"));
this.styleImgSlider.dispatchEvent(new Event("input"));
if (getRndInteger(0, 1)) {
this.styleImgSquare.click();
}
}
// Initialize selectors
this.contentSelect = document.getElementById('content-select');
this.contentSelect.onchange = (evt) => this.setImage(this.contentImg, evt.target.value);
this.contentSelect.onclick = () => this.contentSelect.value = '';
this.styleSelect = document.getElementById('style-select');
this.styleSelect.onchange = (evt) => this.setImage(this.styleImg, evt.target.value);
this.styleSelect.onclick = () => this.styleSelect.value = '';
}
initializeCombineStyles() {
// Initialize images
this.combContentImg = document.getElementById('c-content-img');
this.combContentImg.onerror = () => {
alert("Error loading " + this.combContentImg.src + ".");
}
this.combStyleImg1 = document.getElementById('c-style-img-1');
this.combStyleImg1.onerror = () => {
alert("Error loading " + this.combStyleImg1.src + ".");
}
this.combStyleImg2 = document.getElementById('c-style-img-2');
this.combStyleImg2.onerror = () => {
alert("Error loading " + this.combStyleImg2.src + ".");
}
this.combStylized = document.getElementById('c-stylized');
// Initialize images
this.combContentImgSlider = document.getElementById('c-content-img-size');
this.connectImageAndSizeSlider(this.combContentImg, this.combContentImgSlider);
this.combStyleImg1Slider = document.getElementById('c-style-img-1-size');
this.combStyleImg1Square = document.getElementById('c-style-1-square');
this.connectImageAndSizeSlider(this.combStyleImg1, this.combStyleImg1Slider, this.combStyleImg1Square);
this.combStyleImg2Slider = document.getElementById('c-style-img-2-size');
this.combStyleImg2Square = document.getElementById('c-style-2-square');
this.connectImageAndSizeSlider(this.combStyleImg2, this.combStyleImg2Slider, this.combStyleImg2Square);
this.combStyleRatio = 0.5
this.combStyleRatioSlider = document.getElementById('c-stylized-img-ratio');
this.combStyleRatioSlider.oninput = (evt) => {
this.combStyleRatio = evt.target.value/100.;
}
// Initialize buttons
this.combineButton = document.getElementById('combine-button');
this.combineButton.onclick = () => {
this.disableStylizeButtons();
this.startCombining().finally(() => {
this.enableStylizeButtons();
});
};
this.combRandomizeButton = document.getElementById('c-randomize');
this.combRandomizeButton.onclick = () => {
this.combContentImgSlider.value = getRndInteger(256, 400);
this.combStyleImg1Slider.value = getRndInteger(100, 400);
this.combStyleImg2Slider.value = getRndInteger(100, 400);
this.combStyleRatioSlider.value = getRndInteger(0, 100);
this.combContentImgSlider.dispatchEvent(new Event("input"));
this.combStyleImg1Slider.dispatchEvent(new Event("input"));
this.combStyleImg2Slider.dispatchEvent(new Event("input"));
this.combStyleRatioSlider.dispatchEvent(new Event("input"));
if (getRndInteger(0, 1)) {
this.combStyleImg1Square.click();
}
if (getRndInteger(0, 1)) {
this.combStyleImg2Square.click();
}
}
// Initialize selectors
this.combContentSelect = document.getElementById('c-content-select');
this.combContentSelect.onchange = (evt) => this.setImage(this.combContentImg, evt.target.value);
this.combContentSelect.onclick = () => this.combContentSelect.value = '';
this.combStyle1Select = document.getElementById('c-style-1-select');
this.combStyle1Select.onchange = (evt) => this.setImage(this.combStyleImg1, evt.target.value);
this.combStyle1Select.onclick = () => this.combStyle1Select.value = '';
this.combStyle2Select = document.getElementById('c-style-2-select');
this.combStyle2Select.onchange = (evt) => this.setImage(this.combStyleImg2, evt.target.value);
this.combStyle2Select.onclick = () => this.combStyle2Select.value = '';
}
connectImageAndSizeSlider(img, slider, square) {
slider.oninput = (evt) => {
img.height = evt.target.value;
if (img.style.width) {
// If this branch is triggered, then that means the image was forced to a square using
// a fixed pixel value.
img.style.width = img.height+"px"; // Fix width back to a square
}
}
if (square !== undefined) {
square.onclick = (evt) => {
if (evt.target.checked) {
img.style.width = img.height+"px";
} else {
img.style.width = '';
}
}
}
}
// Helper function for setting an image
setImage(element, selectedValue) {
if (selectedValue === 'file') {
console.log('file selected');
this.fileSelect.onchange = (evt) => {
const f = evt.target.files[0];
const fileReader = new FileReader();
fileReader.onload = ((e) => {
element.src = e.target.result;
});
fileReader.readAsDataURL(f);
this.fileSelect.value = '';
}
this.fileSelect.click();
} else if (selectedValue === 'pic') {
this.openModal(element);
} else if (selectedValue === 'random') {
const randomNumber = Math.floor(Math.random()*links.length);
element.src = links[randomNumber];
} else {
element.src = 'images/' + selectedValue + '.jpg';
}
}
enableStylizeButtons() {
this.styleButton.disabled = false;
this.combineButton.disabled = false;
this.modelSelectStyle.disabled = false;
this.modelSelectTransformer.disabled = false;
this.styleButton.textContent = 'Stylize';
this.combineButton.textContent = 'Combine Styles';
}
disableStylizeButtons() {
this.styleButton.disabled = true;
this.combineButton.disabled = true;
this.modelSelectStyle.disabled = true;
this.modelSelectTransformer.disabled = true;
}
async startStyling() {
await tf.nextFrame();
this.styleButton.textContent = 'Generating 100D style representation';
await tf.nextFrame();
let bottleneck = await tf.tidy(() => {
return this.styleNet.predict(tf.browser.fromPixels(this.styleImg).toFloat().div(tf.scalar(255)).expandDims());
})
if (this.styleRatio !== 1.0) {
this.styleButton.textContent = 'Generating 100D identity style representation';
await tf.nextFrame();
const identityBottleneck = await tf.tidy(() => {
return this.styleNet.predict(tf.browser.fromPixels(this.contentImg).toFloat().div(tf.scalar(255)).expandDims());
})
const styleBottleneck = bottleneck;
bottleneck = await tf.tidy(() => {
const styleBottleneckScaled = styleBottleneck.mul(tf.scalar(this.styleRatio));
const identityBottleneckScaled = identityBottleneck.mul(tf.scalar(1.0-this.styleRatio));
return styleBottleneckScaled.addStrict(identityBottleneckScaled)
})
styleBottleneck.dispose();
identityBottleneck.dispose();
}
this.styleButton.textContent = 'Stylizing image...';
await tf.nextFrame();
const stylized = await tf.tidy(() => {
return this.transformNet.predict([tf.browser.fromPixels(this.contentImg).toFloat().div(tf.scalar(255)).expandDims(), bottleneck]).squeeze();
})
await tf.browser.toPixels(stylized, this.stylized);
bottleneck.dispose(); // Might wanna keep this around
stylized.dispose();
}
async startCombining() {
await tf.nextFrame();
this.combineButton.textContent = 'Generating 100D style representation of image 1';
await tf.nextFrame();
const bottleneck1 = await tf.tidy(() => {
return this.styleNet.predict(tf.browser.fromPixels(this.combStyleImg1).toFloat().div(tf.scalar(255)).expandDims());
})
this.combineButton.textContent = 'Generating 100D style representation of image 2';
await tf.nextFrame();
const bottleneck2 = await tf.tidy(() => {
return this.styleNet.predict(tf.browser.fromPixels(this.combStyleImg2).toFloat().div(tf.scalar(255)).expandDims());
});
this.combineButton.textContent = 'Stylizing image...';
await tf.nextFrame();
const combinedBottleneck = await tf.tidy(() => {
const scaledBottleneck1 = bottleneck1.mul(tf.scalar(1-this.combStyleRatio));
const scaledBottleneck2 = bottleneck2.mul(tf.scalar(this.combStyleRatio));
return scaledBottleneck1.addStrict(scaledBottleneck2);
});
const stylized = await tf.tidy(() => {
return this.transformNet.predict([tf.browser.fromPixels(this.combContentImg).toFloat().div(tf.scalar(255)).expandDims(), combinedBottleneck]).squeeze();
})
await tf.browser.toPixels(stylized, this.combStylized);
bottleneck1.dispose(); // Might wanna keep this around
bottleneck2.dispose();
combinedBottleneck.dispose();
stylized.dispose();
}
async benchmark() {
const x = tf.randomNormal([1, 256, 256, 3]);
const bottleneck = tf.randomNormal([1, 1, 1, 100]);
let styleNet = await this.loadInceptionStyleModel();
let time = await this.benchmarkStyle(x, styleNet);
styleNet.dispose();
styleNet = await this.loadMobileNetStyleModel();
time = await this.benchmarkStyle(x, styleNet);
styleNet.dispose();
let transformNet = await this.loadOriginalTransformerModel();
time = await this.benchmarkTransform(
x, bottleneck, transformNet);
transformNet.dispose();
transformNet = await this.loadSeparableTransformerModel();
time = await this.benchmarkTransform(
x, bottleneck, transformNet);
transformNet.dispose();
x.dispose();
bottleneck.dispose();
}
async benchmarkStyle(x, styleNet) {
const profile = await tf.profile(() => {
tf.tidy(() => {
const dummyOut = styleNet.predict(x);
dummyOut.print();
});
});
console.log(profile);
const time = await tf.time(() => {
tf.tidy(() => {
for (let i = 0; i < 10; i++) {
const y = styleNet.predict(x);
y.print();
}
})
});
console.log(time);
}
async benchmarkTransform(x, bottleneck, transformNet) {
const profile = await tf.profile(() => {
tf.tidy(() => {
const dummyOut = transformNet.predict([x, bottleneck]);
dummyOut.print();
});
});
console.log(profile);
const time = await tf.time(() => {
tf.tidy(() => {
for (let i = 0; i < 10; i++) {
const y = transformNet.predict([x, bottleneck]);
y.print();
}
})
});
console.log(time);
}
}
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
window.mobilecheck = function() {
var check = false;
(function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
return check;
};
window.addEventListener('load', () => new Main());