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|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
---
|
| 4 |
+
|
| 5 |
+
# **U-Net CNN Autoencoder for Image Denoising**
|
| 6 |
+
|
| 7 |
+
A hands-on guide to building a deep-learning model that cleans noisy images, improving downstream classification tasks.
|
| 8 |
+
|
| 9 |
+
When I began experimenting with image-classification projects, I quickly realized how sensitive models are to noise. Small imperfections—sensor noise, compression artifacts, random pixel disturbances—could drastically reduce performance.
|
| 10 |
+
|
| 11 |
+
Instead of training classifiers directly on noisy images, I decided to build a **preprocessing model**: one whose sole purpose is to take a noisy input and output a cleaner version. This approach allows classifiers to focus on meaningful patterns rather than irrelevant distortions.
|
| 12 |
+
|
| 13 |
+
That led me to design a **U-Net–based CNN Autoencoder**.
|
| 14 |
+
|
| 15 |
+
This repository covers:
|
| 16 |
+
|
| 17 |
+
* Why I chose a U-Net structure
|
| 18 |
+
* The design of the autoencoder
|
| 19 |
+
* How noisy images were generated
|
| 20 |
+
* Training and evaluation process
|
| 21 |
+
* Key results and insights
|
| 22 |
+
|
| 23 |
+
**Goal:** Leverage a robust deep-learning architecture to denoise images before feeding them to classifiers.
|
| 24 |
+
|
| 25 |
+
---
|
| 26 |
+
|
| 27 |
+
## 1. Environment Setup
|
| 28 |
+
|
| 29 |
+
The project uses the standard TensorFlow/Keras stack:
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
import tensorflow as tf
|
| 33 |
+
from tensorflow.keras.layers import *
|
| 34 |
+
from tensorflow.keras.models import Model
|
| 35 |
+
import numpy as np
|
| 36 |
+
import matplotlib.pyplot as plt
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
This provides a flexible foundation for building custom CNN architectures.
|
| 40 |
+
|
| 41 |
+
---
|
| 42 |
+
|
| 43 |
+
## 2. Why a U-Net Autoencoder?
|
| 44 |
+
|
| 45 |
+
Traditional autoencoders compress and reconstruct images but often lose important details.
|
| 46 |
+
|
| 47 |
+
**U-Net advantages:**
|
| 48 |
+
|
| 49 |
+
* Downsamples to learn a compact representation
|
| 50 |
+
* Upsamples to reconstruct the image
|
| 51 |
+
* Uses **skip connections** to preserve high-resolution features
|
| 52 |
+
|
| 53 |
+
This makes U-Net ideal for: denoising, segmentation, super-resolution, and image restoration tasks.
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
## 3. Building the Model
|
| 58 |
+
|
| 59 |
+
**Encoder:**
|
| 60 |
+
|
| 61 |
+
```python
|
| 62 |
+
c1 = Conv2D(64, 3, activation='relu', padding='same')(inputs)
|
| 63 |
+
p1 = MaxPooling2D((2, 2))(c1)
|
| 64 |
+
c2 = Conv2D(128, 3, activation='relu', padding='same')(p1)
|
| 65 |
+
p2 = MaxPooling2D((2, 2))(c2)
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
**Bottleneck:**
|
| 69 |
+
|
| 70 |
+
```python
|
| 71 |
+
bn = Conv2D(256, 3, activation='relu', padding='same')(p2)
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
**Decoder:**
|
| 75 |
+
|
| 76 |
+
```python
|
| 77 |
+
u1 = UpSampling2D((2, 2))(bn)
|
| 78 |
+
m1 = concatenate([u1, c2])
|
| 79 |
+
c3 = Conv2D(128, 3, activation='relu', padding='same')(m1)
|
| 80 |
+
u2 = UpSampling2D((2, 2))(c3)
|
| 81 |
+
m2 = concatenate([u2, c1])
|
| 82 |
+
c4 = Conv2D(64, 3, activation='relu', padding='same')(m2)
|
| 83 |
+
outputs = Conv2D(1, 3, activation='sigmoid', padding='same')(c4)
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
Core concept: **down → compress → up → reconnect → reconstruct**
|
| 87 |
+
|
| 88 |
+
---
|
| 89 |
+
|
| 90 |
+
## 4. Creating Noisy Data
|
| 91 |
+
|
| 92 |
+
I added Gaussian noise to MNIST digits to generate training pairs:
|
| 93 |
+
|
| 94 |
+
```python
|
| 95 |
+
noise_factor = 0.4
|
| 96 |
+
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
+
Training pairs:
|
| 100 |
+
|
| 101 |
+
* **Clean image**
|
| 102 |
+
* **Noisy version**
|
| 103 |
+
|
| 104 |
+
Perfect for learning a denoising function.
|
| 105 |
+
|
| 106 |
+
---
|
| 107 |
+
|
| 108 |
+
## 5. Training the Autoencoder
|
| 109 |
+
|
| 110 |
+
Compile:
|
| 111 |
+
|
| 112 |
+
```python
|
| 113 |
+
model.compile(optimizer='adam', loss='binary_crossentropy')
|
| 114 |
+
```
|
| 115 |
+
|
| 116 |
+
Train:
|
| 117 |
+
|
| 118 |
+
```python
|
| 119 |
+
model.fit(x_train_noisy, x_train, epochs=10, batch_size=128, validation_split=0.1)
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
The model learns a simple rule: **Noisy input → Clean output**.
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## 6. Visualizing Results
|
| 127 |
+
|
| 128 |
+
After training, comparing:
|
| 129 |
+
|
| 130 |
+
* Noisy input
|
| 131 |
+
* Denoised output
|
| 132 |
+
* Original image
|
| 133 |
+
|
| 134 |
+
The autoencoder effectively removes noise while keeping key structures intact—ideal for lightweight models and MNIST.
|
| 135 |
+
|
| 136 |
+
---
|
| 137 |
+
|
| 138 |
+
## 7. Benefits for Classification
|
| 139 |
+
|
| 140 |
+
A denoising preprocessing step improves real-world image classification pipelines:
|
| 141 |
+
|
| 142 |
+
**Pipeline:**
|
| 143 |
+
`Noisy Image → Autoencoder → Classifier → Prediction`
|
| 144 |
+
|
| 145 |
+
Helps with noise from:
|
| 146 |
+
|
| 147 |
+
* Cameras or sensors
|
| 148 |
+
* Low-light conditions
|
| 149 |
+
* Compression or motion blur
|
| 150 |
+
|
| 151 |
+
Cleaner inputs → better predictions.
|
| 152 |
+
|
| 153 |
+
---
|
| 154 |
+
|
| 155 |
+
## 8. Key Takeaways
|
| 156 |
+
|
| 157 |
+
* U-Net skip connections preserve important features
|
| 158 |
+
* Autoencoders are powerful preprocessing tools
|
| 159 |
+
* Denoising improves classifier performance
|
| 160 |
+
* Lightweight, easy to integrate, scalable to any dataset
|
| 161 |
+
|
| 162 |
+
This method is practical and immediately applicable to real-world noisy data.
|