davidADSP / GDL_code

The official code repository for examples in the O'Reilly book 'Generative Deep Learning'
GNU General Public License v3.0
1.47k stars 739 forks source link

ValueError: Unable to load weights 03_04_vae_digits_analysis.ipynb #85

Open Lana2548 opened 3 years ago

Lana2548 commented 3 years ago

From Japan. I'm not good at English, so it may be hard to read. Sorry.

When I used load_model() in 03_04_vae_digits_analysis.ipynb, I got an error message.


ValueError Traceback (most recent call last)

in ----> 1 vae = load_model(VariationalAutoencoder, RUN_FOLDER) ~\Desktop\study\GDL_code\utils\loaders.py in load_model(model_class, folder) 138 model = model_class(*params) 139 --> 140 model.load_weights(os.path.join(folder, 'weights/weights.h5')) 141 142 return model ~\Desktop\study\GDL_code\models\VAE.py in load_weights(self, filepath) 204 205 def load_weights(self, filepath): --> 206 self.model.load_weights(filepath) 207 208 def train(self ~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\training.py in load_weights(self, filepath, by_name, skip_mismatch, options) 2221 if not self._is_graph_network and not self.built: 2222 raise ValueError( -> 2223 'Unable to load weights saved in HDF5 format into a subclassed ' 2224 'Model which has not created its variables yet. Call the Model ' 2225 'first, then load the weights.') ValueError: Unable to load weights saved in HDF5 format into a subclassed Model which has not created its variables yet. Call the Model first, then load the weights. Please give me any advice. Thank you.
eiler-partner commented 3 years ago

Hi, I am using the code from tensorflow_2 directory I have the same problem:


ValueError Traceback (most recent call last)

in ----> 1 vae = load_model(VariationalAutoencoder, RUN_FOLDER) ~\Deep_Learning\H_Generative_Deep_Learning\utils\loaders.py in load_model(model_class, run_folder) 138 model = model_class(*params) 139 --> 140 model.load_weights(os.path.join(run_folder, 'weights/weights.h5')) 141 142 return model ~\Deep_Learning\H_Generative_Deep_Learning\models\VAE.py in load_weights(self, filepath) 204 205 def load_weights(self, filepath): --> 206 self.model.load_weights(filepath) 207 208 def train(self, x_train, batch_size, epochs, run_folder, print_every_n_batches = 100, initial_epoch = 0, lr_decay = 1): ~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in load_weights(self, filepath, by_name, skip_mismatch, options) 2266 '`load_weights` requires h5py when loading weights from HDF5.') 2267 if not self._is_graph_network and not self.built: -> 2268 raise ValueError( 2269 'Unable to load weights saved in HDF5 format into a subclassed ' 2270 'Model which has not created its variables yet. Call the Model ' ValueError: Unable to load weights saved in HDF5 format into a subclassed Model which has not created its variables yet. Call the Model first, then load the weights. Any ideas for a fix? Best regards, Patrick
cia-rana commented 3 years ago

I encountered the same problem, and I solved it by setting self.built = True after initializing VAEModel.

class VAEModel(Model):
    def __init__(self, encoder, decoder, r_loss_factor, **kwargs):
        super(VAEModel, self).__init__(**kwargs)
        self.encoder = encoder
        self.decoder = decoder
        self.r_loss_factor = r_loss_factor
        self.built = True # added