Spaces:
Sleeping
Sleeping
File size: 2,295 Bytes
931223d |
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 |
import torch
from transformers import AutoProcessor, AutoModel
import numpy as np
from PIL import Image, ImageDraw
# Configuration
MODEL_DIR = r"D:\oeil d'elephant"
def test_inference():
print(f"Loading model from {MODEL_DIR}...")
try:
model = AutoModel.from_pretrained(MODEL_DIR, local_files_only=True)
processor = AutoProcessor.from_pretrained(MODEL_DIR, local_files_only=True)
model.eval()
if hasattr(model, 'logit_scale'):
with torch.no_grad():
model.logit_scale.data.fill_(4.60517) # exp(4.6) = 100
print("Model loaded.")
except Exception as e:
print(f"Failed to load model: {e}")
return
# Synthetic Chest X-ray
image = Image.new('RGB', (448, 448), color=(0, 0, 0))
draw = ImageDraw.Draw(image)
draw.ellipse([100, 100, 200, 350], fill=(200, 200, 200))
draw.ellipse([248, 100, 348, 350], fill=(200, 200, 200)) # Lungs
# Simple Prompts Hypothesis
prompts = [
'Os',
'Poumons',
'Peau',
'Oeil',
'Sein',
'Tissu'
]
# Also test slightly descriptive
prompts_v2 = [
'Radiographie Os',
'Radiographie Poumons',
'Photo Peau',
'Fond d\'oeil',
'Mammographie Sein',
'Microscope Tissu'
]
print("\nTesting Simple Prompts on Synthetic Chest X-ray:")
for p_set in [prompts, prompts_v2]:
with torch.no_grad():
inputs = processor(text=p_set, images=image, padding="max_length", return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits_per_image
probs = torch.sigmoid(logits)[0]
# Also calculate Softmax
probs_softmax = torch.softmax(logits, dim=1)[0]
for i, prompt in enumerate(p_set):
l = logits[0][i].item()
p_sig = probs[i].item()
p_soft = probs_softmax[i].item()
print(f"Prompt: '{prompt:<20}' | Logit: {l:.4f} | Sigmoid: {p_sig*100:.6f}% | Softmax: {p_soft*100:.2f}%")
print("-" * 60)
if __name__ == "__main__":
test_inference()
|