Error Message: tensorflow:Found duplicated `Variable`s in Model's `weights`

Hello all,

i’am getting this Warning: WARNING:tensorflow:Found duplicated Variables in Model’s weights. This is usually caused by Variables being shared by Layers in the Model. These Variables will be treated as separate Variables when the Model is restored. To avoid this, please save with save_format="tf".


from keras.models import Model
import keras
from keras.datasets import mnist
from keras.layers import Dense, Input
from keras.layers import Conv2D, Flatten, Lambda,LeakyReLU
from keras.layers import Reshape, Conv2DTranspose
from keras import backend as K
from keras.losses import binary_crossentropy
from numpy import reshape
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
#import tensorflow._api.v2.compat.v1 as tf
#tf.disable_v2_behavior()

TrainingsSet = np.load('TrainingsSet_CONV.npy')  
ValidationSet = np.load('ValidationSet_CONV.npy') 


print(TrainingsSet.shape)
print(ValidationSet.shape)


latent_dim = 1500

def sample_z(args):
  z_mu, z_sigma = args
  eps = K.random_normal(shape=(K.shape(z_mu)[0], K.int_shape(z_mu)[1]))
  return z_mu + K.exp(z_sigma / 2) * eps


input_img = tf.keras.layers.Input(shape=(64,256,3))
x = tf.keras.layers.Conv2D(128, (2, 2), strides=(2,2), activation=LeakyReLU(alpha=0.3), padding='same')(input_img)
x = tf.keras.layers.Conv2D(64, (2, 2), strides=(2,2), activation=LeakyReLU(alpha=0.3), padding='same')(x)
x = tf.keras.layers.Conv2D(16, (2, 2), strides=(2,2), activation=LeakyReLU(alpha=0.3), padding='same')(x)
conv_shape = K.int_shape(x)
x = Flatten()(x)
x = Dense(32, activation='relu')(x)
z_mu = Dense(latent_dim, name='latent_mu')(x)   #Mean values of encoded input
z_sigma = Dense(latent_dim, name='latent_sigma')(x)  #Std dev. (variance) of encoded input
z = Lambda(sample_z, output_shape=(latent_dim, ), name='z')([z_mu, z_sigma])
encoder = Model(input_img, [z_mu, z_sigma, z], name='encoder')
print(encoder.summary())


decoder_input = Input(shape=(latent_dim, ), name='decoder_input')
x = Dense(conv_shape[1]*conv_shape[2]*conv_shape[3], activation='relu')(decoder_input)
x = Reshape((conv_shape[1], conv_shape[2], conv_shape[3]))(x)
x = tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same', activation=LeakyReLU(alpha=0.3))(x)
x = tf.keras.layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same', activation=LeakyReLU(alpha=0.3))(x)
x = tf.keras.layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same', activation=LeakyReLU(alpha=0.3))(x)
decoder_outputs = tf.keras.layers.Conv2DTranspose(3, (3, 3), padding='same', activation=LeakyReLU(alpha=0.3))(x)


decoder = Model(decoder_input, decoder_outputs, name='decoder')
decoder.summary()

outputs = decoder(encoder(input_img)[2])
vae = Model(input_img, outputs, name='vae')

reconst_loss = binary_crossentropy(K.flatten(input_img), K.flatten(outputs))
reconst_loss *= 64 *256*3
kl_loss =  -0.5 * K.sum(1 + z_sigma - K.square(z_mu) - K.exp(z_sigma), axis = 1)
vae_loss = K.mean(reconst_loss + kl_loss)

vae.add_loss(vae_loss)
vae.compile(optimizer='adam')
vae.summary()

vae.fit(TrainingsSet,epochs=1,batch_size=128,shuffle=True,validation_data=(ValidationSet,None))
vae.save("modellname.h5", save_format="tf")

my Code is above. How can i save my VAE properly? My Network works fine. But i cant save my model, so i can start training it on antoher day. Please can someone help me.

yours MU SO

@MU_SO,

Please can you try to save and reload the vae model as shown below

# Calling `save('modellname')` creates a SavedModel folder `modellname`.
vae.save("modellname")

# It can be used to reconstruct the model identically.
reconstructed_vae= keras.models.load_model("modellname")

Note: Please import keras module from tensorflow

Thank you!

Thanks for reply’ing :)! but with that i’am getting this error: WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _update_step_xla, _jit_compiled_convolution_op while saving (showing 5 of 14). These functions will not be directly callable after loading.

MU SO

Could you please share above files to reproduce the issue?

Any updates on this? I’m getting same error message while training. Assuming it may be due to model_checkpoint callback however I cannot find save_format parameter in the doc.