Update app.py
Browse files
app.py
CHANGED
@@ -5,58 +5,59 @@ import numpy as np
|
|
5 |
import tensorflow as tf
|
6 |
import gradio as gr
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
|
|
10 |
|
11 |
-
# 2. Denoising pipeline function
|
12 |
def process_and_denoise(image):
|
13 |
"""
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
16 |
"""
|
17 |
-
#
|
18 |
-
# If color, convert to gray:
|
19 |
if image.ndim == 3 and image.shape[2] == 3:
|
20 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
21 |
else:
|
22 |
-
#
|
23 |
-
gray = image[...,0] if image.ndim == 3 else image
|
24 |
|
25 |
-
# Resize
|
26 |
-
orig = cv2.resize(gray, (64,64))
|
27 |
orig_norm = orig.astype(np.float32) / 255.0
|
28 |
|
29 |
-
#
|
30 |
-
sigma = 0.1
|
31 |
noisy = orig_norm + sigma * np.random.randn(*orig_norm.shape)
|
32 |
noisy = np.clip(noisy, 0.0, 1.0)
|
33 |
|
34 |
-
#
|
35 |
-
inp = noisy[np.newaxis, ..., np.newaxis]
|
36 |
-
pred = model.predict(inp)[0, ..., 0]
|
37 |
|
38 |
-
#
|
39 |
-
orig_disp
|
40 |
-
noisy_disp
|
41 |
-
recon_disp
|
42 |
|
43 |
return orig_disp, noisy_disp, recon_disp
|
44 |
|
45 |
-
# 3. Build Gradio interface with 3 outputs
|
46 |
demo = gr.Interface(
|
47 |
fn=process_and_denoise,
|
48 |
inputs=gr.Image(type="numpy", label="Input Image"),
|
49 |
outputs=[
|
50 |
gr.Image(type="numpy", label="Original (64×64)"),
|
51 |
gr.Image(type="numpy", label="Noisy (σ=0.1)"),
|
52 |
-
gr.Image(type="numpy", label="
|
53 |
],
|
54 |
title="Denoising Autoencoder Demo",
|
55 |
description=(
|
56 |
"Upload any image (grayscale or color). "
|
57 |
-
"This
|
58 |
-
"then
|
59 |
-
"See all three side by side!"
|
60 |
)
|
61 |
)
|
62 |
|
|
|
5 |
import tensorflow as tf
|
6 |
import gradio as gr
|
7 |
|
8 |
+
# Load your trained model without trying to restore the old optimizer/loss,
|
9 |
+
# then re-compile it so we can call predict()
|
10 |
+
model = tf.keras.models.load_model("best_model.h5", compile=False)
|
11 |
+
model.compile(optimizer="adam", loss="mse")
|
12 |
|
|
|
13 |
def process_and_denoise(image):
|
14 |
"""
|
15 |
+
Takes any input image (color or grayscale), converts it to 64×64 grayscale,
|
16 |
+
adds Gaussian noise, and returns:
|
17 |
+
1) the resized original
|
18 |
+
2) the noisy version
|
19 |
+
3) the model’s denoised reconstruction
|
20 |
"""
|
21 |
+
# If color, convert to gray; otherwise accept 1-channel
|
|
|
22 |
if image.ndim == 3 and image.shape[2] == 3:
|
23 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
24 |
else:
|
25 |
+
# Could be (H,W,1) or (H,W)
|
26 |
+
gray = image[..., 0] if image.ndim == 3 else image
|
27 |
|
28 |
+
# Resize and normalize
|
29 |
+
orig = cv2.resize(gray, (64, 64))
|
30 |
orig_norm = orig.astype(np.float32) / 255.0
|
31 |
|
32 |
+
# Add Gaussian noise
|
33 |
+
sigma = 0.1
|
34 |
noisy = orig_norm + sigma * np.random.randn(*orig_norm.shape)
|
35 |
noisy = np.clip(noisy, 0.0, 1.0)
|
36 |
|
37 |
+
# Denoise via the autoencoder
|
38 |
+
inp = noisy[np.newaxis, ..., np.newaxis] # shape (1,64,64,1)
|
39 |
+
pred = model.predict(inp)[0, ..., 0] # shape (64,64)
|
40 |
|
41 |
+
# Convert back to uint8 for display
|
42 |
+
orig_disp = (orig_norm * 255).astype(np.uint8)
|
43 |
+
noisy_disp = (noisy * 255).astype(np.uint8)
|
44 |
+
recon_disp = (pred * 255).astype(np.uint8)
|
45 |
|
46 |
return orig_disp, noisy_disp, recon_disp
|
47 |
|
|
|
48 |
demo = gr.Interface(
|
49 |
fn=process_and_denoise,
|
50 |
inputs=gr.Image(type="numpy", label="Input Image"),
|
51 |
outputs=[
|
52 |
gr.Image(type="numpy", label="Original (64×64)"),
|
53 |
gr.Image(type="numpy", label="Noisy (σ=0.1)"),
|
54 |
+
gr.Image(type="numpy", label="Denoised Output")
|
55 |
],
|
56 |
title="Denoising Autoencoder Demo",
|
57 |
description=(
|
58 |
"Upload any image (grayscale or color). "
|
59 |
+
"This will convert it to 64×64 grayscale, add Gaussian noise, "
|
60 |
+
"and then denoise it with the trained autoencoder."
|
|
|
61 |
)
|
62 |
)
|
63 |
|