Spaces:
Sleeping
Sleeping
File size: 13,909 Bytes
5d8e5dc 31fba7f 5d8e5dc d3b3241 5d8e5dc 59c1cb0 e612c62 3a18723 e612c62 59c1cb0 e612c62 3a18723 e612c62 fb9f4ee 1af3e2d d487974 1af3e2d 57bf398 31fba7f 1af3e2d 31fba7f 964b0e1 1af3e2d 964b0e1 1af3e2d 964b0e1 31fba7f 964b0e1 5d8e5dc c3a9f35 964b0e1 31fba7f 964b0e1 31fba7f 964b0e1 31fba7f 1af3e2d 31fba7f 5d8e5dc 31fba7f 5d8e5dc 31fba7f 5d8e5dc 31fba7f 5d8e5dc 31fba7f 5d8e5dc 31fba7f 5d8e5dc 31fba7f 5d8e5dc 31fba7f 7e50e78 31fba7f 57bf398 66959a8 31fba7f 66959a8 7e50e78 57bf398 7e50e78 66959a8 7e50e78 5d8e5dc 31fba7f 5d8e5dc 31fba7f e3f447c 31fba7f 5d8e5dc e3f447c 5d8e5dc 31fba7f 5d8e5dc 57bf398 c3a9f35 57bf398 c3a9f35 57bf398 31fba7f e3f447c 31fba7f 5d8e5dc 31fba7f 5d8e5dc 31fba7f 5d8e5dc 31fba7f e3f447c 31fba7f 16cf785 5d8e5dc | 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | import os, io, json, logging
from typing import List, Dict, Any
import numpy as np
from fastapi import FastAPI, UploadFile, File, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from PIL import Image
import tensorflow as tf
from huggingface_hub import snapshot_download,hf_hub_download
import cv2
import gradio as gr
cnn_model = None
last_conv_layer_name = None
# optional gatekeep
try:
HAS_OPENCV = True
except Exception:
HAS_OPENCV = False
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("skinclassify")
# ---------------------- Config ----------------------
DERM_MODEL_ID = os.getenv("DERM_MODEL_ID", "google/derm-foundation")
DERM_LOCAL_DIR = os.getenv("DERM_LOCAL_DIR", "")
MODEL_REPO = "ChantaroNtw/Skin-model"
HEAD_PATH = hf_hub_download(
repo_id=MODEL_REPO,
filename="mlp_best.keras"
)
MU_PATH = hf_hub_download(
repo_id=MODEL_REPO,
filename="mu.npy"
)
SD_PATH = hf_hub_download(
repo_id=MODEL_REPO,
filename="sd.npy"
)
THRESHOLDS_PATH = hf_hub_download(
repo_id=MODEL_REPO,
filename="mlp_thresholds.npy"
)
LABELS_PATH = hf_hub_download(
repo_id=MODEL_REPO,
filename="class_names.json"
)
NPZ_PATH = os.getenv("NPZ_PATH", "")
TOPK = int(os.getenv("TOPK", "5"))
# Gate keep params
MIN_W, MIN_H = int(os.getenv("MIN_W", "128")), int(os.getenv("MIN_H", "128"))
MIN_ASPECT, MAX_ASPECT = float(os.getenv("MIN_ASPECT", "0.5")), float(os.getenv("MAX_ASPECT", "2.0"))
MIN_BRIGHT, MAX_BRIGHT = float(os.getenv("MIN_BRIGHT", "20")), float(os.getenv("MAX_BRIGHT", "235"))
MIN_SKIN_RATIO = float(os.getenv("MIN_SKIN_RATIO", "0.15"))
MIN_SHARPNESS = float(os.getenv("MIN_SHARPNESS", "30.0"))
# Performance: กัน OOM บน Free Space
os.environ.setdefault("TF_NUM_INTRAOP_THREADS", "1")
os.environ.setdefault("TF_NUM_INTEROP_THREADS", "1")
os.environ.setdefault("OMP_NUM_THREADS", "1")
os.environ.setdefault("TF_CPP_MIN_LOG_LEVEL", "2")
MAX_UPLOAD = int(os.getenv("MAX_UPLOAD", str(6 * 1024 * 1024))) # 6MB
DF_SIZE = (448, 448)
app = FastAPI(title="SkinClassify API (Derm-Foundation)", version="2.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=os.getenv("ALLOW_ORIGINS", "*").split(","),
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------------------- Load labels ----------------------
def _load_json(path):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
if os.path.exists(LABELS_PATH):
CLASS_NAMES: List[str] = _load_json(LABELS_PATH)
logger.info(f"Loaded class_names from {LABELS_PATH}")
elif NPZ_PATH and os.path.exists(NPZ_PATH):
arr = np.load(NPZ_PATH, allow_pickle=True)
if "class_names" in arr:
CLASS_NAMES = list(arr["class_names"])
logger.info(f"Loaded class_names from {NPZ_PATH}:class_names")
else:
raise RuntimeError("No LABELS_PATH and class_names not found in NPZ")
else:
raise RuntimeError("LABELS_PATH not found and NPZ_PATH not provided.")
C = len(CLASS_NAMES)
# ---------------------- Load head (.keras via Keras3) ----------------------
def load_head_keras3(path: str):
import keras
logger.info(f"Loading head (.keras) via Keras3 from {path}")
return keras.saving.load_model(path, compile=False)
head = load_head_keras3(HEAD_PATH)
# ---------------------- Load mu/sd ----------------------
def _load_mu_sd():
if os.path.exists(MU_PATH) and os.path.exists(SD_PATH):
mu_ = np.load(MU_PATH).astype("float32")
sd_ = np.load(SD_PATH).astype("float32")
return mu_, sd_
if NPZ_PATH and os.path.exists(NPZ_PATH):
arr = np.load(NPZ_PATH, allow_pickle=True)
mu_ = arr["mu"].astype("float32")
sd_ = arr["sd"].astype("float32")
return mu_, sd_
raise RuntimeError("mu/sd not found (MU_PATH/SD_PATH or NPZ_PATH).")
mu, sd = _load_mu_sd()
logger.info("Loaded mu/sd")
# ---------------------- Load thresholds ----------------------
if os.path.exists(THRESHOLDS_PATH):
best_th = np.load(THRESHOLDS_PATH).astype("float32")
if best_th.shape[0] != C:
raise RuntimeError(f"thresholds size {best_th.shape[0]} != #classes {C}")
else:
logger.warning("THRESHOLDS_PATH not found -> default 0.5 for all classes")
best_th = np.full(C, 0.5, dtype="float32")
# ---------------------- Load derm-foundation ----------------------
from huggingface_hub import snapshot_download
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
CACHE_DIR = os.getenv("HF_HOME", "/app/.cache")
LOCAL_DERM = os.getenv("DERM_LOCAL_DIR", "/app/derm-foundation")
os.makedirs(CACHE_DIR, exist_ok=True)
os.makedirs(LOCAL_DERM, exist_ok=True)
logger.info("Loading Derm Foundation (first time may take a while)...")
try:
if os.path.isdir(LOCAL_DERM) and os.path.exists(os.path.join(LOCAL_DERM, "saved_model.pb")):
derm_dir = LOCAL_DERM
logger.info(f"Loaded Derm Foundation from local: {derm_dir}")
else:
logger.info(f"Downloading derm-foundation from hub: {DERM_MODEL_ID}")
derm_dir = snapshot_download(
repo_id=DERM_MODEL_ID,
repo_type="model",
allow_patterns=["saved_model.pb", "variables/*"],
token=HF_TOKEN,
cache_dir=CACHE_DIR,
local_dir=LOCAL_DERM,
local_dir_use_symlinks=False,
)
logger.info(f"Derm Foundation downloaded to: {derm_dir}")
derm = tf.saved_model.load(derm_dir)
infer = derm.signatures["serving_default"]
except Exception as e:
raise RuntimeError(
f"Failed to load derm-foundation: {e}. "
"Make sure you accepted the model terms and set HF_TOKEN in Space Settings."
)
import tempfile
def create_tf_example(img_arr):
img_uint8 = (img_arr * 255).astype(np.uint8)
encoded = tf.io.encode_jpeg(img_uint8).numpy()
feature = {
"image/encoded": tf.train.Feature(
bytes_list=tf.train.BytesList(value=[encoded])
)
}
example = tf.train.Example(
features=tf.train.Features(feature=feature)
)
return example.SerializeToString()
def get_embedding(img_arr):
example = create_tf_example(img_arr)
tensor = tf.constant([example])
out = infer(inputs=tensor)
return out["embedding"].numpy()[0]
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b) + 1e-8)
def make_patch_heatmap(img, size=32, stride=16):
img = img.resize((224, 224))
img_arr = np.array(img) / 255.0
base_emb = get_embedding(img_arr)
examples = []
coords = []
for y in range(0, 224, stride):
for x in range(0, 224, stride):
occluded = img_arr.copy()
occluded[y:y+size, x:x+size] = 0
examples.append(create_tf_example(occluded))
coords.append((y, x))
tensor = tf.constant(examples)
outputs = infer(inputs=tensor)["embedding"].numpy()
heatmap = np.zeros((224, 224))
for i, (y, x) in enumerate(coords):
diff = np.linalg.norm(base_emb - outputs[i])
heatmap[y:y+size, x:x+size] = diff
heatmap = cv2.normalize(heatmap, None, 0, 1, cv2.NORM_MINMAX)
return heatmap
# ---------------------- Utils ----------------------
def pil_to_png_bytes_448(pil_img: Image.Image) -> bytes:
pil_img = pil_img.convert("RGB").resize(DF_SIZE)
arr = np.array(pil_img, dtype=np.uint8)
return tf.io.encode_png(arr).numpy()
def _brightness(np_img_rgb: np.ndarray) -> float:
r,g,b = np_img_rgb[...,0], np_img_rgb[...,1], np_img_rgb[...,2]
y = 0.2126*r + 0.7152*g + 0.0722*b
return float(y.mean())
def _sharpness(np_img_rgb: np.ndarray) -> float:
if not HAS_OPENCV:
return 100.0
gray = cv2.cvtColor(np_img_rgb, cv2.COLOR_RGB2GRAY)
return float(cv2.Laplacian(gray, cv2.CV_64F).var())
def _skin_ratio(np_img_rgb: np.ndarray) -> float:
img = Image.fromarray(np_img_rgb).convert("YCbCr")
ycbcr = np.array(img)
Cb = ycbcr[...,1]; Cr = ycbcr[...,2]
mask = (Cb >= 77) & (Cb <= 127) & (Cr >= 133) & (Cr <= 173)
return float(mask.mean())
def gatekeep_image(img_bytes: bytes) -> Dict[str, Any]:
try:
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
except Exception:
return {"ok": False, "reasons": ["invalid_image"], "metrics": {}}
w,h = img.size
metrics = {"width": w, "height": h}
reasons = []
if w < MIN_W or h < MIN_H:
reasons.append("too_small")
aspect = w / h
metrics["aspect"] = float(aspect)
if not (MIN_ASPECT <= aspect <= MAX_ASPECT):
reasons.append("weird_aspect")
np_img = np.array(img)
bright = _brightness(np_img)
metrics["brightness"] = bright
if bright < MIN_BRIGHT: reasons.append("too_dark")
if bright > MAX_BRIGHT: reasons.append("too_bright")
if HAS_OPENCV:
sharp = _sharpness(np_img)
metrics["sharpness"] = sharp
if sharp < MIN_SHARPNESS: reasons.append("too_blurry")
ratio = _skin_ratio(np_img)
metrics["skin_ratio"] = ratio
if ratio < MIN_SKIN_RATIO: reasons.append("not_skin_like")
return {"ok": len(reasons)==0, "reasons": reasons, "metrics": metrics}
def predict_probs(img_bytes: bytes) -> np.ndarray:
pil = Image.open(io.BytesIO(img_bytes)).convert("RGB").resize(DF_SIZE)
by = pil_to_png_bytes_448(pil)
ex = tf.train.Example(features=tf.train.Features(
feature={'image/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[by]))}
)).SerializeToString()
out = infer(inputs=tf.constant([ex]))
if "embedding" not in out:
raise RuntimeError(f"Unexpected derm-foundation outputs: {list(out.keys())}")
emb = out["embedding"].numpy().astype("float32") # (1, 6144)
z = (emb - mu) / (sd + 1e-6)
probs = head.predict(z, verbose=0)[0] # head (.keras) โดยตรง
return probs
# ---------------------- Endpoints ----------------------
@app.get("/health")
def health():
return {
"ok": True,
"classes": len(CLASS_NAMES),
"derm": DERM_MODEL_ID or DERM_LOCAL_DIR,
"has_opencv": HAS_OPENCV
}
@app.post("/predict")
async def predict(request: Request, file: UploadFile = File(...)):
cl = request.headers.get("content-length")
if cl and int(cl) > MAX_UPLOAD:
raise HTTPException(413, "File too large")
img_bytes = await file.read()
if len(img_bytes) > MAX_UPLOAD:
raise HTTPException(413, "File too large")
image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
gate = gatekeep_image(img_bytes)
if not gate["ok"]:
return JSONResponse(status_code=200, content={
"ok": False,
"reason": "gate_reject",
"gate": gate
})
probs = predict_probs(img_bytes)
order = np.argsort(probs)[::-1]
top = [{"label": CLASS_NAMES[i], "prob": float(probs[i])} for i in order[:TOPK]]
preds = (probs >= best_th).astype(np.int32)
positives = [{"label": CLASS_NAMES[i], "prob": float(probs[i])}
for i in range(C) if preds[i] == 1]
heatmap = make_gradcam_heatmap(image)
overlay = overlay_heatmap(image, heatmap)
return {
"ok": True,
"gate": gate,
"result": {
"type": "multilabel",
"thresholds_used": {CLASS_NAMES[i]: float(best_th[i]) for i in range(C)},
"positives": positives,
"topk": top,
"probs": {CLASS_NAMES[i]: float(probs[i]) for i in range(C)}
},
"has_heatmap": overlay is not None
}
#----------------------------Over_lay-------------------------------
def overlay_heatmap(img, heatmap):
img = img.resize((224, 224))
img_np = np.array(img)
heatmap = cv2.GaussianBlur(heatmap, (21, 21), 0)
heatmap = np.power(heatmap, 1.5)
heatmap = cv2.normalize(heatmap, None, 0, 1, cv2.NORM_MINMAX)
heatmap_uint8 = np.uint8(255 * heatmap)
heatmap_color = cv2.applyColorMap(heatmap_uint8, cv2.COLORMAP_JET)
threshold = np.mean(heatmap) + np.std(heatmap)
mask = heatmap > threshold
overlay = img_np.copy()
overlay[mask] = (
0.6 * overlay[mask] +
0.4 * heatmap_color[mask]
).astype(np.uint8)
return overlay
#------------------------------UI-----------------------------------
def gradio_predict(image):
if image is None:
return {}, None, "❗ Upload image first"
buf = io.BytesIO()
image.save(buf, format="PNG")
img_bytes = buf.getvalue()
gate = gatekeep_image(img_bytes)
if not gate["ok"]:
return {}, None, "❌ Image rejected"
probs = predict_probs(img_bytes)
order = np.argsort(probs)[::-1]
result = {
CLASS_NAMES[i]: float(probs[i])
for i in order[:5]
}
#heatmap = make_patch_heatmap(image)
# stage 1
coarse_map = make_patch_heatmap(image)
# หา region ที่สำคัญ
mask = coarse_map > np.mean(coarse_map)
# stage 2 เฉพาะ mask
refined_map = refine_heatmap(image, mask)
overlay = overlay_heatmap(image, refined_map)
return result, overlay, "✅ Done"
with gr.Blocks() as demo:
gr.Markdown("# 🧠 Skin Disease Classifier")
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="Upload Image")
btn = gr.Button("🔍 Analyze", variant="primary")
with gr.Column():
output_label = gr.Label(num_top_classes=5, label="Prediction")
output_image = gr.Image(label="Heatmap")
status = gr.Markdown("")
btn.click(
gradio_predict,
inputs=image_input,
outputs=[output_label, output_image]
)
demo.launch(server_name="0.0.0.0", server_port=7860)
|