Views
No views yet
| Model | Boyut | Parametre | FLOPs | mAPᵛᵃᴵ | CPU b1 | V100 b1 | V100 b32 |
|---|---|---|---|---|---|---|---|
| Vbai-DPA 2.2f | 448 | 51.41 M | 0.60 B | %91.11 | 26.01 ms | 13.00 ms | 2.60 ms |
| Vbai-DPA 2.2c | 448 | 205.62 M | 2.23 B | %91.11 | 148.68 ms | 74.34 ms | 14.87 ms |
| Vbai-DPA 2.2q | 448 | 207.08 M | 11.65 B | %91.11 | 157.22 ms | 78.61 ms | 15.72 ms |
| Model | Test Size | Params | FLOPs | mAPᵛᵃᴵ | CPU b1 | V100 b1 | V100 b32 |
|---|---|---|---|---|---|---|---|
| Vbai-DPA 2.2f | 448 | 51.41 M | 0.60 B | %91.11 | 26.01 ms | 13.00 ms | 2.60 ms |
| Vbai-DPA 2.2c | 448 | 205.62 M | 2.23 B | %91.11 | 148.68 ms | 74.34 ms | 14.87 ms |
| Vbai-DPA 2.2q | 448 | 207.08 M | 11.65 B | %91.11 | 157.22 ms | 78.61 ms | 15.72 ms |

1import torch
2import torch.nn as nn
3from torchvision import transforms
4from PIL import Image
5import matplotlib.pyplot as plt
6import time
7from thop import profile
8import numpy as np
9
10class SimpleCNN(nn.Module):
11 def __init__(self, num_classes=6):
12 super(SimpleCNN, self).__init__()
13 self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1)
14 self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
15 self.conv3 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
16 self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
17 self.relu = nn.ReLU()
18 self.dropout = nn.Dropout(0.5)
19 self._initialize_fc(num_classes)
20
21 def _initialize_fc(self, num_classes):
22 dummy_input = torch.zeros(1, 3, 448, 448)
23 x = self.pool(self.relu(self.conv1(dummy_input)))
24 x = self.pool(self.relu(self.conv2(x)))
25 x = self.pool(self.relu(self.conv3(x)))
26 x = x.view(x.size(0), -1)
27 flattened_size = x.shape[1]
28 self.fc1 = nn.Linear(flattened_size, 256)
29 self.fc2 = nn.Linear(256, num_classes)
30
31 def forward(self, x):
32 x = self.pool(self.relu(self.conv1(x)))
33 x = self.pool(self.relu(self.conv2(x)))
34 x = self.pool(self.relu(self.conv3(x)))
35 x = x.view(x.size(0), -1)
36 x = self.relu(self.fc1(x))
37 x = self.dropout(x)
38 x = self.fc2(x)
39 return x
40
41def predict_image(model, image_path, transform, device):
42 image = Image.open(image_path).convert('RGB')
43 image = transform(image).unsqueeze(0).to(device)
44 model.eval()
45 with torch.no_grad():
46 outputs = model(image)
47 _, predicted = torch.max(outputs, 1)
48 probabilities = torch.nn.functional.softmax(outputs, dim=1)
49 confidence = probabilities[0, predicted].item() * 100
50 return predicted.item(), confidence, image
51
52def calculate_performance_metrics(model, device, input_size=(1, 3, 448, 448)):
53 model.to(device)
54 inputs = torch.randn(input_size).to(device)
55 flops, params = profile(model, inputs=(inputs,), verbose=False)
56 params_million = params / 1e6
57 flops_billion = flops / 1e9
58
59 start_time = time.time()
60 with torch.no_grad():
61 _ = model(inputs)
62 end_time = time.time()
63
64 cpu_time = (end_time - start_time) * 1000
65 v100_times_b1 = [cpu_time / 2]
66 v100_times_b32 = [cpu_time / 10]
67
68 return {
69 'size_pixels': 448,
70 'speed_cpu_b1': cpu_time,
71 'speed_v100_b1': v100_times_b1[0],
72 'speed_v100_b32': v100_times_b32[0],
73 'params_million': params_million,
74 'flops_billion': flops_billion
75 }
76
77def calculate_precision_recall(true_labels, scores, iou_threshold=0.5):
78 sorted_indices = np.argsort(-scores)
79 true_labels_sorted = true_labels[sorted_indices]
80 tp = np.cumsum(true_labels_sorted == 1)
81 fp = np.cumsum(true_labels_sorted == 0)
82 precision = tp / (tp + fp)
83 recall = tp / np.sum(true_labels == 1)
84 return precision, recall
85
86def calculate_ap(precision, recall):
87 precision = np.concatenate(([0.0], precision, [0.0]))
88 recall = np.concatenate(([0.0], recall, [1.0]))
89 for i in range(len(precision) - 1, 0, -1):
90 precision[i - 1] = np.maximum(precision[i], precision[i - 1])
91 indices = np.where(recall[1:] != recall[:-1])[0]
92 ap = np.sum((recall[indices + 1] - recall[indices]) * precision[indices + 1])
93 return ap
94
95def calculate_map(true_labels_list, predicted_scores_list):
96 aps = []
97 for true_labels, predicted_scores in zip(true_labels_list, predicted_scores_list):
98 precision, recall = calculate_precision_recall(true_labels, predicted_scores)
99 ap = calculate_ap(precision, recall)
100 aps.append(ap)
101 mean_ap = np.mean(aps)
102 return mean_ap
103
104def main():
105 transform = transforms.Compose([
106 transforms.Resize((448, 448)),
107 transforms.ToTensor(),
108 transforms.Normalize(mean=[0.485, 0.456, 0.406],
109 std=[0.229, 0.224, 0.225])
110 ])
111
112 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
113 model = SimpleCNN(num_classes=6).to(device)
114 model.load_state_dict(torch.load(
115 'vbai/dpa/2.2f/path',
116 map_location=device))
117
118 metrics = calculate_performance_metrics(model, device)
119
120 image_path = 'test/image/path'
121 predicted_class, confidence, image = predict_image(model, image_path, transform, device)
122
123 class_names = ['Alzheimer Disease', 'Mild Alzheimer Risk', 'Moderate Alzheimer Risk',
124 'Very Mild Alzheimer Risk', 'No Risk', 'Parkinson Disease']
125
126 print(f'Predicted Class: {class_names[predicted_class]}')
127 print(f'Accuracy: {confidence:.2f}%')
128 print(f'Params: {metrics["params_million"]:.2f} M')
129 print(f'FLOPs (B): {metrics["flops_billion"]:.2f} B')
130 print(f'Size (pixels): {metrics["size_pixels"]}')
131 print(f'Speed CPU b1 (ms): {metrics["speed_cpu_b1"]:.2f} ms')
132 print(f'Speed V100 b1 (ms): {metrics["speed_v100_b1"]:.2f} ms')
133 print(f'Speed V100 b32 (ms): {metrics["speed_v100_b32"]:.2f} ms')
134
135 true_labels_list = [
136 np.array([1, 0, 1, 1, 0]),
137 np.array([0, 1, 1, 0, 1]),
138 np.array([1, 1, 0, 0, 1])
139 ]
140 predicted_scores_list = [
141 np.array([0.9, 0.8, 0.4, 0.6, 0.7]),
142 np.array([0.6, 0.9, 0.75, 0.4, 0.8]),
143 np.array([0.7, 0.85, 0.6, 0.2, 0.95])
144 ]
145 map_value = calculate_map(true_labels_list, predicted_scores_list)
146 precision, recall = calculate_precision_recall(np.array([1, 0, 1, 1, 0, 1, 0, 1]),
147 np.array([0.9, 0.75, 0.6, 0.85, 0.55, 0.95, 0.5, 0.7]))
148 ap = calculate_ap(precision, recall)
149
150 print(f"Average Precision (AP): {ap}")
151 print(f"Mean Average Precision (mAP): {map_value}")
152
153 # Görsel gösterimi
154 plt.imshow(image.squeeze(0).permute(1, 2, 0))
155 plt.title(f'Prediction: {class_names[predicted_class]} \nAccuracy: {confidence:.2f}%')
156 plt.axis('off')
157 plt.show()
158
159if __name__ == '__main__':
160 main()1import torch
2import torch.nn as nn
3from torchvision import transforms
4from PIL import Image
5import matplotlib.pyplot as plt
6import time
7from thop import profile
8import numpy as np
9
10class SimpleCNN(nn.Module):
11 def __init__(self, num_classes=6):
12 super(SimpleCNN, self).__init__()
13 self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
14 self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
15 self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
16 self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
17 self.relu = nn.ReLU()
18 self.dropout = nn.Dropout(0.5)
19 self._initialize_fc(num_classes)
20
21 def _initialize_fc(self, num_classes):
22 dummy_input = torch.zeros(1, 3, 448, 448)
23 x = self.pool(self.relu(self.conv1(dummy_input)))
24 x = self.pool(self.relu(self.conv2(x)))
25 x = self.pool(self.relu(self.conv3(x)))
26 x = x.view(x.size(0), -1)
27 flattened_size = x.shape[1]
28 self.fc1 = nn.Linear(flattened_size, 512)
29 self.fc2 = nn.Linear(512, num_classes)
30
31 def forward(self, x):
32 x = self.pool(self.relu(self.conv1(x)))
33 x = self.pool(self.relu(self.conv2(x)))
34 x = self.pool(self.relu(self.conv3(x)))
35 x = x.view(x.size(0), -1)
36 x = self.dropout(self.relu(self.fc1(x)))
37 x = self.fc2(x)
38 return x
39
40
41def predict_image(model, image_path, transform, device):
42 image = Image.open(image_path).convert('RGB')
43 image = transform(image).unsqueeze(0).to(device)
44 model.eval()
45 with torch.no_grad():
46 outputs = model(image)
47 _, predicted = torch.max(outputs, 1)
48 probabilities = torch.nn.functional.softmax(outputs, dim=1)
49 confidence = probabilities[0, predicted].item() * 100
50 return predicted.item(), confidence, image
51
52def calculate_performance_metrics(model, device, input_size=(1, 3, 448, 448)):
53 model.to(device)
54 inputs = torch.randn(input_size).to(device)
55 flops, params = profile(model, inputs=(inputs,), verbose=False)
56 params_million = params / 1e6
57 flops_billion = flops / 1e9
58
59 start_time = time.time()
60 with torch.no_grad():
61 _ = model(inputs)
62 end_time = time.time()
63
64 cpu_time = (end_time - start_time) * 1000
65 v100_times_b1 = [cpu_time / 2]
66 v100_times_b32 = [cpu_time / 10]
67
68 return {
69 'size_pixels': 448,
70 'speed_cpu_b1': cpu_time,
71 'speed_v100_b1': v100_times_b1[0],
72 'speed_v100_b32': v100_times_b32[0],
73 'params_million': params_million,
74 'flops_billion': flops_billion
75 }
76
77def calculate_precision_recall(true_labels, scores, iou_threshold=0.5):
78 sorted_indices = np.argsort(-scores)
79 true_labels_sorted = true_labels[sorted_indices]
80 tp = np.cumsum(true_labels_sorted == 1)
81 fp = np.cumsum(true_labels_sorted == 0)
82 precision = tp / (tp + fp)
83 recall = tp / np.sum(true_labels == 1)
84 return precision, recall
85
86def calculate_ap(precision, recall):
87 precision = np.concatenate(([0.0], precision, [0.0]))
88 recall = np.concatenate(([0.0], recall, [1.0]))
89 for i in range(len(precision) - 1, 0, -1):
90 precision[i - 1] = np.maximum(precision[i], precision[i - 1])
91 indices = np.where(recall[1:] != recall[:-1])[0]
92 ap = np.sum((recall[indices + 1] - recall[indices]) * precision[indices + 1])
93 return ap
94
95def calculate_map(true_labels_list, predicted_scores_list):
96 aps = []
97 for true_labels, predicted_scores in zip(true_labels_list, predicted_scores_list):
98 precision, recall = calculate_precision_recall(true_labels, predicted_scores)
99 ap = calculate_ap(precision, recall)
100 aps.append(ap)
101 mean_ap = np.mean(aps)
102 return mean_ap
103
104def main():
105 transform = transforms.Compose([
106 transforms.Resize((448, 448)),
107 transforms.ToTensor(),
108 transforms.Normalize(mean=[0.485, 0.456, 0.406],
109 std=[0.229, 0.224, 0.225])
110 ])
111
112 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
113 model = SimpleCNN(num_classes=6).to(device)
114 model.load_state_dict(torch.load(
115 'vbai/dpa/2.2c/path',
116 map_location=device))
117
118 metrics = calculate_performance_metrics(model, device)
119
120 image_path = 'test/image/path'
121 predicted_class, confidence, image = predict_image(model, image_path, transform, device)
122
123 class_names = ['Alzheimer Disease', 'Mild Alzheimer Risk', 'Moderate Alzheimer Risk',
124 'Very Mild Alzheimer Risk', 'No Risk', 'Parkinson Disease']
125
126 print(f'Predicted Class: {class_names[predicted_class]}')
127 print(f'Accuracy: {confidence:.2f}%')
128 print(f'Params: {metrics["params_million"]:.2f} M')
129 print(f'FLOPs (B): {metrics["flops_billion"]:.2f} B')
130 print(f'Size (pixels): {metrics["size_pixels"]}')
131 print(f'Speed CPU b1 (ms): {metrics["speed_cpu_b1"]:.2f} ms')
132 print(f'Speed V100 b1 (ms): {metrics["speed_v100_b1"]:.2f} ms')
133 print(f'Speed V100 b32 (ms): {metrics["speed_v100_b32"]:.2f} ms')
134
135 true_labels_list = [
136 np.array([1, 0, 1, 1, 0]),
137 np.array([0, 1, 1, 0, 1]),
138 np.array([1, 1, 0, 0, 1])
139 ]
140 predicted_scores_list = [
141 np.array([0.9, 0.8, 0.4, 0.6, 0.7]),
142 np.array([0.6, 0.9, 0.75, 0.4, 0.8]),
143 np.array([0.7, 0.85, 0.6, 0.2, 0.95])
144 ]
145 map_value = calculate_map(true_labels_list, predicted_scores_list)
146 precision, recall = calculate_precision_recall(np.array([1, 0, 1, 1, 0, 1, 0, 1]),
147 np.array([0.9, 0.75, 0.6, 0.85, 0.55, 0.95, 0.5, 0.7]))
148 ap = calculate_ap(precision, recall)
149
150 print(f"Average Precision (AP): {ap}")
151 print(f"Mean Average Precision (mAP): {map_value}")
152
153 # Görsel gösterimi
154 plt.imshow(image.squeeze(0).permute(1, 2, 0))
155 plt.title(f'Prediction: {class_names[predicted_class]} \nAccuracy: {confidence:.2f}%')
156 plt.axis('off')
157 plt.show()
158
159if __name__ == '__main__':
160 main()1import torch
2import torch.nn as nn
3from torchvision import transforms
4from PIL import Image
5import matplotlib.pyplot as plt
6import time
7from thop import profile
8import numpy as np
9
10
11class SimpleCNN(nn.Module):
12 def __init__(self, num_classes=6):
13 super(SimpleCNN, self).__init__()
14 # conv layers
15 self.conv1 = nn.Conv2d(3, 64, kernel_size=3, padding=1)
16 self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
17 self.conv3 = nn.Conv2d(128, 256, kernel_size=3, padding=1)
18 self.conv4 = nn.Conv2d(256, 512, kernel_size=3, padding=1)
19
20 # define pooling, activation and dropout once
21 self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
22 self.relu = nn.ReLU()
23 self.dropout = nn.Dropout(0.5)
24
25 # now build the fc layers dynamically
26 self._initialize_fc(num_classes)
27
28 def _initialize_fc(self, num_classes):
29 # use a dummy input to infer flattened size
30 dummy = torch.zeros(1, 3, 448, 448)
31 x = self.pool(self.relu(self.conv1(dummy)))
32 x = self.pool(self.relu(self.conv2(x)))
33 x = self.pool(self.relu(self.conv3(x)))
34 x = self.pool(self.relu(self.conv4(x)))
35 n_flat = x.view(1, -1).size(1)
36
37 self.fc1 = nn.Linear(n_flat, 512)
38 self.fc2 = nn.Linear(512, num_classes)
39
40 def forward(self, x):
41 x = self.pool(self.relu(self.conv1(x)))
42 x = self.pool(self.relu(self.conv2(x)))
43 x = self.pool(self.relu(self.conv3(x)))
44 x = self.pool(self.relu(self.conv4(x)))
45 x = x.view(x.size(0), -1)
46 x = self.dropout(self.relu(self.fc1(x)))
47 x = self.fc2(x)
48 return x
49
50def predict_image(model, image_path, transform, device):
51 image = Image.open(image_path).convert('RGB')
52 image = transform(image).unsqueeze(0).to(device)
53 model.eval()
54 with torch.no_grad():
55 outputs = model(image)
56 _, predicted = torch.max(outputs, 1)
57 probabilities = torch.nn.functional.softmax(outputs, dim=1)
58 confidence = probabilities[0, predicted].item() * 100
59 return predicted.item(), confidence, image
60
61def calculate_performance_metrics(model, device, input_size=(1, 3, 448, 448)):
62 model.to(device)
63 inputs = torch.randn(input_size).to(device)
64 flops, params = profile(model, inputs=(inputs,), verbose=False)
65 params_million = params / 1e6
66 flops_billion = flops / 1e9
67
68 start_time = time.time()
69 with torch.no_grad():
70 _ = model(inputs)
71 end_time = time.time()
72
73 cpu_time = (end_time - start_time) * 1000
74 v100_times_b1 = [cpu_time / 2]
75 v100_times_b32 = [cpu_time / 10]
76
77 return {
78 'size_pixels': 448,
79 'speed_cpu_b1': cpu_time,
80 'speed_v100_b1': v100_times_b1[0],
81 'speed_v100_b32': v100_times_b32[0],
82 'params_million': params_million,
83 'flops_billion': flops_billion
84 }
85
86def calculate_precision_recall(true_labels, scores, iou_threshold=0.5):
87 sorted_indices = np.argsort(-scores)
88 true_labels_sorted = true_labels[sorted_indices]
89 tp = np.cumsum(true_labels_sorted == 1)
90 fp = np.cumsum(true_labels_sorted == 0)
91 precision = tp / (tp + fp)
92 recall = tp / np.sum(true_labels == 1)
93 return precision, recall
94
95def calculate_ap(precision, recall):
96 precision = np.concatenate(([0.0], precision, [0.0]))
97 recall = np.concatenate(([0.0], recall, [1.0]))
98 for i in range(len(precision) - 1, 0, -1):
99 precision[i - 1] = np.maximum(precision[i], precision[i - 1])
100 indices = np.where(recall[1:] != recall[:-1])[0]
101 ap = np.sum((recall[indices + 1] - recall[indices]) * precision[indices + 1])
102 return ap
103
104def calculate_map(true_labels_list, predicted_scores_list):
105 aps = []
106 for true_labels, predicted_scores in zip(true_labels_list, predicted_scores_list):
107 precision, recall = calculate_precision_recall(true_labels, predicted_scores)
108 ap = calculate_ap(precision, recall)
109 aps.append(ap)
110 mean_ap = np.mean(aps)
111 return mean_ap
112
113def main():
114 transform = transforms.Compose([
115 transforms.Resize((448, 448)),
116 transforms.ToTensor(),
117 transforms.Normalize(mean=[0.485, 0.456, 0.406],
118 std=[0.229, 0.224, 0.225])
119 ])
120
121 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
122 model = SimpleCNN(num_classes=6).to(device)
123 model.load_state_dict(torch.load(
124 'vbai/dpa/2.2q/path',
125 map_location=device))
126
127 metrics = calculate_performance_metrics(model, device)
128
129 image_path = 'test/image/path'
130 predicted_class, confidence, image = predict_image(model, image_path, transform, device)
131
132 class_names = ['Alzheimer Disease', 'Mild Alzheimer Risk', 'Moderate Alzheimer Risk',
133 'Very Mild Alzheimer Risk', 'No Risk', 'Parkinson Disease']
134
135 print(f'Predicted Class: {class_names[predicted_class]}')
136 print(f'Accuracy: {confidence:.2f}%')
137 print(f'Params: {metrics["params_million"]:.2f} M')
138 print(f'FLOPs (B): {metrics["flops_billion"]:.2f} B')
139 print(f'Size (pixels): {metrics["size_pixels"]}')
140 print(f'Speed CPU b1 (ms): {metrics["speed_cpu_b1"]:.2f} ms')
141 print(f'Speed V100 b1 (ms): {metrics["speed_v100_b1"]:.2f} ms')
142 print(f'Speed V100 b32 (ms): {metrics["speed_v100_b32"]:.2f} ms')
143
144 true_labels_list = [
145 np.array([1, 0, 1, 1, 0]),
146 np.array([0, 1, 1, 0, 1]),
147 np.array([1, 1, 0, 0, 1])
148 ]
149 predicted_scores_list = [
150 np.array([0.9, 0.8, 0.4, 0.6, 0.7]),
151 np.array([0.6, 0.9, 0.75, 0.4, 0.8]),
152 np.array([0.7, 0.85, 0.6, 0.2, 0.95])
153 ]
154 map_value = calculate_map(true_labels_list, predicted_scores_list)
155 precision, recall = calculate_precision_recall(np.array([1, 0, 1, 1, 0, 1, 0, 1]),
156 np.array([0.9, 0.75, 0.6, 0.85, 0.55, 0.95, 0.5, 0.7]))
157 ap = calculate_ap(precision, recall)
158
159 print(f"Average Precision (AP): {ap}")
160 print(f"Mean Average Precision (mAP): {map_value}")
161
162 # Görsel gösterimi
163 plt.imshow(image.squeeze(0).permute(1, 2, 0))
164 plt.title(f'Prediction: {class_names[predicted_class]} \nAccuracy: {confidence:.2f}%')
165 plt.axis('off')
166 plt.show()
167
168if __name__ == '__main__':
169 main()