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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -5,58 +5,59 @@ import numpy as np
5
  import tensorflow as tf
6
  import gradio as gr
7
 
8
- # 1. Load your trained model
9
- model = tf.keras.models.load_model("best_model.h5")
 
 
10
 
11
- # 2. Denoising pipeline function
12
  def process_and_denoise(image):
13
  """
14
- Input: HxW grayscale or color image (numpy array).
15
- Outputs: (orig, noisy, denoised) all as 64×64 uint8 arrays.
 
 
 
16
  """
17
- # --- Preprocess Original ---
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
- # could be (H,W,1) or (H,W)
23
- gray = image[...,0] if image.ndim == 3 else image
24
 
25
- # Resize to 64×64
26
- orig = cv2.resize(gray, (64,64))
27
  orig_norm = orig.astype(np.float32) / 255.0
28
 
29
- # --- Add Gaussian Noise ---
30
- sigma = 0.1 # same noise level you trained with
31
  noisy = orig_norm + sigma * np.random.randn(*orig_norm.shape)
32
  noisy = np.clip(noisy, 0.0, 1.0)
33
 
34
- # --- Denoise with the autoencoder ---
35
- inp = noisy[np.newaxis, ..., np.newaxis] # shape (1,64,64,1)
36
- pred = model.predict(inp)[0, ..., 0] # shape (64,64)
37
 
38
- # --- Convert all to uint8 for display ---
39
- orig_disp = (orig_norm * 255).astype(np.uint8)
40
- noisy_disp = (noisy * 255).astype(np.uint8)
41
- recon_disp = (pred * 255).astype(np.uint8)
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="Reconstructed")
53
  ],
54
  title="Denoising Autoencoder Demo",
55
  description=(
56
  "Upload any image (grayscale or color). "
57
- "This app converts it to 64×64 grayscale, adds Gaussian noise, "
58
- "then denoises it with the trained autoencoder. "
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