yzhao062 / pyod

A Python Library for Outlier and Anomaly Detection, Integrating Classical and Deep Learning Techniques
http://pyod.readthedocs.io
BSD 2-Clause "Simplified" License
8.28k stars 1.35k forks source link

How to save VAE model #328

Open csyuhao opened 2 years ago

csyuhao commented 2 years ago

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 commented 2 years ago

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

yzhao062 commented 2 years ago

for self-reference: https://github.com/keras-team/keras/issues/6442

csyuhao commented 2 years ago

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.

bai-by commented 2 years ago

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

jjjzy commented 2 years ago

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 commented 2 years ago

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

bai-by commented 2 years ago

Thanks for your help.

BlingBlingss commented 2 years ago

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.

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