isana25 commited on
Commit
4a285bb
·
verified ·
1 Parent(s): e282cf9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -5,59 +5,63 @@ import numpy as np
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
 
 
5
  import tensorflow as tf
6
  import gradio as gr
7
 
8
+ # Load the trained model without restoring old optimizer/loss, then re-compile
 
9
  model = tf.keras.models.load_model("best_model.h5", compile=False)
10
  model.compile(optimizer="adam", loss="mse")
11
 
12
  def process_and_denoise(image):
13
  """
14
  Takes any input image (color or grayscale), converts it to 64×64 grayscale,
15
+ adds Gaussian noise, runs the autoencoder, and returns all three stages
16
+ upsampled to 128×128 for clearer display:
17
+ 1) Original resized
18
+ 2) Noisy version
19
+ 3) Denoised reconstruction
20
  """
21
+ # Convert to single-channel grayscale
22
  if image.ndim == 3 and image.shape[2] == 3:
23
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
24
  else:
 
25
  gray = image[..., 0] if image.ndim == 3 else image
26
 
27
+ # Resize down to 64×64 for the model
28
+ small = cv2.resize(gray, (64, 64))
29
+ norm = small.astype(np.float32) / 255.0
30
 
31
  # Add Gaussian noise
32
  sigma = 0.1
33
+ noisy = norm + sigma * np.random.randn(*norm.shape)
34
  noisy = np.clip(noisy, 0.0, 1.0)
35
 
36
+ # Denoise with the autoencoder
37
+ inp = noisy[np.newaxis, ..., np.newaxis] # (1,64,64,1)
38
+ pred = model.predict(inp)[0, ..., 0] # (64,64)
39
 
40
+ # Convert back to uint8
41
+ orig_disp = (norm * 255).astype(np.uint8)
42
+ noisy_disp = (noisy * 255).astype(np.uint8)
43
+ recon_disp = (pred * 255).astype(np.uint8)
44
 
45
+ # Upsample each to 128×128 for display
46
+ def upsample(img):
47
+ return cv2.resize(img, (128, 128), interpolation=cv2.INTER_LINEAR)
48
+
49
+ return upsample(orig_disp), upsample(noisy_disp), upsample(recon_disp)
50
 
51
  demo = gr.Interface(
52
  fn=process_and_denoise,
53
  inputs=gr.Image(type="numpy", label="Input Image"),
54
  outputs=[
55
+ gr.Image(type="numpy", label="Original ▶ 128×128"),
56
+ gr.Image(type="numpy", label="Noisy (σ=0.1) ▶ 128×128"),
57
+ gr.Image(type="numpy", label="Denoised ▶ 128×128")
58
  ],
59
  title="Denoising Autoencoder Demo",
60
  description=(
61
+ "Upload any image (grayscale or color).\n"
62
+ "- Internally resized to 64×64 for denoising.\n"
63
+ "- Adds Gaussian noise (σ=0.1).\n"
64
+ "- Displays all stages upsampled to 128×128 for clearer viewing."
65
  )
66
  )
67