-
Notifications
You must be signed in to change notification settings - Fork 418
/
predict.js
680 lines (595 loc) · 16.2 KB
/
predict.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// Copyright 2017 Google Inc.
//
// 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.
/**
* Author: David Ha <[email protected]>
*
* @fileoverview Basic p5.js sketch to show how to use sketch-rnn
* to finish the user's incomplete drawing, and loop through different
* endings automatically.
*/
var sketch = function( p ) {
"use strict";
var small_class_list = ['ant',
'antyoga',
'alarm_clock',
'ambulance',
'angel',
'backpack',
'barn',
'basket',
'bear',
'bee',
'beeflower',
'bicycle',
'bird',
'book',
'brain',
'bridge',
'bulldozer',
'bus',
'butterfly',
'cactus',
'calendar',
'castle',
'cat',
'catbus',
'catpig',
'chair',
'couch',
'crab',
'crabchair',
'crabrabbitfacepig',
'cruise_ship',
'diving_board',
'dog',
'dogbunny',
'dolphin',
'duck',
'elephant',
'elephantpig',
'eye',
'face',
'fan',
'fire_hydrant',
'firetruck',
'flamingo',
'flower',
'floweryoga',
'frog',
'frogsofa',
'garden',
'hand',
'hedgeberry',
'hedgehog',
'helicopter',
'kangaroo',
'key',
'lantern',
'lighthouse',
'lion',
'lionsheep',
'lobster',
'map',
'mermaid',
'monapassport',
'monkey',
'mosquito',
'octopus',
'owl',
'paintbrush',
'palm_tree',
'parrot',
'passport',
'peas',
'penguin',
'pig',
'pigsheep',
'pineapple',
'pool',
'postcard',
'power_outlet',
'rabbit',
'rabbitturtle',
'radio',
'radioface',
'rain',
'rhinoceros',
'rifle',
'roller_coaster',
'sandwich',
'scorpion',
'sea_turtle',
'sheep',
'skull',
'snail',
'snowflake',
'speedboat',
'spider',
'squirrel',
'steak',
'stove',
'strawberry',
'swan',
'swing_set',
'the_mona_lisa',
'tiger',
'toothbrush',
'toothpaste',
'tractor',
'trombone',
'truck',
'whale',
'windmill',
'yoga',
'yogabicycle'];
var large_class_list = ['ant',
'ambulance',
'angel',
'alarm_clock',
'antyoga',
'backpack',
'barn',
'basket',
'bear',
'bee',
'beeflower',
'bicycle',
'bird',
'book',
'brain',
'bridge',
'bulldozer',
'bus',
'butterfly',
'cactus',
'calendar',
'castle',
'cat',
'catbus',
'catpig',
'chair',
'couch',
'crab',
'crabchair',
'crabrabbitfacepig',
'cruise_ship',
'diving_board',
'dog',
'dogbunny',
'dolphin',
'duck',
'elephant',
'elephantpig',
'everything',
'eye',
'face',
'fan',
'fire_hydrant',
'firetruck',
'flamingo',
'flower',
'floweryoga',
'frog',
'frogsofa',
'garden',
'hand',
'hedgeberry',
'hedgehog',
'helicopter',
'kangaroo',
'key',
'lantern',
'lighthouse',
'lion',
'lionsheep',
'lobster',
'map',
'mermaid',
'monapassport',
'monkey',
'mosquito',
'octopus',
'owl',
'paintbrush',
'palm_tree',
'parrot',
'passport',
'peas',
'penguin',
'pig',
'pigsheep',
'pineapple',
'pool',
'postcard',
'power_outlet',
'rabbit',
'rabbitturtle',
'radio',
'radioface',
'rain',
'rhinoceros',
'rifle',
'roller_coaster',
'sandwich',
'scorpion',
'sea_turtle',
'sheep',
'skull',
'snail',
'snowflake',
'speedboat',
'spider',
'squirrel',
'steak',
'stove',
'strawberry',
'swan',
'swing_set',
'the_mona_lisa',
'tiger',
'toothbrush',
'toothpaste',
'tractor',
'trombone',
'truck',
'whale',
'windmill',
'yoga',
'yogabicycle'];
var use_large_models = true;
var class_list = small_class_list;
if (use_large_models) {
class_list = large_class_list;
}
// sketch_rnn model
var model;
var model_data;
var temperature = 0.25;
var min_sequence_length = 5;
var model_pdf; // store all the parameters of a mixture-density distribution
var model_state, model_state_orig;
var model_prev_pen;
var model_dx, model_dy;
var model_pen_down, model_pen_up, model_pen_end;
var model_x, model_y;
var model_is_active;
// variables for the sketch input interface.
var pen;
var prev_pen;
var x, y; // absolute coordinates on the screen of where the pen is
var start_x, start_y;
var has_started; // set to true after user starts writing.
var just_finished_line;
var epsilon = 2.0; // to ignore data from user's pen staying in one spot.
var raw_lines;
var current_raw_line;
var strokes;
var line_color, predict_line_color;
// UI
var screen_width, screen_height, temperature_slider;
var line_width = 2.0;
var screen_scale_factor = 3.0;
// dom
var reset_button, model_sel, random_model_button;
var text_title, text_temperature;
var title_text = "sketch-rnn predictor.";
var set_title_text = function(new_text) {
title_text = new_text.split('_').join(' ');
text_title.html(title_text);
text_title.position(screen_width/2-12*title_text.length/2+10, 0);
};
var update_temperature_text = function() {
var the_color="rgba("+Math.round(255*temperature)+",0,"+255+",1)";
text_temperature.style("color", the_color); // ff990a
text_temperature.html(""+Math.round(temperature*100));
};
var draw_example = function(example, start_x, start_y, line_color) {
var i;
var x=start_x, y=start_y;
var dx, dy;
var pen_down, pen_up, pen_end;
var prev_pen = [1, 0, 0];
for(i=0;i<example.length;i++) {
// sample the next pen's states from our probability distribution
[dx, dy, pen_down, pen_up, pen_end] = example[i];
if (prev_pen[2] == 1) { // end of drawing.
break;
}
// only draw on the paper if the pen is touching the paper
if (prev_pen[0] == 1) {
p.stroke(line_color);
p.strokeWeight(line_width);
p.line(x, y, x+dx, y+dy); // draw line connecting prev point to current point.
}
// update the absolute coordinates from the offsets
x += dx;
y += dy;
// update the previous pen's state to the current one we just sampled
prev_pen = [pen_down, pen_up, pen_end];
}
};
var init = function() {
// model
ModelImporter.set_init_model(model_raw_data);
if (use_large_models) {
ModelImporter.set_model_url("https://storage.googleapis.com/quickdraw-models/sketchRNN/large_models/");
}
model_data = ModelImporter.get_model_data();
model = new SketchRNN(model_data);
model.set_pixel_factor(screen_scale_factor);
screen_width = p.windowWidth; //window.innerWidth
screen_height = p.windowHeight; //window.innerHeight
// dom
reset_button = p.createButton('clear drawing');
reset_button.position(10, screen_height-27-27);
reset_button.mousePressed(reset_button_event); // attach button listener
// random model buttom
random_model_button = p.createButton('random');
random_model_button.position(117, screen_height-27-27);
random_model_button.mousePressed(random_model_button_event); // attach button listener
// model selection
model_sel = p.createSelect();
model_sel.position(195, screen_height-27-27);
for (var i=0;i<class_list.length;i++) {
model_sel.option(class_list[i]);
}
model_sel.changed(model_sel_event);
// temp
temperature_slider = p.createSlider(1, 100, temperature*100);
temperature_slider.position(0*screen_width/2-10*0+10, screen_height-27);
temperature_slider.style('width', screen_width/1-25+'px');
temperature_slider.changed(temperature_slider_event);
// title
text_title = p.createP(title_text);
text_title.style("font-family", "Courier New");
text_title.style("font-size", "20");
text_title.style("color", "#3393d1"); // ff990a
set_title_text(title_text);
// temperature text
text_temperature = p.createP();
text_temperature.style("font-family", "Courier New");
text_temperature.style("font-size", "16");
text_temperature.position(screen_width-40, screen_height-64);
update_temperature_text(title_text);
};
var encode_strokes = function(sequence) {
model_state_orig = model.zero_state();
if (sequence.length <= min_sequence_length) {
return;
}
// encode sequence
model_state_orig = model.update(model.zero_input(), model_state_orig);
for (var i=0;i<sequence.length-1;i++) {
model_state_orig = model.update(sequence[i], model_state_orig);
}
restart_model(sequence);
model_is_active = true;
}
var restart_model = function(sequence) {
model_state = model.copy_state(model_state_orig); // bounded
var idx = raw_lines.length-1;
var last_point = raw_lines[idx][raw_lines[idx].length-1];
var last_x = last_point[0];
var last_y = last_point[1];
// individual models:
var sx = last_x;
var sy = last_y;
var dx, dy, pen_down, pen_up, pen_end;
var s = sequence[sequence.length-1];
model_x = sx;
model_y = sy;
dx = s[0];
dy = s[1];
pen_down = s[2];
pen_up = s[3];
pen_end = s[4];
model_dx = dx;
model_dy = dy;
model_prev_pen = [pen_down, pen_up, pen_end];
}
var restart = function() {
// reinitialize variables before calling p5.js setup.
line_color = p.color(p.random(64, 224), p.random(64, 224), p.random(64, 224));
predict_line_color = p.color(p.random(64, 224), p.random(64, 224), p.random(64, 224));
// make sure we enforce some minimum size of our demo
screen_width = Math.max(window.innerWidth, 480);
screen_height = Math.max(window.innerHeight, 320);
// variables for the sketch input interface.
pen = 0;
prev_pen = 1;
has_started = false; // set to true after user starts writing.
just_finished_line = false;
raw_lines = [];
current_raw_line = [];
strokes = [];
// start drawing from somewhere in middle of the canvas
x = screen_width/2.0;
y = screen_height/2.0;
start_x = x;
start_y = y;
has_started = false;
model_x = x;
model_y = y;
model_prev_pen = [0, 1, 0];
model_is_active = false;
};
var clear_screen = function() {
p.background(255, 255, 255, 255);
p.fill(255, 255, 255, 255);
};
p.setup = function() {
init();
restart();
p.createCanvas(screen_width, screen_height);
p.frameRate(60);
clear_screen();
console.log('ready.');
};
// tracking mouse touchpad
var tracking = {
down: false,
x: 0,
y: 0
};
p.draw = function() {
deviceEvent();
// record pen drawing from user:
if (tracking.down && (tracking.x > 0) && tracking.y < (screen_height-60)) { // pen is touching the paper
if (has_started == false) { // first time anything is written
has_started = true;
x = tracking.x;
y = tracking.y;
start_x = x;
start_y = y;
pen = 0;
}
var dx0 = tracking.x-x; // candidate for dx
var dy0 = tracking.y-y; // candidate for dy
if (dx0*dx0+dy0*dy0 > epsilon*epsilon) { // only if pen is not in same area
var dx = dx0;
var dy = dy0;
pen = 0;
if (prev_pen == 0) {
p.stroke(line_color);
p.strokeWeight(line_width); // nice thick line
p.line(x, y, x+dx, y+dy); // draw line connecting prev point to current point.
}
// update the absolute coordinates from the offsets
x += dx;
y += dy;
// update raw_lines
current_raw_line.push([x, y]);
just_finished_line = true;
// using the previous pen states, and hidden state, get next hidden state
// update_rnn_state();
}
} else { // pen is above the paper
pen = 1;
if (just_finished_line) {
var current_raw_line_simple = DataTool.simplify_line(current_raw_line);
var idx, last_point, last_x, last_y;
if (current_raw_line_simple.length > 1) {
if (raw_lines.length === 0) {
last_x = start_x;
last_y = start_y;
} else {
idx = raw_lines.length-1;
last_point = raw_lines[idx][raw_lines[idx].length-1];
last_x = last_point[0];
last_y = last_point[1];
}
var stroke = DataTool.line_to_stroke(current_raw_line_simple, [last_x, last_y]);
raw_lines.push(current_raw_line_simple);
strokes = strokes.concat(stroke);
// initialize rnn:
encode_strokes(strokes);
// redraw simplified strokes
clear_screen();
draw_example(strokes, start_x, start_y, line_color);
/*
p.stroke(line_color);
p.strokeWeight(2.0);
p.ellipse(x, y, 5, 5); // draw line connecting prev point to current point.
*/
} else {
if (raw_lines.length === 0) {
has_started = false;
}
}
current_raw_line = [];
just_finished_line = false;
}
// have machine take over the drawing here:
if (model_is_active) {
model_pen_down = model_prev_pen[0];
model_pen_up = model_prev_pen[1];
model_pen_end = model_prev_pen[2];
model_state = model.update([model_dx, model_dy, model_pen_down, model_pen_up, model_pen_end], model_state);
model_pdf = model.get_pdf(model_state);
[model_dx, model_dy, model_pen_down, model_pen_up, model_pen_end] = model.sample(model_pdf, temperature);
if (model_pen_end === 1) {
restart_model(strokes);
//model_pen_end = 0;
//model_pen_down = 1;
//model_pen_up = 1;
predict_line_color = p.color(p.random(64, 224), p.random(64, 224), p.random(64, 224));
clear_screen();
draw_example(strokes, start_x, start_y, line_color);
} else {
if (model_prev_pen[0] === 1) {
// draw line connecting prev point to current point.
p.stroke(predict_line_color);
p.strokeWeight(line_width);
p.line(model_x, model_y, model_x+model_dx, model_y+model_dy);
}
model_prev_pen = [model_pen_down, model_pen_up, model_pen_end];
model_x += model_dx;
model_y += model_dy;
}
}
}
prev_pen = pen;
};
var model_sel_event = function() {
var c = model_sel.value();
var model_mode = "gen";
console.log("user wants to change to model "+c);
var call_back = function(new_model) {
model = new_model;
model.set_pixel_factor(screen_scale_factor);
encode_strokes(strokes);
clear_screen();
draw_example(strokes, start_x, start_y, line_color);
set_title_text('draw '+model.info.name+'.');
}
set_title_text('loading '+c+' model...');
ModelImporter.change_model(model, c, model_mode, call_back);
};
var random_model_button_event = function() {
var item = class_list[Math.floor(Math.random()*class_list.length)];
model_sel.value(item);
model_sel_event();
};
var reset_button_event = function() {
restart();
clear_screen();
};
var temperature_slider_event = function() {
temperature = temperature_slider.value()/100;
clear_screen();
draw_example(strokes, start_x, start_y, line_color);
update_temperature_text();
};
var deviceReleased = function() {
"use strict";
tracking.down = false;
}
var devicePressed = function(x, y) {
if (y < (screen_height-60)) {
tracking.x = x;
tracking.y = y;
if (!tracking.down) {
tracking.down = true;
}
}
};
var deviceEvent = function() {
if (p.mouseIsPressed) {
devicePressed(p.mouseX, p.mouseY);
} else {
deviceReleased();
}
}
};
var custom_p5 = new p5(sketch, 'sketch');