Avkash commited on
Commit
b84b28c
·
1 Parent(s): ec415e6

Create app.py

Browse files

Created the app.py

Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import 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
+ import gradio as gr
14
+
15
+ def jaccard_coef(y_true, y_pred):
16
+ y_true_flatten = K.flatten(y_true)
17
+ y_pred_flatten = K.flatten(y_pred)
18
+ intersection = K.sum(y_true_flatten * y_pred_flatten)
19
+ final_coef_value = (intersection + 1.0) / (K.sum(y_true_flatten) + K.sum(y_pred_flatten) - intersection + 1.0)
20
+ return final_coef_value
21
+
22
+
23
+ weights = [0.1666, 0.1666, 0.1666, 0.1666, 0.1666, 0.1666]
24
+ dice_loss = sm.losses.DiceLoss(class_weights = weights)
25
+ focal_loss = sm.losses.CategoricalFocalLoss()
26
+ total_loss = dice_loss + (1 * focal_loss)
27
+
28
+
29
+ satellite_model = load_model('model/satellite-imagery.h5', custom_objects=({'dice_loss_plus_1focal_loss': total_loss, 'jaccard_coef': jaccard_coef}))
30
+
31
+ def process_input_image(image_source):
32
+ image = np.expand_dims(image_source, 0)
33
+
34
+ prediction = satellite_model.predict(image)
35
+ predicted_image = np.argmax(prediction, axis=3)
36
+
37
+ predicted_image = predicted_image[0,:,:]
38
+ predicted_image = predicted_image * 50
39
+ return 'Predicted Masked Image', predicted_image
40
+
41
+
42
+ my_app = gr.Blocks()
43
+
44
+ with my_app:
45
+ gr.Markdown("Statellite Image Segmentation Application UI with Gradio")
46
+ with gr.Tabs():
47
+ with gr.TabItem("Select your image"):
48
+ with gr.Row():
49
+ with gr.Column():
50
+ img_source = gr.Image(label="Please select source Image", shape=(256, 256))
51
+ source_image_loader = gr.Button("Load above Image")
52
+ with gr.Column():
53
+ output_label = gr.Label(label="Image Info")
54
+ img_output = gr.Image(label="Image Output")
55
+ source_image_loader.click(
56
+ process_input_image,
57
+ [
58
+ img_source
59
+ ],
60
+ [
61
+ output_label,
62
+ img_output
63
+ ]
64
+ )
65
+
66
+
67
+ my_app.launch(debug=True)