|
import torch
|
|
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
|
|
|
|
|
class ImageAnalyzer:
|
|
def __init__(self):
|
|
|
|
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}")
|
|
|
|
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}
|
|
|
|
try:
|
|
inputs = self.feature_extractor(images=image, return_tensors="pt").to(
|
|
self.device
|
|
)
|
|
with torch.no_grad():
|
|
outputs = self.model(**inputs)
|
|
|
|
|
|
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"}
|
|
|