forked from micah5/sneaker-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
335 lines (324 loc) Β· 11.6 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons' rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<style>
#app {
background-color: #ffffff;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
h1 {
font-size: 60px;
line-height: 55px;
}
.bar {
position: absolute;
height: 5px;
width: 100%;
bottom: 0;
z-index: 1;
}
.rainbow {
background: linear-gradient(270deg, #ff0000, #f9ff00, #00ff20, #00fdff, #0011ff, #dc00ff);
background-size: 1200% 1200%;
-webkit-animation: AnimationName 26s ease infinite;
-moz-animation: AnimationName 26s ease infinite;
animation: AnimationName 26s ease infinite;
}
@-webkit-keyframes AnimationName {
0%{background-position:0% 50%}
50%{background-position:100% 50%}
100%{background-position:0% 50%}
}
@-moz-keyframes AnimationName {
0%{background-position:0% 50%}
50%{background-position:100% 50%}
100%{background-position:0% 50%}
}
@keyframes AnimationName {
0%{background-position:0% 50%}
50%{background-position:100% 50%}
100%{background-position:0% 50%}
}
.custom-loader {
animation: loader 1s infinite;
display: flex;
}
@-moz-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
@-webkit-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
@-o-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
@keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div id="app">
<v-app>
<v-content>
<v-container>
<h1 style="position: relative" :class="$vuetify.breakpoint.smAndDown ? 'mt-5' : 'mt-0'">
<div class="rainbow bar"></div>
<div style="position: relative; z-index: 2">βSneaker Generatorβ<div>
</h1>
<a class="pa-3 mr-5" href="https://github.com/98mprice/sneaker-generator" style="position: absolute; top: 0; right: 0; z-index: 5;">
<h2>fork me! π½</h2>
</a>
<template grid-list-sm fluid>
<v-layout row wrap>
<v-flex
v-for="n in random12"
:key="n"
xs3 sm2 lg1
d-flex
>
<v-card flat tile class="d-flex">
<v-img
:src="`github-pages/img/shoe${n + 1}.png`"
aspect-ratio="1"
class="grey lighten-2"
>
<v-layout
slot="placeholder"
fill-height
align-center
justify-center
ma-0
>
<v-progress-circular indeterminate color="grey lighten-5"></v-progress-circular>
</v-layout>
</v-img>
</v-card>
</v-flex>
</v-layout>
</template>
<h2>Neural Network that makes shoes. Pointless, but kinda fun.</h2>
<h2 class="mt-4">Try it on your machine</h2>
<p>You'll need some things installed, but you can run it either with <a href="https://github.com/98mprice/sneaker-generator/blob/master/node/predict.js">Node.js</a> or <a href="https://github.com/98mprice/sneaker-generator/blob/master/python/predict.py">Python</a>.</p>
<h2>Try it in the browser</h2>
<p class="mb-0"><b>Due to a limitation with Tensorflow.js in the browser, model prediction can only happen on the CPU.</b></p>
<p class="mb-0">Therefore, depending on the speed of your CPU, it can be pretty slow. On my 1,6 GHz i5 it takes ~10 mins.
<br>On the upside, one iteration produces 64 shoes.
</p>
<v-layout row wrap class="mt-3">
<v-flex xs12 sm6>
<div class="rainbow" style="position: relative; width: 266px; height: 266px; display: block; margin-left: auto; margin-right: auto;">
<canvas id="myCanvas" width="256" height="256" style="position: absolute; width: 256px; height: 256px; background-color: white; left: 5px; top: 5px;"></canvas>
</div>
</v-flex>
<v-flex xs12 sm6>
<v-layout column fill-height>
<v-btn
flat
:loading="loading_model"
:disabled="loading_model || model != null"
@click="downloadModel"
>
<v-icon left dark>cloud_download</v-icon>fetch model
<span slot="loader" class="custom-loader">
<v-icon light>cached</v-icon>
</span>
</v-btn>
<v-btn
flat
:loading="loading_predict"
:disabled="model == null"
@click="predict"
>
start
<span slot="loader" class="custom-loader">
<v-icon light>cached</v-icon>
</span>
</v-btn>
<v-dialog
v-model="loading_predict"
hide-overlay
persistent
width="300"
>
<v-card
class="rainbow"
>
<v-card-text>
<p>Currently {{status}}...</p>
<b>This tab might freeze!
Don't be alarmed, it's just doing the prediction.
Leave it for a while and let it do it's thing.
</b>
<v-progress-linear
indeterminate
color="black"
class="mb-0"
></v-progress-linear>
</v-card-text>
</v-card>
</v-dialog>
<v-spacer></v-spacer>
<v-slide-y-transition>
<v-layout row v-show="preds != null">
<v-flex xs6>
<v-btn flat block @click="drawShoe">
<v-icon left dark>arrow_forward</v-icon>next shoe
</v-btn>
</v-flex>
<v-flex xs6>
<v-btn flat block @click="save">
<v-icon left dark>save_alt</v-icon>save
</v-btn>
<a id="download" download="triangle.png"></a>
</v-flex>
</v-layout>
</v-slide-y-transition>
</v-layout>
</v-flex>
</v-layout>
</v-container>
</v-content>
</v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify/dist/vuetify.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"> </script>
<script src="https://cdn.jsdelivr.net/gh/nicolaspanel/[email protected]/dist/numjs.min.js"></script>
<script>
new Vue({
el: '#app',
data: {
loading_model: false,
loading_predict: false,
model: null,
status: 'preparing',
count: 0,
preds: null
},
mounted: function() {
tf.setBackend('cpu')
},
computed: {
random12: function() {
let x = Array(12).fill(0).map((e,i)=>i+1)
this.shuffle(x)
return x
}
},
methods: {
shuffle: function(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
},
downloadModel: async function() {
this.loading_model = true;
console.log('doing')
const model = await tf.loadModel('https://raw.githubusercontent.com/98mprice/sneaker-test/master/src/generator/model.json');
console.log('done')
this.model = model;
this.loading_model = false;
},
randn_bm: function(min, max, skew) {
var u = 0, v = 0;
while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
while(v === 0) v = Math.random();
let num = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
num = num / 10.0 + 0.5; // Translate to 0 -> 1
if (num > 1 || num < 0) num = randn_bm(min, max, skew); // resample between 0 and 1 if out of range
num = Math.pow(num, skew); // Skew
num *= max - min; // Stretch to fill range
num += min; // offset to min
return num;
},
test: function() {
this.loading_predict = true
this.status = "doing stuff"
setTimeout(() => (this.loading_predict = false), 4000)
},
save: function() {
var download = document.getElementById("download");
var image = document.getElementById("myCanvas").toDataURL("image/png").replace("image/png", "image/octet-stream");
download.setAttribute("href", image);
},
drawShoe: function() {
if (this.count >= this.preds.length) {
this.count = 0
}
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var imgData=ctx.createImageData(256,256);
for (var i = 0; i<imgData.data.length; i += 4) {
r = (this.preds[this.count] + 1)*127.5
g = (this.preds[this.count+1] + 1)*127.5
b = (this.preds[this.count+2] + 1)*127.5
imgData.data[i+0]=r;
imgData.data[i+1]=b;
imgData.data[i+2]=g;
imgData.data[i+3]=255;
this.count += 3
}
ctx.putImageData(imgData,0,0);
},
predict: async function() {
this.loading_predict = true
console.log('predict')
const batch_size = 64
// Generate noise
console.log('generating noise')
this.status = "generating noise"
let noise = nj.zeros([batch_size, 1, 1, 100])
for (var i = 0; i < batch_size; i++) {
for (var j = 0; j < 100; j++) {
noise.set(i, 0, 0, j, this.randn_bm(-5, 5, 1))
}
}
let noise_tensor = tf.tensor4d(noise.tolist())
noise_tensor.print(true)
// Generate images
console.log('generating images')
this.status = "creating shoes"
await new Promise(resolve => setTimeout(resolve, 1000));
let generated_images = this.model.predict(noise_tensor)
let output_data = await generated_images.dataSync()
this.preds = Array.prototype.slice.call(output_data);
// Draw image
console.log('drawing image')
this.status = "drawing first shoe " + (this.preds == null)
this.drawShoe()
this.loading_predict = false
}
}
})
</script>
</body>
</html>