AndrewMaru commited on
Commit
e067be4
·
verified ·
1 Parent(s): 417c9c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -28
app.py CHANGED
@@ -8,7 +8,6 @@ st.title("🌊 Flood Segmentation with U-Net")
8
  # 1. Load Model
9
  @st.cache_resource
10
  def load_model():
11
- # Load model mentah (Raw SavedModel)
12
  model = tf.saved_model.load("unet_savedmodel")
13
  return model
14
 
@@ -22,32 +21,39 @@ if uploaded:
22
  img = Image.open(uploaded).convert("RGB")
23
  img_resized = img.resize((256, 256))
24
 
 
25
  x = np.array(img_resized, dtype=np.float32) / 255.0
26
- x = np.expand_dims(x, axis=0) # Menambah batch dimension
27
-
28
- # 3. Prediksi (PERBAIKAN UTAMA DISINI)
29
- # Ambil fungsi inferensi spesifik dari dalam model
30
- infer = model.signatures["serving_default"]
31
-
32
- # Jalankan inferensi (input harus dijadikan Tensor constant)
33
- output_dict = infer(tf.constant(x))
34
-
35
- # Hasilnya adalah dictionary (misal: {'conv2d_output': tensor}).
36
- # Kita ambil value pertama dari dictionary tersebut.
37
- pred_tensor = list(output_dict.values())[0]
38
-
39
- # Konversi Tensor ke Numpy
40
- pred = pred_tensor.numpy()[0, :, :, 0]
41
 
42
- # 4. Post-processing
43
- mask = (pred > 0.5).astype(np.uint8) * 255
44
- mask_img = Image.fromarray(mask)
45
-
46
- # 5. Tampilkan Hasil
47
- col1, col2 = st.columns(2)
48
- with col1:
49
- st.write("Original Image")
50
- st.image(img, use_container_width=True)
51
- with col2:
52
- st.write("Segmentation Mask")
53
- st.image(mask_img, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # 1. Load Model
9
  @st.cache_resource
10
  def load_model():
 
11
  model = tf.saved_model.load("unet_savedmodel")
12
  return model
13
 
 
21
  img = Image.open(uploaded).convert("RGB")
22
  img_resized = img.resize((256, 256))
23
 
24
+ # Normalisasi
25
  x = np.array(img_resized, dtype=np.float32) / 255.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # --- PERBAIKAN KHUSUS UNTUK ERROR 3 VS 6 ---
28
+ # Model kamu butuh 6 channel (kemungkinan Before + After image).
29
+ # Karena cuma ada 1 gambar, kita gabung gambar ini dengan dirinya sendiri
30
+ # agar menjadi 6 channel (Mocking the second image).
31
+ x = np.concatenate([x, x], axis=-1)
32
+ # Sekarang shape x adalah (256, 256, 6)
33
+ # -------------------------------------------
34
+
35
+ x = np.expand_dims(x, axis=0) # Menambah batch dimension menjadi (1, 256, 256, 6)
36
+
37
+ # 3. Prediksi
38
+ try:
39
+ infer = model.signatures["serving_default"]
40
+ output_dict = infer(tf.constant(x))
41
+ pred_tensor = list(output_dict.values())[0]
42
+ pred = pred_tensor.numpy()[0, :, :, 0]
43
+
44
+ # 4. Post-processing
45
+ mask = (pred > 0.5).astype(np.uint8) * 255
46
+ mask_img = Image.fromarray(mask)
47
+
48
+ # 5. Tampilkan Hasil
49
+ col1, col2 = st.columns(2)
50
+ with col1:
51
+ st.write("Original Image")
52
+ st.image(img, use_container_width=True)
53
+ with col2:
54
+ st.write("Segmentation Mask")
55
+ st.image(mask_img, use_container_width=True)
56
+
57
+ except Exception as e:
58
+ st.error(f"Terjadi error saat prediksi: {e}")
59
+ st.write("Debug Info - Input Shape:", x.shape)