Amarthya7's picture
Upload 3 files
06e0c89 verified
raw
history blame
1.85 kB
import torch
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
class ImageAnalyzer:
def __init__(self):
# Load the chest X-ray analysis model
try:
model_name = "facebook/deit-base-patch16-224-medical-cxr"
self.feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
self.model = AutoModelForImageClassification.from_pretrained(model_name)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model.to(self.device)
print(f"Image model loaded on {self.device}")
except Exception as e:
print(f"Error loading image model: {e}")
# Fallback to a dummy model
self.model = None
self.feature_extractor = None
def analyze(self, image):
"""Analyze an X-ray image and return predictions with confidence scores"""
if self.model is None or self.feature_extractor is None:
return {"No findings": 0.7, "Abnormal": 0.3} # Dummy results
try:
inputs = self.feature_extractor(images=image, return_tensors="pt").to(
self.device
)
with torch.no_grad():
outputs = self.model(**inputs)
# Process outputs to get predicted class and confidence
probabilities = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
predictions = {}
for i, prob in enumerate(probabilities):
label = self.model.config.id2label[i]
predictions[label] = float(prob)
return predictions
except Exception as e:
print(f"Error during image analysis: {e}")
return {"Error": "Could not analyze image"}