Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
| 6 |
+
import segmentation_models as sm
|
| 7 |
+
from matplotlib import pyplot as plt
|
| 8 |
+
import random
|
| 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 |
+
weights = [0.1666, 0.1666, 0.1666, 0.1666, 0.1666, 0.1666]
|
| 23 |
+
dice_loss = sm.losses.DiceLoss(class_weights = weights)
|
| 24 |
+
focal_loss = sm.losses.CategoricalFocalLoss()
|
| 25 |
+
total_loss = dice_loss + (1 * focal_loss)
|
| 26 |
+
|
| 27 |
+
satellite_model = load_model('model/satellite-imagery.h5',
|
| 28 |
+
custom_objects=({'dice_loss_plus_1focal_loss': total_loss,
|
| 29 |
+
'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 |
+
my_app = gr.Blocks()
|
| 42 |
+
|
| 43 |
+
with my_app:
|
| 44 |
+
gr.Markdown("Statellite Image Segmentation Application UI with Gradio")
|
| 45 |
+
with gr.Tabs():
|
| 46 |
+
with gr.TabItem("Select your image"):
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column():
|
| 49 |
+
img_source = gr.Image(label="Please select source Image", shape=(256, 256))
|
| 50 |
+
source_image_loader = gr.Button("Load above Image")
|
| 51 |
+
with gr.Column():
|
| 52 |
+
output_label = gr.Label(label="Image Info")
|
| 53 |
+
img_output = gr.Image(label="Image Output")
|
| 54 |
+
source_image_loader.click(
|
| 55 |
+
process_input_image,
|
| 56 |
+
[
|
| 57 |
+
img_source
|
| 58 |
+
],
|
| 59 |
+
[
|
| 60 |
+
output_label,
|
| 61 |
+
img_output
|
| 62 |
+
]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
my_app.launch(debug=True)
|