File size: 3,137 Bytes
1eebc45
 
 
 
6ea82b1
 
 
 
 
 
 
 
 
 
 
1eebc45
a8b4012
 
 
 
 
 
1eebc45
 
f62790a
12a3d1b
 
1eebc45
28fb1f0
5dba2ed
12a3d1b
632f959
5dba2ed
 
12a3d1b
 
 
 
 
 
 
 
28fb1f0
12a3d1b
bcaa4d3
12a3d1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28fb1f0
12a3d1b
 
 
8628f1a
12a3d1b
28fb1f0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-

# %%capture
# #Use capture to not show the output of installing the libraries!

#model_multi = tf.keras.models.load_model("densenet")

# define the labels for the multi-label classification model
#labels_multi = {0: 'healthy', 1: 'mild', 2: 'moderate'}


#model = tf.keras.models.load_model('/content/drive/MyDrive/project_image_2023_NO/saved_models/saved_model/densenet')
#labels = ['Healthy', 'Patient']
#labels = {0: 'healthy', 1: 'patient'}

import gradio as gr
import requests
import torch
import torch.nn as nn
from PIL import Image
from torchvision.models import resnet50
from torchvision.transforms import functional as F
import numpy as np
import tensorflow as tf
from transformers import pipeline
from tensorflow.keras.preprocessing import image as image_utils
from tensorflow.keras.applications import densenet, efficientnet


# load the binary classification model
model_cnn = tf.keras.models.load_model("CNN_binary")
model_efficientnet = tf.keras.models.load_model("efficientNet_binary")

# define the labels for the multi-label classification model
labels_cnn = {0: 'healthy', 1: 'patient'}
labels_efficientnet = {0: 'healthy', 1: 'patient'}

def classify_cnn(inp):
    img = np.array(inp)
    img = img.reshape((1, 224, 224, 3))
    img = densenet.preprocess_input(img)
    prediction = model_cnn.predict(img)
    confidence = float(prediction[0])
    return {labels_cnn[prediction.argmax()]: confidence}

def classify_efficientnet(inp):
    img = np.array(inp)
    img = img.reshape((1, 224, 224, 3))
    img = efficientnet.preprocess_input(img)
    prediction = model_efficientnet.predict(img)
    confidence = float(prediction[0])
    return {labels_efficientnet[prediction.argmax()]: confidence}

cnn_interface = gr.Interface(fn=classify_cnn, 
                             inputs=gr.inputs.Image(shape=(224, 224)),
                             outputs=gr.outputs.Label(num_top_classes=2),
                             title="CNN Binary Image Classification",
                             description="Classify an image as healthy or patient using a CNN model.",
                             examples=[['300104.png']]
                            )

efficientnet_interface = gr.Interface(fn=classify_efficientnet, 
                                       inputs=gr.inputs.Image(shape=(224, 224)),
                                       outputs=gr.outputs.Label(num_top_classes=2),
                                       title="EfficientNet Binary Image Classification",
                                       description="Classify an image as healthy or patient using an EfficientNet model.",
                                       examples=[['300104.png']]
                                      )
# create a combined interface with tabs for each binary classification model
demo = gr.Interface([cnn_interface, efficientnet_interface], 
                     "tab",
                     title="Binary Image Classification",
                     description="Classify an image as healthy or patient using different binary classification models."
                    )

demo.launch()