File size: 4,420 Bytes
2017198 a31e792 2017198 a31e792 336ff17 2017198 2be7343 88452e6 2be7343 88452e6 2017198 |
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 |
---
language: en
license: mit
tags:
- image-denoising
- tensorflow
- unet
- computer-vision
inference: true
datasets:
- AIOmarRehan/Cropped_Yale_Faces
---
[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)
---
# **U-Net CNN Autoencoder for Image Denoising**
A hands-on guide to building a deep-learning model that cleans noisy images, improving downstream classification tasks.
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.
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.
That led me to design a **U-Net–based CNN Autoencoder**.
This repository covers:
* Why I chose a U-Net structure
* The design of the autoencoder
* How noisy images were generated
* Training and evaluation process
* Key results and insights
**Goal:** Leverage a robust deep-learning architecture to denoise images before feeding them to classifiers.
---
## 1. Environment Setup
The project uses the standard TensorFlow/Keras 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 provides a flexible foundation for building custom CNN architectures.
---
## 2. Why a U-Net Autoencoder?
Traditional autoencoders compress and reconstruct images but often lose important details.
**U-Net advantages:**
* Downsamples to learn a compact representation
* Upsamples to reconstruct the image
* Uses **skip connections** to preserve high-resolution features
This makes U-Net ideal for: denoising, segmentation, super-resolution, and image restoration tasks.
---
## 3. Building the Model
**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)
outputs = Conv2D(1, 3, activation='sigmoid', padding='same')(c4)
```
Core concept: **down → compress → up → reconnect → reconstruct**
---
## 4. Creating Noisy Data
I added Gaussian noise to MNIST digits to generate training pairs:
```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)
```
Training pairs:
* **Clean image**
* **Noisy version**
Perfect for learning a denoising function.
---
## 5. Training the Autoencoder
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 model learns a simple rule: **Noisy input → Clean output**.
---
## 6. Visualizing Results
After training, comparing:
* Noisy input
* Denoised output
* Original image
The autoencoder effectively removes noise while keeping key structures intact—ideal for lightweight models and MNIST.
---
## 7. Benefits for Classification
A denoising preprocessing step improves real-world image classification pipelines:
**Pipeline:**
`Noisy Image → Autoencoder → Classifier → Prediction`
Helps with noise from:
* Cameras or sensors
* Low-light conditions
* Compression or motion blur
Cleaner inputs → better predictions.
---
## 8. Key Takeaways
* U-Net skip connections preserve important features
* Autoencoders are powerful preprocessing tools
* Denoising improves classifier performance
* Lightweight, easy to integrate, scalable to any dataset
This method is practical and immediately applicable to real-world noisy data. |