Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

import tensorflow as tf from tensorflow.keras.layers import Input, Dense, Reshape,…

  1. 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

    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)

    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}")

    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) 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=321565)

    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, activation='relu', padding='same')(input_img) x = Conv2D(64, kernel_size=3, strides=2, activation='relu', padding='same')(x) x_shape = tf.keras.backend.int_shape(x)[1:] # Zachowanie kształtu przed spłaszczeniem x = Flatten()(x) x = Dense(128, activation='relu')(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

    def build_conv_decoder(x_shape, latent_dim, output_shape): latent_inputs = Input(shape=(latent_dim,)) x = Dense(np.prod(x_shape), activation='relu')(latent_inputs) x = Reshape(x_shape)(x) x = Conv2DTranspose(64, kernel_size=3, strides=2, activation='relu', padding='same')(x) x = Conv2DTranspose(32, kernel_size=3, strides=2, activation='relu', padding='same')(x)

    # Calculate current size after upsampling
    current_size = x_shape[0] * 2**2  # Adjust based on strides
    target_size = output_shape[0]
    
    while current_size < target_size:
        x = Conv2DTranspose(16, kernel_size=3, strides=1, activation='relu', padding='same')(x)
        current_size += 1  # Adjust as needed
        # Remove `break` to allow multiple layers if needed
    
    outputs = Conv2DTranspose(
        output_shape[2],
        kernel_size=3,
        activation='sigmoid',
        padding='same'
    )(x)
    decoder = Model(latent_inputs, outputs, name='decoder')
    return decoder
    

    class VAE(Model): def init(self, encoder, decoder, kl_weight=1.0, **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 call(self, inputs):
        z_mean, z_log_var, z = self.encoder(inputs)
        reconstruction = self.decoder(z)
        return reconstruction
    
    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(
                    tf.keras.losses.binary_crossentropy(data, reconstruction),
                    axis=(1, 2)
                )
            )
            kl_loss = -0.5 * tf.reduce_mean(
                tf.reduce_sum(
                    z_log_var - tf.square(z_mean) - tf.exp(z_log_var) + 1,
                    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()}
    
    def test_step(self, data):
        z_mean, z_log_var, z = self.encoder(data)
        reconstruction = self.decoder(z)
        reconstruction_loss = tf.reduce_mean(
            tf.reduce_sum(
                tf.keras.losses.binary_crossentropy(data, reconstruction),
                axis=(1, 2)
            )
        )
        kl_loss = -0.5 * tf.reduce_mean(
            tf.reduce_sum(
                z_log_var - tf.square(z_mean) - tf.exp(z_log_var) + 1,
                axis=1
            )
        )
        total_loss = reconstruction_loss + self.kl_weight * kl_loss
    
        # 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()}
    
    @property
    def metrics(self):
        return [self.reconstruction_loss_tracker, self.kl_loss_tracker]
    

    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.1)
    vae.compile(optimizer='adam')
    
    # 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 vae
    

    Funkcja 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' )

    Epoch 1/30 469/469 [==============================] - 14s 25ms/step - loss: 276.7942 - reconstruction_loss: 272.9627 - kl_loss: 39.1739 - val_loss: 222.8884 - val_reconstruction_loss: 234.0510 - val_kl_loss: 41.6633

    Epoch 30/30 469/469 [==============================] - 12s 25ms/step - loss: 214.3455 - reconstruction_loss: 209.9006 - kl_loss: 44.2100 - val_loss: 203.3299 - val_reconstruction_loss: 212.1218 - val_kl_loss: 44.7102

    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=32, x_train=x_train_cifar, x_val=x_test_cifar, epochs=30, batch_size=256, dataset_name='CIFAR-10' )

    Epoch 1/30 196/196 [==============================] - 14s 64ms/step - loss: 662.2015 - reconstruction_loss: 659.5975 - kl_loss: 27.7675 - val_loss: 634.7189 - val_reconstruction_loss: 626.0488 - val_kl_loss: 47.2625

    Epoch 30/30 196/196 [==============================] - 13s 64ms/step - loss: 599.1610 - reconstruction_loss: 593.8020 - kl_loss: 53.2766 - val_loss: 597.2408 - val_reconstruction_loss: 595.1567 - val_kl_loss: 54.3474

    Trenowanie VAE na CelebA

    vae_celeba = train_vae( input_shape=(64, 64, 3), latent_dim=64, x_train=x_train_celeba, x_val=x_val_celeba, epochs=12, batch_size=128, dataset_name='CelebA' )

    Udoskonal mój kod abym mógł lepiej wytrenować mój model

  2. Assistant

    To improve your Variational Autoencoder (VAE) model and achieve better training results, especially on the CelebA dataset, you can consider the following enhancements:

    1. Change the Loss Function: Use Mean Squared Error (MSE) instead of Binary Crossentropy for the reconstruction loss. MSE is more suitable for continuous data like images with pixel values.

    2. Adjust the Output Activation: Change the activation function of the output layer in the decoder from 'sigmoid' to 'linear'. This allows the model to output a wider range of values, which is beneficial when using MSE loss.

    3. Normalize the Data Appropriately: Normalize your image data to have pixel values between -1 and 1. This can improve the training stability and performance.

    4. Enhance the Model Architecture: Increase the depth and complexity of your encoder and decoder networks by adding more convolutional layers and using techniques like Batch Normalization and Dropout.

    5. Adjust Hyperparameters: Experiment with different learning rates, optimizers, and KL divergence weights to find the optimal settings for your model.

    6. Increase Training Epochs: Training the model for more epochs can help it learn better representations, especially with deeper architectures.

    Below is the revised code incorporating these improvements:

    1. Modify Data Normalization

    Normalize the image data to have pixel values between -1 and 1:

    # Update data normalization for CelebA
    x_celeba = (x_celeba - 0.5) * 2  # Scale to [-1, 1]
    

    2. Update the Encoder Architecture

    Enhance the encoder by adding more convolutional layers and including Batch Normalization and LeakyReLU activation:

    from tensorflow.keras.layers import BatchNormalization, LeakyReLU
    
    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
    

    3. Update the Decoder Architecture

    Similarly, enhance the 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='tanh',  # Use 'tanh' activation since data is in [-1, 1]
            padding='same'
        )(x)
        decoder = Model(latent_inputs, outputs, name='decoder')
        return decoder
    

    4. Modify the VAE Loss Function

    Use Mean Squared Error (MSE) for the reconstruction loss and adjust the KL divergence weight:

    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(
                        tf.keras.losses.mean_squared_error(tf.keras.backend.flatten(data),
                                                           tf.keras.backend.flatten(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()}
    

    5. Adjust the Optimizer and Learning Rate

    Use the Adam optimizer with a smaller learning rate:

    # Initialize VAE
    vae = VAE(encoder, decoder, kl_weight=0.0005)
    vae.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005))
    

    6. Increase Training Epochs

    Train the model for more epochs to allow it to learn better representations:

    # Train VAE on CelebA
    vae_celeba = train_vae(
        input_shape=(64, 64, 3),
        latent_dim=128,
        x_train=x_train_celeba,
        x_val=x_val_celeba,
        epochs=50,  # Increase the number of epochs
        batch_size=128,
        dataset_name='CelebA'
    )
    

    7. Update the train_vae Function

    Ensure that the data normalization and loss functions are consistent:

    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)
        )
    
        # Visualization code remains the same...
    

    8. Visualize Training Progress and Results

    After training, visualize the losses and reconstructed images to assess the performance.


    By implementing these changes, your VAE model should have a better capacity to learn complex features from the CelebA dataset. The deeper architecture and adjusted loss function will help the model capture more intricate patterns in the data. Additionally, training for more epochs and fine-tuning hyperparameters like the learning rate and KL divergence weight can lead to improved results.

    Note: Training deep neural networks can be resource-intensive. Ensure you have access to adequate computational resources, such as GPUs, to facilitate faster training.

    Final Remarks

    Remember to adjust the hyperparameters gradually and monitor the training process. If the model starts to overfit, consider adding regularization techniques like Dropout or experimenting with different optimizer settings.