VAE (Variational Autoencoder) میتواند توزیع نهفته دادهها را یاد بگیرد و چهرههای مصنوعی تولید کند.
from tensorflow.keras.layers import Lambda
import tensorflow.keras.backend as K
:def sampling(args)
mu, log_sigma = args
epsilon = K.random_normal(shape=K.shape(mu), mean=0., stddev=1.0)
return mu + K.exp(log_sigma) * epsilon
latent_dim = 2 # دو بعد برای نمایش ویژگیهای چهره
input_layer = Input(shape=(784,))
encoded = Dense(256, activation='relu')(input_layer)
encoded = Dense(128, activation='relu')(encoded)
mu = Dense(latent_dim)(encoded)
log_sigma = Dense(latent_dim)(encoded)
z = Lambda(sampling, output_shape=(latent_dim,))([mu, log_sigma])
decoded = Dense(128, activation='relu')(z)
decoded = Dense(256, activation='relu')(decoded)
decoded = Dense(784, activation='sigmoid')(decoded)
vae = Model(input_layer, decoded)
vae.compile(optimizer='adam', loss='binary_crossentropy')
· FID (Fréchet Inception Distance)
from scipy.linalg import sqrtm
def calculate_fid(real_images, generated_images)
mu1, sigma1 = np.mean(real_images, axis=0), np.cov(real_images, rowvar=False)
mu2, sigma2 = np.mean(generated_images, axis=0), np.cov(generated_images, rowvar=False)
fid = np.linalg.norm(mu1 - mu2) + np.trace(sigma1 + sigma2 - 2 * sqrtm(sigma1 @ sigma2))
return fid
fid_score = calculate_fid(X_real, X_generated)
print(f"FID Score: {fid_score}")
کاهش FID نشاندهنده چهرههای واقعیتر است.