AbdullahImran commited on
Commit
dc12a50
·
verified ·
1 Parent(s): 861eba1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -1
app.py CHANGED
@@ -2,13 +2,29 @@ import gradio as gr
2
  import tensorflow as tf
3
  from PIL import Image
4
  import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Load models
7
  vgg16_model = tf.keras.models.load_model(
8
  "vgg16_best_model.keras"
9
  )
 
10
  xception_model = tf.keras.models.load_model(
11
- "xception_best.keras"
 
12
  )
13
 
14
 
 
2
  import tensorflow as tf
3
  from PIL import Image
4
  import numpy as np
5
+ import tensorflow as tf
6
+ import keras.backend as K
7
+
8
+
9
+ # Define focal loss
10
+ def focal_loss(gamma=2., alpha=0.25):
11
+ def focal_loss_fixed(y_true, y_pred):
12
+ y_pred = tf.clip_by_value(y_pred, K.epsilon(), 1. - K.epsilon())
13
+ cross_entropy = -y_true * tf.math.log(y_pred)
14
+ weight = alpha * y_true * tf.math.pow((1 - y_pred), gamma)
15
+ loss = weight * cross_entropy
16
+ return tf.reduce_sum(loss, axis=-1)
17
+ return focal_loss_fixed
18
+
19
 
20
  # Load models
21
  vgg16_model = tf.keras.models.load_model(
22
  "vgg16_best_model.keras"
23
  )
24
+
25
  xception_model = tf.keras.models.load_model(
26
+ 'xception_best.keras',
27
+ custom_objects={'focal_loss_fixed': focal_loss()}
28
  )
29
 
30