5

!pip install tensorflow

import tensorflow as tf




#1. Load and preprocess the CIFAR-10 dataset:

from tensorflow.keras.datasets import cifar10

# Load dataset

(X_train, _), (X_test, _) = cifar10.load_data()

# Normalize the data

X_train = X_train.astype('float32') / 255.0

X_test = X_test.astype('float32') / 255.0

#2. Add noise to the CIFAR-10 images:

import numpy as np

noise_factor = 0.5

X_train_noisy = X_train + noise_factor * np.random.normal(loc=0.0,

scale=1.0, size=X_train.shape)

X_test_noisy = X_test + noise_factor * np.random.normal(loc=0.0,

scale=1.0, size=X_test.shape)

# Clip to keep pixel values between 0 and 1

X_train_noisy = np.clip(X_train_noisy, 0., 1.)

X_test_noisy = np.clip(X_test_noisy, 0., 1.)

#3. Define the convolutional autoencoder (CAE):

model = tf.keras.Sequential([

 # Encoder

 tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same',

input_shape=(32, 32, 3)),

 tf.keras.layers.MaxPooling2D((2, 2), padding='same'),

 tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'),

 tf.keras.layers.MaxPooling2D((2, 2), padding='same'),

 # Decoder

 tf.keras.layers.Conv2DTranspose(64, (3, 3), activation='relu',

padding='same'),

 tf.keras.layers.UpSampling2D((2, 2)),

tf.keras.layers.Conv2DTranspose(32, (3, 3), activation='relu',

padding='same'),

 tf.keras.layers.UpSampling2D((2, 2)),

 tf.keras.layers.Conv2D(3, (3, 3), activation='sigmoid',

padding='same')

])

model.compile(optimizer='adam', loss='mse')

#4. Train the autoencoder on noisy CIFAR-10 images:

history = model.fit(X_train_noisy, X_train, epochs=50, batch_size=128,

validation_data=(X_test_noisy, X_test))

#5. Visualize original, noisy, and reconstructed images:

import matplotlib.pyplot as plt

# Reconstruct images

decoded_imgs = model.predict(X_test_noisy)

# Display original, noisy, and reconstructed images

n = 10

plt.figure(figsize=(20, 6))

for i in range(n):

 # Display original

 ax = plt.subplot(3, n, i + 1)

 plt.imshow(X_test[i])

 plt.title("Original")

 plt.axis('off')

 # Display noisy

 ax = plt.subplot(3, n, i + 1 + n)

 plt.imshow(X_test_noisy[i])

 plt.title("Noisy")

 plt.axis('off')

 # Display reconstruction

 ax = plt.subplot(3, n, i + 1 + 2*n)

 plt.imshow(decoded_imgs[i])

 plt.title("Reconstructed")

 plt.axis('off')

plt.show()

#6. Evaluate the performance using Mean Squared Error (MSE):

mse = np.mean(np.square(X_test - decoded_imgs))

print(f"Reconstruction MSE: {mse}") 

Comments

Popular posts from this blog

2

1

3