🫁 Add fine-tuned ViT-Base lung cancer classifier (normal/malignant/benign)
Browse files- README.md +92 -0
- config.json +36 -0
- model.safetensors +3 -0
- preprocessor_config.json +23 -0
README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- image-classification
|
| 5 |
+
- vision-transformer
|
| 6 |
+
- vit
|
| 7 |
+
- lung-cancer
|
| 8 |
+
- medical-imaging
|
| 9 |
+
- pytorch
|
| 10 |
+
- transformers
|
| 11 |
+
base_model: google/vit-base-patch16-224
|
| 12 |
+
pipeline_tag: image-classification
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# 🫁 ViT Lung Cancer Classifier
|
| 16 |
+
|
| 17 |
+
Fine-tuned **Vision Transformer (ViT-Base/16)** for lung cancer CT image classification
|
| 18 |
+
into 3 classes: **normal**, **malignant**, and **benign**.
|
| 19 |
+
|
| 20 |
+
## 📊 Model Details
|
| 21 |
+
|
| 22 |
+
| Property | Value |
|
| 23 |
+
|---|---|
|
| 24 |
+
| Base Model | `google/vit-base-patch16-224` |
|
| 25 |
+
| Task | Image Classification (3 classes) |
|
| 26 |
+
| Input Size | 224 × 224 px |
|
| 27 |
+
| Precision | fp16 |
|
| 28 |
+
| Training | Full fine-tuning + early stopping |
|
| 29 |
+
|
| 30 |
+
## 🏷️ Label Mapping
|
| 31 |
+
|
| 32 |
+
| ID | Label | Description |
|
| 33 |
+
|---|---|---|
|
| 34 |
+
| 0 | `normal` | Normal lung tissue |
|
| 35 |
+
| 1 | `malignant` | Malignant (cancerous) tissue |
|
| 36 |
+
| 2 | `benign` | Benign (non-cancerous) tissue |
|
| 37 |
+
|
| 38 |
+
## 🚀 Usage
|
| 39 |
+
|
| 40 |
+
### Install
|
| 41 |
+
|
| 42 |
+
```bash
|
| 43 |
+
pip install transformers torch pillow
|
| 44 |
+
|
| 45 |
+
### Python Inference
|
| 46 |
+
Inference
|
| 47 |
+
|
| 48 |
+
```python
|
| 49 |
+
from transformers import ViTForImageClassification, ViTImageProcessor
|
| 50 |
+
from PIL import Image
|
| 51 |
+
import torch
|
| 52 |
+
|
| 53 |
+
model_id = "TurkishCodeMan/vit-lung-cancer"
|
| 54 |
+
|
| 55 |
+
processor = ViTImageProcessor.from_pretrained(model_id)
|
| 56 |
+
model = ViTForImageClassification.from_pretrained(model_id)
|
| 57 |
+
|
| 58 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 59 |
+
model.eval().to(device)
|
| 60 |
+
|
| 61 |
+
def predict(image_path: str) -> dict:
|
| 62 |
+
img = Image.open(image_path).convert("RGB")
|
| 63 |
+
inputs = processor(images=img, return_tensors="pt").to(device)
|
| 64 |
+
|
| 65 |
+
with torch.no_grad():
|
| 66 |
+
logits = model(**inputs).logits
|
| 67 |
+
|
| 68 |
+
pred_id = logits.argmax(-1).item()
|
| 69 |
+
probs = torch.softmax(logits.float(), dim=-1)[0]
|
| 70 |
+
|
| 71 |
+
return {
|
| 72 |
+
"prediction": model.config.id2label[pred_id],
|
| 73 |
+
"probabilities": {
|
| 74 |
+
label: round(probs[i].item(), 4)
|
| 75 |
+
for i, label in model.config.id2label.items()
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
result = predict("lung_scan.jpg")
|
| 80 |
+
print(result)
|
| 81 |
+
# {'prediction': 'malignant', 'probabilities': {'normal': 0.02, 'malignant': 0.91, 'benign': 0.07}}
|
| 82 |
+
|
| 83 |
+
🛠️ Training Config
|
| 84 |
+
|
| 85 |
+
Parameter: Value
|
| 86 |
+
Optimizer: AdamW
|
| 87 |
+
Learning Rate: 2e-5
|
| 88 |
+
Batch Size: 16
|
| 89 |
+
Max Epochs: 30
|
| 90 |
+
Early Stopping Patience: 5
|
| 91 |
+
Mixed Precision: fp16
|
| 92 |
+
Best Metric: F1-Macro
|
config.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"ViTForImageClassification"
|
| 4 |
+
],
|
| 5 |
+
"attention_probs_dropout_prob": 0.0,
|
| 6 |
+
"dtype": "float32",
|
| 7 |
+
"encoder_stride": 16,
|
| 8 |
+
"hidden_act": "gelu",
|
| 9 |
+
"hidden_dropout_prob": 0.0,
|
| 10 |
+
"hidden_size": 768,
|
| 11 |
+
"id2label": {
|
| 12 |
+
"0": "normal",
|
| 13 |
+
"1": "malignant",
|
| 14 |
+
"2": "benign"
|
| 15 |
+
},
|
| 16 |
+
"image_size": 224,
|
| 17 |
+
"initializer_range": 0.02,
|
| 18 |
+
"intermediate_size": 3072,
|
| 19 |
+
"label2id": {
|
| 20 |
+
"benign": 2,
|
| 21 |
+
"malignant": 1,
|
| 22 |
+
"normal": 0
|
| 23 |
+
},
|
| 24 |
+
"layer_norm_eps": 1e-12,
|
| 25 |
+
"model_type": "vit",
|
| 26 |
+
"num_attention_heads": 12,
|
| 27 |
+
"num_channels": 3,
|
| 28 |
+
"num_hidden_layers": 12,
|
| 29 |
+
"patch_size": 16,
|
| 30 |
+
"pooler_act": "tanh",
|
| 31 |
+
"pooler_output_size": 768,
|
| 32 |
+
"problem_type": "single_label_classification",
|
| 33 |
+
"qkv_bias": true,
|
| 34 |
+
"transformers_version": "5.2.0",
|
| 35 |
+
"use_cache": false
|
| 36 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a88dd7557323a7795db5729f781bbc440d92ebc487027c117bf10a5e17cb9189
|
| 3 |
+
size 343227052
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"do_convert_rgb": null,
|
| 3 |
+
"do_normalize": true,
|
| 4 |
+
"do_rescale": true,
|
| 5 |
+
"do_resize": true,
|
| 6 |
+
"image_mean": [
|
| 7 |
+
0.5,
|
| 8 |
+
0.5,
|
| 9 |
+
0.5
|
| 10 |
+
],
|
| 11 |
+
"image_processor_type": "ViTImageProcessor",
|
| 12 |
+
"image_std": [
|
| 13 |
+
0.5,
|
| 14 |
+
0.5,
|
| 15 |
+
0.5
|
| 16 |
+
],
|
| 17 |
+
"resample": 2,
|
| 18 |
+
"rescale_factor": 0.00392156862745098,
|
| 19 |
+
"size": {
|
| 20 |
+
"height": 224,
|
| 21 |
+
"width": 224
|
| 22 |
+
}
|
| 23 |
+
}
|