Spaces:
Runtime error
Runtime error
yuragoithf
commited on
Commit
•
d905150
1
Parent(s):
53b1dfc
Upload 7 files
Browse files- 00371aa92.jpg +0 -0
- 0038cbe45.jpg +0 -0
- 003b48a9e.jpg +0 -0
- 003b50a15.jpg +0 -0
- 003e2c95d.jpg +0 -0
- app.py +82 -0
- requirements.txt +5 -0
00371aa92.jpg
ADDED
0038cbe45.jpg
ADDED
003b48a9e.jpg
ADDED
003b50a15.jpg
ADDED
003e2c95d.jpg
ADDED
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import gdown
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
import os
|
7 |
+
import cv2
|
8 |
+
import numpy as np
|
9 |
+
import keras.backend as K
|
10 |
+
|
11 |
+
|
12 |
+
#from tensorflow import keras
|
13 |
+
|
14 |
+
input_shape = (32, 32, 3)
|
15 |
+
resized_shape = (768, 768, 3)
|
16 |
+
IMG_SCALING = (1, 1)
|
17 |
+
|
18 |
+
|
19 |
+
# Download the model file
|
20 |
+
def download_model():
|
21 |
+
url = "https://drive.google.com/uc?id=1FhICkeGn6GcNXWTDn1s83ctC-6Mo1UXk"
|
22 |
+
output = "seg_unet_model.h5"
|
23 |
+
gdown.download(url, output, quiet=False)
|
24 |
+
return output
|
25 |
+
|
26 |
+
model_file = download_model()
|
27 |
+
|
28 |
+
#Custom objects for model
|
29 |
+
|
30 |
+
def Combo_loss(y_true, y_pred, eps=1e-9, smooth=1):
|
31 |
+
targets = tf.dtypes.cast(K.flatten(y_true), tf.float32)
|
32 |
+
inputs = tf.dtypes.cast(K.flatten(y_pred), tf.float32)
|
33 |
+
|
34 |
+
intersection = K.sum(targets * inputs)
|
35 |
+
dice = (2. * intersection + smooth) / (K.sum(targets) + K.sum(inputs) + smooth)
|
36 |
+
inputs = K.clip(inputs, eps, 1.0 - eps)
|
37 |
+
out = - (ALPHA * ((targets * K.log(inputs)) + ((1 - ALPHA) * (1.0 - targets) * K.log(1.0 - inputs))))
|
38 |
+
weighted_ce = K.mean(out, axis=-1)
|
39 |
+
combo = (CE_RATIO * weighted_ce) - ((1 - CE_RATIO) * dice)
|
40 |
+
return combo
|
41 |
+
|
42 |
+
def dice_coef(y_true, y_pred, smooth=1):
|
43 |
+
y_pred = tf.dtypes.cast(y_pred, tf.int32)
|
44 |
+
y_true = tf.dtypes.cast(y_true, tf.int32)
|
45 |
+
intersection = K.sum(y_true * y_pred, axis=[1,2,3])
|
46 |
+
union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3])
|
47 |
+
return K.mean((2 * intersection + smooth) / (union + smooth), axis=0)
|
48 |
+
|
49 |
+
# Load the model
|
50 |
+
seg_model = tf.keras.models.load_model('seg_unet_model.h5', custom_objects={'Combo_loss': Combo_loss, 'dice_coef': dice_coef}
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
inputs = gr.inputs.Image(type="pil", label="Upload an image")
|
57 |
+
# outputs = gr.outputs.HTML() #uncomment for single class output
|
58 |
+
|
59 |
+
def gen_pred(img=inputs, model=seg_model):
|
60 |
+
#rgb_path = os.path.join(test_image_dir,img)
|
61 |
+
#img = cv2.imread(rgb_path)
|
62 |
+
img = img[::IMG_SCALING[0], ::IMG_SCALING[1]]
|
63 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
64 |
+
img = img/255
|
65 |
+
img = tf.expand_dims(img, axis=0)
|
66 |
+
pred = model.predict(img)
|
67 |
+
pred = np.squeeze(pred, axis=0)
|
68 |
+
return inputs, pred
|
69 |
+
|
70 |
+
|
71 |
+
title = "<h1 style='text-align: center;'>Semantic Segmentation</h1>"
|
72 |
+
description = "Upload an image and get prediction mask"
|
73 |
+
# css_code='body{background-image:url("file=wave.mp4");}'
|
74 |
+
|
75 |
+
gr.Interface(fn=gen_pred,
|
76 |
+
inputs=inputs,
|
77 |
+
outputs="image",
|
78 |
+
title=title,
|
79 |
+
examples=[["003e2c95d.jpg"], ["003b50a15.jpg"], ["003b48a9e.jpg"], ["0038cbe45.jpg"], ["00371aa92.jpg"]],
|
80 |
+
# css=css_code,
|
81 |
+
description=description,
|
82 |
+
enable_queue=True).launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow
|
2 |
+
gdown
|
3 |
+
gradio
|
4 |
+
os
|
5 |
+
pillow
|