File size: 6,824 Bytes
1947612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f95e0b4
 
 
 
 
 
1947612
 
f95e0b4
 
1947612
 
 
 
 
 
 
f95e0b4
1947612
 
f95e0b4
1947612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
DermaVision inference engine.
"""

from __future__ import annotations

import io
import logging
import threading
from pathlib import Path
from typing import Any

import numpy as np
import onnxruntime as ort
from django.conf import settings
from PIL import Image

logger = logging.getLogger(__name__)

_MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32)
_STD  = np.array([0.229, 0.224, 0.225], dtype=np.float32)
_SIZE = 224

LABEL_MAP: dict[int, str] = {
    0:  "Acne and Rosacea",
    1:  "Actinic Keratosis Basal Cell Carcinoma",
    2:  "Atopic Dermatitis",
    3:  "Bullous Disease",
    4:  "Cellulitis Impetigo",
    5:  "Eczema",
    6:  "Exanthems and Drug Eruptions",
    7:  "Hair Loss Alopecia",
    8:  "Herpes HPV and STDs",
    9:  "Light Diseases and Pigmentation Disorders",
    10: "Lupus and Connective Tissue Diseases",
    11: "Melanoma Skin Cancer Nevi and Moles",
    12: "Nail Fungus and Nail Disease",
    13: "Poison Ivy and Contact Dermatitis",
    14: "Psoriasis Lichen Planus",
    15: "Scabies Lyme Disease and Infestations",
    16: "Seborrheic Keratoses",
    17: "Systemic Disease",
    18: "Tinea Ringworm Candidiasis",
    19: "Urticaria Hives",
    20: "Vascular Tumors",
    21: "Vasculitis",
    22: "Warts Molluscum and Viral Infections",
}

TOP_K = 3


class _InferenceEngine:

    _lock     = threading.Lock()
    _instance = None

    def __init__(self) -> None:
        self._session: ort.InferenceSession | None = None

    def _load(self) -> None:
        model_path = Path(settings.MODEL_PATH)
        if not model_path.exists():
            raise FileNotFoundError(
                f"ONNX model not found at {model_path}. "
                "Place dermavision.onnx in the model/ directory."
            )

        logger.info("Loading DermaVision ONNX model from %s ...", model_path)
        opts = ort.SessionOptions()
        opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
        opts.intra_op_num_threads      = 4

        providers = (
            ["CUDAExecutionProvider", "CPUExecutionProvider"]
            if "CUDAExecutionProvider" in ort.get_available_providers()
            else ["CPUExecutionProvider"]
        )

        self._session = ort.InferenceSession(str(model_path), opts, providers=providers)
        logger.info("Model ready — providers: %s", self._session.get_providers())

    @property
    def ready(self) -> bool:
        return self._session is not None

    def ensure_loaded(self) -> None:
        if not self.ready:
            with self._lock:
                if not self.ready:
                    self._load()

    def preprocess(self, image_bytes: bytes) -> tuple[np.ndarray, tuple[int, int]]:
        img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
        original_size = img.size

        w, h  = img.size
        scale = 256 / min(w, h)
        img   = img.resize((int(w * scale), int(h * scale)), Image.BICUBIC)

        w, h = img.size
        left = (w - _SIZE) // 2
        top  = (h - _SIZE) // 2
        img  = img.crop((left, top, left + _SIZE, top + _SIZE))

        arr = np.array(img, dtype=np.float32) / 255.0
        arr = (arr - _MEAN) / _STD
        arr = arr.transpose(2, 0, 1)[np.newaxis]

        return arr.astype(np.float32), original_size

    def predict(self, pixel_values: np.ndarray) -> np.ndarray:
        outputs = self._session.run(
            ["logits"],
            {"pixel_values": pixel_values},
        )
        return outputs[0]

    @staticmethod
    def top_k_predictions(logits: np.ndarray, k: int = TOP_K) -> list[dict[str, Any]]:
        logits_1d  = logits[0]
        exp_logits = np.exp(logits_1d - logits_1d.max())
        probs      = exp_logits / exp_logits.sum()

        top_indices = np.argsort(probs)[::-1][:k]
        return [
            {
                "label":      LABEL_MAP.get(int(i), f"class_{i}"),
                "confidence": round(float(probs[i]), 4),
            }
            for i in top_indices
        ]

    @staticmethod
    def attention_heatmap(
        pixel_values: np.ndarray,
        original_size: tuple[int, int],
        logits: np.ndarray,
    ) -> str | None:
        try:
            import base64
            import cv2

            import concurrent.futures

            top_class = int(np.argmax(logits[0]))
            w, h      = original_size

            # Use a coarse 4x4 grid (16 inferences) instead of the full 16x16
            # DINOv2 patch grid (256 inferences). Each coarse cell covers a
            # 56x56 pixel block. The result is bicubic-upsampled to full res.
            COARSE = 4
            cell   = _SIZE // COARSE  # 56px per cell
            importance = np.zeros((COARSE, COARSE), dtype=np.float32)

            def process_patch(row, col):
                r0, r1 = row * cell, (row + 1) * cell
                c0, c1 = col * cell, (col + 1) * cell
                masked = pixel_values.copy()
                masked[:, :, r0:r1, c0:c1] = 0.0
                masked_logits = _engine._session.run(
                    ["logits"], {"pixel_values": masked}
                )[0]
                return row, col, float(logits[0][top_class] - masked_logits[0][top_class])

            with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
                futures = [
                    executor.submit(process_patch, r, c)
                    for r in range(COARSE) for c in range(COARSE)
                ]
                for future in concurrent.futures.as_completed(futures):
                    r, c, val = future.result()
                    importance[r, c] = val

            importance = np.maximum(importance, 0)
            if importance.max() > 0:
                importance /= importance.max()

            cam     = cv2.resize(importance, (w, h), interpolation=cv2.INTER_CUBIC)
            cam     = np.uint8(255 * cam)
            heatmap = cv2.applyColorMap(cam, cv2.COLORMAP_JET)

            _, buf = cv2.imencode(".png", heatmap)
            return base64.b64encode(buf.tobytes()).decode()

        except Exception as exc:
            logger.warning("Heatmap generation failed: %s", exc)
            return None


_engine = _InferenceEngine()


def run_inference(image_bytes: bytes, include_heatmap: bool = True) -> dict[str, Any]:
    _engine.ensure_loaded()

    pixel_values, original_size = _engine.preprocess(image_bytes)
    logits                      = _engine.predict(pixel_values)
    predictions                 = _engine.top_k_predictions(logits)

    heatmap_b64 = (
        _engine.attention_heatmap(pixel_values, original_size, logits)
        if include_heatmap
        else None
    )

    return {
        "predictions": predictions,
        "heatmap_b64": heatmap_b64,
    }


def model_ready() -> bool:
    return _engine.ready