/Image-Denoising-with-Autoencoders

This project focuses on building an image denoising system using autoencoders. The autoencoder is trained to remove noise from the MNIST dataset images, which are commonly used for digit recognition tasks. The project includes several advanced features such as data augmentation, model checkpointing, learning rate scheduling, and comprehensive eval.

Primary LanguageJupyter NotebookMIT LicenseMIT

##Image Denoising with Autoencoders This project focuses on building an image denoising system using autoencoders. The autoencoder is trained to remove noise from the MNIST dataset images, which are commonly used for digit recognition tasks. The project includes several advanced features such as data augmentation, model checkpointing, learning rate scheduling, and comprehensive evaluation metrics.

#Table of Contents

  • Introduction
  • Project Structure
  • Getting Started
  • Prerequisites
  • Installation
  • Model Architecture
  • Training
  • Evaluation
  • Visualization
  • Error Analysis
  • Additional Metrics
  • Future Work
  • Contributors
  1. Introduction Image denoising is a crucial preprocessing step in many computer vision tasks. This project aims to develop a robust image denoising system using an autoencoder. The autoencoder learns to reconstruct clean images from noisy versions of the MNIST dataset, thereby enhancing image quality for downstream tasks.

  2. Project Structure . ├── data │ ├── processed │ ├── raw ├── models │ ├── autoencoder_model.h5 │ ├── best_autoencoder.h5 ├── notebooks │ ├── data_preprocessing.ipynb │ ├── model_training.ipynb │ ├── evaluation_visualization.ipynb ├── README.md └── requirements.txt

  3. Getting Started Prerequisites:

  • Python 3.7+
  • TensorFlow 2.x
  • NumPy
  • scikit-learn
  • Matplotlib
  • joblib
  1. Installation Clone the repository: git clone https://github.com/yourusername/image-denoising-autoencoder.git cd image-denoising-autoencoder

Install the required packages: pip install -r requirements.txt

  1. Model Architecture The autoencoder model consists of an encoder and a decoder. The encoder compresses the input image into a lower-dimensional latent space, while the decoder reconstructs the image from this latent representation. The architecture is as follows:

Encoder:

Conv2D(32 filters, 3x3 kernel, ReLU activation, padding='same') MaxPooling2D(pool size=(2, 2), padding='same') Conv2D(32 filters, 3x3 kernel, ReLU activation, padding='same') MaxPooling2D(pool size=(2, 2), padding='same')

Decoder:

Conv2D(32 filters, 3x3 kernel, ReLU activation, padding='same') UpSampling2D(size=(2, 2)) Conv2D(32 filters, 3x3 kernel, ReLU activation, padding='same') UpSampling2D(size=(2, 2)) Conv2D(1 filter, 3x3 kernel, Sigmoid activation, padding='same')

  1. Training The training process includes the following steps:
  • Data Augmentation: Augmenting the training data to improve model robustness.
  • Model Checkpointing: Saving the best model based on validation loss.
  • Learning Rate Scheduling: Reducing the learning rate when the validation loss plateaus. Python code = from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau

checkpoint = ModelCheckpoint('models/best_autoencoder.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min') reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, min_lr=1e-6, verbose=1) history = autoencoder.fit(x_train_noisy, x_train, epochs=50, batch_size=32, shuffle=True, validation_data=(x_test_noisy, x_test), callbacks=[checkpoint, reduce_lr])

  1. Evaluation Evaluate the model using various metrics like Mean Squared Error (MSE), Structural Similarity Index (SSIM), and Peak Signal-to-Noise Ratio (PSNR).

Python code = from skimage.metrics import structural_similarity as ssim, peak_signal_noise_ratio as psnr

denoised_images = autoencoder.predict(x_test_noisy) ssim_values = [ssim(x_test[i].reshape(28, 28), denoised_images[i].reshape(28, 28)) for i in range(len(x_test))] psnr_values = [psnr(x_test[i].reshape(28, 28), denoised_images[i].reshape(28, 28)) for i in range(len(x_test))]

print(f'Average SSIM: {np.mean(ssim_values)}') print(f'Average PSNR: {np.mean(psnr_values)}')

  1. Visualization Visualize the results to understand the model's performance better:

Denoised Images Python code= def plot_denoised_images(noisy_images, denoised_images, original_images, n=10): plt.figure(figsize=(20, 4)) for i in range(n): ax = plt.subplot(3, n, i + 1) plt.imshow(noisy_images[i].reshape(28, 28), cmap='gray') ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False)

    ax = plt.subplot(3, n, i + 1 + n)
    plt.imshow(denoised_images[i].reshape(28, 28), cmap='gray')
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    ax = plt.subplot(3, n, i + 1 + 2*n)
    plt.imshow(original_images[i].reshape(28, 28), cmap='gray')
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

plot_denoised_images(x_test_noisy, denoised_images, x_test)

  1. PCA Visualization of Latent Space Python code = encoder = Model(inputs=autoencoder.input, outputs=autoencoder.layers[4].output) latent_space = encoder.predict(x_test_noisy) latent_space = latent_space.reshape(latent_space.shape[0], -1) pca = PCA(n_components=2) latent_pca = pca.fit_transform(latent_space)

plt.figure(figsize=(10, 6)) plt.scatter(latent_pca[:, 0], latent_pca[:, 1], c=np.argmax(y_test, axis=1), cmap='tab10', alpha=0.8) plt.colorbar() plt.title('PCA Visualization of Latent Space') plt.xlabel('Principal Component 1') plt.ylabel('Principal Component 2') plt.show()

  1. Error Analysis Identify and visualize the worst-performing denoised images to understand where the model fails.

Python code = from sklearn.metrics import mean_squared_error

mse_scores = [mean_squared_error(x_test[i].flatten(), denoised_images[i].flatten()) for i in range(len(x_test))] worst_indices = np.argsort(mse_scores)[-10:] plot_denoised_images(x_test_noisy[worst_indices], denoised_images[worst_indices], x_test[worst_indices])

  1. Additional Metrics Evaluate the model using SSIM and PSNR metrics.

Python code = from skimage.metrics import structural_similarity as ssim, peak_signal_noise_ratio as psnr

ssim_values = [ssim(x_test[i].reshape(28, 28), denoised_images[i].reshape(28, 28)) for i in range(len(x_test))] psnr_values = [psnr(x_test[i].reshape(28, 28), denoised_images[i].reshape(28, 28)) for i in range(len(x_test))]

print(f'Average SSIM: {np.mean(ssim_values)}') print(f'Average PSNR: {np.mean(psnr_values)}')

  1. Future Work = Hyperparameter Tuning: Experiment with different architectures and hyperparameters to improve performance. Advanced Models: Try more advanced architectures like U-Net or GANs for denoising. Deployment: Deploy the model using Flask or any other web framework to make it accessible as an API. Comparison with Traditional Methods: Compare the performance of the autoencoder with traditional image denoising techniques.

  2. Contributors Your Name - https://github.com/testgithubrittttttt Feel free to reach out if you have any questions or suggestions! Don't forget to follow me and star the repository because it encourages me to make these type of projects.