File size: 2,620 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
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()
        
        # Apply fix
        if hasattr(model, 'logit_scale'):
            with torch.no_grad():
                model.logit_scale.data.fill_(4.60517)
                
        print("Model loaded.")
    except Exception as e:
        print(f"Failed to load model: {e}")
        return

    # Synthetic Pneumonia X-ray
    # Two lungs, one with a big white consolidation
    image = Image.new('RGB', (448, 448), color=(0, 0, 0))
    draw = ImageDraw.Draw(image)
    draw.ellipse([100, 100, 200, 350], fill=(100, 100, 100)) # Left lung (clearer)
    draw.ellipse([248, 100, 348, 350], fill=(200, 200, 200)) # Right lung (consolidated/white)
    
    # Check "Thoracic" specific labels
    labels = [
        'Cardiomédiastin élargi', 'Cardiomégalie', 'Opacité pulmonaire',
        'Lésion pulmonaire', 'Consolidation', 'Œdème', 'Pneumonie',
        'Atelectasis', 'Pneumothorax', 'Effusion pleurale', 'Pleural Autre'
    ]
    
    # Try simplified versions too
    simple_labels = [
        'Coeur', 'Gros coeur', 'Opacité',
        'Lésion', 'Blanc', 'Eau', 'Infection',
        'Ecrasé', 'Air', 'Liquide', 'Autre'
    ]
    
    print("\nTesting Pathology Prompts:")
    
    with torch.no_grad():
        inputs = processor(text=labels, images=image, padding="max_length", return_tensors="pt")
        outputs = model(**inputs)
        logits = outputs.logits_per_image
        probs = torch.sigmoid(logits)[0]
        
        print("\nOriginal Labels:")
        for i, label in enumerate(labels):
            print(f"'{label}': Logit {logits[0][i]:.4f} | Prob {probs[i]:.6f}")

        # Test Simple
        inputs_simple = processor(text=simple_labels, images=image, padding="max_length", return_tensors="pt")
        outputs_simple = model(**inputs_simple)
        logits_simple = outputs_simple.logits_per_image
        probs_simple = torch.sigmoid(logits_simple)[0]

        print("\nSimple Labels:")
        for i, label in enumerate(simple_labels):
            print(f"'{label}': Logit {logits_simple[0][i]:.4f} | Prob {probs_simple[0][i]:.6f}")

if __name__ == "__main__":
    test_inference()