Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
from patchify import patchify
|
6 |
+
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
7 |
+
|
8 |
+
from matplotlib import pyplot as plt
|
9 |
+
import random
|
10 |
+
|
11 |
+
from keras.utils import to_categorical
|
12 |
+
from keras import backend as K
|
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 |
+
|
19 |
+
intersection = K.sum(y_true_flatten*y_pred_flatten) + 1.0
|
20 |
+
union = K.sum(y_true_flatten) + K.sum(y_pred_flatten) - intersection + 1.0
|
21 |
+
iou = intersection / union
|
22 |
+
|
23 |
+
return iou
|
24 |
+
|
25 |
+
weights = [0.166,0.166,0.166,0.166,0.166,0.166]
|
26 |
+
|
27 |
+
import segmentation_models as sm
|
28 |
+
|
29 |
+
dice_loss = sm.losses.DiceLoss(class_weights = weights)
|
30 |
+
|
31 |
+
|
32 |
+
focal_loss = sm.losses.CategoricalFocalLoss()
|
33 |
+
|
34 |
+
total_loss = dice_loss + (1 * focal_loss)
|
35 |
+
|
36 |
+
|
37 |
+
from keras.models import load_model
|
38 |
+
|
39 |
+
|
40 |
+
saved_model = load_model('model/satellite_segmentation_full.h5',
|
41 |
+
custom_objects=({'dice_loss_plus_1focal_loss': total_loss,
|
42 |
+
'jaccard_coef': jaccard_coef}))
|
43 |
+
|
44 |
+
|
45 |
+
def process_input_image(image_source):
|
46 |
+
image = np.expand_dims(image_source, 0)
|
47 |
+
|
48 |
+
prediction = saved_model.predict(image)
|
49 |
+
predicted_image = np.argmax(prediction, axis=3)
|
50 |
+
|
51 |
+
predicted_image = predicted_image[0,:,:]
|
52 |
+
predicted_image = predicted_image * 50
|
53 |
+
return 'Predicted Masked Image', predicted_image
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
my_app = gr.Blocks()
|
58 |
+
|
59 |
+
|
60 |
+
with my_app:
|
61 |
+
gr.Markdown("Statellite Image Segmentation Application UI with Gradio")
|
62 |
+
with gr.Tabs():
|
63 |
+
with gr.TabItem("Select your image"):
|
64 |
+
with gr.Row():
|
65 |
+
with gr.Column():
|
66 |
+
img_source = gr.Image(label="Please select source Image", shape=(256, 256))
|
67 |
+
source_image_loader = gr.Button("Load above Image")
|
68 |
+
with gr.Column():
|
69 |
+
output_label = gr.Label(label="Image Info")
|
70 |
+
img_output = gr.Image(label="Image Output")
|
71 |
+
source_image_loader.click(
|
72 |
+
process_input_image,
|
73 |
+
[
|
74 |
+
img_source
|
75 |
+
],
|
76 |
+
[
|
77 |
+
output_label,
|
78 |
+
img_output
|
79 |
+
]
|
80 |
+
)
|
81 |
+
|
82 |
+
|
83 |
+
my_app.launch(debug=True)
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|