File size: 13,963 Bytes
cea5263
 
 
 
 
 
 
 
 
 
 
 
 
da5e2ab
cea5263
5cf8e2c
 
 
cea5263
 
b4b5a32
 
 
 
 
cea5263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4b5a32
 
 
 
 
cea5263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
---
license: cc-by-nc-sa-4.0
language:
- en
- tr
base_model: Neurazum/Vbai-DPA-2.0
tags:
- mri
- frmri
- image processing
- computer vision
- neuroscience
- brain
pipeline_tag: image-classification
---

![image/png](https://cdn-uploads.huggingface.co/production/uploads/65e21f5133d3600496498125/SJV2wRNb488bQMTOL0TDJ.png)

# Vbai-DPA 2.1 Sürümü (TR)

| Model | Boyut | Parametre | FLOPs | mAPᵛᵃᴵ | CPU b1 | V100 b1 | V100 b32 |
|-------|-------|--------|-------|--------|--------|---------|----------|
| **Vbai-DPA 2.1f** | _224_ | 12.87M | 0.15B | %78.56 | 7.02ms | 3.51ms | 0.70ms |
| **Vbai-DPA 2.1c** | _224_ | 51.48M | 0.56B | %78.0 | 18.11ms | 9.06ms | 1.81ms |
| **Vbai-DPA 2.1q** | _224_ | 104.32M | 2.96B | %79.01 | 38.67ms | 19.33ms | 3.87ms |

## Tanım

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.

### Kitle / Hedef

Vbai modelleri tamamen öncelik olarak hastaneler, sağlık merkezleri ve bilim merkezleri için geliştirilmiştir.

### Sınıflar

 - **Alzheimer Hastası**: Hasta kişi, kesinlikle alzheimer hastasıdır.
 - **Ortalama Alzheimer Riski**: Hasta kişi, yakın bir zamanda alzheimer olabilir.
 - **Hafif Alzheimer Riski**: Hasta kişinin, alzheimer olması için biraz daha zamanı vardır.
 - **Çok Hafif Alzheimer Riski**: Hasta kişinin, alzheimer seviyesine gelmesine zaman vardır.
 - **Risk Yok**: Kişinin herhangi bir riski bulunmamaktadır.
 - **Parkinson Hastası**: Kişi, parkinson hastasıdır.

## Kullanım

```python
import torch
import torch.nn as nn
from torchvision import transforms
from PIL import Image
import matplotlib.pyplot as plt
import time
from thop import profile

class SimpleCNN(nn.Module):
    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.
        super(SimpleCNN, self).__init__()
        self.num_classes = num_classes
        if model_type == 'f':
            self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
            self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
            self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
            self.fc1 = nn.Linear(64 * 28 * 28, 256)
            self.dropout = nn.Dropout(0.5)
        elif model_type == 'c':
            self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
            self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
            self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
            self.fc1 = nn.Linear(128 * 28 * 28, 512)
            self.dropout = nn.Dropout(0.5)
        elif model_type == 'q':
            self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
            self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
            self.conv3 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
            self.conv4 = nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1)
            self.fc1 = nn.Linear(512 * 14 * 14, 1024)
            self.dropout = nn.Dropout(0.3)
        self.fc2 = nn.Linear(self.fc1.out_features, num_classes)
        self.relu = nn.ReLU()
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)

    def forward(self, x):
        x = self.pool(self.relu(self.conv1(x)))
        x = self.pool(self.relu(self.conv2(x)))
        x = self.pool(self.relu(self.conv3(x)))
        if hasattr(self, 'conv4'):
            x = self.pool(self.relu(self.conv4(x)))
        x = x.view(x.size(0), -1)
        x = self.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.fc2(x)
        return x

def predict_image(model, image_path, transform, device):
    image = Image.open(image_path).convert('RGB')
    image = transform(image).unsqueeze(0).to(device)
    model.eval()

    with torch.no_grad():
        image = image.to(device)
        outputs = model(image)
        _, predicted = torch.max(outputs, 1)
        probabilities = torch.nn.functional.softmax(outputs, dim=1)
        confidence = probabilities[0, predicted].item() * 100

    return predicted.item(), confidence, image

def calculate_performance_metrics(model, device, input_size=(1, 3, 224, 224)):
    model.to(device)
    inputs = torch.randn(input_size).to(device)

    flops, params = profile(model, inputs=(inputs,), verbose=False)
    params_million = params / 1e6
    flops_billion = flops / 1e9

    cpu_times = []
    v100_times_b1 = []
    v100_times_b32 = []

    with torch.no_grad():
        start_time = time.time()
        _ = model(inputs)
        end_time = time.time()

        cpu_time = (end_time - start_time) * 1000
        cpu_times.append(cpu_time)

        v100_times_b1 = [cpu_time / 2]
        v100_times_b32 = [cpu_time / 10]

    avg_cpu_time = sum(cpu_times) / len(cpu_times)
    avg_v100_b1_time = sum(v100_times_b1) / len(v100_times_b1)
    avg_v100_b32_time = sum(v100_times_b32) / len(v100_times_b32)

    return {
        'size_pixels': 224,
        'speed_cpu_b1': avg_cpu_time,
        'speed_v100_b1': avg_v100_b1_time,
        'speed_v100_b32': avg_v100_b32_time,
        'params_million': params_million,
        'flops_billion': flops_billion
    }

def main():
    transform = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = SimpleCNN(num_classes=6).to(device)
    model.load_state_dict(
        torch.load('Vbai-DPA 2.1(f, c, q)/modeli/yolu',
                   map_location=device))

    metrics = calculate_performance_metrics(model, device)

    image_path = 'test/görüntü/yolu'

    predicted_class, confidence, image = predict_image(model, image_path, transform, device)

    class_names = ['Alzheimer Hastası', 'Hafif Alzheimer Riski', 'Ortalama Alzheimer Riski', 'Çok Hafif Alzheimer Riski',
                   'Risk Yok', 'Parkinson Hastası']

    print(f'Tahmin edilen sınıf: {class_names[predicted_class]}')
    print(f'Doğruluk: {confidence}%')
    print(f'Parametre sayısı: {metrics["params_million"]:.2f} M')
    print(f'FLOPs (B): {metrics["flops_billion"]:.2f} B')
    print(f'Boyut (piksel): {metrics["size_pixels"]}')
    print(f'Hız CPU b1 (ms): {metrics["speed_cpu_b1"]:.2f} ms')
    print(f'Hız V100 b1 (ms): {metrics["speed_v100_b1"]:.2f} ms')
    print(f'Hız V100 b32 (ms): {metrics["speed_v100_b32"]:.2f} ms')

    plt.imshow(image.squeeze(0).permute(1, 2, 0))
    plt.title(f'Tahmin: {class_names[predicted_class]} \nDoğruluk: {confidence:.2f}%')
    plt.axis('off')
    plt.show()

if __name__ == '__main__':
    main()

```

#### Lisans: CC-BY-NC-SA-4.0

## ----------------------------------------

# Vbai-DPA 2.1 Versions (EN)

| Model | Test Size | Params | FLOPs | mAPᵛᵃᴵ | CPU b1 | V100 b1 | V100 b32 |
|-------|-------|--------|-------|--------|--------|---------|----------|
| **Vbai-DPA 2.1f** | _224_ | 12.87M | 0.15B | %78.56 | 7.02ms | 3.51ms | 0.70ms |
| **Vbai-DPA 2.1c** | _224_ | 51.48M | 0.56B | %78.0 | 18.11ms | 9.06ms | 1.81ms |
| **Vbai-DPA 2.1q** | _224_ | 104.32M | 2.96B | %79.01 | 38.67ms | 19.33ms | 3.87ms |

## Description

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.

#### Audience / Target

Vbai models are developed exclusively for hospitals, health centres and science centres.

### Classes

 - **Alzheimer's disease**: The sick person definitely has Alzheimer's disease.
 - **Average Risk of Alzheimer's Disease**: The sick person may develop Alzheimer's disease in the near future.
 - **Mild Alzheimer's Risk**: The sick person has a little more time to develop Alzheimer's disease.
 - **Very Mild Alzheimer's Risk**: The sick person has time to reach the level of Alzheimer's disease.
 - **No Risk**: The person does not have any risk.
 - **Parkinson's Disease**: The person has Parkinson's disease.

## Usage

```python
import torch
import torch.nn as nn
from torchvision import transforms
from PIL import Image
import matplotlib.pyplot as plt
import time
from thop import profile

class SimpleCNN(nn.Module):
    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.
        super(SimpleCNN, self).__init__()
        self.num_classes = num_classes
        if model_type == 'f':
            self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
            self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
            self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
            self.fc1 = nn.Linear(64 * 28 * 28, 256)
            self.dropout = nn.Dropout(0.5)
        elif model_type == 'c':
            self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
            self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
            self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
            self.fc1 = nn.Linear(128 * 28 * 28, 512)
            self.dropout = nn.Dropout(0.5)
        elif model_type == 'q':
            self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
            self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
            self.conv3 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
            self.conv4 = nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1)
            self.fc1 = nn.Linear(512 * 14 * 14, 1024)
            self.dropout = nn.Dropout(0.3)
        self.fc2 = nn.Linear(self.fc1.out_features, num_classes)
        self.relu = nn.ReLU()
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)

    def forward(self, x):
        x = self.pool(self.relu(self.conv1(x)))
        x = self.pool(self.relu(self.conv2(x)))
        x = self.pool(self.relu(self.conv3(x)))
        if hasattr(self, 'conv4'):
            x = self.pool(self.relu(self.conv4(x)))
        x = x.view(x.size(0), -1)
        x = self.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.fc2(x)
        return x

def predict_image(model, image_path, transform, device):
    image = Image.open(image_path).convert('RGB')
    image = transform(image).unsqueeze(0).to(device)
    model.eval()

    with torch.no_grad():
        image = image.to(device)
        outputs = model(image)
        _, predicted = torch.max(outputs, 1)
        probabilities = torch.nn.functional.softmax(outputs, dim=1)
        confidence = probabilities[0, predicted].item() * 100

    return predicted.item(), confidence, image

def calculate_performance_metrics(model, device, input_size=(1, 3, 224, 224)):
    model.to(device)
    inputs = torch.randn(input_size).to(device)

    flops, params = profile(model, inputs=(inputs,), verbose=False)
    params_million = params / 1e6
    flops_billion = flops / 1e9

    cpu_times = []
    v100_times_b1 = []
    v100_times_b32 = []

    with torch.no_grad():
        start_time = time.time()
        _ = model(inputs)
        end_time = time.time()

        cpu_time = (end_time - start_time) * 1000
        cpu_times.append(cpu_time)

        v100_times_b1 = [cpu_time / 2]
        v100_times_b32 = [cpu_time / 10]

    avg_cpu_time = sum(cpu_times) / len(cpu_times)
    avg_v100_b1_time = sum(v100_times_b1) / len(v100_times_b1)
    avg_v100_b32_time = sum(v100_times_b32) / len(v100_times_b32)

    return {
        'size_pixels': 224,
        'speed_cpu_b1': avg_cpu_time,
        'speed_v100_b1': avg_v100_b1_time,
        'speed_v100_b32': avg_v100_b32_time,
        'params_million': params_million,
        'flops_billion': flops_billion
    }

def main():
    transform = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = SimpleCNN(num_classes=6).to(device)
    model.load_state_dict(
        torch.load('Vbai-DPA 2.1(f, c, q)/model/path',
                   map_location=device))

    metrics = calculate_performance_metrics(model, device)

    image_path = 'test/image/path'

    predicted_class, confidence, image = predict_image(model, image_path, transform, device)

    class_names = ['Alzheimer Disease', 'Mild Alzheimer Risk', 'Moderate Alzheimer Risk', 'Very Mild Alzheimer Risk',
                   'No Risk', 'Parkinson Disease']

    print(f'Predicted Class: {class_names[predicted_class]}')
    print(f'Accuracy: {confidence}%')
    print(f'Params: {metrics["params_million"]:.2f} M')
    print(f'FLOPs (B): {metrics["flops_billion"]:.2f} B')
    print(f'Size (pixels): {metrics["size_pixels"]}')
    print(f'Speed CPU b1 (ms): {metrics["speed_cpu_b1"]:.2f} ms')
    print(f'Speed V100 b1 (ms): {metrics["speed_v100_b1"]:.2f} ms')
    print(f'Speed V100 b32 (ms): {metrics["speed_v100_b32"]:.2f} ms')

    plt.imshow(image.squeeze(0).permute(1, 2, 0))
    plt.title(f'Prediction: {class_names[predicted_class]} \nAccuracy: {confidence:.2f}%')
    plt.axis('off')
    plt.show()

if __name__ == '__main__':
    main()

```

#### License: CC-BY-NC-SA-4.0