File size: 5,438 Bytes
fa51247 5d3b587 fa51247 07aae5e fa51247 c0d2e51 07aae5e b8a4e5c fa51247 5d3b587 98694b8 5d3b587 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
---
title: CNN Autoencoder For Image Denoising
emoji: 😻
colorFrom: purple
colorTo: pink
sdk: gradio
sdk_version: 6.0.0
app_file: app.py
pinned: false
license: mit
short_description: U-Net based CNN autoencoder designed to denoise noisy image.
models:
- AIOmarRehan/CNN-DenoiseU-Net
datasets:
- AIOmarRehan/Cropped_Yale_Faces
---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
---
[If you would like a detailed explanation of this project, please refer to the Medium article below.](https://medium.com/@ai.omar.rehan/a-u-net-based-cnn-autoencoder-for-cleaning-noisy-images-before-classification-132e27b828e2)
---
# A U-Net–Based CNN Autoencoder for Cleaning Noisy Images Before Classification
A practical walkthrough of how I built and trained a deep-learning model to denoise images and boost classification performance.
When I first started working with image-classification tasks, I noticed something that kept hurting my models: **noise**. Even small distortions—random dots, compression artifacts, sensor noise—were enough to confuse the classifier.
The obvious solution was to train on noisy data… but that never felt elegant. Instead, I wanted a **preprocessing model** whose sole job is to take a noisy image and return a clean version of it. The classifier would then work on much better input.
That idea led me to build a **U-Net–based CNN Autoencoder**.
This article walks you through:
* why I chose a U-Net structure
* how the autoencoder was built
* how noisy images were generated
* how the model was trained and evaluated
* what results I achieved
**Goal:**
*Use a smart deep-learning architecture to clean images before classification.*
---
## 🔧 1. Setting Up the Environment
I started by loading the usual deep-learning stack:
```python
import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
import numpy as np
import matplotlib.pyplot as plt
```
This is the typical setup for building custom architectures using Keras.
---
## 2. Why a U-Net Autoencoder?
A normal autoencoder compresses an image into a bottleneck and then reconstructs it. It works—but often loses details.
A **U-Net**, however, uses **skip connections**, meaning it:
* compresses the image (downsampling)
* learns a compact representation
* reconstructs it (upsampling)
* *also* reconnects high-resolution features from earlier layers
This makes U-Net excellent for:
* denoising
* segmentation
* super-resolution
* restoration tasks
So instead of a plain autoencoder, I built one using a U-shaped architecture.
---
## 3. Building the U-Net Autoencoder
### Encoder
```python
c1 = Conv2D(64, 3, activation='relu', padding='same')(inputs)
p1 = MaxPooling2D((2, 2))(c1)
c2 = Conv2D(128, 3, activation='relu', padding='same')(p1)
p2 = MaxPooling2D((2, 2))(c2)
```
### Bottleneck
```python
bn = Conv2D(256, 3, activation='relu', padding='same')(p2)
```
### Decoder
```python
u1 = UpSampling2D((2, 2))(bn)
m1 = concatenate([u1, c2])
c3 = Conv2D(128, 3, activation='relu', padding='same')(m1)
u2 = UpSampling2D((2, 2))(c3)
m2 = concatenate([u2, c1])
c4 = Conv2D(64, 3, activation='relu', padding='same')(m2)
```
### Output
```python
outputs = Conv2D(1, 3, activation='sigmoid', padding='same')(c4)
```
Even though the full architecture is larger, the core idea is:
**down → compress → up → reconnect → reconstruct**
---
## 4. Generating & Preprocessing Noisy Images
Instead of downloading a noisy dataset, I artificially added **Gaussian noise** to MNIST digits:
```python
noise_factor = 0.4
x_train_noisy = x_train + noise_factor * np.random.normal(
loc=0.0, scale=1.0, size=x_train.shape
)
```
This created image pairs:
* clean MNIST digit
* noisy version of the same digit
Perfect for training an autoencoder.
---
## 5. Training the Model
Compile:
```python
model.compile(optimizer='adam', loss='binary_crossentropy')
```
Train:
```python
model.fit(
x_train_noisy, x_train,
epochs=10,
batch_size=128,
validation_split=0.1
)
```
The autoencoder learns one simple rule:
**Input:** noisy image
**Output:** clean image
---
## 6. Visualizing the Results
After training, I checked:
* noisy input
* autoencoder output
* original clean image
The model consistently removed a large amount of noise, smoothing textures while preserving structure. Not perfect—but for MNIST and a lightweight U-Net, the results were very encouraging.
---
## 7. Why This Helps Classification
If you already have (or plan to build) a classifier—CNN, ResNet, etc.—you can use a pipeline like:
```
Noisy Image → Autoencoder (denoising) → Classifier → Prediction
```
This helps with real-world noise sources like:
* camera noise
* poor lighting
* compression artifacts
* motion blur
**Clean input → better predictions.**
---
## 8. Key Takeaways
- **U-Net skip connections** help preserve important features.
- **Autoencoders** serve as powerful preprocessing tools.
- **Denoised images** can significantly improve classification accuracy.
- The **model is lightweight** and easy to integrate.
- The approach **scales to any image dataset**.
This approach is not just theoretical—it’s extremely practical.
Any project involving real-world noisy data can benefit from this denoising layer.
---
## 9. Results
[Watch Demo Video](Results/A_U-Net_Autoencoder.mp4) |