pemujo commited on
Commit
67de374
·
1 Parent(s): ef1dbfe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -4
app.py CHANGED
@@ -1,12 +1,46 @@
1
  import gradio as gr
2
  import tensorflow as tf
 
 
 
3
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- def greet(name):
6
- return "Hello " + name + "!!"
 
 
 
 
 
7
 
8
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
9
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
 
12
  # first we need to load the model from somewhere - probably by using model.load on a keras file (which could be saved in our huggingface space repo)
 
1
  import gradio as gr
2
  import tensorflow as tf
3
+ from keras.models import load_model
4
+ import segmentation_models as sm
5
+ from keras.metrics import MeanIoU
6
 
7
+ def jaccard_coef(y_true, y_pred):
8
+ """
9
+ Defines custom jaccard coefficient metric
10
+ """
11
+
12
+ y_true_flatten = K.flatten(y_true)
13
+ y_pred_flatten = K.flatten(y_pred)
14
+ intersection = K.sum(y_true_flatten * y_pred_flatten)
15
+ final_coef_value = (intersection + 1.0) / (K.sum(y_true_flatten) + K.sum(y_pred_flatten) - intersection + 1.0)
16
+ return final_coef_value
17
 
18
+ def real_dice_coeff(y_true, y_pred):
19
+ smooth = 0.0001
20
+ y_true_flatten = K.flatten(y_true)
21
+ y_pred_flatten = K.flatten(y_pred)
22
+ intersection = K.sum(y_true_flatten * y_pred_flatten)
23
+ dice_score = (2.0 * intersection + smooth) / (K.sum(y_true_flatten) + K.sum(y_pred_flatten) + smooth)
24
+ return dice_score
25
 
26
+
27
+ dice_loss = sm.losses.DiceLoss(class_weights = weights)
28
+ focal_loss = sm.losses.CategoricalFocalLoss()
29
+ TOTAL_LOSS_FACTOR = 5
30
+ total_loss = dice_loss + (TOTAL_LOSS_FACTOR * focal_loss)
31
+
32
+ metrics = [real_dice_coeff, tf.keras.metrics.MeanIoU(num_classes=2, sparse_y_true= False, sparse_y_pred=False, name="Mean IOU"), "accuracy", jaccard_coef, sm.metrics.FScore(threshold=0.6, name="Dice Coeficient")]
33
+
34
+
35
+ model = load_model('../../../fast-disk/w210-capstone/models/' + model_name, custom_objects={'dice_loss_plus_5focal_loss': total_loss, 'jaccard_coef': jaccard_coef, 'IOU score' : sm.metrics.IOUScore(threshold=0.9, name="IOU score"), 'Dice Coeficient' : sm.metrics.FScore(threshold=0.6, name="Dice Coeficient")}, compile=False)
36
+ model.compile(metrics=metrics)
37
+
38
+
39
+ # def greet(name):
40
+ # return "Hello " + name + "!!"
41
+
42
+ # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
43
+ # iface.launch()
44
 
45
 
46
  # first we need to load the model from somewhere - probably by using model.load on a keras file (which could be saved in our huggingface space repo)