Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import gradio as gr
|
|
| 5 |
from datasets import load_dataset
|
| 6 |
import random
|
| 7 |
|
| 8 |
-
# Load pre-trained model
|
| 9 |
model = load_model("unet_model.h5", compile=False)
|
| 10 |
|
| 11 |
# Load Hugging Face dataset
|
|
@@ -19,41 +19,66 @@ def preprocess_image(image, target_size=(192, 176)):
|
|
| 19 |
image = np.expand_dims(image, axis=-1)
|
| 20 |
return np.expand_dims(image, axis=0)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Prediction function
|
| 23 |
-
def predict(img=None, use_dataset=False):
|
| 24 |
if use_dataset:
|
| 25 |
-
# Pick
|
| 26 |
example = random.choice(dataset["train"])
|
| 27 |
img = example["image"]
|
| 28 |
|
| 29 |
if img is None:
|
| 30 |
-
return None
|
| 31 |
|
| 32 |
img = img.convert("L")
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
pred = model.predict(input_data)[0]
|
| 37 |
|
| 38 |
if pred.ndim == 3 and pred.shape[-1] == 1:
|
| 39 |
pred = np.squeeze(pred, axis=-1)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
return
|
| 45 |
|
| 46 |
# Gradio Interface
|
| 47 |
interface = gr.Interface(
|
| 48 |
fn=predict,
|
| 49 |
inputs=[
|
| 50 |
gr.Image(type="pil", label="Upload Image"),
|
| 51 |
-
gr.Checkbox(label="Use Random Dataset Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
],
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"The U-Net model will provide a denoised output."
|
| 57 |
)
|
| 58 |
|
| 59 |
# Launch the app
|
|
|
|
| 5 |
from datasets import load_dataset
|
| 6 |
import random
|
| 7 |
|
| 8 |
+
# Load pre-trained U-Net model-
|
| 9 |
model = load_model("unet_model.h5", compile=False)
|
| 10 |
|
| 11 |
# Load Hugging Face dataset
|
|
|
|
| 19 |
image = np.expand_dims(image, axis=-1)
|
| 20 |
return np.expand_dims(image, axis=0)
|
| 21 |
|
| 22 |
+
# Salt-and-pepper noise function
|
| 23 |
+
def add_salt_and_pepper_noise(image, amount=0.05):
|
| 24 |
+
"""
|
| 25 |
+
image: PIL Image in grayscale ('L') or RGB
|
| 26 |
+
amount: fraction of pixels to corrupt
|
| 27 |
+
"""
|
| 28 |
+
img_array = np.array(image)
|
| 29 |
+
# Salt noise
|
| 30 |
+
num_salt = np.ceil(amount * img_array.size * 0.5)
|
| 31 |
+
coords = [np.random.randint(0, i - 1, int(num_salt)) for i in img_array.shape]
|
| 32 |
+
img_array[tuple(coords)] = 255
|
| 33 |
+
# Pepper noise
|
| 34 |
+
num_pepper = np.ceil(amount * img_array.size * 0.5)
|
| 35 |
+
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in img_array.shape]
|
| 36 |
+
img_array[tuple(coords)] = 0
|
| 37 |
+
return Image.fromarray(img_array)
|
| 38 |
+
|
| 39 |
# Prediction function
|
| 40 |
+
def predict(img=None, use_dataset=False, add_noise=False):
|
| 41 |
if use_dataset:
|
| 42 |
+
# Pick random image from dataset
|
| 43 |
example = random.choice(dataset["train"])
|
| 44 |
img = example["image"]
|
| 45 |
|
| 46 |
if img is None:
|
| 47 |
+
return None, None
|
| 48 |
|
| 49 |
img = img.convert("L")
|
| 50 |
|
| 51 |
+
noisy_img = img
|
| 52 |
+
if add_noise:
|
| 53 |
+
noisy_img = add_salt_and_pepper_noise(img)
|
| 54 |
+
|
| 55 |
+
input_data = preprocess_image(noisy_img)
|
| 56 |
|
| 57 |
pred = model.predict(input_data)[0]
|
| 58 |
|
| 59 |
if pred.ndim == 3 and pred.shape[-1] == 1:
|
| 60 |
pred = np.squeeze(pred, axis=-1)
|
| 61 |
|
| 62 |
+
denoised_img = (pred * 255).astype(np.uint8)
|
| 63 |
+
denoised_img = Image.fromarray(denoised_img)
|
| 64 |
|
| 65 |
+
return noisy_img, denoised_img
|
| 66 |
|
| 67 |
# Gradio Interface
|
| 68 |
interface = gr.Interface(
|
| 69 |
fn=predict,
|
| 70 |
inputs=[
|
| 71 |
gr.Image(type="pil", label="Upload Image"),
|
| 72 |
+
gr.Checkbox(label="Use Random Dataset Image"),
|
| 73 |
+
gr.Checkbox(label="Add Salt-and-Pepper Noise")
|
| 74 |
+
],
|
| 75 |
+
outputs=[
|
| 76 |
+
gr.Image(type="pil", label="Noisy Input Image"),
|
| 77 |
+
gr.Image(type="pil", label="Denoised Output Image")
|
| 78 |
],
|
| 79 |
+
title="U-Net Image Denoising with Salt-and-Pepper Noise",
|
| 80 |
+
description="Upload an image or pick a random image from the Cropped Yale Faces dataset. "
|
| 81 |
+
"Optionally add salt-and-pepper noise to the image before denoising."
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
# Launch the app
|