Spaces:
Sleeping
Sleeping
File size: 2,890 Bytes
0325a51 4ff3206 f127968 0cc5da8 a507bad 0325a51 4eefd61 0325a51 388a9c4 4eefd61 0325a51 4eefd61 374cd0b 0325a51 4eefd61 374cd0b 4eefd61 374cd0b 4eefd61 374cd0b 4eefd61 8248a35 388a9c4 374cd0b 0325a51 8248a35 4eefd61 288c418 0325a51 1753020 72428c5 0bdb269 72428c5 1753020 185a760 0325a51 185a760 1753020 185a760 374cd0b 72428c5 185a760 1753020 |
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 72 73 74 75 76 77 |
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
provided_imgs = [img for img in imgs if img is not None]
if not provided_imgs: # Check if no images were provided
return "Please upload at least one image for prediction."
# Convert provided images to PILImage and predict with each model
for img, key in zip(imgs, modality_keys):
if img is not None:
pil_img = PILImage.create(img)
pred, _, probs = models[key].predict(pil_img)
prob_pred = probs.max() # Get the highest probability score
predictions.append(pred)
detailed_predictions.append(f"{key}: {pred} ({prob_pred.item()*100:.2f}%)")
else:
detailed_predictions.append(f"{key}: No image provided")
# Calculate the final decision based on provided predictions
if predictions:
final_decision_count = max(set(predictions), key=predictions.count)
final_decision = f"Final Decision: {final_decision_count} based on {len(predictions)} inputs."
else:
final_decision = "No final decision (Insufficient data)"
detailed_predictions.append(final_decision)
return "\n".join(detailed_predictions)
# Define the Gradio interface inputs
inputs = [gr.Image(label=f"{modality} Image") for modality in modality_keys]
output = gr.Text(label="Predictions")
# Adjust the paths to where you store your example images for each modality
example_images = [
['Ultrasound.png', 'OCT.jpg', 'Fundus.jpg', 'FAF.jpg']
]
# The rest of your code remains unchanged
intf = gr.Interface(fn=classify_images,
inputs=inputs,
outputs=output,
title="ODD Detection from Multiple Imaging Modalities",
description="Upload images for each modality (as available). It's not required to upload an image for every input field. At least one image is required for a prediction.",
examples=example_images) # Now correctly formatted for multiple inputs
intf.launch(share=True)
|