Ethium's picture
Update app_2.py
f39bf0e verified
raw
history blame
No virus
2.2 kB
from fastai.vision.all import *
import gradio as gr
from pathlib import Path
import pandas as pd
def get_x(row):
# All files are assumed to be '.jpg', so we directly return the path with '.jpg' extension
return path_image_combined / f"{row['file_name']}.jpg"
def get_y(row):
return row['Buried ODD']
# Load the models into a dictionary
models = {
'Ultrasound': load_learner('ODDUltrasound.pkl'),
'OCT': load_learner('ODDOCT.pkl'),
'Fundus': load_learner('ODDfundus.pkl'),
'Fluorescence': load_learner('ODDfluorescence.pkl')
}
modality_keys = ['Ultrasound', 'OCT', 'Fundus', 'Fluorescence']
def classify_images(img_ultrasound, img_oct, img_fundus, img_fluorescence):
imgs = [img_ultrasound, img_oct, img_fundus, img_fluorescence]
predictions = []
detailed_predictions = [] # To store detailed predictions for each modality
# Convert images to PILImage and predict with each model
for img, key in zip(imgs, modality_keys):
pil_img = PILImage.create(img)
pred, _, probs = models[key].predict(pil_img)
predictions.append(pred)
# Assuming binary classification, extract the probability for the predicted class
prob_pred = probs.max() # Get the highest probability score
detailed_predictions.append(f"{key}: {pred} ({prob_pred:.2f}%)") # Format prediction with percentage
# Majority vote for final decision
final_decision = max(set(predictions), key=predictions.count)
detailed_predictions.append(f"Final Decision: {final_decision}") # Add the final decision
return "\n".join(detailed_predictions) # Return detailed predictions as a single string
# Define the Gradio interface inputs and outputs
inputs = [gr.Image(label=f"{modality} Image") for modality in modality_keys]
output = gr.Text(label="Predictions")
intf = gr.Interface(fn=classify_images, inputs=inputs, outputs=output,
title="ODD Detection from Multiple Imaging Modalities",
description="Upload images for each modality and receive individual predictions with percentages and a binary prediction for Optic Disk Drusen presence.")
intf.launch(share=True)