badenlt commited on
Commit
4ee7112
·
verified ·
1 Parent(s): 8dd7bca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -23
app.py CHANGED
@@ -1,17 +1,18 @@
1
- import keras
2
  import numpy as np
3
- import gradio as gr
4
  from PIL import Image
 
5
 
6
  MODEL_PATH = "small32cnn_mlbt_mmat.keras"
7
- STATS_PATH = "preproc_stats_smallcnn.npz"
8
 
9
- # Load with standalone Keras 3 (matches the saver)
10
  model = keras.models.load_model(MODEL_PATH, compile=False)
11
 
12
  stats = np.load(STATS_PATH)
13
- MEAN = float(stats["MEAN"])
14
- STD = float(stats["STD"])
 
 
15
 
16
  CLASS_NAMES = ["MMAT", "MLBT"]
17
 
@@ -19,34 +20,28 @@ CLASS_NAMES = ["MMAT", "MLBT"]
19
 
20
  # ---- Preprocessing: Image -> (1, 32, 32, 1) normalized ----
21
  def preprocess(img: np.ndarray) -> np.ndarray:
22
- """
23
- img: H x W x C in [0, 255] from Gradio (numpy)
24
- returns: (1, 32, 32, 1) float32, z-score normalized
25
- """
26
-
27
- # Convert to grayscale
28
  if img.ndim == 3 and img.shape[2] == 3:
29
- # RGB -> grayscale
30
  img_gray = np.dot(img[..., :3], [0.2989, 0.5870, 0.1140])
31
  elif img.ndim == 3 and img.shape[2] == 1:
32
  img_gray = img[..., 0]
33
  elif img.ndim == 2:
34
  img_gray = img
35
  else:
36
- # fallback: take first channel
37
  img_gray = img[..., 0]
38
 
39
- # Resize to 32x32
40
  pil_img = Image.fromarray(img_gray.astype("uint8"))
41
  pil_img = pil_img.resize((32, 32), Image.BILINEAR)
42
  arr = np.array(pil_img).astype("float32")
43
 
44
- # z-score normalization using training stats
45
- arr = (arr - MEAN) / STD
 
 
 
 
 
 
46
 
47
- # Add batch and channel dims: (1, 32, 32, 1)
48
- arr = arr[None, ..., None]
49
- return arr
50
 
51
 
52
  # ---- Prediction function for Gradio ----
@@ -55,15 +50,16 @@ def predict(img: np.ndarray):
55
  raw = float(model.predict(x, verbose=0)[0, 0])
56
  print("Raw model output:", raw, flush=True)
57
 
58
- # raw ≈ P(MMAT)
59
- prob_mmat = raw
60
- prob_mlbt = 1.0 - prob_mmat
61
 
62
  return {"MLBT": prob_mlbt, "MMAT": prob_mmat}
63
 
64
 
65
 
66
 
 
67
  # ---- Gradio interface ----
68
  input_component = gr.Image(
69
  type="numpy",
 
 
1
  import numpy as np
2
+ import keras
3
  from PIL import Image
4
+ import gradio as gr
5
 
6
  MODEL_PATH = "small32cnn_mlbt_mmat.keras"
7
+ STATS_PATH = "jet_image_scale_and_stats.npz"
8
 
 
9
  model = keras.models.load_model(MODEL_PATH, compile=False)
10
 
11
  stats = np.load(STATS_PATH)
12
+ SCALE = float(stats["SCALE"])
13
+ MEAN = float(stats["MEAN"])
14
+ STD = float(stats["STD"])
15
+ print("Loaded SCALE/MEAN/STD:", SCALE, MEAN, STD, flush=True)
16
 
17
  CLASS_NAMES = ["MMAT", "MLBT"]
18
 
 
20
 
21
  # ---- Preprocessing: Image -> (1, 32, 32, 1) normalized ----
22
  def preprocess(img: np.ndarray) -> np.ndarray:
 
 
 
 
 
 
23
  if img.ndim == 3 and img.shape[2] == 3:
 
24
  img_gray = np.dot(img[..., :3], [0.2989, 0.5870, 0.1140])
25
  elif img.ndim == 3 and img.shape[2] == 1:
26
  img_gray = img[..., 0]
27
  elif img.ndim == 2:
28
  img_gray = img
29
  else:
 
30
  img_gray = img[..., 0]
31
 
 
32
  pil_img = Image.fromarray(img_gray.astype("uint8"))
33
  pil_img = pil_img.resize((32, 32), Image.BILINEAR)
34
  arr = np.array(pil_img).astype("float32")
35
 
36
+ # Invert the global scaling to approximate original X
37
+ arr_unscaled = arr / SCALE
38
+
39
+ # Now apply the same normalization as during training
40
+ arr_norm = (arr_unscaled - MEAN) / (STD + 1e-8)
41
+
42
+ arr_norm = arr_norm[None, ..., None]
43
+ return arr_norm
44
 
 
 
 
45
 
46
 
47
  # ---- Prediction function for Gradio ----
 
50
  raw = float(model.predict(x, verbose=0)[0, 0])
51
  print("Raw model output:", raw, flush=True)
52
 
53
+ # raw ≈ P(MLBT) as in training
54
+ prob_mlbt = raw
55
+ prob_mmat = 1.0 - prob_mlbt
56
 
57
  return {"MLBT": prob_mlbt, "MMAT": prob_mmat}
58
 
59
 
60
 
61
 
62
+
63
  # ---- Gradio interface ----
64
  input_component = gr.Image(
65
  type="numpy",