sanjay-subramanya commited on
Commit
b89dd42
1 Parent(s): d027ab4

Create application.py

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