import tensorflow as tf from tensorflow.keras.layers import Input, Dense, Reshape,…
- User
import tensorflow as tf from tensorflow.keras.layers import Input, Dense, Reshape, Conv2DTranspose, Conv2D, Flatten from tensorflow.keras.models import Model from tensorflow.keras.datasets import fashion_mnist import numpy as np from tensorflow.keras.layers import Layer, BatchNormalization, LeakyReLU
from tensorflow.keras.datasets import mnist
Załadowanie danych MNIST
(x_train_mnist, _), (x_test_mnist, _) = mnist.load_data() x_train_mnist = x_train_mnist.astype('float32') / 255. x_test_mnist = x_test_mnist.astype('float32') / 255. x_train_mnist = np.expand_dims(x_train_mnist, -1) # Dodanie kanału x_test_mnist = np.expand_dims(x_test_mnist, -1)
from tensorflow.keras.datasets import fashion_mnist
Załadowanie danych Fashion MNIST
(x_train_fashion, _), (x_test_fashion, _) = fashion_mnist.load_data() x_train_fashion = x_train_fashion.astype('float32') / 255. x_test_fashion = x_test_fashion.astype('float32') / 255. x_train_fashion = np.expand_dims(x_train_fashion, -1) # Dodanie kanału: (60000, 28, 28, 1) x_test_fashion = np.expand_dims(x_test_fashion, -1)
Sprawdzenie kształtu
print(f"x_train_fashion shape: {x_train_fashion.shape}") # Powinno być (60000, 28, 28, 1) print(f"x_test_fashion shape: {x_test_fashion.shape}") # Powinno być (10000, 28, 28, 1)
x_train_fashion shape: (60000, 28, 28, 1) x_test_fashion shape: (10000, 28, 28, 1)
from tensorflow.keras.datasets import cifar10
Załadowanie danych CIFAR-10
(x_train_cifar, _), (x_test_cifar, _) = cifar10.load_data() x_train_cifar = x_train_cifar.astype('float32') / 255. x_test_cifar = x_test_cifar.astype('float32') / 255.
#Sprawdzenie krztałtu print(f"x_train_cifar shape: {x_train_cifar.shape}") print(f"x_test_cifar shape: {x_test_cifar.shape}")
x_train_cifar shape: (50000, 32, 32, 3) x_test_cifar shape: (10000, 32, 32, 3)
import os import zipfile import gdown # Biblioteka do pobierania plików z Google Drive
Bezpośredni link do pliku ZIP
file_id = '1A2dNWabg6_um-V3lhw1tyead5hCpjaW8' zip_url = f'https://drive.google.com/uc?id={file_id}'
Ścieżka do pobranego pliku ZIP i lokalizacja rozpakowania
zip_path = '/content/image.zip' local_extract_path = '/content/images'
Pobieranie pliku ZIP
if not os.path.exists(zip_path): # Sprawdzanie, czy plik ZIP już istnieje print(f"Pobieranie pliku ZIP z Google Drive: {zip_url}...") gdown.download(zip_url, zip_path, quiet=False) else: print(f"Plik ZIP już istnieje: {zip_path}")
Rozpakowanie archiwum
if not os.path.exists(local_extract_path): # Sprawdzanie, czy katalog już istnieje os.makedirs(local_extract_path) # Tworzenie katalogu, jeśli nie istnieje print(f"Rozpakowywanie pliku ZIP: {zip_path} do {local_extract_path}...") with zipfile.ZipFile(zip_path, 'r') as zip_ref: zip_ref.extractall(local_extract_path) print("Rozpakowywanie zakończone!") else: print(f"Katalog docelowy już istnieje: {local_extract_path}")
import glob import os import zipfile # Dodano moduł zipfile from sklearn.model_selection import train_test_split import numpy as np from tensorflow.keras.preprocessing.image import load_img, img_to_array
Path to CelebA images
celeba_path = '/content/images/image' # Update this path as needed
def load_celeba_images(path, img_size=(64, 64)): image_paths = glob.glob(os.path.join(path, '*.jpg')) images = [] for img_path in image_paths: img = load_img(img_path, target_size=img_size) img = img_to_array(img) / 255. images.append(img) return np.array(images)
Load CelebA data
x_celeba = load_celeba_images(celeba_path) x_celeba = (x_celeba - 0.5) * 2 # normalizacja danych print(f"x_celeba shape: {x_celeba.shape}") # e.g., (30000, 64, 64, 3)
Split into training and validation sets
x_train_celeba, x_val_celeba = train_test_split(x_celeba, test_size=0.3, random_state=42)
x_celeba shape: (30000, 64, 64, 3)
class Sampling(Layer): def call(self, inputs): z_mean, z_log_var = inputs epsilon = tf.keras.backend.random_normal(shape=tf.shape(z_mean)) return z_mean + tf.exp(0.5 * z_log_var) * epsilon
def build_conv_encoder(input_shape, latent_dim): input_img = Input(shape=input_shape) x = Conv2D(32, kernel_size=3, strides=2, padding='same')(input_img) x = BatchNormalization()(x) x = LeakyReLU()(x)
x = Conv2D(64, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2D(128, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2D(256, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x_shape = tf.keras.backend.int_shape(x)[1:] # Save shape before flattening x = Flatten()(x) x = Dense(512)(x) x = LeakyReLU()(x) z_mean = Dense(latent_dim, name='z_mean')(x) z_log_var = Dense(latent_dim, name='z_log_var')(x) z = Sampling()([z_mean, z_log_var]) encoder = Model(input_img, [z_mean, z_log_var, z], name='encoder') return encoder, x_shapedef build_conv_decoder(x_shape, latent_dim, output_shape): latent_inputs = Input(shape=(latent_dim,)) x = Dense(512)(latent_inputs) x = LeakyReLU()(x) x = Dense(np.prod(x_shape))(x) x = LeakyReLU()(x) x = Reshape(x_shape)(x)
x = Conv2DTranspose(256, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2DTranspose(128, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2DTranspose(64, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2DTranspose(32, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) outputs = Conv2DTranspose( output_shape[2], kernel_size=3, activation='tanh', # Use 'tanh' activation since data is in [-1, 1] padding='same' )(x) decoder = Model(latent_inputs, outputs, name='decoder') return decoderdef mean_squared_error(y_true, y_pred): diff = y_true - y_pred squared_diff = tf.square(diff) mse = tf.reduce_mean(squared_diff) return mse
class VAE(Model): def init(self, encoder, decoder, kl_weight=0.0005, **kwargs): super(VAE, self).init(**kwargs) self.encoder = encoder self.decoder = decoder self.kl_weight = kl_weight self.reconstruction_loss_tracker = tf.keras.metrics.Mean(name="reconstruction_loss") self.kl_loss_tracker = tf.keras.metrics.Mean(name="kl_loss")
def train_step(self, data): with tf.GradientTape() as tape: z_mean, z_log_var, z = self.encoder(data) reconstruction = self.decoder(z) reconstruction_loss = tf.reduce_mean( tf.reduce_sum( mean_squared_error(data, reconstruction), axis=0 ) ) kl_loss = -0.5 * tf.reduce_mean( tf.reduce_sum( 1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var), axis=1 ) ) total_loss = reconstruction_loss + self.kl_weight * kl_loss grads = tape.gradient(total_loss, self.trainable_weights) self.optimizer.apply_gradients(zip(grads, self.trainable_weights)) # Update metrics self.reconstruction_loss_tracker.update_state(reconstruction_loss) self.kl_loss_tracker.update_state(kl_loss) return {"loss": total_loss, "reconstruction_loss": self.reconstruction_loss_tracker.result(), "kl_loss": self.kl_loss_tracker.result()}import matplotlib.pyplot as plt
Funkcja do trenowania VAE
def train_vae(input_shape, latent_dim, x_train, x_val, epochs=30, batch_size=128, dataset_name='Dataset'): # Build Encoder encoder, x_shape = build_conv_encoder(input_shape, latent_dim)
# Build Decoder decoder = build_conv_decoder(x_shape, latent_dim, input_shape) # Initialize VAE vae = VAE(encoder, decoder, kl_weight=0.0005) vae.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005)) # Train VAE history = vae.fit( x_train, epochs=epochs, batch_size=batch_size, validation_data=(x_val, None) ) # Wizualizacja strat podczas treningu plt.figure(figsize=(8,6)) plt.plot(history.history['loss'], label='Całkowita strata treningowa') plt.plot(history.history['reconstruction_loss'], label='Strata rekonstrukcji treningowa') plt.xlabel('Epoka') plt.ylabel('Strata') plt.legend() plt.title(f'Straty podczas treningu VAE na {dataset_name}') plt.show() # Rekonstrukcja obrazów testowych x_reconstructed = vae.predict(x_val[:10]) # Funkcja do wyświetlania oryginalnych i zrekonstruowanych obrazów plot_reconstructions(x_val[:10], x_reconstructed, input_shape) return vaeFunkcja do wizualizacji rekonstrukcji
def plot_reconstructions(x_original, x_reconstructed, input_shape, num_images=10): plt.figure(figsize=(20, 4)) for i in range(num_images): # Wyświetlenie oryginalnych obrazów ax = plt.subplot(2, num_images, i + 1) if input_shape[-1] == 1: plt.imshow(x_original[i].squeeze(), cmap='gray') else: plt.imshow(x_original[i]) ax.axis('off')
# Wyświetlenie zrekonstruowanych obrazów ax = plt.subplot(2, num_images, i + 1 + num_images) if x_reconstructed.shape[-1] == 1: plt.imshow(x_reconstructed[i].squeeze(), cmap='gray') else: plt.imshow(x_reconstructed[i]) ax.axis('off') plt.tight_layout() plt.show()from tensorflow.keras.datasets import fashion_mnist
Load Fashion MNIST data
(x_train_fashion, _), (x_test_fashion, _) = fashion_mnist.load_data() x_train_fashion = x_train_fashion.astype('float32') / 255. x_test_fashion = x_test_fashion.astype('float32') / 255. x_train_fashion = np.expand_dims(x_train_fashion, -1) x_test_fashion = np.expand_dims(x_test_fashion, -1)
Train VAE on Fashion MNIST
vae_fashion = train_vae( input_shape=(28, 28, 1), latent_dim=16, x_train=x_train_fashion, x_val=x_test_fashion, epochs=30, batch_size=128, dataset_name='Fashion MNIST' )
ValueError: Kernel shape must have the same length as input, but received kernel of shape (3, 3, 1, 32) and input of shape (None, None, 28, 28, 1).
Dostosuj kod tak aby mógł przeprowadzać terning na różnych zbiorach danych
from tensorflow.keras.datasets import cifar10
Załadowanie i przygotowanie danych CIFAR-10
(x_train_cifar, _), (x_test_cifar, _) = cifar10.load_data() x_train_cifar = x_train_cifar.astype('float32') / 255. x_test_cifar = x_test_cifar.astype('float32') / 255.
Nie dodawaj dodatkowego wymiaru
print(f"x_train_cifar shape: {x_train_cifar.shape}") # (50000,32,32,3) print(f"x_test_cifar shape: {x_test_cifar.shape}") # (10000,32,32,3)
Trenowanie VAE na CIFAR-10
vae_cifar = train_vae( input_shape=(32, 32, 3), latent_dim=128, x_train=x_train_cifar, x_val=x_test_cifar, epochs=50, batch_size=256, dataset_name='CIFAR-10' )
Trenowanie VAE na CelebA
vae_celeba = train_vae( input_shape=(64, 64, 3), latent_dim=256, x_train=x_train_celeba, x_val=x_val_celeba, epochs=100, batch_size=128, dataset_name='CelebA' )
Dostosuj kod tak aby mógł przeprowadzać terning na różnych zbiorach danych, wypisz cały poprawiony kod
- Assistant
Certainly! The issue you're encountering is due to an extra dimension being passed to the model during training, specifically in the
train_stepmethod of theVAEclass. In thefitmethod, when you pass(x_val, None)as the validation data, Keras interprets this as(inputs, targets), and sincetargetsisNone, it introduces an unexpected extra dimension.To fix this and make your code adaptable to different datasets, we need to adjust the
train_stepmethod to correctly handle the input data and modify how validation data is passed. Below is the entire corrected code with the necessary adjustments:import tensorflow as tf from tensorflow.keras.layers import Input, Dense, Reshape, Conv2DTranspose, Conv2D, Flatten, BatchNormalization, LeakyReLU from tensorflow.keras.models import Model from tensorflow.keras.datasets import fashion_mnist, cifar10 import numpy as np import matplotlib.pyplot as plt import glob import os from sklearn.model_selection import train_test_split from tensorflow.keras.preprocessing.image import load_img, img_to_array # Sampling layer for reparameterization trick class Sampling(tf.keras.layers.Layer): def call(self, inputs): z_mean, z_log_var = inputs epsilon = tf.keras.backend.random_normal(shape=tf.shape(z_mean)) return z_mean + tf.exp(0.5 * z_log_var) * epsilon # Encoder def build_conv_encoder(input_shape, latent_dim): input_img = Input(shape=input_shape) x = Conv2D(32, kernel_size=3, strides=2, padding='same')(input_img) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2D(64, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2D(128, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2D(256, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x_shape = tf.keras.backend.int_shape(x)[1:] # Save shape before flattening x = Flatten()(x) x = Dense(512)(x) x = LeakyReLU()(x) z_mean = Dense(latent_dim, name='z_mean')(x) z_log_var = Dense(latent_dim, name='z_log_var')(x) z = Sampling()([z_mean, z_log_var]) encoder = Model(input_img, [z_mean, z_log_var, z], name='encoder') return encoder, x_shape # Decoder def build_conv_decoder(x_shape, latent_dim, output_shape): latent_inputs = Input(shape=(latent_dim,)) x = Dense(512)(latent_inputs) x = LeakyReLU()(x) x = Dense(np.prod(x_shape))(x) x = LeakyReLU()(x) x = Reshape(x_shape)(x) x = Conv2DTranspose(256, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2DTranspose(128, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2DTranspose(64, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2DTranspose(32, kernel_size=3, strides=2, padding='same')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) outputs = Conv2DTranspose( output_shape[2], kernel_size=3, activation='sigmoid', # Use 'sigmoid' activation since data is in [0, 1] padding='same' )(x) decoder = Model(latent_inputs, outputs, name='decoder') return decoder # VAE class class VAE(Model): def __init__(self, encoder, decoder, kl_weight=0.0005, **kwargs): super(VAE, self).__init__(**kwargs) self.encoder = encoder self.decoder = decoder self.kl_weight = kl_weight self.total_loss_tracker = tf.keras.metrics.Mean(name='total_loss') self.reconstruction_loss_tracker = tf.keras.metrics.Mean(name='reconstruction_loss') self.kl_loss_tracker = tf.keras.metrics.Mean(name='kl_loss') @property def metrics(self): return [self.total_loss_tracker, self.reconstruction_loss_tracker, self.kl_loss_tracker] def train_step(self, data): if isinstance(data, tuple): data = data[0] with tf.GradientTape() as tape: z_mean, z_log_var, z = self.encoder(data) reconstruction = self.decoder(z) # Flatten the images for loss computation data_flat = tf.reshape(data, [tf.shape(data)[0], -1]) reconstruction_flat = tf.reshape(reconstruction, [tf.shape(reconstruction)[0], -1]) # Reconstruction loss reconstruction_loss = tf.reduce_mean( tf.reduce_sum( tf.keras.losses.mean_squared_error( data_flat, reconstruction_flat ), axis=1 ) ) # KL divergence loss kl_loss = -0.5 * tf.reduce_mean( tf.reduce_sum( 1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var), axis=1 ) ) # Total loss total_loss = reconstruction_loss + self.kl_weight * kl_loss # Gradients and optimization step grads = tape.gradient(total_loss, self.trainable_weights) self.optimizer.apply_gradients(zip(grads, self.trainable_weights)) # Update metrics self.total_loss_tracker.update_state(total_loss) self.reconstruction_loss_tracker.update_state(reconstruction_loss) self.kl_loss_tracker.update_state(kl_loss) return { "loss": self.total_loss_tracker.result(), "reconstruction_loss": self.reconstruction_loss_tracker.result(), "kl_loss": self.kl_loss_tracker.result(), } def call(self, inputs): # For prediction and encoding z_mean, z_log_var, z = self.encoder(inputs) return self.decoder(z) # Function to train the VAE def train_vae(input_shape, latent_dim, x_train, x_val, epochs=30, batch_size=128, dataset_name='Dataset'): encoder, x_shape = build_conv_encoder(input_shape, latent_dim) decoder = build_conv_decoder(x_shape, latent_dim, input_shape) vae = VAE(encoder, decoder, kl_weight=0.0005) vae.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005)) history = vae.fit( x_train, epochs=epochs, batch_size=batch_size, validation_data=x_val ) # Plot training losses plt.figure(figsize=(8,6)) plt.plot(history.history['loss'], label='Total Training Loss') plt.plot(history.history['reconstruction_loss'], label='Reconstruction Loss') plt.xlabel('Epoch') plt.ylabel('Loss') plt.legend() plt.title(f'Training Losses on {dataset_name}') plt.show() # Reconstruct validation images x_reconstructed = vae.predict(x_val[:10]) # Plot original and reconstructed images plot_reconstructions(x_val[:10], x_reconstructed, input_shape) return vae # Function to plot reconstructions def plot_reconstructions(x_original, x_reconstructed, input_shape, num_images=10): plt.figure(figsize=(20, 4)) for i in range(num_images): # Original images ax = plt.subplot(2, num_images, i + 1) if input_shape[-1] == 1: plt.imshow(x_original[i].squeeze(), cmap='gray') else: plt.imshow(x_original[i]) ax.axis('off') # Reconstructed images ax = plt.subplot(2, num_images, i + 1 + num_images) if input_shape[-1] == 1: plt.imshow(x_reconstructed[i].squeeze(), cmap='gray') else: plt.imshow(x_reconstructed[i]) ax.axis('off') plt.tight_layout() plt.show() # Load and preprocess Fashion MNIST data (x_train_fashion, _), (x_test_fashion, _) = fashion_mnist.load_data() x_train_fashion = x_train_fashion.astype('float32') / 255. x_test_fashion = x_test_fashion.astype('float32') / 255. x_train_fashion = np.expand_dims(x_train_fashion, -1) # (60000, 28, 28, 1) x_test_fashion = np.expand_dims(x_test_fashion, -1) # (10000, 28, 28, 1) # Train VAE on Fashion MNIST vae_fashion = train_vae( input_shape=(28, 28, 1), latent_dim=16, x_train=x_train_fashion, x_val=x_test_fashion, epochs=30, batch_size=128, dataset_name='Fashion MNIST' ) # Load and preprocess CIFAR-10 data (x_train_cifar, _), (x_test_cifar, _) = cifar10.load_data() x_train_cifar = x_train_cifar.astype('float32') / 255. x_test_cifar = x_test_cifar.astype('float32') / 255. print(f"x_train_cifar shape: {x_train_cifar.shape}") # (50000, 32, 32, 3) print(f"x_test_cifar shape: {x_test_cifar.shape}") # (10000, 32, 32, 3) # Train VAE on CIFAR-10 vae_cifar = train_vae( input_shape=(32, 32, 3), latent_dim=128, x_train=x_train_cifar, x_val=x_test_cifar, epochs=50, batch_size=256, dataset_name='CIFAR-10' ) # Function to load CelebA images def load_celeba_images(path, img_size=(64, 64)): image_paths = glob.glob(os.path.join(path, '*.jpg')) images = [] for img_path in image_paths: img = load_img(img_path, target_size=img_size) img = img_to_array(img) / 255. images.append(img) return np.array(images) # Load and preprocess CelebA data celeba_path = '/content/images/image' # Update this path as needed x_celeba = load_celeba_images(celeba_path) print(f"x_celeba shape: {x_celeba.shape}") # e.g., (30000, 64, 64, 3) x_train_celeba, x_val_celeba = train_test_split(x_celeba, test_size=0.3, random_state=42) # Train VAE on CelebA vae_celeba = train_vae( input_shape=(64, 64, 3), latent_dim=256, x_train=x_train_celeba, x_val=x_val_celeba, epochs=100, batch_size=128, dataset_name='CelebA' )Key Adjustments:
-
Handling Validation Data:
- Changed the way validation data is passed during training from
validation_data=(x_val, None)tovalidation_data=x_valto prevent an extraNonedimension.
- Changed the way validation data is passed during training from
-
Adjusting
train_stepMethod:- Modified the
train_stepmethod in theVAEclass to check if the inputdatais a tuple and extract the first element. This ensures that only the input data is used for training.
- Modified the
-
Using Built-in Loss Functions:
- Replaced the custom
mean_squared_errorfunction with TensorFlow's built-intf.keras.losses.mean_squared_errorfor more stability.
- Replaced the custom
-
Flattening Inputs for Loss Computation:
- Flattened the inputs and reconstructions when computing the reconstruction loss to ensure the correct shape and avoid dimension mismatches.
-
Consistent Activation Functions:
- Used
sigmoidactivation in the decoder's output layer since the data is normalized between 0 and 1.
- Used
-
Unified Preprocessing Across Datasets:
- Ensured that all datasets are preprocessed in a consistent manner, suitable for the model architecture.
Now, the code should successfully train on different datasets without any dimension mismatches or errors.
-