Skip to content
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

Can't save a model with Lambda layer #7184

Closed
3 of 4 tasks
flamoedo opened this issue Jun 29, 2017 · 5 comments
Closed
3 of 4 tasks

Can't save a model with Lambda layer #7184

flamoedo opened this issue Jun 29, 2017 · 5 comments

Comments

@flamoedo
Copy link

Hi,

Although it has been subject of many correction, the problem still there, and I can't save the model with Lambda layer, using fit_generator. If I use fit (no batches) the problem don't occurs.

Error message:

UnicodeDecodeError: 'rawunicodeescape' codec can't decode bytes in position 80-84: truncated \uXXXX

If I switch to BatchNormalization, the model can be saved, but the loss is much higher then using a simple normalization, like X / 255 - 0.5 .

Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on StackOverflow or join the Keras Slack channel and ask there instead of filing a GitHub issue.

Thank you!

  • Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

  • If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

  • If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
    pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

  • Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

@jorgecarleitao
Copy link
Contributor

Can you provide a minimal and complete example that reproduces the issue?

@flamoedo
Copy link
Author

flamoedo commented Jun 30, 2017

Maybe would helps to know that it don't happens in windows server 2012.

Here is a example:

from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Conv2D, Cropping2D
from keras.layers import MaxPooling2D
from keras.layers.normalization import BatchNormalization

def cnn_model(input_shape):
    model = Sequential()
    model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=input_shape))
    model.add(Cropping2D(cropping=((70,25),(0,0))))
    model.add(Conv2D(24,(5,5),strides=(2,2),activation='relu', padding='valid', kernel_initializer='he_normal'))
    model.add(Conv2D(36,(5,5),strides=(2,2),activation='relu', padding='valid', kernel_initializer='he_normal'))
    model.add(Conv2D(48,(5,5),strides=(2,2),activation='relu', padding='valid', kernel_initializer='he_normal'))
    model.add(Conv2D(64,(3,3),activation='relu', padding='valid', kernel_initializer='he_normal'))
    model.add(Conv2D(64,(3,3),activation='relu', padding='valid', kernel_initializer='he_normal'))
    model.add(Flatten())
    model.add(Dense(1164, kernel_initializer='he_normal'))
    model.add(Dense(100, kernel_initializer='he_normal'))
    model.add(Dense(50, kernel_initializer='he_normal'))
    model.add(Dense(10, kernel_initializer='he_normal'))
    model.add(Dense(1, kernel_initializer='he_normal'))
    
    return model

#Instantiating the model:
model = cnn_model((160,320,3))

#Try to save:
model.save('model.h5')

#Error:
#UnicodeDecodeError: 'rawunicodeescape' codec can't decode bytes in position 80-84: truncated \uXXXX

@FernanOrtega
Copy link

@profPlum
Copy link

I've got a similar issues except on a linux server and it's saying it can't pickle NotImplementedType object. Again trying to save a model with a lambda layer as an h5 file. What could be causing this?

@profPlum
Copy link

profPlum commented Oct 18, 2017

Actually looks like my problem has been mentioned and solved before #6442. Apparently lambda layers are just fragile when it comes to saving them to file, many gotchas, and it's often just better to make and use a custom layer (this what I ended up doing).

@flamoedo flamoedo closed this as completed Feb 6, 2018
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

No branches or pull requests

4 participants