ombui commited on
Commit
1569c10
·
1 Parent(s): 4873cba

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ mport os
2
+ import cv2
3
+ from PIL import Image
4
+ import numpy as np
5
+ import segmentation_models as sm
6
+ from matplotlib import pyplot as plt
7
+ import random
8
+
9
+
10
+ from keras import backend as K
11
+ from keras.models import load_model
12
+
13
+
14
+ import gradio as gr
15
+
16
+
17
+ def jaccard_coef(y_true, y_pred):
18
+ y_true_flatten = K.flatten(y_true)
19
+ y_pred_flatten = K.flatten(y_pred)
20
+ intersection = K.sum(y_true_flatten * y_pred_flatten)
21
+ final_coef_value = (intersection + 1.0) / (K.sum(y_true_flatten) + K.sum(y_pred_flatten) - intersection + 1.0)
22
+ return final_coef_value
23
+
24
+ weights = [0.2,0.2,0.2,0.2,0.2]
25
+ dice_loss = sm.losses.DiceLoss(class_weights = weights)
26
+ focal_loss = sm.losses.CategoricalFocalLoss()
27
+ total_loss = dice_loss + (1 * focal_loss)
28
+
29
+ satellite_model = load_model('model/C:/Users/sa/Desktop/Model_Training/satellite_segmentation_full.h5',
30
+ custom_objects=({'dice_loss_plus_1focal_loss': total_loss,|'jaccard_coef': jaccard_coef}))
31
+
32
+ def process_input_image(image_source):
33
+ image = np.expand_dims(image_source, 0)
34
+
35
+ prediction = satellite_model.predict(image)
36
+ predicted_colored = np.argmax(prediction, axis=3)
37
+
38
+ predicted_colored = predicted_colored[0,:,:]
39
+ predicted_colored = predicted_colored * 50
40
+ return 'Predicted Masked Image', predicted_colored
41
+
42
+
43
+ my_app = gr.Blocks()
44
+
45
+
46
+ with my_app:
47
+ gr.Markdown("Statellite Image Segmentation Application UI with Gradio")
48
+ with gr.Tabs():
49
+ with gr.TabItem("Select your image"):
50
+ with gr.Row():
51
+ with gr.Column():
52
+ img_source = gr.Image(label="Please select source Image", shape=(256, 256))
53
+ source_image_loader = gr.Button("Load above Image")
54
+ with gr.Column():
55
+ output_label = gr.Label(label="Image Info")
56
+ img_output = gr.Image(label="Image Output")
57
+ source_image_loader.click(
58
+ process_input_image,
59
+ [
60
+ img_source
61
+ ],
62
+ [
63
+ output_label,
64
+ img_output
65
+ ]
66
+ )
67
+
68
+
69
+ my_app.launch(debug=True)