transcranial / keras-js Goto Github PK
View Code? Open in Web Editor NEWRun Keras models in the browser, with GPU support using WebGL
Home Page: https://transcranial.github.io/keras-js
License: MIT License
Run Keras models in the browser, with GPU support using WebGL
Home Page: https://transcranial.github.io/keras-js
License: MIT License
I have a model with two inputs named input_a
and input_b
, defined in that order.
Trying to make a prediction with a keras.js model, this works:
model.predict({'input_a': data_a, 'input_b': data_b})
.then(outputData => {
// ...
})
However, if you order them backwards in the object being passed to model.predict()
, it throws an error:
model.predict({'input_b': data_b, 'input_a': data_a})
.then(outputData => {
// ...
})
Error: predict() must take an object where the keys are the named inputs of the model: input_a,input_b.
Yikes! Small problem, but it stumped me for about an hour to figure out why it wasn't working, and the error message is misleading in this case
Great work, btw!
It seems the script is assuming weblasTensor
is available after calling createWeblasTensor
. However, when MAX_TEXTURE_SIZE
is not enough, createWeblasTensor
may create weblasTensorsSplit
instead (or raise an error if it is really not enough).
If it is hard to use weblasTensorsSplit
for this, I think it would be better to show some warning and gracefully fallback to CPU.
Is there a reason weblas isn't included as a dependency in npm? Setting this lib up in package managed builds is a touch more difficult because of how weblas is included.
0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'run', 'watch' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prewatch', 'watch', 'postwatch' ]
5 info lifecycle [email protected]~prewatch: [email protected]
6 silly lifecycle [email protected]~prewatch: no script for prewatch, continuing
7 info lifecycle [email protected]~watch: [email protected]
8 verbose lifecycle [email protected]~watch: unsafe-perm in lifecycle true
9 verbose lifecycle [email protected]~watch: PATH: /usr/share/npm/bin/node-gyp-bin:/home/y/crypto/keras-js/node_modules/.bin:/home/y/bin:/home/y/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
10 verbose lifecycle [email protected]~watch: CWD: /home/y/crypto/keras-js
11 silly lifecycle [email protected]~watch: Args: [ '-c', 'webpack --watch --config webpack.dev.config.js' ]
12 info lifecycle [email protected]~watch: Failed to exec watch script
13 verbose stack Error: [email protected] watch: `webpack --watch --config webpack.dev.config.js`
13 verbose stack spawn ENOENT
13 verbose stack at ChildProcess.<anonymous> (/usr/share/npm/lib/utils/spawn.js:17:16)
13 verbose stack at emitTwo (events.js:87:13)
13 verbose stack at ChildProcess.emit (events.js:172:7)
13 verbose stack at maybeClose (internal/child_process.js:821:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
14 verbose pkgid [email protected]
15 verbose cwd /home/y/crypto/keras-js
16 error Linux 4.4.0-45-generic
17 error argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "watch"
18 error node v4.2.6
19 error npm v3.5.2
20 error file sh
21 error code ELIFECYCLE
22 error errno ENOENT
23 error syscall spawn
24 error [email protected] watch: `webpack --watch --config webpack.dev.config.js`
24 error spawn ENOENT
25 error Failed at the [email protected] watch script 'webpack --watch --config webpack.dev.config.js'.
25 error Make sure you have the latest version of node.js and npm installed.
25 error If you do, this is most likely a problem with the keras-js package,
25 error not with npm itself.
25 error Tell the author that this fails on your system:
25 error webpack --watch --config webpack.dev.config.js
25 error You can get information on how to open an issue for this project with:
25 error npm bugs keras-js
25 error Or if that isn't available, you can get their info via:
25 error npm owner ls keras-js
25 error There is likely additional logging output above.
26 verbose exit [ 1, true ]
When installing:
npm WARN optional Skipping failed optional dependency /chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: [email protected]
npm WARN [email protected] scripts['server'] should probably be scripts['start'].
Just made a toy example of an insult dector using a character level CNN:
https://keras-predict-r.c9users.io/insults.html
The F1 score on the validation set is around 0.65, so it's not great but it works for many insults. It was trained on a small Kaggle insult dataset of around 3,500 examples.
It will take up to 10 seconds to load the first time, and a text form field will appear. The form appears after the model Promise says the model is ready.
In the mobile version, the problem is that never detects any insults that are detected in the non-mobile version on my laptop (desktop chrome and safari both work).
The weights are around 10mb, so I'm guessing this has to do with memory, unless it could be a gpu issue.
P.S. I did just notice this issue as well: #6
I use react-redux starter kit.
I installed keras-js with npm install keras-js --save
when I try to include {Model} from 'keras-js' then I get the following errors:
ERROR in .//keras-js/lib/Model.js/keras-js/lib/Model.js 215:50-63
Module not found: Error: Cannot resolve module 'fs' in /media/v/a218f425-0329-45ac-8571-5497bea24d71/home/v/workspaces/webstorm/newMIDIcheck/node_modules/keras-js/lib
@ ./
ERROR in .//keras-js/lib/Tensor.js/keras-js/lib/Tensor.js 142:40-70
Module not found: Error: Cannot resolve 'file' or 'directory' ./texture_copy.glsl in /media/v/a218f425-0329-45ac-8571-5497bea24d71/home/v/workspaces/webstorm/newMIDIcheck/node_modules/keras-js/lib
@ ./
it misses a lot of other glsl files, and yes, they aren't there. Also as far as fs goes, It shouldn't be used in the browser, how to get around that?
Thanks everyone & srry for noob question.
Keras supports variable-size input layers; they're specified as follows (where the two "None"s would typically be width and height):
inputlayer = Input(shape=(None, None, 4))
This feature is very useful for certain applications (e.g. entirely convolutional networks for image processing). This input layer is then encoded to JSON by encoder.py
as:
...
"config": {
"batch_input_shape": [null, null, null, 4],
"input_dtype": "float32",
"sparse": false,
"name": "input_1"
},
...
However, trying to use this model in keras.js results in a "Specified shape incompatible with data." error from checkShape
when trying to predict (since shape.reduce((a, b) => a * b, 1)
is 0 if shape
contains a null
). This suggests that variable-size input layers are not yet supported as part of Keras.js.
I'm not sure if this is a planned feature, unplanned feature, or already implemented (and I just missed a configuration step somewhere), so feel free to label/delete this issue as appropriate. Thanks for the amazing library!
This library appears to be focused on the Model structure, which is okay, but most of my trained networks are in Sequential form. Specifically LSTM Sequentials.
LSTM Sequentials generally expect a three-dimensional input (batchSize, X,Y). However this repo requires there to be only one flattened input for Sequential, named "input". Has anyone found a good way to convert Sequentials to a Model or a way around this?
I would like to just do prediction in javascript. That means the model shouldn't even need to be compiled, so maybe I can just hack something together myself.
I noticed an error building Convolutional1D layer in js when the input has 1 feature layer. I added a failing test for it to this fork, so that it can be easily reproduced.
Here is the error reported at localhost:3000/test (all other tests pass)
Error: cwise: Arrays do not all have the same shape!
at assign_cwise_thunk (eval at createThunk (eval at <anonymous> (http://localhost:3000/dist/keras.js:2858:1)), <anonymous>:8:71)
at Object.assign_ndarrayops [as assign] (eval at makeOp (eval at <anonymous> (http://localhost:3000/dist/keras.js:108:1)), <anonymous>:3:43)
at Convolution2D._w2row (eval at <anonymous> (http://localhost:3000/dist/keras.js:493:1), <anonymous>:274:30)
at Convolution2D.setWeights (eval at <anonymous> (http://localhost:3000/dist/keras.js:493:1), <anonymous>:126:12)
at Convolution1D.setWeights (eval at <anonymous> (http://localhost:3000/dist/keras.js:1355:1), <anonymous>:103:20)
at r.<anonymous> (convolutional/Convolution1D.js:61:19)
at r (https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.min.js:2:7852)
at r.run (https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.min.js:2:8853)
at i.runTest (https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.min.js:2:13553)
at https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.min.js:2:14192
at r (https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.min.js:2:13024)
at https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.min.js:2:13000
at n (https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.min.js:2:12791)
at https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.min.js:2:12855
at i (https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.min.js:1:559)
I'm running 100-300 predictions at a time in an interactive data visualization (like this: https://www.nytimes.com/interactive/2014/upshot/buy-rent-calculator.html)
I've read this thread: #27 and this works properly:
for (let arr of batch) {
let output = await model.predict({ input: arr })
}
But I'm hoping to speed this up. The code above runs the predictions synchronously. For 100 predictions, I get 366ms with gpu: false
and 2600ms with gpu: true
(the GPU running slower is strange, but that's not the issue I'm posting about).
It seems like you could run the promises (and calculations?) concurrently. Here is a good overview: https://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html (scroll down to concurrent loops).
let promises = docs.map((doc) => db.post(doc));
let results = await Promise.all(promises);
console.log(results);
When I try that with a batch of predictions, all of my predictions turn out the same (the last prediction).
async function getPredictions(inputs) {
await model.ready();
const promises = inputs.map(input => model.predict({ input: new Float32Array(input.data) }));
const results = await Promise.all(promises);
}
getPredictions(inputs);
The synchronous (correctly working) code gives me back [0, 10, 73, 0, 77]
. With the concurrent code above, I get [77, 77, 77, 77, 77]
.
I don't know a lot about concurrent promises. I know that javascript is typically single threaded. But we're in interesting territory accessing the GPU. I'm wonder if there is a way to use concurrent promises and the GPU (or CPU) to speed up predictions.
I'm also wondering why all of the predictions are returned as the final value. I understand if running it concurrently wasn't faster (because it's still on a single CPU), but it shouldn't be incorrect.
Any ideas on this?
Reallllly stoked for keras-js, this might help the project:
https://nat.org/aigrant-get-5-000-for-your-open-source-ai-project-1118dd7db083
The visualization of the network is much better than the current Keras model.summary(). Can we make the visualization component into a widget that can be used to visualize Keras model within jupyter notebook?
Hi transcranial, I just wanted to write a simple web page to import keras-js.
After cloning this repo, I create a file test.html
containing the following codes:
<html>
<head>
<title>keras-js test</title>
<script src="dist/keras.js"></script>
<script type="text/javascript">
function periodic() {
var d = document.getElementById('egdiv');
d.innerHTML = 'Random number: ' + Math.random()
}
var net; // declared outside -> global variable in window scope
function start() {
setInterval(periodic, 500);
}
}
</script>
</head>
<body onload="start()">
<div id="egdiv"></div>
</body>
</html>
Then I dragged this file into my Chrome browser, but in the console I received the following error:
Uncaught SyntaxError: Invalid regular expression: /[ªµºÀ-ÖØ-öø-Ë�ˆ-Ë‘Ë -ˤˬˮͰ-ʹͶͷͺ-ͽΆΈ-ΊΌΎ-ΡΣ-ϵϷ-Ò�ÒŠ-Ô§Ô±-Õ–Õ™Õ¡-Ö‡×�-תװ-×²Ø -يٮٯٱ-Û“Û•Û¥Û¦Û®Û¯Ûº-Û¼Û¿Ü�Ü’-ܯÝ�-ޥޱߊ-ßªß´ßµßºà €-à •à šà ¤à ¨à¡€-ࡘࢠࢢ-ࢬऄ-हऽà¥�क़-ॡॱ-ॷॹ-ॿঅ-ঌà¦�à¦�ও-নপ-রলশ-হঽৎড়à§�য়-ৡৰৱਅ-ਊà¨�à¨�ਓ-ਨਪ-ਰਲਲ਼ਵਸ਼ਸਹਖ਼-ੜਫ਼ੲ-ੴઅ-àª�àª�-ઑઓ-નપ-રલળવ-હઽà«�ૠૡଅ-ଌà¬�à¬�ଓ-ନପ-ରଲଳଵ-ହଽàœà�àŸ-à¡à±à®ƒà®…-ஊஎ-à®�à®’-கஙசஜஞடணதந-பம-ஹà¯�à°…-ఌఎ-à°�à°’-నప-ళవ-హఽౘౙౠౡಅ-ಌಎ-à²�ಒ-ನಪ-ಳವ-ಹಽೞೠೡೱೲഅ-ഌഎ-à´�à´’-ഺഽൎൠൡൺ-ൿඅ-ඖක-නඳ-රලව-à·†à¸�-ะาำเ-ๆàº�ຂຄງຈຊàº�ດ-ທນ-ຟມ-ຣລວສຫàº-ະາຳຽເ-ໄໆໜ-ໟༀཀ-ཇཉ-ཬྈ-ྌက-ဪဿá��-á�•á�š-á��á�¡á�¥á�¦á�®-á�°á�µ-á‚�á‚Žá‚ -ჅჇáƒ�áƒ�-ჺჼ-ቈቊ-á‰�á‰�-ቖቘቚ-á‰�በ-ኈኊ-áŠ�áŠ�-ኰኲ-ኵኸ-ኾዀዂ-ዅወ-ዖዘ-áŒ�ጒ-ጕጘ-á�šáŽ€-áŽ�Ꭰ-á�´á��-ᙬᙯ-ᙿáš�-áššáš -ᛪᛮ-ᛰᜀ-ᜌᜎ-ᜑᜠ-ᜱá�€-á�‘á� -á�¬á�®-á�°áž€-ឳៗៜá -ᡷᢀ-ᢨᢪᢰ-ᣵᤀ-ᤜá¥�-á¥á¥°-ᥴᦀ-ᦫá§�-ᧇᨀ-ᨖᨠ-ᩔᪧᬅ-ᬳá…-á‹á®ƒ-ᮠᮮᮯᮺ-ᯥᰀ-á°£á±�-á±�ᱚ-ᱽᳩ-ᳬᳮ-ᳱᳵᳶᴀ-ᶿḀ-ἕἘ-á¼�á¼ -ὅὈ-á½�á½�-ὗὙὛá½�Ὗ-ώᾀ-ᾴᾶ-ᾼιῂ-ῄῆ-á¿Œá¿�-á¿“á¿–-Ίῠ-Ῥῲ-ῴῶ-ῼâ�±â�¿â‚�-ₜℂℇℊ-â„“â„•â„™-â„�ℤΩℨK-â„ℯ-ℹℼ-â„¿â……-â…‰â…Žâ… -ↈⰀ-Ⱞⰰ-ⱞⱠ-ⳤⳫ-ⳮⳲⳳⴀ-ⴥⴧâ´â´°-ⵧⵯⶀ-ⶖⶠ-ⶦⶨ-ⶮⶰ-ⶶⶸ-ⶾⷀ-ⷆⷈ-â·Žâ·�-â·–â·˜-ⷞⸯ々-〇〡-〩〱-〵〸-〼ã��-ã‚–ã‚�-ã‚Ÿã‚¡-ヺー-ヿㄅ-ã„ㄱ-ㆎㆠ-ㆺㇰ-ㇿã�€-䶵一-鿌ꀀ-ê’Œê“�-ꓽꔀ-ꘌê˜�-ꘟꘪꘫꙀ-ꙮꙿ-êš—êš -ꛯꜗ-ꜟꜢ-ꞈꞋ-ꞎêž�-êž“êž -Ɦꟸ-ê �ê ƒ-ê …ê ‡-ê Šê Œ-ê ¢ê¡€-ꡳꢂ-ꢳꣲ-ꣷꣻꤊ-ꤥꤰ-ê¥†ê¥ -ꥼꦄ-ꦲê§�ꨀ-ꨨꩀ-ê©‚ê©„-ê©‹ê© -ꩶꩺꪀ-ꪯꪱꪵꪶꪹ-ꪽꫀꫂꫛ-ê«�ê« -ꫪꫲ-ê«´ê¬�-ꬆꬉ-ꬎꬑ-ê¬–ê¬ -ꬦꬨ-ꬮꯀ-ꯢ가-힣ힰ-ퟆퟋ-ퟻ豈-ï©ï©°-龎ff-stﬓ-ﬗï¬�ײַ-ﬨשׁ-זּטּ-לּמּï€ï�ïƒï„ï†-ﮱﯓ-ï´½ïµ�-ï¶�ﶒ-ﷇﷰ-ﷻﹰ-ﹴﹶ-ﻼA-Zï½�-zヲ-하-ᅦᅧ-ï¿�ï¿’-ï¿—ï¿š-ï¿œ]/: Range out of order in character class
at new RegExp (<anonymous>)
at Object.<anonymous> (keras.js:31478)
at keras.js:31221
at Object.e.read.s (keras.js:31221)
at e (keras.js:1)
at Object.<anonymous> (keras.js:31221)
at Object.o (keras.js:31221)
at e (keras.js:1)
at Object.<anonymous> (keras.js:1)
at e (keras.js:1)
at Object.<anonymous> (keras.js:1)
at e (keras.js:1)
at Object.t.__esModule.default (keras.js:10644)
at e (keras.js:1)
at Object.t.__esModule.default (keras.js:10644)
at e (keras.js:1)
Can you tell me why this happened and how to deal with it?
Best wishes,
Yiming
I'm running the latest Keras JS on a trained Keras model. Following the instructions given on the README, I tested loading my model before training (random initialized weights) and it works.
However, after I perform training and use the exact same way to called model.save_weights
, Keras JS is unable to read the model file. When trying to parse the metadata, it gives [Model] error loading weights.
Any insight into why this would happen?
hi. it is great.
but it is not working with safari or chrome browser in ios without gpu option
could you fix it?
thanks
Hi, I am trying to load the mnist_vae model weights demo with the following html:
<html>
<head>
<meta charset="utf-8">
<script language="javascript" type="text/javascript" src="scripts/lib/keras.js"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
const model = new KerasJS.Model({
filepaths: {
model: 'static/model/mnist_vae.json',
weights: 'static/model/mnist_vae_weights.buf',
metadata: 'static/model/mnist_vae_metadata.json'
},
gpu: false
});
</script>
</body>
</html>
This is the error I get in Chrome:
RangeError: Invalid typed array length
at new Float32Array (native)
at http://0.0.0.0:8000/scripts/lib/keras.js:4:18505
at Array.map (native)
at http://0.0.0.0:8000/scripts/lib/keras.js:4:18250
at Array.forEach (native)
at t.value (http://0.0.0.0:8000/scripts/lib/keras.js:4:16687)
at http://0.0.0.0:8000/scripts/lib/keras.js:4:15092
From previous event:
at t.value (http://0.0.0.0:8000/scripts/lib/keras.js:4:15067)
at new t (http://0.0.0.0:8000/scripts/lib/keras.js:4:14730)
at http://0.0.0.0:8000/keras.html:8:18
I get a similar error in Firefox that begins with:
Error: WebGL: Exceeded 16 live WebGL contexts for this principal, losing the least recently used one.
RangeError: attempting to construct out-of-bounds TypedArray on ArrayBuffer
Stack trace:
[...]
Any ideas what might be going wrong?
is there any solution for the problem?
I get this error message from weblas:
%cUnhandled rejection Error: Second dimension must be of same size for input Tensors (second Tensor is transposed).
Comparing the model.json in the working example and mine, I've noticed that dimension ordering in mine is Theano convention (3x64x64 instead of 64x64x3). I think this should be documented, at least.
First, very cool - great work! :-D
For your reference, keras-js runs fast in Opera; appears to run (slower) in Firefox, but does not display the top-5 classes (50-layer ResNet tried; supplied wolf image).
Firefox 49.0.1 - lots of plugins [re: this issue, disabled AdBlockPlus; uBlock Origin; Ghostery]
Opera 40.0 - minimal plugins
Arch Linux x86_64; Intel Core i7-4790 CPU; ...
Private message me -- if desired -- for my Firefox addons, configuration [about:support]; I don't want to share them, publicly.
It's saying that there are a token |=
but my code only has one file index.html
and it has no such symbol.
What is the cause here?
SyntaxError: Unexpected token |=
at new Function (<anonymous>)
at u (https://off99555.github.io/HoNNeT/keras-js/keras.js:13:8807)
at assigns_cwise_thunk (eval at r (https://off99555.github.io/HoNNeT/keras-js/keras.js:13:10619), <anonymous>:7:29)
at Object.assigns_ndarrayops [as assigns] (eval at i (https://off99555.github.io/HoNNeT/keras-js/keras.js:1:6744), <anonymous>:3:44)
at e.value (https://off99555.github.io/HoNNeT/keras-js/keras.js:7:19739)
at e.value (https://off99555.github.io/HoNNeT/keras-js/keras.js:7:21879)
at t.value (https://off99555.github.io/HoNNeT/keras-js/keras.js:4:15902)
at t.<anonymous> (https://off99555.github.io/HoNNeT/keras-js/keras.js:4:16786)
at o (https://off99555.github.io/HoNNeT/keras-js/keras.js:16:7592)
at Generator._invoke (https://off99555.github.io/HoNNeT/keras-js/keras.js:16:9001)
at Generator.t.(anonymous function) [as next] (https://off99555.github.io/HoNNeT/keras-js/keras.js:16:7771)
at Generator.i (https://off99555.github.io/HoNNeT/keras-js/keras.js:11:1532)
at u._promiseFulfilled (https://off99555.github.io/HoNNeT/keras-js/keras.js:9:22799)
at t.<anonymous> (https://off99555.github.io/HoNNeT/keras-js/keras.js:9:24615)
From previous event:
at t (https://off99555.github.io/HoNNeT/keras-js/keras.js:4:15972)
at t.<anonymous> (https://off99555.github.io/HoNNeT/keras-js/keras.js:4:18474)
at o (https://off99555.github.io/HoNNeT/keras-js/keras.js:16:7592)
at Generator._invoke (https://off99555.github.io/HoNNeT/keras-js/keras.js:16:9001)
at Generator.t.(anonymous function) [as next] (https://off99555.github.io/HoNNeT/keras-js/keras.js:16:7771)
From previous event:
at t (https://off99555.github.io/HoNNeT/keras-js/keras.js:4:17225)
at model.ready.then (https://off99555.github.io/HoNNeT/:165:16)
From previous event:
at https://off99555.github.io/HoNNeT/:158:20
Here's the javascript part of that index.html file:
const N_AVAIL_HEROES = 260
const HERO_INPUT = new Array(2 * N_AVAIL_HEROES).fill(0)
const vm = new Vue({
el: '#app',
data() {
return {
model: null,
modelReady: false
}
},
watch: {
modelReady: function() {
if (this.modelReady)
console.log('the model is ready now')
}
}
})
const model = new KerasJS.Model({
filepaths: {
model: 'honnet_brain_architecture.json',
weights: 'honnet_brain_weights_weights.buf',
metadata: 'honnet_brain_weights_metadata.json'
}
})
function getInputData(legion, hellbourne) {
console.log('predicting with data')
const inputData = {
'hero_input': new Float32Array(HERO_INPUT)
}
return inputData
}
model.ready().then(() => {
vm.model = model
vm.modelReady = true
// make predictions
// outputData is an object keyed by names of the output layers
// or `output` for Sequential models
model.predict(getInputData([], [])).then(function(outputData) { // <====== error happens here!
console.log('output')
console.log(outputData)
}).catch(err => {
// handle error
console.log('pred error: ')
console.log(err)
})
}).catch(err => {
// handle error
console.log('ready error: ')
console.log(err)
})
Hi,
Very very impressive library, hope to let it run on a NodeJS server.
Did you perform some benchmark to compare the library to Tensorflor / Caffe app ? I assume it's slower but I wondering how much ?
Excellent project Leon! While running the demos using existing images, computational flow seems to stop 2-3 layers before end. I am running Firefox 49 on Mac OSX.
If you cannot replicate this, I can send you debug info from the console.
Great job again!
Is there any known workaround for using conv2d_tranpose
in keras.js? (which doesn't look like it is supported?)
edit: apparently deconv2d is the same as conv2d_tranpose in keras
Hello everyone,
I am not an experienced developer with JS but I get weird error.
This is my whole code (its just a simple testing code):
<!DOCTYPE html>
<html>
<head>
<title>Testing Deep Tingle</title>
<meta charset="utf-8" />
</head>
<body>
</body>
<script src="dist/keras.js"></script>
<script>
const model = new KerasJS.Model({
filepaths: {
model: 'Gabb_Data/model.json',
weights: 'Gabb_Data/model_weights.buf',
metadata: 'Gabb_Data/model_metadata.json'
},
gpu: true
});
// model.ready().then(() => {}).catch(err => {});
</script>
</html>
Loading ... 0%
That's what I've got.
Works perfectly with Firefox though.
System: Ubuntu 16.04 LTS with NVIDIA Titan X Pascal.
I think the implementation of 'dot' mode for Merge layer ignores the fact that the dot axis can be a tuple too instead of an integer. When a tuple dotAxes is sent as part of attr to Merge layer for dot mode in Merge.js
this.dotAxes = dotAxes <= 0 ? dotAxes : dotAxes - 1
sets this.dotAxes
to NaN
Do you planning to adapt this module for nodeJS?
Thanks
Hi,
I'm trying to get SSD300 (single shot detector) working.
I converted the model/weights over, but when I try to instantiate the model I get this error.
[Error: cwise: Arrays do not all have the same shape!]
I tried debugging it and found the problem starts in lib/Model.js:446 -> layer.setWeights(weights);
debug> n
break in node_modules/keras-js/lib/Model.js:462
460 _this4.modelDAG[prevLayerConfig.name].outbound.push(layerConfig.name);
461 }
>462 } else if (modelClass === 'Model') {
463 if (layerDef.inbound_nodes && layerDef.inbound_nodes.length) {
464 layerDef.inbound_nodes[0].forEach(function (node) {
debug> n
break in node_modules/keras-js/lib/Model.js:463
461 }
462 } else if (modelClass === 'Model') {
>463 if (layerDef.inbound_nodes && layerDef.inbound_nodes.length) {
464 layerDef.inbound_nodes[0].forEach(function (node) {
465 var inboundLayerName = node[0];
debug> n
break in node_modules/keras-js/lib/Model.js:464
462 } else if (modelClass === 'Model') {
463 if (layerDef.inbound_nodes && layerDef.inbound_nodes.length) {
>464 layerDef.inbound_nodes[0].forEach(function (node) {
465 var inboundLayerName = node[0];
466 _this4.modelDAG[layerConfig.name].inbound.push(inboundLayerName);
debug> n
break in node_modules/keras-js/lib/Model.js:471
469 }
470 }
>471 });
472 }
473
debug> n
< [Error: cwise: Arrays do not all have the same shape!]
Do you have any ideas what might cause this? The same model works fine in keras. The only change I had to make was renaming Conv2D -> Convolution2D.
Hello, if I understand correctly, this can only work for pre-trained networks? If so, do you know of another deep NN library that works in the browser and uses the GPU (WebGL) and allows training in the browser Thanks.
Hello. Thanks for a nice implementation!
I am new to Javascript and I would like you to explain how to use this library in detail.
I installed the keras-js using npm and I tried to check if the model created in Keras(Python) can be loaded correctly. But it didn't work.
Here is my log.
➜ simple-keras-js node
> const KerasJS = require('keras-js');
undefined
>
> var model = new KerasJS.Model({
... filepaths: {
..... model: './data/mnist_cnn.json',
..... weights: './data/mnist_cnn_weights.buf',
..... metadata: './data/mnist_cnn_metadata.json'
..... },
... filesystem: true
... });
TypeError: expecting a function but got [object Undefined]
at Function.e.promisify (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:391803)
at t.value (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:111338)
at /Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:111107
at Array.map (native)
at t.value (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:111069)
at new t (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:110823)
at repl:1:13
at ContextifyScript.Script.runInThisContext (vm.js:23:33)
at REPLServer.defaultEval (repl.js:339:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:536:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:191:7)
at REPLServer.Interface._onLine (readline.js:241:10)
at REPLServer.Interface._line (readline.js:590:8)
>
(To exit, press ^C again or type .exit)
> %
Hi!.
How could I do predictions in batch?
Taking mnist-cnn as example, how could I do
model.predict([Float32Array[784], Float32Array[784], Float32Array[784]])
?
Just for one input is working: model.predict({input:
[Float32Array[784]]})`
Thanks a lot!
Can I use my own model in keras.js? If yes then how?
dot
[core.Merge.9] should produce expected values in dot mode (2D x 2D, dotAxes=1) ‣
AssertionError: expected [ 3, 3 ] to deeply equal [ 2, 2 ]
[core.Merge.10] should produce expected values in dot mode (2D x 2D, dotAxes=2) ‣
AssertionError: expected [ 2, 2 ] to deeply equal [ 3, 3 ]
cos
[core.Merge.11] should produce expected values in cos mode (2D x 2D, dotAxes=1) ‣
AssertionError: expected [ 1, 3, 3 ] to deeply equal [ 1, 2, 2 ]
[core.Merge.12] should produce expected values in cos mode (2D x 2D, dotAxes=2) ‣
AssertionError: expected [ 1, 2, 2 ] to deeply equal [ 1, 3, 3 ]
WebGL2 is here, and there are a number of features worth exploring:
Will see if they offer any significant speedups.
https://webgl2fundamentals.org/webgl/lessons/webgl2-whats-new.html
https://blog.chromium.org/2017/03/faster-3d-rendering-with-webgl-20.html
On Debian testing at least but probably on other distros as well. Could be also broken on Mac, the issue with Fira sans was revealed in habrahabr.ru redesign (worked fine on Windows but was totally invisible on Mac/Linux). It looks like this:
Disabling font-family
in body
reveals the text. Please replace the font with the working version.
Error loading weights without much information even though the network shows 200 for all the files. Below is the stacktrace.
Error: [Model] error loading weights.
at http://localhost:5444/node_modules/keras-js/dist/keras.js:4:18404
at Array.map (native)
at http://localhost:5444/node_modules/keras-js/dist/keras.js:4:18250
at Array.forEach (native)
at t.value (http://localhost:5444/node_modules/keras-js/dist/keras.js:4:16687)
at http://localhost:5444/node_modules/keras-js/dist/keras.js:4:15092
From previous event:
at t.value (http://localhost:5444/node_modules/keras-js/dist/keras.js:4:15067)
at new t (http://localhost:5444/node_modules/keras-js/dist/keras.js:4:14730)
at Object.38 (http://localhost:5444/dist/browser.js:79:877)
at s (http://localhost:5444/dist/browser.js:1:254)
at http://localhost:5444/dist/browser.js:1:305
at Object.41../Utils/AverageGain (http://localhost:5444/dist/browser.js:85:1110)
at s (http://localhost:5444/dist/browser.js:1:254)
at http://localhost:5444/dist/browser.js:1:305
at Object. (http://localhost:5444/dist/browser.js:5:29)
at Object.2../lib (http://localhost:5444/dist/browser.js:6:4)
at s (http://localhost:5444/dist/browser.js:1:254)
at http://localhost:5444/dist/browser.js:1:305
at Object.1../index.js (http://localhost:5444/dist/browser.js:2:14)
at s (http://localhost:5444/dist/browser.js:1:254)
at e (http://localhost:5444/dist/browser.js:1:425)
at http://localhost:5444/dist/browser.js:1:443
I downloaded the github repo on my MBP (running Mac OSX 10.12). Then, at the directory containing "index.html", I started a HTTP server locally by using npm's http-server.
It is strange that demos cannot run in my Chrome browser. For example, in the mnist_cnn demo, I can draw numbers but the CNN prediction never occurs.
Any idea on the possible reason.
Update: After re-starting Chrome, the issue disappears.
Saw that there were recently some updates on the demo web build, which as a result broke some relative paths to some model files and image files.
Hello. Thanks for a nice implementation!
I am new to Javascript and I would like you to explain how to use this library in detail.
I installed the keras-js using npm and I tried to check if the model created in Keras(Python) can be loaded correctly. But it didn't work.
Here is my log.
➜ simple-keras-js node
> const KerasJS = require('keras-js');
undefined
>
> var model = new KerasJS.Model({
... filepaths: {
..... model: './data/mnist_cnn.json',
..... weights: './data/mnist_cnn_weights.buf',
..... metadata: './data/mnist_cnn_metadata.json'
..... },
... gpu: false
... });
undefined
> ReferenceError: XMLHttpRequest is not defined
at /Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:75206
at t.exports (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:75103)
at t.exports (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:196561)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
From previous event:
at i.d [as _captureStackTrace] (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:346877)
at s (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:404873)
at r (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:404546)
at s._iterate (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:388606)
at s.t [as _init] (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:388390)
at new s (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:387636)
at Function.i.all (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:377696)
at t.value (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:111063)
at new t (/Users/kwatanabe/node_modules/keras-js/dist/keras.js:1:110823)
at repl:1:13
at ContextifyScript.Script.runInThisContext (vm.js:23:33)
at REPLServer.defaultEval (repl.js:339:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:536:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:191:7)
at REPLServer.Interface._onLine (readline.js:241:10)
at REPLServer.Interface._line (readline.js:590:8)
at REPLServer.Interface._ttyWrite (readline.js:869:14)
at REPLServer.self._ttyWrite (repl.js:609:7)
at ReadStream.onkeypress (readline.js:120:10)
>
Thanks, advance.
Just a reminder to monitor Browsers implementation stauts of OffscreenCanvas
Ok so I am trying to get this working in Node backend.
I only have these few lines of codes:
const KerasJS = require('keras-js');
// in node, gpu flag will always be off
// paths can be filesystem paths or absolute URLs
// if filesystem path, this must be specified:
const model = new KerasJS.Model({
filepaths: {
model: 'model.json',
weights: 'first_try_weights.buf',
metadata: 'first_try_metadata.json'
},
filesystem: true
})
I have installed keras-js using
npm install keras-js --save
At first it is complaining a babel-polyfill related error.
Then I installed that:
npm install babel-polyfill --save
Then I am facing this error:
node-test/node_modules/keras-js/lib/Model.js:67
var axiosSource = _axios2.default.CancelToken.source();
^
TypeError: Cannot read property 'source' of undefined
at Object.<anonymous> (.../Keras/node-test/node_modules/keras-js/lib/Model.js:67:46)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (.../Keras/node-test/node_modules/keras-js/lib/index.js:10:14)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
What is that?
Cloning the repo is failing for me; this appears to be a git-lfs quota issue.
Cloning into 'keras-js'...
remote: Counting objects: 3557, done.
remote: Total 3557 (delta 0), reused 0 (delta 0), pack-reused 3556
Receiving objects: 100% (3557/3557), 6.06 MiB | 1.08 MiB/s, done.
Resolving deltas: 100% (2493/2493), done.
Checking connectivity... done.
Downloading demos/data/imdb_bidirectional_lstm/imdb_bidirectional_lstm_weights.buf (4.98 MB)Error downloading object: demos/data/imdb_bidirectional_lstm/imdb_bidirectional_lstm_weights.buf (66197c7e9d1414f49902004854b612732d743668166390b3f2d3ba16eceee73a)
Errors logged to /develop/nets/keras-js/.git/lfs/objects/logs/20170329T220458.783176265.log
Use `git lfs logs last` to view the log.
error: external filter git-lfs smudge -- %f failed 2
error: external filter git-lfs smudge -- %f failed
fatal: demos/data/imdb_bidirectional_lstm/imdb_bidirectional_lstm_weights.buf: smudge filter lfs failedwarning: Clone succeeded, but checkout failed.
You can inspect what was checked out with 'git status'
and retry the checkout with 'git checkout -f HEAD'
$ git lfs pull
Git LFS: (0 of 5 files) 0 B / 201.85 MB
batch response: This repository is over its data quota. Purchase more data packs to restore access.
(after a decade of using git this is the first repo that's ever failed to clone for me; I blame github)
Lost my .hdf5 weights, is it possible to decode the metadata, weights, etc into a .hdf5?
Breaking changes
API changes
I'm getting this error when running a Convolution1D
model. The error is probably due to this.bias
being undefined here.
Setting this.bias = true
before that line resolves the error. Is there a smarter way than doing that? E.g. looking up if the bias contained in the weights metadata?
Possibly related to #21?
Yarin Gal's thesis demonstrated that by allowing dropout during prediction, not just training, a set predictions can be produced and then analyzed to reveal model uncertainty (the quantiles of the predictions can produce confidence intervals, for example--like bootstrapping). The thesis has been praised several times by more senior researchers, although I don't have those links right now (the thesis is here: http://mlg.eng.cam.ac.uk/yarin/blog_2248.html ). One link is here, where Yann LeCun mentions that he developed a similar technique decades ago, and that he was Bayesian before it was cool: http://mlg.eng.cam.ac.uk/yarin/website/blog_3d801aa532c1ce.html
Today I got keras-js working, but after searching the code it appears the dropout layers are passthrough for prediciton.
In Keras, dropout can be done during prediction also with K.Function(model.inputs + [K.learning_phase()], model.outputs)
or something like this ( see yaringal/BayesianRNN#3 (comment) ) . There is also a rule for recurrent layers so the layers use the same dropout mask in each layer, as is also the case in native tensorflow and torch.
Unfortunately, I don't know how to implement this in keras-js because it requires a particular combination of expert knowledge of javascript, keras and neural networks, but could perhaps help a bit because i am familiar with the three (e.g I've taken Andrew Ng's coursera course and am almost finished with Hinton's). Please advise.
I envision this as a possible 'killer application' of keras-js...producing confidence intervals on particular predictions without the overhead of taxing a main server, data visualization, visualizing uncertainty, and more. Please consider this.
I guess in the current Model.js it is assumed that for the Sequential class the first layer is the input layer. How difficult it would be to modify it for a model such as the MemNN (https://github.com/fchollet/keras/blob/master/examples/babi_memnn.py) where we have input from 3 layers and an architecture such as below -:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.