File size: 2,901 Bytes
1eebc45
 
 
 
6ea82b1
 
 
 
 
 
 
 
 
 
 
1eebc45
a8b4012
 
 
 
 
 
1eebc45
 
f62790a
1eebc45
28fb1f0
5dba2ed
55fb13e
5dba2ed
28fb1f0
 
 
5dba2ed
02b49f6
5dba2ed
 
6ea82b1
f62790a
6ea82b1
28fb1f0
 
 
 
 
bcaa4d3
6ea82b1
28fb1f0
 
 
 
 
bcaa4d3
6ea82b1
28fb1f0
 
 
 
 
 
bcaa4d3
6ea82b1
28fb1f0
 
 
 
 
 
 
 
 
 
 
6ea82b1
8628f1a
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
# -*- 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


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

# define the labels for the binary classification model
labels_binary = {0: 'healthy', 1: 'Patients'}

# load the multi-label classification model
model_multi = tf.keras.models.load_model("CNN_multiclass")

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

def classify_binary(inp):
    inp = inp.reshape((-1, 224, 224, 3))
    inp = tf.keras.applications.densenet.preprocess_input(inp)
    prediction = model_binary.predict(inp)
    confidence = float(prediction[0])
    return {labels_binary[prediction.argmax()]: confidence}

def classify_multi(inp):
    inp = inp.reshape((-1, 224, 224, 3))
    inp = tf.keras.applications.densenet.preprocess_input(inp)
    prediction = model_multi.predict(inp)
    confidences = {labels_multi[i]: float(prediction[0][i]) for i in range(len(labels_multi))}
    return confidences

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

multi_interface = gr.Interface(fn=classify_multi, 
                               inputs=gr.inputs.Image(shape=(224, 224)),
                               outputs=gr.outputs.Label(num_top_classes=3),
                               title="Multi-class Image Classification",
                               description="Classify an image as healthy, mild or moderate.",
                               examples=[['300104.png']]
                              )

demo = gr.Interface([binary_interface, multi_interface], 
                     "tab",
                     title="Binary and Multi-class Image Classification",
                     description="Classify an image as healthy, mild or moderate.")
demo.launch()