Spaces:
Running
Running
# -*- 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() | |