File size: 2,879 Bytes
cb92718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Basic Docker SDK Space
emoji: 🐳
colorFrom: purple
colorTo: gray
sdk: docker
app_port: 7860
---

# Derm Foundation FastAPI Two-Stage Classifier

This project deploys a two-stage inference pipeline:

```text
image -> Google Derm Foundation SavedModel -> embedding -> PyTorch MLP head -> class probabilities
```

The preprocessing follows the notebook pipeline:

```text
RGB -> resize 448x448 -> PNG bytes -> tf.train.Example with key image/encoded
```

## Project structure

```text
derm_fastapi_project/
  app/
    main.py                         # FastAPI app and endpoints
    config.py                       # Environment settings
    schemas.py                      # API response models
    services/
      preprocessing.py              # image -> serialized tf.train.Example
      derm_backbone.py              # Derm Foundation wrapper
      predictor.py                  # two-stage sequential forward pass
    models/
      mlp_head.py                   # load model_state_dict from .pt checkpoint
  scripts/
    test_request.py                 # local API test client
  requirements.txt
  class_names.json                  # replace with your real class order
  .env.example
```

## Setup

Create a virtual environment, then install dependencies:

```bash
pip install -r requirements.txt
```

Put your PyTorch checkpoint in the project root:

```text
derm_foundation_mlp_head.pt
```

The checkpoint should contain:

```python
{
    "model_state_dict": mlp_head.state_dict(),
    # optional but recommended:
    "class_names": [...]
}
```

If the checkpoint does not contain `class_names`, edit `class_names.json` so the order exactly matches your training label order.

## Hugging Face token

Do not put your token in the source code.

Use an environment variable:

```bash
export HF_TOKEN="hf_your_token_here"
```

You must already have access to `google/derm-foundation` on Hugging Face.

## Run the API

```bash
uvicorn app.main:app --host 0.0.0.0 --port 8000
```

Test in browser:

```text
http://127.0.0.1:8000/docs
```

Test with Python:

```bash
python scripts/test_request.py path/to/image.jpg
```

## API endpoint

### POST `/predict`

Input: multipart image upload named `file`.

Output:

```json
{
  "predicted_index": 0,
  "predicted_class": "class_0",
  "confidence": 0.91,
  "probabilities": [
    {"index": 0, "class_name": "class_0", "probability": 0.91},
    {"index": 1, "class_name": "class_1", "probability": 0.02}
  ]
}
```

## Important note about the MLP head

`app/models/mlp_head.py` reconstructs the MLP from Linear layer tensors in `model_state_dict`.
It assumes Linear layers with ReLU between hidden layers. This is usually fine for a simple MLP head.
If your original head used a different activation, BatchNorm, or a more complex custom architecture, replace `InferredMLPHead` with the exact same class used during training.