Spaces:
Runtime error
Runtime error
Commit
·
b9cc85a
1
Parent(s):
ee7152a
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from keras import backend as K
|
10 |
+
from keras.models import load_model
|
11 |
+
|
12 |
+
import gradio as gr
|
13 |
+
|
14 |
+
def jaccard_coef(y_true, y_pred):
|
15 |
+
y_true_flatten = K.flatten(y_true)
|
16 |
+
y_pred_flatten = K.flatten(y_pred)
|
17 |
+
intersection = K.sum(y_true_flatten * y_pred_flatten)
|
18 |
+
final_coef_value = (intersection + 1.0) / (K.sum(y_true_flatten) + K.sum(y_pred_flatten) - intersection + 1.0)
|
19 |
+
return final_coef_value
|
20 |
+
|
21 |
+
weights = [0.1666, 0.1666, 0.1666, 0.1666, 0.1666, 0.1666]
|
22 |
+
dice_loss = sm.losses.DiceLoss(class_weights = weights)
|
23 |
+
focal_loss = sm.losses.CategoricalFocalLoss()
|
24 |
+
total_loss = dice_loss + (1 * focal_loss)
|
25 |
+
|
26 |
+
|
27 |
+
satellite_model = load_model('model/model_checkpoint.h5',custom_objects=({'dice_loss_plus_1focal_loss': total_loss, 'jaccard_coef': jaccard_coef}))
|
28 |
+
|
29 |
+
|
30 |
+
def process_input_image(image_source):
|
31 |
+
image = np.expand_dims(image_source, 0)
|
32 |
+
|
33 |
+
prediction = satellite_model.predict(image)
|
34 |
+
predicted_image = np.argmax(prediction, axis=3)
|
35 |
+
|
36 |
+
predicted_image = predicted_image[0,:,:]
|
37 |
+
predicted_image = predicted_image * 50
|
38 |
+
return 'Predicted Masked Image', predicted_image
|
39 |
+
|
40 |
+
|
41 |
+
my_app = gr.Blocks()
|
42 |
+
|
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)
|
68 |
+
|