--- license: cc-by-nc-4.0 language: - tr - en base_model: - Neurazum/Vbai-DPA-2.1 pipeline_tag: image-classification tags: - neuroscience - brain - mri - fmri - python - pytorch - imageprocessing - health --- # Vbai-DPA 2.2 Sürümü (TR) | 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 | ## Tanım Vbai-DPA 2.2 (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.1'e 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. ## ---------------------------------------- # Vbai-DPA 2.2 Version (EN) | 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 | ## Description The Vbai-DPA 2.2 (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.1, 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. ![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F19445420%2Fe50ae92acaa0df7afe9c44367553626f%2Fvbai%20logo%20850x680%20WB.png?generation=1729626904738191&alt=media) # Kullanım / Usage ## Vbai-DPA 2.2f ```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 import numpy as np class SimpleCNN(nn.Module): def __init__(self, num_classes=6): super(SimpleCNN, self).__init__() self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1) self.conv3 = nn.Conv2d(32, 64, kernel_size=3, padding=1) self.pool = nn.MaxPool2d(kernel_size=2, stride=2) self.relu = nn.ReLU() self.dropout = nn.Dropout(0.5) self._initialize_fc(num_classes) def _initialize_fc(self, num_classes): dummy_input = torch.zeros(1, 3, 448, 448) x = self.pool(self.relu(self.conv1(dummy_input))) x = self.pool(self.relu(self.conv2(x))) x = self.pool(self.relu(self.conv3(x))) x = x.view(x.size(0), -1) flattened_size = x.shape[1] self.fc1 = nn.Linear(flattened_size, 256) self.fc2 = nn.Linear(256, num_classes) 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))) 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(): 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, 448, 448)): 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 start_time = time.time() with torch.no_grad(): _ = model(inputs) end_time = time.time() cpu_time = (end_time - start_time) * 1000 v100_times_b1 = [cpu_time / 2] v100_times_b32 = [cpu_time / 10] return { 'size_pixels': 448, 'speed_cpu_b1': cpu_time, 'speed_v100_b1': v100_times_b1[0], 'speed_v100_b32': v100_times_b32[0], 'params_million': params_million, 'flops_billion': flops_billion } def calculate_precision_recall(true_labels, scores, iou_threshold=0.5): sorted_indices = np.argsort(-scores) true_labels_sorted = true_labels[sorted_indices] tp = np.cumsum(true_labels_sorted == 1) fp = np.cumsum(true_labels_sorted == 0) precision = tp / (tp + fp) recall = tp / np.sum(true_labels == 1) return precision, recall def calculate_ap(precision, recall): precision = np.concatenate(([0.0], precision, [0.0])) recall = np.concatenate(([0.0], recall, [1.0])) for i in range(len(precision) - 1, 0, -1): precision[i - 1] = np.maximum(precision[i], precision[i - 1]) indices = np.where(recall[1:] != recall[:-1])[0] ap = np.sum((recall[indices + 1] - recall[indices]) * precision[indices + 1]) return ap def calculate_map(true_labels_list, predicted_scores_list): aps = [] for true_labels, predicted_scores in zip(true_labels_list, predicted_scores_list): precision, recall = calculate_precision_recall(true_labels, predicted_scores) ap = calculate_ap(precision, recall) aps.append(ap) mean_ap = np.mean(aps) return mean_ap def main(): transform = transforms.Compose([ transforms.Resize((448, 448)), 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.2f/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:.2f}%') 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') true_labels_list = [ np.array([1, 0, 1, 1, 0]), np.array([0, 1, 1, 0, 1]), np.array([1, 1, 0, 0, 1]) ] predicted_scores_list = [ np.array([0.9, 0.8, 0.4, 0.6, 0.7]), np.array([0.6, 0.9, 0.75, 0.4, 0.8]), np.array([0.7, 0.85, 0.6, 0.2, 0.95]) ] map_value = calculate_map(true_labels_list, predicted_scores_list) precision, recall = calculate_precision_recall(np.array([1, 0, 1, 1, 0, 1, 0, 1]), np.array([0.9, 0.75, 0.6, 0.85, 0.55, 0.95, 0.5, 0.7])) ap = calculate_ap(precision, recall) print(f"Average Precision (AP): {ap}") print(f"Mean Average Precision (mAP): {map_value}") # Görsel gösterimi 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() ``` ## Vbai-DPA 2.2c ```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 import numpy as np class SimpleCNN(nn.Module): def __init__(self, num_classes=6): super(SimpleCNN, self).__init__() self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1) self.pool = nn.MaxPool2d(kernel_size=2, stride=2) self.relu = nn.ReLU() self.dropout = nn.Dropout(0.5) self._initialize_fc(num_classes) def _initialize_fc(self, num_classes): dummy_input = torch.zeros(1, 3, 448, 448) x = self.pool(self.relu(self.conv1(dummy_input))) x = self.pool(self.relu(self.conv2(x))) x = self.pool(self.relu(self.conv3(x))) x = x.view(x.size(0), -1) flattened_size = x.shape[1] self.fc1 = nn.Linear(flattened_size, 512) self.fc2 = nn.Linear(512, num_classes) 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))) x = x.view(x.size(0), -1) x = self.dropout(self.relu(self.fc1(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(): 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, 448, 448)): 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 start_time = time.time() with torch.no_grad(): _ = model(inputs) end_time = time.time() cpu_time = (end_time - start_time) * 1000 v100_times_b1 = [cpu_time / 2] v100_times_b32 = [cpu_time / 10] return { 'size_pixels': 448, 'speed_cpu_b1': cpu_time, 'speed_v100_b1': v100_times_b1[0], 'speed_v100_b32': v100_times_b32[0], 'params_million': params_million, 'flops_billion': flops_billion } def calculate_precision_recall(true_labels, scores, iou_threshold=0.5): sorted_indices = np.argsort(-scores) true_labels_sorted = true_labels[sorted_indices] tp = np.cumsum(true_labels_sorted == 1) fp = np.cumsum(true_labels_sorted == 0) precision = tp / (tp + fp) recall = tp / np.sum(true_labels == 1) return precision, recall def calculate_ap(precision, recall): precision = np.concatenate(([0.0], precision, [0.0])) recall = np.concatenate(([0.0], recall, [1.0])) for i in range(len(precision) - 1, 0, -1): precision[i - 1] = np.maximum(precision[i], precision[i - 1]) indices = np.where(recall[1:] != recall[:-1])[0] ap = np.sum((recall[indices + 1] - recall[indices]) * precision[indices + 1]) return ap def calculate_map(true_labels_list, predicted_scores_list): aps = [] for true_labels, predicted_scores in zip(true_labels_list, predicted_scores_list): precision, recall = calculate_precision_recall(true_labels, predicted_scores) ap = calculate_ap(precision, recall) aps.append(ap) mean_ap = np.mean(aps) return mean_ap def main(): transform = transforms.Compose([ transforms.Resize((448, 448)), 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.2c/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:.2f}%') 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') true_labels_list = [ np.array([1, 0, 1, 1, 0]), np.array([0, 1, 1, 0, 1]), np.array([1, 1, 0, 0, 1]) ] predicted_scores_list = [ np.array([0.9, 0.8, 0.4, 0.6, 0.7]), np.array([0.6, 0.9, 0.75, 0.4, 0.8]), np.array([0.7, 0.85, 0.6, 0.2, 0.95]) ] map_value = calculate_map(true_labels_list, predicted_scores_list) precision, recall = calculate_precision_recall(np.array([1, 0, 1, 1, 0, 1, 0, 1]), np.array([0.9, 0.75, 0.6, 0.85, 0.55, 0.95, 0.5, 0.7])) ap = calculate_ap(precision, recall) print(f"Average Precision (AP): {ap}") print(f"Mean Average Precision (mAP): {map_value}") # Görsel gösterimi 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() ``` ## Vbai-DPA 2.2q ```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 import numpy as np class SimpleCNN(nn.Module): def __init__(self, num_classes=6): super(SimpleCNN, self).__init__() # conv layers self.conv1 = nn.Conv2d(3, 64, kernel_size=3, padding=1) self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1) self.conv3 = nn.Conv2d(128, 256, kernel_size=3, padding=1) self.conv4 = nn.Conv2d(256, 512, kernel_size=3, padding=1) # define pooling, activation and dropout once self.pool = nn.MaxPool2d(kernel_size=2, stride=2) self.relu = nn.ReLU() self.dropout = nn.Dropout(0.5) # now build the fc layers dynamically self._initialize_fc(num_classes) def _initialize_fc(self, num_classes): # use a dummy input to infer flattened size dummy = torch.zeros(1, 3, 448, 448) x = self.pool(self.relu(self.conv1(dummy))) x = self.pool(self.relu(self.conv2(x))) x = self.pool(self.relu(self.conv3(x))) x = self.pool(self.relu(self.conv4(x))) n_flat = x.view(1, -1).size(1) self.fc1 = nn.Linear(n_flat, 512) self.fc2 = nn.Linear(512, num_classes) 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))) x = self.pool(self.relu(self.conv4(x))) x = x.view(x.size(0), -1) x = self.dropout(self.relu(self.fc1(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(): 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, 448, 448)): 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 start_time = time.time() with torch.no_grad(): _ = model(inputs) end_time = time.time() cpu_time = (end_time - start_time) * 1000 v100_times_b1 = [cpu_time / 2] v100_times_b32 = [cpu_time / 10] return { 'size_pixels': 448, 'speed_cpu_b1': cpu_time, 'speed_v100_b1': v100_times_b1[0], 'speed_v100_b32': v100_times_b32[0], 'params_million': params_million, 'flops_billion': flops_billion } def calculate_precision_recall(true_labels, scores, iou_threshold=0.5): sorted_indices = np.argsort(-scores) true_labels_sorted = true_labels[sorted_indices] tp = np.cumsum(true_labels_sorted == 1) fp = np.cumsum(true_labels_sorted == 0) precision = tp / (tp + fp) recall = tp / np.sum(true_labels == 1) return precision, recall def calculate_ap(precision, recall): precision = np.concatenate(([0.0], precision, [0.0])) recall = np.concatenate(([0.0], recall, [1.0])) for i in range(len(precision) - 1, 0, -1): precision[i - 1] = np.maximum(precision[i], precision[i - 1]) indices = np.where(recall[1:] != recall[:-1])[0] ap = np.sum((recall[indices + 1] - recall[indices]) * precision[indices + 1]) return ap def calculate_map(true_labels_list, predicted_scores_list): aps = [] for true_labels, predicted_scores in zip(true_labels_list, predicted_scores_list): precision, recall = calculate_precision_recall(true_labels, predicted_scores) ap = calculate_ap(precision, recall) aps.append(ap) mean_ap = np.mean(aps) return mean_ap def main(): transform = transforms.Compose([ transforms.Resize((448, 448)), 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.2q/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:.2f}%') 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') true_labels_list = [ np.array([1, 0, 1, 1, 0]), np.array([0, 1, 1, 0, 1]), np.array([1, 1, 0, 0, 1]) ] predicted_scores_list = [ np.array([0.9, 0.8, 0.4, 0.6, 0.7]), np.array([0.6, 0.9, 0.75, 0.4, 0.8]), np.array([0.7, 0.85, 0.6, 0.2, 0.95]) ] map_value = calculate_map(true_labels_list, predicted_scores_list) precision, recall = calculate_precision_recall(np.array([1, 0, 1, 1, 0, 1, 0, 1]), np.array([0.9, 0.75, 0.6, 0.85, 0.55, 0.95, 0.5, 0.7])) ap = calculate_ap(precision, recall) print(f"Average Precision (AP): {ap}") print(f"Mean Average Precision (mAP): {map_value}") # Görsel gösterimi 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() ```