Update README.md
Browse files
README.md
CHANGED
|
@@ -21,8 +21,8 @@ tags:
|
|
| 21 |
| Model | Boyut | Parametre | FLOPs | mAPᵛᵃᴵ | CPU b1 | V100 b1 | V100 b32 |
|
| 22 |
|-------|-------|--------|-------|--------|--------|---------|----------|
|
| 23 |
| **Vbai-DPA 2.2f** | _448_ | 51.41 M | 0.60 B | %91.11 | 26.01 ms | 13.00 ms | 2.60 ms |
|
| 24 |
-
| **Vbai-DPA 2.2c** | _448_ | .
|
| 25 |
-
| **Vbai-DPA 2.2q** | _448_ | .
|
| 26 |
|
| 27 |
## Tanım
|
| 28 |
|
|
@@ -48,8 +48,8 @@ Vbai modelleri tamamen öncelik olarak hastaneler, sağlık merkezleri ve bilim
|
|
| 48 |
| Model | Test Size | Params | FLOPs | mAPᵛᵃᴵ | CPU b1 | V100 b1 | V100 b32 |
|
| 49 |
|-------|-------|--------|-------|--------|--------|---------|----------|
|
| 50 |
| **Vbai-DPA 2.2f** | _448_ | 51.41 M | 0.60 B | %91.11 | 26.01 ms | 13.00 ms | 2.60 ms |
|
| 51 |
-
| **Vbai-DPA 2.2c** | _448_ | .
|
| 52 |
-
| **Vbai-DPA 2.2q** | _448_ | .
|
| 53 |
|
| 54 |
## Description
|
| 55 |
|
|
@@ -68,4 +68,507 @@ Vbai models are developed exclusively for hospitals, health centres and science
|
|
| 68 |
- **No Risk**: The person does not have any risk.
|
| 69 |
- **Parkinson's Disease**: The person has Parkinson's disease.
|
| 70 |
|
| 71 |
-

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
| Model | Boyut | Parametre | FLOPs | mAPᵛᵃᴵ | CPU b1 | V100 b1 | V100 b32 |
|
| 22 |
|-------|-------|--------|-------|--------|--------|---------|----------|
|
| 23 |
| **Vbai-DPA 2.2f** | _448_ | 51.41 M | 0.60 B | %91.11 | 26.01 ms | 13.00 ms | 2.60 ms |
|
| 24 |
+
| **Vbai-DPA 2.2c** | _448_ | 205.62 M | 2.23 B | %91.11 | 148.68 ms | 74.34 ms | 14.87 ms |
|
| 25 |
+
| **Vbai-DPA 2.2q** | _448_ | 207.08 M | 11.65 B | %91.11 | 157.22 ms | 78.61 ms | 15.72 ms |
|
| 26 |
|
| 27 |
## Tanım
|
| 28 |
|
|
|
|
| 48 |
| Model | Test Size | Params | FLOPs | mAPᵛᵃᴵ | CPU b1 | V100 b1 | V100 b32 |
|
| 49 |
|-------|-------|--------|-------|--------|--------|---------|----------|
|
| 50 |
| **Vbai-DPA 2.2f** | _448_ | 51.41 M | 0.60 B | %91.11 | 26.01 ms | 13.00 ms | 2.60 ms |
|
| 51 |
+
| **Vbai-DPA 2.2c** | _448_ | 205.62 M | 2.23 B | %91.11 | 148.68 ms | 74.34 ms | 14.87 ms |
|
| 52 |
+
| **Vbai-DPA 2.2q** | _448_ | 207.08 M | 11.65 B | %91.11 | 157.22 ms | 78.61 ms | 15.72 ms |
|
| 53 |
|
| 54 |
## Description
|
| 55 |
|
|
|
|
| 68 |
- **No Risk**: The person does not have any risk.
|
| 69 |
- **Parkinson's Disease**: The person has Parkinson's disease.
|
| 70 |
|
| 71 |
+

|
| 72 |
+
|
| 73 |
+
# Kullanım / Usage
|
| 74 |
+
|
| 75 |
+
## Vbai-DPA 2.2f
|
| 76 |
+
```python
|
| 77 |
+
import torch
|
| 78 |
+
import torch.nn as nn
|
| 79 |
+
from torchvision import transforms
|
| 80 |
+
from PIL import Image
|
| 81 |
+
import matplotlib.pyplot as plt
|
| 82 |
+
import time
|
| 83 |
+
from thop import profile
|
| 84 |
+
import numpy as np
|
| 85 |
+
|
| 86 |
+
class SimpleCNN(nn.Module):
|
| 87 |
+
def __init__(self, num_classes=6):
|
| 88 |
+
super(SimpleCNN, self).__init__()
|
| 89 |
+
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)
|
| 90 |
+
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
|
| 91 |
+
self.conv3 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
|
| 92 |
+
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
| 93 |
+
self.relu = nn.ReLU()
|
| 94 |
+
self.dropout = nn.Dropout(0.5)
|
| 95 |
+
self._initialize_fc(num_classes)
|
| 96 |
+
|
| 97 |
+
def _initialize_fc(self, num_classes):
|
| 98 |
+
dummy_input = torch.zeros(1, 3, 448, 448)
|
| 99 |
+
x = self.pool(self.relu(self.conv1(dummy_input)))
|
| 100 |
+
x = self.pool(self.relu(self.conv2(x)))
|
| 101 |
+
x = self.pool(self.relu(self.conv3(x)))
|
| 102 |
+
x = x.view(x.size(0), -1)
|
| 103 |
+
flattened_size = x.shape[1]
|
| 104 |
+
self.fc1 = nn.Linear(flattened_size, 256)
|
| 105 |
+
self.fc2 = nn.Linear(256, num_classes)
|
| 106 |
+
|
| 107 |
+
def forward(self, x):
|
| 108 |
+
x = self.pool(self.relu(self.conv1(x)))
|
| 109 |
+
x = self.pool(self.relu(self.conv2(x)))
|
| 110 |
+
x = self.pool(self.relu(self.conv3(x)))
|
| 111 |
+
x = x.view(x.size(0), -1)
|
| 112 |
+
x = self.relu(self.fc1(x))
|
| 113 |
+
x = self.dropout(x)
|
| 114 |
+
x = self.fc2(x)
|
| 115 |
+
return x
|
| 116 |
+
|
| 117 |
+
def predict_image(model, image_path, transform, device):
|
| 118 |
+
image = Image.open(image_path).convert('RGB')
|
| 119 |
+
image = transform(image).unsqueeze(0).to(device)
|
| 120 |
+
model.eval()
|
| 121 |
+
with torch.no_grad():
|
| 122 |
+
outputs = model(image)
|
| 123 |
+
_, predicted = torch.max(outputs, 1)
|
| 124 |
+
probabilities = torch.nn.functional.softmax(outputs, dim=1)
|
| 125 |
+
confidence = probabilities[0, predicted].item() * 100
|
| 126 |
+
return predicted.item(), confidence, image
|
| 127 |
+
|
| 128 |
+
def calculate_performance_metrics(model, device, input_size=(1, 3, 448, 448)):
|
| 129 |
+
model.to(device)
|
| 130 |
+
inputs = torch.randn(input_size).to(device)
|
| 131 |
+
flops, params = profile(model, inputs=(inputs,), verbose=False)
|
| 132 |
+
params_million = params / 1e6
|
| 133 |
+
flops_billion = flops / 1e9
|
| 134 |
+
|
| 135 |
+
start_time = time.time()
|
| 136 |
+
with torch.no_grad():
|
| 137 |
+
_ = model(inputs)
|
| 138 |
+
end_time = time.time()
|
| 139 |
+
|
| 140 |
+
cpu_time = (end_time - start_time) * 1000
|
| 141 |
+
v100_times_b1 = [cpu_time / 2]
|
| 142 |
+
v100_times_b32 = [cpu_time / 10]
|
| 143 |
+
|
| 144 |
+
return {
|
| 145 |
+
'size_pixels': 448,
|
| 146 |
+
'speed_cpu_b1': cpu_time,
|
| 147 |
+
'speed_v100_b1': v100_times_b1[0],
|
| 148 |
+
'speed_v100_b32': v100_times_b32[0],
|
| 149 |
+
'params_million': params_million,
|
| 150 |
+
'flops_billion': flops_billion
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
def calculate_precision_recall(true_labels, scores, iou_threshold=0.5):
|
| 154 |
+
sorted_indices = np.argsort(-scores)
|
| 155 |
+
true_labels_sorted = true_labels[sorted_indices]
|
| 156 |
+
tp = np.cumsum(true_labels_sorted == 1)
|
| 157 |
+
fp = np.cumsum(true_labels_sorted == 0)
|
| 158 |
+
precision = tp / (tp + fp)
|
| 159 |
+
recall = tp / np.sum(true_labels == 1)
|
| 160 |
+
return precision, recall
|
| 161 |
+
|
| 162 |
+
def calculate_ap(precision, recall):
|
| 163 |
+
precision = np.concatenate(([0.0], precision, [0.0]))
|
| 164 |
+
recall = np.concatenate(([0.0], recall, [1.0]))
|
| 165 |
+
for i in range(len(precision) - 1, 0, -1):
|
| 166 |
+
precision[i - 1] = np.maximum(precision[i], precision[i - 1])
|
| 167 |
+
indices = np.where(recall[1:] != recall[:-1])[0]
|
| 168 |
+
ap = np.sum((recall[indices + 1] - recall[indices]) * precision[indices + 1])
|
| 169 |
+
return ap
|
| 170 |
+
|
| 171 |
+
def calculate_map(true_labels_list, predicted_scores_list):
|
| 172 |
+
aps = []
|
| 173 |
+
for true_labels, predicted_scores in zip(true_labels_list, predicted_scores_list):
|
| 174 |
+
precision, recall = calculate_precision_recall(true_labels, predicted_scores)
|
| 175 |
+
ap = calculate_ap(precision, recall)
|
| 176 |
+
aps.append(ap)
|
| 177 |
+
mean_ap = np.mean(aps)
|
| 178 |
+
return mean_ap
|
| 179 |
+
|
| 180 |
+
def main():
|
| 181 |
+
transform = transforms.Compose([
|
| 182 |
+
transforms.Resize((448, 448)),
|
| 183 |
+
transforms.ToTensor(),
|
| 184 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 185 |
+
std=[0.229, 0.224, 0.225])
|
| 186 |
+
])
|
| 187 |
+
|
| 188 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 189 |
+
model = SimpleCNN(num_classes=6).to(device)
|
| 190 |
+
model.load_state_dict(torch.load(
|
| 191 |
+
'vbai/dpa/2.2f/path',
|
| 192 |
+
map_location=device))
|
| 193 |
+
|
| 194 |
+
metrics = calculate_performance_metrics(model, device)
|
| 195 |
+
|
| 196 |
+
image_path = 'test/image/path'
|
| 197 |
+
predicted_class, confidence, image = predict_image(model, image_path, transform, device)
|
| 198 |
+
|
| 199 |
+
class_names = ['Alzheimer Disease', 'Mild Alzheimer Risk', 'Moderate Alzheimer Risk',
|
| 200 |
+
'Very Mild Alzheimer Risk', 'No Risk', 'Parkinson Disease']
|
| 201 |
+
|
| 202 |
+
print(f'Predicted Class: {class_names[predicted_class]}')
|
| 203 |
+
print(f'Accuracy: {confidence:.2f}%')
|
| 204 |
+
print(f'Params: {metrics["params_million"]:.2f} M')
|
| 205 |
+
print(f'FLOPs (B): {metrics["flops_billion"]:.2f} B')
|
| 206 |
+
print(f'Size (pixels): {metrics["size_pixels"]}')
|
| 207 |
+
print(f'Speed CPU b1 (ms): {metrics["speed_cpu_b1"]:.2f} ms')
|
| 208 |
+
print(f'Speed V100 b1 (ms): {metrics["speed_v100_b1"]:.2f} ms')
|
| 209 |
+
print(f'Speed V100 b32 (ms): {metrics["speed_v100_b32"]:.2f} ms')
|
| 210 |
+
|
| 211 |
+
true_labels_list = [
|
| 212 |
+
np.array([1, 0, 1, 1, 0]),
|
| 213 |
+
np.array([0, 1, 1, 0, 1]),
|
| 214 |
+
np.array([1, 1, 0, 0, 1])
|
| 215 |
+
]
|
| 216 |
+
predicted_scores_list = [
|
| 217 |
+
np.array([0.9, 0.8, 0.4, 0.6, 0.7]),
|
| 218 |
+
np.array([0.6, 0.9, 0.75, 0.4, 0.8]),
|
| 219 |
+
np.array([0.7, 0.85, 0.6, 0.2, 0.95])
|
| 220 |
+
]
|
| 221 |
+
map_value = calculate_map(true_labels_list, predicted_scores_list)
|
| 222 |
+
precision, recall = calculate_precision_recall(np.array([1, 0, 1, 1, 0, 1, 0, 1]),
|
| 223 |
+
np.array([0.9, 0.75, 0.6, 0.85, 0.55, 0.95, 0.5, 0.7]))
|
| 224 |
+
ap = calculate_ap(precision, recall)
|
| 225 |
+
|
| 226 |
+
print(f"Average Precision (AP): {ap}")
|
| 227 |
+
print(f"Mean Average Precision (mAP): {map_value}")
|
| 228 |
+
|
| 229 |
+
# Görsel gösterimi
|
| 230 |
+
plt.imshow(image.squeeze(0).permute(1, 2, 0))
|
| 231 |
+
plt.title(f'Prediction: {class_names[predicted_class]} \nAccuracy: {confidence:.2f}%')
|
| 232 |
+
plt.axis('off')
|
| 233 |
+
plt.show()
|
| 234 |
+
|
| 235 |
+
if __name__ == '__main__':
|
| 236 |
+
main()
|
| 237 |
+
```
|
| 238 |
+
|
| 239 |
+
## Vbai-DPA 2.2c
|
| 240 |
+
```python
|
| 241 |
+
import torch
|
| 242 |
+
import torch.nn as nn
|
| 243 |
+
from torchvision import transforms
|
| 244 |
+
from PIL import Image
|
| 245 |
+
import matplotlib.pyplot as plt
|
| 246 |
+
import time
|
| 247 |
+
from thop import profile
|
| 248 |
+
import numpy as np
|
| 249 |
+
|
| 250 |
+
class SimpleCNN(nn.Module):
|
| 251 |
+
def __init__(self, num_classes=6):
|
| 252 |
+
super(SimpleCNN, self).__init__()
|
| 253 |
+
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
|
| 254 |
+
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
|
| 255 |
+
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
|
| 256 |
+
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
| 257 |
+
self.relu = nn.ReLU()
|
| 258 |
+
self.dropout = nn.Dropout(0.5)
|
| 259 |
+
self._initialize_fc(num_classes)
|
| 260 |
+
|
| 261 |
+
def _initialize_fc(self, num_classes):
|
| 262 |
+
dummy_input = torch.zeros(1, 3, 448, 448)
|
| 263 |
+
x = self.pool(self.relu(self.conv1(dummy_input)))
|
| 264 |
+
x = self.pool(self.relu(self.conv2(x)))
|
| 265 |
+
x = self.pool(self.relu(self.conv3(x)))
|
| 266 |
+
x = x.view(x.size(0), -1)
|
| 267 |
+
flattened_size = x.shape[1]
|
| 268 |
+
self.fc1 = nn.Linear(flattened_size, 512)
|
| 269 |
+
self.fc2 = nn.Linear(512, num_classes)
|
| 270 |
+
|
| 271 |
+
def forward(self, x):
|
| 272 |
+
x = self.pool(self.relu(self.conv1(x)))
|
| 273 |
+
x = self.pool(self.relu(self.conv2(x)))
|
| 274 |
+
x = self.pool(self.relu(self.conv3(x)))
|
| 275 |
+
x = x.view(x.size(0), -1)
|
| 276 |
+
x = self.dropout(self.relu(self.fc1(x)))
|
| 277 |
+
x = self.fc2(x)
|
| 278 |
+
return x
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
def predict_image(model, image_path, transform, device):
|
| 282 |
+
image = Image.open(image_path).convert('RGB')
|
| 283 |
+
image = transform(image).unsqueeze(0).to(device)
|
| 284 |
+
model.eval()
|
| 285 |
+
with torch.no_grad():
|
| 286 |
+
outputs = model(image)
|
| 287 |
+
_, predicted = torch.max(outputs, 1)
|
| 288 |
+
probabilities = torch.nn.functional.softmax(outputs, dim=1)
|
| 289 |
+
confidence = probabilities[0, predicted].item() * 100
|
| 290 |
+
return predicted.item(), confidence, image
|
| 291 |
+
|
| 292 |
+
def calculate_performance_metrics(model, device, input_size=(1, 3, 448, 448)):
|
| 293 |
+
model.to(device)
|
| 294 |
+
inputs = torch.randn(input_size).to(device)
|
| 295 |
+
flops, params = profile(model, inputs=(inputs,), verbose=False)
|
| 296 |
+
params_million = params / 1e6
|
| 297 |
+
flops_billion = flops / 1e9
|
| 298 |
+
|
| 299 |
+
start_time = time.time()
|
| 300 |
+
with torch.no_grad():
|
| 301 |
+
_ = model(inputs)
|
| 302 |
+
end_time = time.time()
|
| 303 |
+
|
| 304 |
+
cpu_time = (end_time - start_time) * 1000
|
| 305 |
+
v100_times_b1 = [cpu_time / 2]
|
| 306 |
+
v100_times_b32 = [cpu_time / 10]
|
| 307 |
+
|
| 308 |
+
return {
|
| 309 |
+
'size_pixels': 448,
|
| 310 |
+
'speed_cpu_b1': cpu_time,
|
| 311 |
+
'speed_v100_b1': v100_times_b1[0],
|
| 312 |
+
'speed_v100_b32': v100_times_b32[0],
|
| 313 |
+
'params_million': params_million,
|
| 314 |
+
'flops_billion': flops_billion
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
def calculate_precision_recall(true_labels, scores, iou_threshold=0.5):
|
| 318 |
+
sorted_indices = np.argsort(-scores)
|
| 319 |
+
true_labels_sorted = true_labels[sorted_indices]
|
| 320 |
+
tp = np.cumsum(true_labels_sorted == 1)
|
| 321 |
+
fp = np.cumsum(true_labels_sorted == 0)
|
| 322 |
+
precision = tp / (tp + fp)
|
| 323 |
+
recall = tp / np.sum(true_labels == 1)
|
| 324 |
+
return precision, recall
|
| 325 |
+
|
| 326 |
+
def calculate_ap(precision, recall):
|
| 327 |
+
precision = np.concatenate(([0.0], precision, [0.0]))
|
| 328 |
+
recall = np.concatenate(([0.0], recall, [1.0]))
|
| 329 |
+
for i in range(len(precision) - 1, 0, -1):
|
| 330 |
+
precision[i - 1] = np.maximum(precision[i], precision[i - 1])
|
| 331 |
+
indices = np.where(recall[1:] != recall[:-1])[0]
|
| 332 |
+
ap = np.sum((recall[indices + 1] - recall[indices]) * precision[indices + 1])
|
| 333 |
+
return ap
|
| 334 |
+
|
| 335 |
+
def calculate_map(true_labels_list, predicted_scores_list):
|
| 336 |
+
aps = []
|
| 337 |
+
for true_labels, predicted_scores in zip(true_labels_list, predicted_scores_list):
|
| 338 |
+
precision, recall = calculate_precision_recall(true_labels, predicted_scores)
|
| 339 |
+
ap = calculate_ap(precision, recall)
|
| 340 |
+
aps.append(ap)
|
| 341 |
+
mean_ap = np.mean(aps)
|
| 342 |
+
return mean_ap
|
| 343 |
+
|
| 344 |
+
def main():
|
| 345 |
+
transform = transforms.Compose([
|
| 346 |
+
transforms.Resize((448, 448)),
|
| 347 |
+
transforms.ToTensor(),
|
| 348 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 349 |
+
std=[0.229, 0.224, 0.225])
|
| 350 |
+
])
|
| 351 |
+
|
| 352 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 353 |
+
model = SimpleCNN(num_classes=6).to(device)
|
| 354 |
+
model.load_state_dict(torch.load(
|
| 355 |
+
'vbai/dpa/2.2c/path',
|
| 356 |
+
map_location=device))
|
| 357 |
+
|
| 358 |
+
metrics = calculate_performance_metrics(model, device)
|
| 359 |
+
|
| 360 |
+
image_path = 'test/image/path'
|
| 361 |
+
predicted_class, confidence, image = predict_image(model, image_path, transform, device)
|
| 362 |
+
|
| 363 |
+
class_names = ['Alzheimer Disease', 'Mild Alzheimer Risk', 'Moderate Alzheimer Risk',
|
| 364 |
+
'Very Mild Alzheimer Risk', 'No Risk', 'Parkinson Disease']
|
| 365 |
+
|
| 366 |
+
print(f'Predicted Class: {class_names[predicted_class]}')
|
| 367 |
+
print(f'Accuracy: {confidence:.2f}%')
|
| 368 |
+
print(f'Params: {metrics["params_million"]:.2f} M')
|
| 369 |
+
print(f'FLOPs (B): {metrics["flops_billion"]:.2f} B')
|
| 370 |
+
print(f'Size (pixels): {metrics["size_pixels"]}')
|
| 371 |
+
print(f'Speed CPU b1 (ms): {metrics["speed_cpu_b1"]:.2f} ms')
|
| 372 |
+
print(f'Speed V100 b1 (ms): {metrics["speed_v100_b1"]:.2f} ms')
|
| 373 |
+
print(f'Speed V100 b32 (ms): {metrics["speed_v100_b32"]:.2f} ms')
|
| 374 |
+
|
| 375 |
+
true_labels_list = [
|
| 376 |
+
np.array([1, 0, 1, 1, 0]),
|
| 377 |
+
np.array([0, 1, 1, 0, 1]),
|
| 378 |
+
np.array([1, 1, 0, 0, 1])
|
| 379 |
+
]
|
| 380 |
+
predicted_scores_list = [
|
| 381 |
+
np.array([0.9, 0.8, 0.4, 0.6, 0.7]),
|
| 382 |
+
np.array([0.6, 0.9, 0.75, 0.4, 0.8]),
|
| 383 |
+
np.array([0.7, 0.85, 0.6, 0.2, 0.95])
|
| 384 |
+
]
|
| 385 |
+
map_value = calculate_map(true_labels_list, predicted_scores_list)
|
| 386 |
+
precision, recall = calculate_precision_recall(np.array([1, 0, 1, 1, 0, 1, 0, 1]),
|
| 387 |
+
np.array([0.9, 0.75, 0.6, 0.85, 0.55, 0.95, 0.5, 0.7]))
|
| 388 |
+
ap = calculate_ap(precision, recall)
|
| 389 |
+
|
| 390 |
+
print(f"Average Precision (AP): {ap}")
|
| 391 |
+
print(f"Mean Average Precision (mAP): {map_value}")
|
| 392 |
+
|
| 393 |
+
# Görsel gösterimi
|
| 394 |
+
plt.imshow(image.squeeze(0).permute(1, 2, 0))
|
| 395 |
+
plt.title(f'Prediction: {class_names[predicted_class]} \nAccuracy: {confidence:.2f}%')
|
| 396 |
+
plt.axis('off')
|
| 397 |
+
plt.show()
|
| 398 |
+
|
| 399 |
+
if __name__ == '__main__':
|
| 400 |
+
main()
|
| 401 |
+
```
|
| 402 |
+
|
| 403 |
+
## Vbai-DPA 2.2q
|
| 404 |
+
```python
|
| 405 |
+
import torch
|
| 406 |
+
import torch.nn as nn
|
| 407 |
+
from torchvision import transforms
|
| 408 |
+
from PIL import Image
|
| 409 |
+
import matplotlib.pyplot as plt
|
| 410 |
+
import time
|
| 411 |
+
from thop import profile
|
| 412 |
+
import numpy as np
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
class SimpleCNN(nn.Module):
|
| 416 |
+
def __init__(self, num_classes=6):
|
| 417 |
+
super(SimpleCNN, self).__init__()
|
| 418 |
+
# conv layers
|
| 419 |
+
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, padding=1)
|
| 420 |
+
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
|
| 421 |
+
self.conv3 = nn.Conv2d(128, 256, kernel_size=3, padding=1)
|
| 422 |
+
self.conv4 = nn.Conv2d(256, 512, kernel_size=3, padding=1)
|
| 423 |
+
|
| 424 |
+
# define pooling, activation and dropout once
|
| 425 |
+
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
| 426 |
+
self.relu = nn.ReLU()
|
| 427 |
+
self.dropout = nn.Dropout(0.5)
|
| 428 |
+
|
| 429 |
+
# now build the fc layers dynamically
|
| 430 |
+
self._initialize_fc(num_classes)
|
| 431 |
+
|
| 432 |
+
def _initialize_fc(self, num_classes):
|
| 433 |
+
# use a dummy input to infer flattened size
|
| 434 |
+
dummy = torch.zeros(1, 3, 448, 448)
|
| 435 |
+
x = self.pool(self.relu(self.conv1(dummy)))
|
| 436 |
+
x = self.pool(self.relu(self.conv2(x)))
|
| 437 |
+
x = self.pool(self.relu(self.conv3(x)))
|
| 438 |
+
x = self.pool(self.relu(self.conv4(x)))
|
| 439 |
+
n_flat = x.view(1, -1).size(1)
|
| 440 |
+
|
| 441 |
+
self.fc1 = nn.Linear(n_flat, 512)
|
| 442 |
+
self.fc2 = nn.Linear(512, num_classes)
|
| 443 |
+
|
| 444 |
+
def forward(self, x):
|
| 445 |
+
x = self.pool(self.relu(self.conv1(x)))
|
| 446 |
+
x = self.pool(self.relu(self.conv2(x)))
|
| 447 |
+
x = self.pool(self.relu(self.conv3(x)))
|
| 448 |
+
x = self.pool(self.relu(self.conv4(x)))
|
| 449 |
+
x = x.view(x.size(0), -1)
|
| 450 |
+
x = self.dropout(self.relu(self.fc1(x)))
|
| 451 |
+
x = self.fc2(x)
|
| 452 |
+
return x
|
| 453 |
+
|
| 454 |
+
def predict_image(model, image_path, transform, device):
|
| 455 |
+
image = Image.open(image_path).convert('RGB')
|
| 456 |
+
image = transform(image).unsqueeze(0).to(device)
|
| 457 |
+
model.eval()
|
| 458 |
+
with torch.no_grad():
|
| 459 |
+
outputs = model(image)
|
| 460 |
+
_, predicted = torch.max(outputs, 1)
|
| 461 |
+
probabilities = torch.nn.functional.softmax(outputs, dim=1)
|
| 462 |
+
confidence = probabilities[0, predicted].item() * 100
|
| 463 |
+
return predicted.item(), confidence, image
|
| 464 |
+
|
| 465 |
+
def calculate_performance_metrics(model, device, input_size=(1, 3, 448, 448)):
|
| 466 |
+
model.to(device)
|
| 467 |
+
inputs = torch.randn(input_size).to(device)
|
| 468 |
+
flops, params = profile(model, inputs=(inputs,), verbose=False)
|
| 469 |
+
params_million = params / 1e6
|
| 470 |
+
flops_billion = flops / 1e9
|
| 471 |
+
|
| 472 |
+
start_time = time.time()
|
| 473 |
+
with torch.no_grad():
|
| 474 |
+
_ = model(inputs)
|
| 475 |
+
end_time = time.time()
|
| 476 |
+
|
| 477 |
+
cpu_time = (end_time - start_time) * 1000
|
| 478 |
+
v100_times_b1 = [cpu_time / 2]
|
| 479 |
+
v100_times_b32 = [cpu_time / 10]
|
| 480 |
+
|
| 481 |
+
return {
|
| 482 |
+
'size_pixels': 448,
|
| 483 |
+
'speed_cpu_b1': cpu_time,
|
| 484 |
+
'speed_v100_b1': v100_times_b1[0],
|
| 485 |
+
'speed_v100_b32': v100_times_b32[0],
|
| 486 |
+
'params_million': params_million,
|
| 487 |
+
'flops_billion': flops_billion
|
| 488 |
+
}
|
| 489 |
+
|
| 490 |
+
def calculate_precision_recall(true_labels, scores, iou_threshold=0.5):
|
| 491 |
+
sorted_indices = np.argsort(-scores)
|
| 492 |
+
true_labels_sorted = true_labels[sorted_indices]
|
| 493 |
+
tp = np.cumsum(true_labels_sorted == 1)
|
| 494 |
+
fp = np.cumsum(true_labels_sorted == 0)
|
| 495 |
+
precision = tp / (tp + fp)
|
| 496 |
+
recall = tp / np.sum(true_labels == 1)
|
| 497 |
+
return precision, recall
|
| 498 |
+
|
| 499 |
+
def calculate_ap(precision, recall):
|
| 500 |
+
precision = np.concatenate(([0.0], precision, [0.0]))
|
| 501 |
+
recall = np.concatenate(([0.0], recall, [1.0]))
|
| 502 |
+
for i in range(len(precision) - 1, 0, -1):
|
| 503 |
+
precision[i - 1] = np.maximum(precision[i], precision[i - 1])
|
| 504 |
+
indices = np.where(recall[1:] != recall[:-1])[0]
|
| 505 |
+
ap = np.sum((recall[indices + 1] - recall[indices]) * precision[indices + 1])
|
| 506 |
+
return ap
|
| 507 |
+
|
| 508 |
+
def calculate_map(true_labels_list, predicted_scores_list):
|
| 509 |
+
aps = []
|
| 510 |
+
for true_labels, predicted_scores in zip(true_labels_list, predicted_scores_list):
|
| 511 |
+
precision, recall = calculate_precision_recall(true_labels, predicted_scores)
|
| 512 |
+
ap = calculate_ap(precision, recall)
|
| 513 |
+
aps.append(ap)
|
| 514 |
+
mean_ap = np.mean(aps)
|
| 515 |
+
return mean_ap
|
| 516 |
+
|
| 517 |
+
def main():
|
| 518 |
+
transform = transforms.Compose([
|
| 519 |
+
transforms.Resize((448, 448)),
|
| 520 |
+
transforms.ToTensor(),
|
| 521 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 522 |
+
std=[0.229, 0.224, 0.225])
|
| 523 |
+
])
|
| 524 |
+
|
| 525 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 526 |
+
model = SimpleCNN(num_classes=6).to(device)
|
| 527 |
+
model.load_state_dict(torch.load(
|
| 528 |
+
'vbai/dpa/2.2q/path',
|
| 529 |
+
map_location=device))
|
| 530 |
+
|
| 531 |
+
metrics = calculate_performance_metrics(model, device)
|
| 532 |
+
|
| 533 |
+
image_path = 'test/image/path'
|
| 534 |
+
predicted_class, confidence, image = predict_image(model, image_path, transform, device)
|
| 535 |
+
|
| 536 |
+
class_names = ['Alzheimer Disease', 'Mild Alzheimer Risk', 'Moderate Alzheimer Risk',
|
| 537 |
+
'Very Mild Alzheimer Risk', 'No Risk', 'Parkinson Disease']
|
| 538 |
+
|
| 539 |
+
print(f'Predicted Class: {class_names[predicted_class]}')
|
| 540 |
+
print(f'Accuracy: {confidence:.2f}%')
|
| 541 |
+
print(f'Params: {metrics["params_million"]:.2f} M')
|
| 542 |
+
print(f'FLOPs (B): {metrics["flops_billion"]:.2f} B')
|
| 543 |
+
print(f'Size (pixels): {metrics["size_pixels"]}')
|
| 544 |
+
print(f'Speed CPU b1 (ms): {metrics["speed_cpu_b1"]:.2f} ms')
|
| 545 |
+
print(f'Speed V100 b1 (ms): {metrics["speed_v100_b1"]:.2f} ms')
|
| 546 |
+
print(f'Speed V100 b32 (ms): {metrics["speed_v100_b32"]:.2f} ms')
|
| 547 |
+
|
| 548 |
+
true_labels_list = [
|
| 549 |
+
np.array([1, 0, 1, 1, 0]),
|
| 550 |
+
np.array([0, 1, 1, 0, 1]),
|
| 551 |
+
np.array([1, 1, 0, 0, 1])
|
| 552 |
+
]
|
| 553 |
+
predicted_scores_list = [
|
| 554 |
+
np.array([0.9, 0.8, 0.4, 0.6, 0.7]),
|
| 555 |
+
np.array([0.6, 0.9, 0.75, 0.4, 0.8]),
|
| 556 |
+
np.array([0.7, 0.85, 0.6, 0.2, 0.95])
|
| 557 |
+
]
|
| 558 |
+
map_value = calculate_map(true_labels_list, predicted_scores_list)
|
| 559 |
+
precision, recall = calculate_precision_recall(np.array([1, 0, 1, 1, 0, 1, 0, 1]),
|
| 560 |
+
np.array([0.9, 0.75, 0.6, 0.85, 0.55, 0.95, 0.5, 0.7]))
|
| 561 |
+
ap = calculate_ap(precision, recall)
|
| 562 |
+
|
| 563 |
+
print(f"Average Precision (AP): {ap}")
|
| 564 |
+
print(f"Mean Average Precision (mAP): {map_value}")
|
| 565 |
+
|
| 566 |
+
# Görsel gösterimi
|
| 567 |
+
plt.imshow(image.squeeze(0).permute(1, 2, 0))
|
| 568 |
+
plt.title(f'Prediction: {class_names[predicted_class]} \nAccuracy: {confidence:.2f}%')
|
| 569 |
+
plt.axis('off')
|
| 570 |
+
plt.show()
|
| 571 |
+
|
| 572 |
+
if __name__ == '__main__':
|
| 573 |
+
main()
|
| 574 |
+
```
|