Nina-HK's picture
model_update
28fb1f0
# -*- 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()