File size: 2,391 Bytes
1eebc45
 
 
 
6ea82b1
 
 
 
 
 
 
 
 
 
 
1eebc45
a8b4012
 
 
 
 
 
1eebc45
 
f62790a
1eebc45
5dba2ed
6ea82b1
5dba2ed
 
6ea82b1
5dba2ed
 
6ea82b1
f62790a
6ea82b1
 
 
 
 
 
 
bcaa4d3
6ea82b1
bcaa4d3
 
6ea82b1
 
bcaa4d3
 
6ea82b1
 
 
bcaa4d3
 
 
 
 
6ea82b1
 
 
bcaa4d3
 
 
 
 
6ea82b1
 
 
8628f1a
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
# -*- 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("densenet")

# load the multi-label classification model
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'}

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])
  label = {0: 'healthy', 1: 'patient'}
  return {label: 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.TabbedInterface([binary_interface, multi_interface], ["Binary", "Multi-class"])

demo.launch()