iamcode6 commited on
Commit
5e8eb58
·
verified ·
1 Parent(s): 1aa8dc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -11
app.py CHANGED
@@ -3,6 +3,8 @@
3
  Loads `best_ema_v2.pt` from iamcode6/dermnet-skin23-eva02 on startup and serves
4
  predictions via Gradio. Runs on CPU (free HF Space tier).
5
  """
 
 
6
  import gradio as gr
7
  import torch
8
  import timm
@@ -10,6 +12,9 @@ from huggingface_hub import hf_hub_download
10
  from PIL import Image
11
  from timm.data import create_transform
12
 
 
 
 
13
  # ==============================================================================
14
  # Load model from HF Hub at startup
15
  # ==============================================================================
@@ -46,7 +51,17 @@ val_tf = create_transform(
46
 
47
 
48
  # ==============================================================================
49
- # Inference (with light TTA: identity + hflip)
 
 
 
 
 
 
 
 
 
 
50
  # ==============================================================================
51
  @torch.no_grad()
52
  def predict(image):
@@ -56,12 +71,9 @@ def predict(image):
56
  image = Image.fromarray(image)
57
  image = image.convert("RGB")
58
 
59
- x = val_tf(image).unsqueeze(0)
60
- x_flip = torch.flip(x, dims=[-1])
61
- batch = torch.cat([x, x_flip], dim=0).to(device)
62
-
63
- logits = model(batch)
64
- probs = torch.softmax(logits, dim=-1).mean(dim=0)
65
  return {idx_to_class[i]: float(probs[i]) for i in range(NUM_CLASSES)}
66
 
67
 
@@ -75,7 +87,7 @@ DESCRIPTION = """# DermNet-Skin23 Classifier
75
  **Single-model accuracy**: **81.48%** acc / **0.7969** macro F1 on a 3,856-image val split.
76
  **Full 5-model ensemble reaches 82.86% / 0.8113** — see the ensemble repo linked below.
77
 
78
- Upload a clinical or dermoscopy photo and the model returns the top-5 most likely categories with calibrated confidence.
79
 
80
  ---
81
 
@@ -91,7 +103,7 @@ LINKS = """
91
  **Trained on**: AMD Instinct MI300X (192 GB HBM3) via DigitalOcean, ROCm 7.0, PyTorch with HIP.
92
  """
93
 
94
- with gr.Blocks(title="DermNet-Skin23 Classifier", theme=gr.themes.Soft()) as demo:
95
  gr.Markdown(DESCRIPTION)
96
 
97
  with gr.Row():
@@ -103,9 +115,10 @@ with gr.Blocks(title="DermNet-Skin23 Classifier", theme=gr.themes.Soft()) as dem
103
 
104
  gr.Markdown(LINKS)
105
 
 
 
106
  predict_btn.click(predict, inputs=input_image, outputs=output_label)
107
- input_image.change(predict, inputs=input_image, outputs=output_label)
108
 
109
  if __name__ == "__main__":
110
- demo.launch()
111
 
 
3
  Loads `best_ema_v2.pt` from iamcode6/dermnet-skin23-eva02 on startup and serves
4
  predictions via Gradio. Runs on CPU (free HF Space tier).
5
  """
6
+ import os
7
+
8
  import gradio as gr
9
  import torch
10
  import timm
 
12
  from PIL import Image
13
  from timm.data import create_transform
14
 
15
+ # Use both vCPUs on the free Space tier
16
+ torch.set_num_threads(int(os.environ.get("TORCH_NUM_THREADS", 2)))
17
+
18
  # ==============================================================================
19
  # Load model from HF Hub at startup
20
  # ==============================================================================
 
51
 
52
 
53
  # ==============================================================================
54
+ # Warm up the model (first forward pass is JIT-slower; pre-pay it at startup)
55
+ # ==============================================================================
56
+ print("Warming up model...")
57
+ with torch.no_grad():
58
+ dummy = torch.zeros(1, 3, IMG_SIZE, IMG_SIZE)
59
+ _ = model(dummy)
60
+ print("Warmup done — ready to serve.")
61
+
62
+
63
+ # ==============================================================================
64
+ # Inference — single forward pass (no TTA on free CPU; would 2x latency)
65
  # ==============================================================================
66
  @torch.no_grad()
67
  def predict(image):
 
71
  image = Image.fromarray(image)
72
  image = image.convert("RGB")
73
 
74
+ x = val_tf(image).unsqueeze(0).to(device)
75
+ logits = model(x)
76
+ probs = torch.softmax(logits, dim=-1).squeeze(0)
 
 
 
77
  return {idx_to_class[i]: float(probs[i]) for i in range(NUM_CLASSES)}
78
 
79
 
 
87
  **Single-model accuracy**: **81.48%** acc / **0.7969** macro F1 on a 3,856-image val split.
88
  **Full 5-model ensemble reaches 82.86% / 0.8113** — see the ensemble repo linked below.
89
 
90
+ Upload a clinical or dermoscopy photo, click **Classify**, and the model returns the top-5 most likely categories with calibrated confidence. **Inference takes ~30-45 seconds** on the free CPU tier — large vision transformer at 448×448 resolution.
91
 
92
  ---
93
 
 
103
  **Trained on**: AMD Instinct MI300X (192 GB HBM3) via DigitalOcean, ROCm 7.0, PyTorch with HIP.
104
  """
105
 
106
+ with gr.Blocks(title="DermNet-Skin23 Classifier") as demo:
107
  gr.Markdown(DESCRIPTION)
108
 
109
  with gr.Row():
 
115
 
116
  gr.Markdown(LINKS)
117
 
118
+ # Only run on explicit button click — avoids racing the Gradio queue
119
+ # if a user uploads multiple images quickly
120
  predict_btn.click(predict, inputs=input_image, outputs=output_label)
 
121
 
122
  if __name__ == "__main__":
123
+ demo.launch(theme=gr.themes.Soft())
124