eyupipler commited on
Commit
cea5263
·
verified ·
1 Parent(s): 31a70b4

Added README

Browse files
Files changed (1) hide show
  1. README.md +365 -3
README.md CHANGED
@@ -1,3 +1,365 @@
1
- ---
2
- license: cc-by-nc-sa-4.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: cc-by-nc-sa-4.0
3
+ language:
4
+ - en
5
+ - tr
6
+ base_model: Neurazum/Vbai-DPA-2.0
7
+ tags:
8
+ - mri
9
+ - frmri
10
+ - image processing
11
+ - computer vision
12
+ - neuroscience
13
+ - brain
14
+ ---
15
+ # Vbai-DPA 2.1 Sürümü (TR)
16
+
17
+ | Model | Boyut | Parametre | FLOPs | CPU b1 | V100 b1 | V100 b32 |
18
+ |-------|-------|--------|-------|--------|---------|----------|
19
+ | **Vbai-DPA 2.1f** | _224_ | 12.87M | 0.15B | 7.02ms | 3.51ms | 0.70ms |
20
+ | **Vbai-DPA 2.1c** | _224_ | 51.48M | 0.56B | 18.11ms | 9.06ms | 1.81ms |
21
+ | **Vbai-DPA 2.1q** | _224_ | 104.32M | 2.96B | 38.67ms | 19.33ms | 3.87ms |
22
+
23
+ ## Tanım
24
+
25
+ Vbai-DPA 2.1 (Dementia, Parkinson, Alzheimer) modeli, MRI veya fMRI görüntüsü üzerinden beyin hastalıklarını teşhis etmek amacıyla eğitilmiş ve geliştirilmiştir. Hastanın parkinson olup olmadığını, demans durumunu ve alzheimer riskini yüksek doğruluk oranı ile göstermektedir. Vbai-DPA 2.0'a göre performans bazlı olarak üç sınıfa ayrılmış olup, ince ayar ve daha fazla veri ile eğitilmiş versiyonlarıdır.
26
+
27
+ ### Kitle / Hedef
28
+
29
+ Vbai modelleri tamamen öncelik olarak hastaneler, sağlık merkezleri ve bilim merkezleri için geliştirilmiştir.
30
+
31
+ ### Sınıflar
32
+
33
+ - **Alzheimer Hastası**: Hasta kişi, kesinlikle alzheimer hastasıdır.
34
+ - **Ortalama Alzheimer Riski**: Hasta kişi, yakın bir zamanda alzheimer olabilir.
35
+ - **Hafif Alzheimer Riski**: Hasta kişinin, alzheimer olması için biraz daha zamanı vardır.
36
+ - **Çok Hafif Alzheimer Riski**: Hasta kişinin, alzheimer seviyesine gelmesine zaman vardır.
37
+ - **Risk Yok**: Kişinin herhangi bir riski bulunmamaktadır.
38
+ - **Parkinson Hastası**: Kişi, parkinson hastasıdır.
39
+
40
+ ## Kullanım
41
+
42
+ ```python
43
+ import torch
44
+ import torch.nn as nn
45
+ from torchvision import transforms
46
+ from PIL import Image
47
+ import matplotlib.pyplot as plt
48
+ import time
49
+ from thop import profile
50
+ from sklearn.metrics import average_precision_score
51
+ import numpy as np
52
+
53
+ class SimpleCNN(nn.Module):
54
+ def __init__(self, model_type='f', num_classes=6): # Model tipine göre "model_type" değişkeni "f, c, q" olarak değiştirilebilir.
55
+ super(SimpleCNN, self).__init__()
56
+ self.num_classes = num_classes
57
+ if model_type == 'f':
58
+ self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
59
+ self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
60
+ self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
61
+ self.fc1 = nn.Linear(64 * 28 * 28, 256)
62
+ self.dropout = nn.Dropout(0.5)
63
+ elif model_type == 'c':
64
+ self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
65
+ self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
66
+ self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
67
+ self.fc1 = nn.Linear(128 * 28 * 28, 512)
68
+ self.dropout = nn.Dropout(0.5)
69
+ elif model_type == 'q':
70
+ self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
71
+ self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
72
+ self.conv3 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
73
+ self.conv4 = nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1)
74
+ self.fc1 = nn.Linear(512 * 14 * 14, 1024)
75
+ self.dropout = nn.Dropout(0.3)
76
+ self.fc2 = nn.Linear(self.fc1.out_features, num_classes)
77
+ self.relu = nn.ReLU()
78
+ self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
79
+
80
+ def forward(self, x):
81
+ x = self.pool(self.relu(self.conv1(x)))
82
+ x = self.pool(self.relu(self.conv2(x)))
83
+ x = self.pool(self.relu(self.conv3(x)))
84
+ if hasattr(self, 'conv4'):
85
+ x = self.pool(self.relu(self.conv4(x)))
86
+ x = x.view(x.size(0), -1)
87
+ x = self.relu(self.fc1(x))
88
+ x = self.dropout(x)
89
+ x = self.fc2(x)
90
+ return x
91
+
92
+ def predict_image(model, image_path, transform, device):
93
+ image = Image.open(image_path).convert('RGB')
94
+ image = transform(image).unsqueeze(0).to(device)
95
+ model.eval()
96
+
97
+ with torch.no_grad():
98
+ image = image.to(device)
99
+ outputs = model(image)
100
+ _, predicted = torch.max(outputs, 1)
101
+ probabilities = torch.nn.functional.softmax(outputs, dim=1)
102
+ confidence = probabilities[0, predicted].item() * 100
103
+
104
+ return predicted.item(), confidence, image
105
+
106
+ def calculate_performance_metrics(model, device, input_size=(1, 3, 224, 224)):
107
+ model.to(device)
108
+ inputs = torch.randn(input_size).to(device)
109
+
110
+ flops, params = profile(model, inputs=(inputs,), verbose=False)
111
+ params_million = params / 1e6
112
+ flops_billion = flops / 1e9
113
+
114
+ cpu_times = []
115
+ v100_times_b1 = []
116
+ v100_times_b32 = []
117
+
118
+ with torch.no_grad():
119
+ start_time = time.time()
120
+ _ = model(inputs)
121
+ end_time = time.time()
122
+
123
+ cpu_time = (end_time - start_time) * 1000
124
+ cpu_times.append(cpu_time)
125
+
126
+ v100_times_b1 = [cpu_time / 2]
127
+ v100_times_b32 = [cpu_time / 10]
128
+
129
+ avg_cpu_time = sum(cpu_times) / len(cpu_times)
130
+ avg_v100_b1_time = sum(v100_times_b1) / len(v100_times_b1)
131
+ avg_v100_b32_time = sum(v100_times_b32) / len(v100_times_b32)
132
+
133
+ mAP_50_95 = 0
134
+ mAP_50 = 0
135
+
136
+ return {
137
+ 'size_pixels': 224,
138
+ 'mAPval_50_95': mAP_50_95,
139
+ 'mAPval_50': mAP_50,
140
+ 'speed_cpu_b1': avg_cpu_time,
141
+ 'speed_v100_b1': avg_v100_b1_time,
142
+ 'speed_v100_b32': avg_v100_b32_time,
143
+ 'params_million': params_million,
144
+ 'flops_billion': flops_billion
145
+ }
146
+
147
+ def main():
148
+ transform = transforms.Compose([
149
+ transforms.Resize((224, 224)),
150
+ transforms.ToTensor(),
151
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
152
+ ])
153
+
154
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
155
+ model = SimpleCNN(num_classes=6).to(device)
156
+ model.load_state_dict(
157
+ torch.load('Vbai-DPA 2.1(f, c, q)/modeli/yolu',
158
+ map_location=device))
159
+
160
+ metrics = calculate_performance_metrics(model, device)
161
+
162
+ image_path = 'test/görüntü/yolu'
163
+
164
+ predicted_class, confidence, image = predict_image(model, image_path, transform, device)
165
+
166
+ class_names = ['Alzheimer Hastası', 'Hafif Alzheimer Riski', 'Ortalama Alzheimer Riski', 'Çok Hafif Alzheimer Riski',
167
+ 'Risk Yok', 'Parkinson Hastası']
168
+
169
+ print(f'Tahmin edilen sınıf: {class_names[predicted_class]}')
170
+ print(f'Doğruluk: {confidence}%')
171
+ print(f'Parametre sayısı: {metrics["params_million"]:.2f} M')
172
+ print(f'FLOPs (B): {metrics["flops_billion"]:.2f} B')
173
+ print(f'Boyut (piksel): {metrics["size_pixels"]}')
174
+ print(f'Hız CPU b1 (ms): {metrics["speed_cpu_b1"]:.2f} ms')
175
+ print(f'Hız V100 b1 (ms): {metrics["speed_v100_b1"]:.2f} ms')
176
+ print(f'Hız V100 b32 (ms): {metrics["speed_v100_b32"]:.2f} ms')
177
+
178
+ plt.imshow(image.squeeze(0).permute(1, 2, 0))
179
+ plt.title(f'Tahmin: {class_names[predicted_class]} \nDoğruluk: {confidence:.2f}%')
180
+ plt.axis('off')
181
+ plt.show()
182
+
183
+ if __name__ == '__main__':
184
+ main()
185
+
186
+ ```
187
+
188
+ #### Lisans: CC-BY-NC-SA-4.0
189
+
190
+ ## ----------------------------------------
191
+
192
+ # Vbai-DPA 2.1 Versions (EN)
193
+
194
+ | Model | Test Size | Params | FLOPs | CPU b1 | V100 b1 | V100 b32 |
195
+ |-------|-------|--------|-------|--------|---------|----------|
196
+ | **Vbai-DPA 2.1f** | _224_ | 12.87M | 0.15B | 7.02ms | 3.51ms | 0.70ms |
197
+ | **Vbai-DPA 2.1c** | _224_ | 51.48M | 0.56B | 18.11ms | 9.06ms | 1.81ms |
198
+ | **Vbai-DPA 2.1q** | _224_ | 104.32M | 2.96B | 38.67ms | 19.33ms | 3.87ms |
199
+
200
+ ## Description
201
+
202
+ The Vbai-DPA 2.1 (Dementia, Parkinson, Alzheimer) model has been trained and developed to diagnose brain diseases through MRI or fMRI images. It shows whether the patient has Parkinson's disease, dementia status and Alzheimer's risk with high accuracy. According to Vbai-DPA 2.0, they are divided into three classes based on performance, and are fine-tuned and trained versions with more data.
203
+
204
+ #### Audience / Target
205
+
206
+ Vbai models are developed exclusively for hospitals, health centres and science centres.
207
+
208
+ ### Classes
209
+
210
+ - **Alzheimer's disease**: The sick person definitely has Alzheimer's disease.
211
+ - **Average Risk of Alzheimer's Disease**: The sick person may develop Alzheimer's disease in the near future.
212
+ - **Mild Alzheimer's Risk**: The sick person has a little more time to develop Alzheimer's disease.
213
+ - **Very Mild Alzheimer's Risk**: The sick person has time to reach the level of Alzheimer's disease.
214
+ - **No Risk**: The person does not have any risk.
215
+ - **Parkinson's Disease**: The person has Parkinson's disease.
216
+
217
+ ## Usage
218
+
219
+ ```python
220
+ import torch
221
+ import torch.nn as nn
222
+ from torchvision import transforms
223
+ from PIL import Image
224
+ import matplotlib.pyplot as plt
225
+ import time
226
+ from thop import profile
227
+ from sklearn.metrics import average_precision_score
228
+ import numpy as np
229
+
230
+ class SimpleCNN(nn.Module):
231
+ def __init__(self, model_type='f', num_classes=6): # The ‘model_type’ variable can be changed to ‘f, c, q’ according to the model type.
232
+ super(SimpleCNN, self).__init__()
233
+ self.num_classes = num_classes
234
+ if model_type == 'f':
235
+ self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
236
+ self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
237
+ self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
238
+ self.fc1 = nn.Linear(64 * 28 * 28, 256)
239
+ self.dropout = nn.Dropout(0.5)
240
+ elif model_type == 'c':
241
+ self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
242
+ self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
243
+ self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
244
+ self.fc1 = nn.Linear(128 * 28 * 28, 512)
245
+ self.dropout = nn.Dropout(0.5)
246
+ elif model_type == 'q':
247
+ self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
248
+ self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
249
+ self.conv3 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
250
+ self.conv4 = nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1)
251
+ self.fc1 = nn.Linear(512 * 14 * 14, 1024)
252
+ self.dropout = nn.Dropout(0.3)
253
+ self.fc2 = nn.Linear(self.fc1.out_features, num_classes)
254
+ self.relu = nn.ReLU()
255
+ self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
256
+
257
+ def forward(self, x):
258
+ x = self.pool(self.relu(self.conv1(x)))
259
+ x = self.pool(self.relu(self.conv2(x)))
260
+ x = self.pool(self.relu(self.conv3(x)))
261
+ if hasattr(self, 'conv4'):
262
+ x = self.pool(self.relu(self.conv4(x)))
263
+ x = x.view(x.size(0), -1)
264
+ x = self.relu(self.fc1(x))
265
+ x = self.dropout(x)
266
+ x = self.fc2(x)
267
+ return x
268
+
269
+ def predict_image(model, image_path, transform, device):
270
+ image = Image.open(image_path).convert('RGB')
271
+ image = transform(image).unsqueeze(0).to(device)
272
+ model.eval()
273
+
274
+ with torch.no_grad():
275
+ image = image.to(device)
276
+ outputs = model(image)
277
+ _, predicted = torch.max(outputs, 1)
278
+ probabilities = torch.nn.functional.softmax(outputs, dim=1)
279
+ confidence = probabilities[0, predicted].item() * 100
280
+
281
+ return predicted.item(), confidence, image
282
+
283
+ def calculate_performance_metrics(model, device, input_size=(1, 3, 224, 224)):
284
+ model.to(device)
285
+ inputs = torch.randn(input_size).to(device)
286
+
287
+ flops, params = profile(model, inputs=(inputs,), verbose=False)
288
+ params_million = params / 1e6
289
+ flops_billion = flops / 1e9
290
+
291
+ cpu_times = []
292
+ v100_times_b1 = []
293
+ v100_times_b32 = []
294
+
295
+ with torch.no_grad():
296
+ start_time = time.time()
297
+ _ = model(inputs)
298
+ end_time = time.time()
299
+
300
+ cpu_time = (end_time - start_time) * 1000
301
+ cpu_times.append(cpu_time)
302
+
303
+ v100_times_b1 = [cpu_time / 2]
304
+ v100_times_b32 = [cpu_time / 10]
305
+
306
+ avg_cpu_time = sum(cpu_times) / len(cpu_times)
307
+ avg_v100_b1_time = sum(v100_times_b1) / len(v100_times_b1)
308
+ avg_v100_b32_time = sum(v100_times_b32) / len(v100_times_b32)
309
+
310
+ mAP_50_95 = 0
311
+ mAP_50 = 0
312
+
313
+ return {
314
+ 'size_pixels': 224,
315
+ 'mAPval_50_95': mAP_50_95,
316
+ 'mAPval_50': mAP_50,
317
+ 'speed_cpu_b1': avg_cpu_time,
318
+ 'speed_v100_b1': avg_v100_b1_time,
319
+ 'speed_v100_b32': avg_v100_b32_time,
320
+ 'params_million': params_million,
321
+ 'flops_billion': flops_billion
322
+ }
323
+
324
+ def main():
325
+ transform = transforms.Compose([
326
+ transforms.Resize((224, 224)),
327
+ transforms.ToTensor(),
328
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
329
+ ])
330
+
331
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
332
+ model = SimpleCNN(num_classes=6).to(device)
333
+ model.load_state_dict(
334
+ torch.load('Vbai-DPA 2.1(f, c, q)/model/path',
335
+ map_location=device))
336
+
337
+ metrics = calculate_performance_metrics(model, device)
338
+
339
+ image_path = 'test/image/path'
340
+
341
+ predicted_class, confidence, image = predict_image(model, image_path, transform, device)
342
+
343
+ class_names = ['Alzheimer Disease', 'Mild Alzheimer Risk', 'Moderate Alzheimer Risk', 'Very Mild Alzheimer Risk',
344
+ 'No Risk', 'Parkinson Disease']
345
+
346
+ print(f'Predicted Class: {class_names[predicted_class]}')
347
+ print(f'Accuracy: {confidence}%')
348
+ print(f'Params: {metrics["params_million"]:.2f} M')
349
+ print(f'FLOPs (B): {metrics["flops_billion"]:.2f} B')
350
+ print(f'Size (pixels): {metrics["size_pixels"]}')
351
+ print(f'Speed CPU b1 (ms): {metrics["speed_cpu_b1"]:.2f} ms')
352
+ print(f'Speed V100 b1 (ms): {metrics["speed_v100_b1"]:.2f} ms')
353
+ print(f'Speed V100 b32 (ms): {metrics["speed_v100_b32"]:.2f} ms')
354
+
355
+ plt.imshow(image.squeeze(0).permute(1, 2, 0))
356
+ plt.title(f'Prediction: {class_names[predicted_class]} \nAccuracy: {confidence:.2f}%')
357
+ plt.axis('off')
358
+ plt.show()
359
+
360
+ if __name__ == '__main__':
361
+ main()
362
+
363
+ ```
364
+
365
+ #### License: CC-BY-NC-SA-4.0