transcranial / keras-js

Run Keras models in the browser, with GPU support using WebGL
https://transcranial.github.io/keras-js
MIT License
4.96k stars 501 forks source link

Minimal Lambda layer implementation #115

Open linusmartensson opened 6 years ago

linusmartensson commented 6 years ago

I had a finished model and needed it to fit into keras-js. Seeing as I had some lambda layers in that model, it seemed like the easiest solution was to implement this in keras-js and push it back to master.

The implementation is more or less low-effort, but works perfectly fine for my scenario. There may be a cleaner method to push through the lambda functions, but I can't claim enough experience with keras-js to determine how that should be done.

linusmartensson commented 6 years ago

I welcome anyone who wants to write tests and verify this implementation. I wrote the original in a version that was precompiled by babel, and did not have the opportunity to verify this version.

MingwangLin commented 6 years ago

Hi linusmartensson,

Because keras.js don't support instance normalization layer(contrast normalization), after saw your pull requests, I tried using Lambda layer to achieve instance norm function. My Lambda layer codes are:

def contrast_norm(x):
    epsilon=1e-3
    dims = list(range(1,K.ndim(x)-1))
    x_mean = K.mean(x, dims, keepdims=True)
    x_stddev = K.std(x, dims, keepdims=True) + epsilon
    x = (x - x_mean) / x_stddev
    return x
def instancenorm():
    return Lambda(contrast_norm)

Then I did following steps:

  1. Replace batchnorm layer of my keras model with this Lambda layer, then train and convert model as usual.
  2. Use demos of keras.js, just replace demos' model files with my model's. (My model run successfully before when using batchnorm layer)
  3. Clone your codes and call npm run build in terminal to build codes, then call npm run demos:watch. There's no error appeared.
  4. Call npm run server and open demos in browser, it says"Note: this browser does not support WebGL 2 or the features necessary to run in GPU mode', and the browser stop responding. (This problem didn't happen before)

In your commit, it says"This layer requires you to re-implement lambda nodes in javascript", I'm sorry I didn't understand it. Could you please tell me what's the meaning of this words or which step I miss?

Great thanks!

linusmartensson commented 6 years ago

Hello Ming,

The meaning in my commit was that your contrast_norm-function will have to be recreated in javascript, since it is not possible for javascript to deserialize and run the python code in your Lambda node. You will still have to reimplement the lambda using tensors and/or webgl as necessary to achieve your intended result.

Best Regards, Linus

ons 14 mars 2018 kl 08:12 skrev ming notifications@github.com:

Hi linusmartensson,

Because keras.js don't support instance normalization layer(contrast normalization), after saw your pull requests, I tried using Lambda layer to achieve instance norm function. My Lambda layer codes are:

def contrast_norm(x): epsilon=1e-3 dims = list(range(1,K.ndim(x)-1)) x_mean = K.mean(x, dims, keepdims=True) x_stddev = K.std(x, dims, keepdims=True) + epsilon x = (x - x_mean) / x_stddev return x def instancenorm(): return Lambda(contrast_norm)

Then I did following steps:

  1. I replaced batchnorm layer of my keras model with this Lambda layer, then trained and converted model as usual.
  2. I used demos of keras.js, just replaced demos' model files with my model's. (My model run successfully before when using batchnorm layer)
  3. I cloned your codes and call 'npm run build' to build codes, then call 'npm run demos:watch'. There was no error appeared.

When I open demos in browser, it says"Note: this browser does not support WebGL 2 or the features necessary to run in GPU mode', and the browser stop responding. (This problem didn't happen before)

In your commit, it says"This layer requires you to re-implement lambda nodes in javascript", I'm sorry I didn't understand it. Could you please tell me what's the meaning of this words or which step I miss?

Great thanks!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/transcranial/keras-js/pull/115#issuecomment-372925545, or mute the thread https://github.com/notifications/unsubscribe-auth/AAbmSZqWkJ_vkDz7vGVERZ35O61VTH3sks5teMLMgaJpZM4SkuOk .

MingwangLin commented 6 years ago

Hi Linus,

Thanks for your explanation! It made me more clear.

After days of source codes reading, reimplementing the lambda using tensors or webgl is still complicated to me. But I tried to implement contrast norm layer by adding some codes on batch norm layer. Many Thanks to your help!