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

How to save VAE model #328

Open
csyuhao opened this issue Aug 7, 2021 · 8 comments
Open

How to save VAE model #328

csyuhao opened this issue Aug 7, 2021 · 8 comments
Assignees

Comments

@csyuhao
Copy link

csyuhao commented Aug 7, 2021

I have a problem when I want to save the VAE model after the training. This is because this

z = Lambda(self.sampling, output_shape=(self.latent_dim,))(
    [z_mean, z_log])

Specifically, the Lambda layer cannot be saved into file. Could you give me any advice to save it?

@yzhao062
Copy link
Owner

I just searched around and realize lambda layer is very fragile. we will have to replace that with a custom layer.

@yzhao062 yzhao062 self-assigned this Aug 15, 2021
@yzhao062
Copy link
Owner

for self-reference: keras-team/keras#6442

@csyuhao
Copy link
Author

csyuhao commented Aug 24, 2021

I solve this problem. While saving the paramters of VAE model, I also save the the initial parameters of VAE class at the same time.

@csyuhao csyuhao closed this as completed Aug 30, 2021
@bai-by
Copy link

bai-by commented Sep 10, 2021

I can't save Deep svdd model, Could you tell me how you saved VAE model?

@jjjzy
Copy link

jjjzy commented Sep 10, 2021

As the poster of the issue said, you save the model first, then you save the class.

In the VAE class, there is a build model function. In fit function, it calls the build model function and assign it to self.model_. What you need to do is that you first use keras.save to save the self.model_. Then you make self.model_ = None. Then you can joblib.dump the whole class.

When you load, you first use joblib.load to load the class, then you use keras.load to load self.model_. And you put self.model_ inside the class.

But this will cause error. As I searched, when you are loading the keras model, you need to load the custome Lamda function inside the Lamda layer which is self.sampling. So what you need to do is that copy the self.sampling function in your file, and load it using custome object.

The order looks like this:
Save:
your VAE class.model_.save()
your VAE class.model_ = None

joblib.dump(your VAE class, path)

Load:
another VAE class = joblib.load(path)
another VAE class.model_ = keras.models.load_model(path, custom_objects={'sampling': sampling})

It works for me. But it needs some testing. At least there is no error saving and loading.

@yzhao062 yzhao062 reopened this Sep 13, 2021
@yzhao062
Copy link
Owner

@jjjzy Thanks for this information. This sounds like a workaround for now.

@bai-by
Copy link

bai-by commented Sep 13, 2021

Thanks for your help.

@BlingBlingss
Copy link

I solve this problem. While saving the paramters of VAE model, I also save the the initial parameters of VAE class at the same time.

兄弟,能分享下代码吗谢谢

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

5 participants