EngReem85 commited on
Commit
bbbee9c
·
verified ·
1 Parent(s): e8fd25a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import torch
3
+ import torch.nn as nn
4
+ import torchvision.transforms as transforms
5
+ from PIL import Image
6
+ import numpy as np
7
+ import gradio as gr
8
+
9
+ # -------------------------------
10
+ # 1️⃣ إعداد الفئات
11
+ # -------------------------------
12
+ CLASSES = ["NONE", "INFECTION", "ISCHAEMIA", "BOTH"]
13
+
14
+ # -------------------------------
15
+ # 2️⃣ تعريف نموذج DenseShuffleGCANet (مثال مختصر، عدلي حسب نموذجك الحقيقي)
16
+ # -------------------------------
17
+ class DenseShuffleGCANet(nn.Module):
18
+ def __init__(self, num_classes=4, handcrafted_feature_dim=41):
19
+ super(DenseShuffleGCANet, self).__init__()
20
+ # مثال على backbone، عدلي حسب الكود الأصلي
21
+ self.backbone = nn.Sequential(
22
+ nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
23
+ nn.ReLU(),
24
+ nn.AdaptiveAvgPool2d((1,1))
25
+ )
26
+ self.fc_handcrafted = nn.Linear(handcrafted_feature_dim, 32)
27
+ self.classifier = nn.Linear(64 + 32, num_classes)
28
+
29
+ def forward(self, x_image, x_features):
30
+ x_img = self.backbone(x_image)
31
+ x_img = x_img.view(x_img.size(0), -1)
32
+ x_feat = self.fc_handcrafted(x_features)
33
+ x = torch.cat([x_img, x_feat], dim=1)
34
+ out = self.classifier(x)
35
+ return out
36
+
37
+ # -------------------------------
38
+ # 3️⃣ تحميل النموذج المدرب
39
+ # -------------------------------
40
+ model = DenseShuffleGCANet(num_classes=4, handcrafted_feature_dim=41)
41
+ model.load_state_dict(torch.load("best_model.pth", map_location=torch.device('cpu')))
42
+ model.eval()
43
+
44
+ # -------------------------------
45
+ # 4️⃣ دالة استخراج الخصائص اليدوية
46
+ # -------------------------------
47
+ def extract_handcrafted_features(image_array):
48
+ """
49
+ ضع هنا كود استخراج الخصائص اليدوية الذي كنتِ تستخدمينه أثناء التدريب
50
+ يجب أن تعيد numpy array بحجم (41,)
51
+ """
52
+ # مثال عشوائي لتوضيح الفكرة
53
+ features = np.random.rand(41).astype(np.float32)
54
+ return torch.tensor(features)
55
+
56
+ # -------------------------------
57
+ # 5️⃣ دالة التنبؤ
58
+ # -------------------------------
59
+ def predict_image(image: Image.Image):
60
+ # تحويل الصورة للصيغة المناسبة للنموذج
61
+ transform = transforms.Compose([
62
+ transforms.Resize((224, 224)),
63
+ transforms.ToTensor(),
64
+ transforms.Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225])
65
+ ])
66
+ image_tensor = transform(image).unsqueeze(0) # إضافة batch dimension
67
+
68
+ # استخراج الخصائص اليدوية
69
+ features = extract_handcrafted_features(np.array(image)).unsqueeze(0)
70
+
71
+ # توقع النموذج
72
+ with torch.no_grad():
73
+ outputs = model(image_tensor, features)
74
+ probs = torch.softmax(outputs, dim=1).cpu().numpy()[0]
75
+ pred_class = CLASSES[np.argmax(probs)]
76
+
77
+ # إرجاع النتيجة ك probabilities + الفئة المتوقعة
78
+ return {CLASSES[i]: float(probs[i]) for i in range(len(CLASSES))}, pred_class
79
+
80
+ # -------------------------------
81
+ # 6️⃣ واجهة Gradio
82
+ # -------------------------------
83
+ interface = gr.Interface(
84
+ fn=predict_image,
85
+ inputs=gr.Image(type="pil"),
86
+ outputs=[gr.Label(num_top_classes=4), gr.Textbox(label="Predicted Class")],
87
+ title="DFU Foot Ulcer Classifier",
88
+ description="Upload an image of a foot ulcer to classify it as NONE, INFECTION, ISCHAEMIA, or BOTH."
89
+ )
90
+
91
+ if __name__ == "__main__":
92
+ interface.launch()