Instructions to use eyad-ai/SmartChestXRay with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use eyad-ai/SmartChestXRay with PEFT:
Task type is invalid.
- Transformers
How to use eyad-ai/SmartChestXRay with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("eyad-ai/SmartChestXRay", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files- backbone/config.json +48 -0
- backbone/model.safetensors +3 -0
- classifier_head.pt +3 -0
- handler.py +82 -0
- meta.json +20 -0
- preprocessor_config.json +27 -0
- requirements.txt +6 -0
backbone/config.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"apply_layernorm": true,
|
| 3 |
+
"architectures": [
|
| 4 |
+
"Dinov2Model"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.0,
|
| 7 |
+
"drop_path_rate": 0.0,
|
| 8 |
+
"dtype": "float32",
|
| 9 |
+
"hidden_act": "gelu",
|
| 10 |
+
"hidden_dropout_prob": 0.0,
|
| 11 |
+
"hidden_size": 768,
|
| 12 |
+
"image_size": 518,
|
| 13 |
+
"initializer_range": 0.02,
|
| 14 |
+
"layer_norm_eps": 1e-06,
|
| 15 |
+
"layerscale_value": 1.0,
|
| 16 |
+
"mlp_ratio": 4,
|
| 17 |
+
"model_type": "dinov2",
|
| 18 |
+
"num_attention_heads": 12,
|
| 19 |
+
"num_channels": 3,
|
| 20 |
+
"num_hidden_layers": 12,
|
| 21 |
+
"out_features": [
|
| 22 |
+
"stage12"
|
| 23 |
+
],
|
| 24 |
+
"out_indices": [
|
| 25 |
+
12
|
| 26 |
+
],
|
| 27 |
+
"patch_size": 14,
|
| 28 |
+
"qkv_bias": true,
|
| 29 |
+
"reshape_hidden_states": true,
|
| 30 |
+
"stage_names": [
|
| 31 |
+
"stem",
|
| 32 |
+
"stage1",
|
| 33 |
+
"stage2",
|
| 34 |
+
"stage3",
|
| 35 |
+
"stage4",
|
| 36 |
+
"stage5",
|
| 37 |
+
"stage6",
|
| 38 |
+
"stage7",
|
| 39 |
+
"stage8",
|
| 40 |
+
"stage9",
|
| 41 |
+
"stage10",
|
| 42 |
+
"stage11",
|
| 43 |
+
"stage12"
|
| 44 |
+
],
|
| 45 |
+
"transformers_version": "5.13.1",
|
| 46 |
+
"use_mask_token": true,
|
| 47 |
+
"use_swiglu_ffn": false
|
| 48 |
+
}
|
backbone/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:49547d64df29c4b6e3432449a015859bbe9a5965147a22bdf574d8605aa36c8e
|
| 3 |
+
size 346345912
|
classifier_head.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e40f1daccbe9afa47bcf4ddfea70731fa54961913546cd3d414f1c15463b0cd1
|
| 3 |
+
size 14309
|
handler.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
from transformers import AutoImageProcessor, AutoModel, AutoModelForImageClassification
|
| 8 |
+
|
| 9 |
+
class EndpointHandler:
|
| 10 |
+
def __init__(self, path=""):
|
| 11 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
|
| 13 |
+
# قراءة معلومات الأصناف
|
| 14 |
+
meta_file = os.path.join(path, "meta.json")
|
| 15 |
+
with open(meta_file, "r") as f:
|
| 16 |
+
meta = json.load(f)
|
| 17 |
+
self.classes = meta["classes"]
|
| 18 |
+
self.id2label = {int(k): v for k, v in meta["id2label"].items()}
|
| 19 |
+
self.label2id = meta["label2id"]
|
| 20 |
+
|
| 21 |
+
# تحميل المعالج
|
| 22 |
+
self.processor = AutoImageProcessor.from_pretrained(path)
|
| 23 |
+
|
| 24 |
+
# تحميل الخلفية (Backbone)
|
| 25 |
+
backbone_path = os.path.join(path, "backbone")
|
| 26 |
+
self.backbone = AutoModel.from_pretrained(backbone_path).to(self.device)
|
| 27 |
+
self.backbone.eval()
|
| 28 |
+
|
| 29 |
+
# بناء هيكل النموذج الموحد
|
| 30 |
+
self.model = AutoModelForImageClassification.from_pretrained(
|
| 31 |
+
"microsoft/rad-dino",
|
| 32 |
+
num_labels=len(self.classes),
|
| 33 |
+
id2label=self.id2label,
|
| 34 |
+
label2id=self.label2id
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# ربط الخلفية
|
| 38 |
+
if hasattr(self.model, 'dinov2'):
|
| 39 |
+
self.model.dinov2 = self.backbone
|
| 40 |
+
elif hasattr(self.model, 'backbone'):
|
| 41 |
+
self.model.backbone = self.backbone
|
| 42 |
+
|
| 43 |
+
# تحميل أوزان رأس التصنيف
|
| 44 |
+
head_file = os.path.join(path, "classifier_head.pt")
|
| 45 |
+
state_dict = torch.load(head_file, map_location=self.device)
|
| 46 |
+
self.model.classifier.weight.copy_(state_dict["1.weight"])
|
| 47 |
+
self.model.classifier.bias.copy_(state_dict["1.bias"])
|
| 48 |
+
|
| 49 |
+
self.model.to(self.device)
|
| 50 |
+
self.model.eval()
|
| 51 |
+
|
| 52 |
+
def __call__(self, data):
|
| 53 |
+
inputs = data.get("inputs", data)
|
| 54 |
+
|
| 55 |
+
# معالجة الصورة القادمة أياً كانت صيغتها
|
| 56 |
+
if isinstance(inputs, str):
|
| 57 |
+
image = Image.open(inputs).convert("RGB")
|
| 58 |
+
elif isinstance(inputs, Image.Image):
|
| 59 |
+
image = inputs.convert("RGB")
|
| 60 |
+
elif isinstance(inputs, bytes):
|
| 61 |
+
image = Image.open(io.BytesIO(inputs)).convert("RGB")
|
| 62 |
+
else:
|
| 63 |
+
# إذا أرسلت كملف بايتس عبر الـ API
|
| 64 |
+
image = Image.open(io.BytesIO(inputs)).convert("RGB")
|
| 65 |
+
|
| 66 |
+
processed_inputs = self.processor(images=image, return_tensors="pt").to(self.device)
|
| 67 |
+
|
| 68 |
+
with torch.no_grad():
|
| 69 |
+
outputs = self.model(**processed_inputs)
|
| 70 |
+
logits = outputs.logits
|
| 71 |
+
probs = torch.softmax(logits, dim=-1)[0]
|
| 72 |
+
|
| 73 |
+
results = []
|
| 74 |
+
for idx, prob in enumerate(probs):
|
| 75 |
+
results.append({
|
| 76 |
+
"label": self.id2label[idx],
|
| 77 |
+
"score": float(prob.item())
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
# ترتيب النتائج من الأعلى للأقل
|
| 81 |
+
results = sorted(results, key=lambda x: x["score"], reverse=True)
|
| 82 |
+
return results
|
meta.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"classes": [
|
| 3 |
+
"COVID",
|
| 4 |
+
"Lung_Opacity",
|
| 5 |
+
"Normal",
|
| 6 |
+
"Viral Pneumonia"
|
| 7 |
+
],
|
| 8 |
+
"id2label": {
|
| 9 |
+
"0": "COVID",
|
| 10 |
+
"1": "Lung_Opacity",
|
| 11 |
+
"2": "Normal",
|
| 12 |
+
"3": "Viral Pneumonia"
|
| 13 |
+
},
|
| 14 |
+
"label2id": {
|
| 15 |
+
"0": "COVID",
|
| 16 |
+
"1": "Lung_Opacity",
|
| 17 |
+
"2": "Normal",
|
| 18 |
+
"3": "Viral Pneumonia"
|
| 19 |
+
}
|
| 20 |
+
}
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"crop_size": {
|
| 3 |
+
"height": 518,
|
| 4 |
+
"width": 518
|
| 5 |
+
},
|
| 6 |
+
"do_center_crop": true,
|
| 7 |
+
"do_convert_rgb": true,
|
| 8 |
+
"do_normalize": true,
|
| 9 |
+
"do_rescale": true,
|
| 10 |
+
"do_resize": true,
|
| 11 |
+
"image_mean": [
|
| 12 |
+
0.5307,
|
| 13 |
+
0.5307,
|
| 14 |
+
0.5307
|
| 15 |
+
],
|
| 16 |
+
"image_processor_type": "BitImageProcessor",
|
| 17 |
+
"image_std": [
|
| 18 |
+
0.2583,
|
| 19 |
+
0.2583,
|
| 20 |
+
0.2583
|
| 21 |
+
],
|
| 22 |
+
"resample": 3,
|
| 23 |
+
"rescale_factor": 0.00392156862745098,
|
| 24 |
+
"size": {
|
| 25 |
+
"shortest_edge": 518
|
| 26 |
+
}
|
| 27 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
transformers
|
| 4 |
+
peft
|
| 5 |
+
Pillow
|
| 6 |
+
safetensors
|