veronhii commited on
Commit
ae16aa7
·
1 Parent(s): abbfcd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -10,23 +10,26 @@ with open("classlabel.txt", 'r') as file:
10
  CLASS_LABEL = [x.strip() for x in file.readlines()]
11
 
12
  def normalize_image(img):
13
- img = tf.cast(img, tf.float32)/255.
14
  img = tf.image.resize(img, (IMG_HEIGHT, IMG_WIDTH), method='bilinear')
15
  return img
16
 
17
-
18
- def predict_fn(img):
19
  img = img.convert('RGB')
20
  img_data = normalize_image(img)
21
  x = np.array(img_data)
22
  x = np.expand_dims(x, axis=0)
23
  temp = model.predict(x)
24
-
25
- class_index = np.argmax(temp)
26
- return CLASS_LABEL[class_index]
27
 
 
 
 
 
 
 
 
28
 
29
  model = tf.keras.models.load_model("Xception.h5")
30
 
31
- interface = gr.Interface(predict_fn, gr.inputs.Image(type='pil'), outputs='label')
32
- interface.launch()
 
10
  CLASS_LABEL = [x.strip() for x in file.readlines()]
11
 
12
  def normalize_image(img):
13
+ img = tf.cast(img, tf.float32) / 255.
14
  img = tf.image.resize(img, (IMG_HEIGHT, IMG_WIDTH), method='bilinear')
15
  return img
16
 
17
+ def predict_top_classes(img, num_top_classes=5):
 
18
  img = img.convert('RGB')
19
  img_data = normalize_image(img)
20
  x = np.array(img_data)
21
  x = np.expand_dims(x, axis=0)
22
  temp = model.predict(x)
 
 
 
23
 
24
+ top_class_indices = np.argpartition(temp, -num_top_classes)[-num_top_classes:]
25
+ top_class_indices = top_class_indices[np.argsort(temp[0, top_class_indices])[::-1]]
26
+
27
+ top_classes = [CLASS_LABEL[i] for i in top_class_indices]
28
+ top_probabilities = [temp[0, i] for i in top_class_indices]
29
+
30
+ return dict(zip(top_classes, top_probabilities))
31
 
32
  model = tf.keras.models.load_model("Xception.h5")
33
 
34
+ interface = gr.Interface(predict_top_classes, gr.inputs.Image(type='pil'), outputs='dictionary', args={'num_top_classes': 5})
35
+ interface.launch()