Nina-HK commited on
Commit
5dba2ed
1 Parent(s): ab0c74d

two interfaces

Browse files
Files changed (1) hide show
  1. app.py +38 -34
app.py CHANGED
@@ -16,50 +16,54 @@ from transformers import pipeline
16
  # load the model from the Hugging Face Model Hub
17
  #model = pipeline('image-classification', model='image_classification/densenet')
18
  #model = tf.keras.models.load_model("densenet")
19
- binary_model = tf.keras.models.load_model("densenet")
20
- binary_labels = {0: 'healthy', 1: 'patient'}
 
 
 
 
 
 
 
 
 
21
 
22
 
23
  #model = tf.keras.models.load_model('/content/drive/MyDrive/project_image_2023_NO/saved_models/saved_model/densenet')
24
  #labels = ['Healthy', 'Patient']
25
- labels = {0: 'healthy', 1: 'patient'}
26
 
27
- def classify_image(inp):
28
  inp = inp.reshape((-1, 224, 224, 3))
29
  inp = tf.keras.applications.densenet.preprocess_input(inp)
30
- prediction = model.predict(inp)
31
- confidences = {binary_labels[i]: float(prediction[0][i]) for i in range(2)}
32
- return confidences
 
33
 
34
 
35
- binary_interface = gr.Interface(fn=classify_image,
 
 
 
 
 
 
 
36
  inputs=gr.Image(shape=(224, 224)),
37
- outputs=gr.Label(num_top_classes = 2),
38
  title="Binary Image Classification",
39
- description="Here's a sample image classification. Enjoy!",
40
- examples=[['300104.png']])
41
-
42
- # Multi-Class Image Classification
43
-
44
- multi_model = pipeline('image-classification', model='densenet')
45
- multi_labels = multi_model.model.config.id2label
46
 
47
- def classify_multi_image(inp):
48
- prediction = multi_model(inp)
49
- confidences = {multi_labels[i]: float(prediction[0][i]) for i in range(len(multi_labels))}
50
- return confidences
51
-
52
- multi_interface = gr.Interface(fn=classify_multi_image,
53
- inputs=gr.Image(shape=(224, 224)),
54
- outputs=gr.Label(num_top_classes=len(multi_labels)),
55
- title="Multi-Class Image Classification",
56
- description="Classify an image into multiple classes",
57
- examples=[['300104.png']]
58
- )
59
- iface = gr.Interface([binary_interface, multi_interface],
60
- title="Image Classification App",
61
- layout="vertical",
62
- description="Demo for binary and multi-class image classification"
63
- )
64
 
65
- iface.launch()
 
 
16
  # load the model from the Hugging Face Model Hub
17
  #model = pipeline('image-classification', model='image_classification/densenet')
18
  #model = tf.keras.models.load_model("densenet")
19
+ #binary_model = tf.keras.models.load_model("densenet")
20
+ #binary_labels = {0: 'healthy', 1: 'patient'}
21
+
22
+ # load the binary classification model
23
+ model_binary = tf.keras.models.load_model("densenet_binary")
24
+
25
+ # load the multi-label classification model
26
+ model_multi = tf.keras.models.load_model("densenet_multi")
27
+
28
+ # define the labels for the multi-label classification model
29
+ labels_multi = {0: 'healthy', 1: 'mild', 2: 'moderate'}
30
 
31
 
32
  #model = tf.keras.models.load_model('/content/drive/MyDrive/project_image_2023_NO/saved_models/saved_model/densenet')
33
  #labels = ['Healthy', 'Patient']
34
+ #labels = {0: 'healthy', 1: 'patient'}
35
 
36
+ def classify_image_binary(inp):
37
  inp = inp.reshape((-1, 224, 224, 3))
38
  inp = tf.keras.applications.densenet.preprocess_input(inp)
39
+ prediction = model_binary.predict(inp)
40
+ confidence = float(prediction[0])
41
+ label = "healthy" if confidence < 0.5 else "patient"
42
+ return {label: confidence}
43
 
44
 
45
+ def classify_image_multi(inp):
46
+ inp = inp.reshape((-1, 224, 224, 3))
47
+ inp = tf.keras.applications.densenet.preprocess_input(inp)
48
+ prediction = model_multi.predict(inp)
49
+ confidences = {labels_multi[i]: float(prediction[0][i]) for i in range(3)}
50
+ return confidences
51
+
52
+ iface_binary = gr.Interface(fn=classify_image_binary,
53
  inputs=gr.Image(shape=(224, 224)),
54
+ outputs=gr.Label(num_top_classes=2),
55
  title="Binary Image Classification",
56
+ description="Classify an image as healthy or patient.",
57
+ examples=[['300104.png']]
58
+ )
 
 
 
 
59
 
60
+ iface_multi = gr.Interface(fn=classify_image_multi,
61
+ inputs=gr.Image(shape=(224, 224)),
62
+ outputs=gr.Label(num_top_classes=3),
63
+ title="Multi-Label Image Classification",
64
+ description="Classify an image as healthy, mild, or moderate.",
65
+ examples=[['300104.png']]
66
+ )
 
 
 
 
 
 
 
 
 
 
67
 
68
+ iface_binary.launch()
69
+ iface_multi.launch()