Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,62 +6,38 @@ 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 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 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_segmentation_full.h5',custom_objects=({'dice_loss_plus_1focal_loss': total_loss,'jaccard_coef': jaccard_coef}))
|
| 30 |
|
| 31 |
def process_input_image(image_source):
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
prediction = satellite_model.predict(image)
|
| 35 |
-
predicted_colored = np.argmax(prediction, axis=3)
|
| 36 |
-
|
| 37 |
-
predicted_colored = predicted_colored[0,:,:]
|
| 38 |
-
predicted_colored = predicted_colored * 50
|
| 39 |
-
return 'Predicted Masked Image', predicted_colored
|
| 40 |
-
|
| 41 |
-
my_app = gr.Blocks()
|
| 42 |
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 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(
|
|
|
|
| 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.2, 0.2, 0.2, 0.2, 0.2]
|
| 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 |
+
satellite_model = load_model('model/satellite_segmentation_full.h5', custom_objects={'dice_loss_plus_1focal_loss': total_loss, 'jaccard_coef': jaccard_coef})
|
|
|
|
| 27 |
|
| 28 |
def process_input_image(image_source):
|
| 29 |
+
image = np.expand_dims(image_source, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
prediction = satellite_model.predict(image)
|
| 32 |
+
predicted_colored = np.argmax(prediction, axis=3)
|
| 33 |
|
| 34 |
+
predicted_colored = predicted_colored[0,:,:]
|
| 35 |
+
predicted_colored = predicted_colored * 50
|
| 36 |
+
return 'Predicted Masked Image', predicted_colored
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
my_app = gr.Interface(fn=process_input_image,
|
| 39 |
+
inputs=gr.inputs.Image(label="Please select the source image", shape=(256, 256)),
|
| 40 |
+
outputs="image",
|
| 41 |
+
title="Satellite Image Segmentation Application UI with Gradio")
|
| 42 |
|
| 43 |
+
my_app.launch()
|