Skip to content

Minimal Lambda layer implementation #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

linusmartensson
Copy link

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
Copy link
Author

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
Copy link

MingwangLin commented Mar 14, 2018

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
Copy link
Author

linusmartensson commented Mar 14, 2018 via email

@MingwangLin
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants