# -*- 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 import tensorflow as tf import gradio as gr # load the CNN binary classification model model_cnn = tf.keras.models.load_model("CNN_binary") # define the labels for the binary classification model labels_cnn = {0: 'healthy', 1: 'Patients'} # load the EfficientNet binary classification model model_efn = tf.keras.models.load_model("efficientNet_binary") # define the labels for the binary classification model labels_efn = {0: 'healthy', 1: 'Patients'} def classify_cnn(inp): inp = inp.reshape((-1, 224, 224, 3)) inp = tf.keras.applications.densenet.preprocess_input(inp) prediction = model_cnn.predict(inp) confidences = {labels_cnn[i]: float(prediction[0][i]) for i in range(2)} return confidences def classify_efn(inp): inp = inp.reshape((-1, 224, 224, 3)) inp = tf.keras.applications.efficientnet.preprocess_input(inp) prediction = model_efn.predict(inp) confidences = {labels_efn[i]: float(prediction[0][i]) for i in range(2)} return confidences binary_interface_cnn = gr.Interface(fn=classify_cnn, inputs=gr.Image(shape=(224, 224)), outputs=gr.Label(num_top_classes=2), title="CNN Binary Image Classification", description="Classify an image as healthy or patient using CNN.", examples=[['300104.png']] ) binary_interface_efn = gr.Interface(fn=classify_efn, inputs=gr.Image(shape=(224, 224)), outputs=gr.Label(num_top_classes=2), title="EfficientNet Binary Image Classification", description="Classify an image as healthy or patient using EfficientNet.", examples=[['300104.png']] ) demo = gr.Interface( fn=gr.mix.Parallel([classify_cnn, classify_efn]), 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 using either CNN or EfficientNet.", ) demo.launch()