Tbai-DPA 1.0 Sürümü (TR) [BETA]
Tanım
Tbai-DPA 1.0 (Dementia, Parkinson, Alzheimer) modeli, MRI veya fMRI görüntüsü üzerinden beyin hastalıklarını yorumlayarak daha detaylı 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.
Kitle / Hedef
Tbai modelleri, Vbai ile birlikte çalışarak; öncelikle hastaneler, sağlık merkezleri ve bilim merkezleri için geliştirilmiştir.
Sınıflar
- Alzheimer Hastası
- Ortalama Alzheimer Riski
- Hafif Alzheimer Riski
- Çok Hafif Alzheimer Riski
- Risk Yok
- Parkinson Hastası
----------------------------------------
Tbai-DPA 1.0 Version (EN) [BETA]
Description
The Tbai-DPA 1.0 (Dementia, Parkinson's, Alzheimer's) model has been trained and developed to interpret brain diseases through MRI or fMRI images for more detailed diagnosis. It indicates whether the patient has Parkinson's disease, dementia, and Alzheimer's risk with a high accuracy rate.
Audience / Target
Tbai models, working in conjunction with Vbai, have been developed primarily for hospitals, health centers, and science centers.
Classes
- Alzheimer's disease
- Average Risk of Alzheimer's Disease
- Mild Alzheimer's Risk
- Very Mild Alzheimer's Risk
- No Risk
- Parkinson's Disease
Kullanım / Usage
Sanal ortam oluşturun. / Create a virtual environment.
python -3.9.0 -m venv myenv
Bağımlılıkları yükleyin. / Load dependencies.
pip install -r requirements.txt
Dosyayı çalıştırın. / Run the script.
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=UserWarning)
def load_tbai_model(model_dir: str, device):
tokenizer = T5Tokenizer.from_pretrained(model_dir)
model = T5ForConditionalGeneration.from_pretrained(model_dir).to(device)
return tokenizer, model
def generate_comment_sampling(
tokenizer,
model,
sinif_adi: str,
device,
max_length: int = 128
) -> str:
input_text = f"Sınıf: {sinif_adi}"
inputs = tokenizer(
input_text,
return_tensors="pt",
padding="longest",
truncation=True,
max_length=32
).to(device)
out_ids = model.generate(
**inputs,
max_length=max_length,
do_sample=True,
top_k=50,
top_p=0.95,
no_repeat_ngram_size=2,
early_stopping=True
)
comment = tokenizer.decode(out_ids[0], skip_special_tokens=True)
return comment
def test_with_sampling():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
tokenizer, model = load_tbai_model(
"Tbai/model/dir/path",
device)
test_classes = [
"alzheimer disease",
"mild alzheimer risk",
"moderate alzheimer risk",
"very mild alzheimer risk",
"no risk",
"parkinson disease"
]
for cls in test_classes:
print(f"--- Class: {cls} (Deneme 1) ---")
print(generate_comment_sampling(tokenizer, model, cls, device))
print(f"--- Class: {cls} (Deneme 2) ---")
print(generate_comment_sampling(tokenizer, model, cls, device))
print()
def main():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f">>> Using device: {device}\n")
model_dir = "Tbai/model/dir/path"
tokenizer, model = load_tbai_model(model_dir, device)
print(">>> Tokenizer ve model başarıyla yüklendi.\n")
test_classes = [
"alzheimer disease",
"mild alzheimer risk",
"moderate alzheimer risk",
"very mild alzheimer risk",
"no risk",
"parkinson disease"
]
for cls in test_classes:
generated = generate_comment_sampling(tokenizer, model, cls, device)
print(f"Sınıf: {cls}")
print(f"Üretilen Yorum: {generated}\n")
if __name__ == "__main__":
main()
- Görüntü İşleme Modeli ile Beraber Çalıştırın. / Run Together with Image Processing Model.
import os
import time
import torch
import torch.nn as nn
from torchvision import transforms
from PIL import Image
import matplotlib.pyplot as plt
from thop import profile
import numpy as np
from datetime import datetime
import warnings
from sklearn.metrics import average_precision_score
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=UserWarning)
from transformers import T5Tokenizer, T5ForConditionalGeneration
class SimpleCNN(nn.Module):
def __init__(self, model_type='c', num_classes=6): # Model tipine göre "model_type" değişkeni "f, c, q" olarak değiştirilebilir. / 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.5)
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: nn.Module, image_path: str, transform, device):
img = Image.open(image_path).convert('RGB')
inp = transform(img).unsqueeze(0).to(device)
model.eval()
with torch.no_grad():
out = model(inp)
prob = torch.nn.functional.softmax(out, dim=1)
pred = prob.argmax(dim=1).item()
conf = prob[0, pred].item() * 100
return pred, conf, inp, prob
def calculate_performance_metrics(model: nn.Module, device, input_size=(1, 3, 224, 224)):
model.to(device)
x = torch.randn(input_size).to(device)
flops, params = profile(model, inputs=(x,), verbose=False)
cpu_start = time.time()
_ = model(x)
cpu_time = (time.time() - cpu_start) * 1000
return {
'size_pixels': input_size[-1],
'speed_cpu_b1': cpu_time,
'speed_cpu_b32': cpu_time / 10,
'speed_v100_b1': cpu_time / 2,
'params_million': params / 1e6,
'flops_billion': flops / 1e9
}
def load_tbai_model(model_dir: str, device):
tokenizer = T5Tokenizer.from_pretrained(model_dir)
model = T5ForConditionalGeneration.from_pretrained(model_dir).to(device)
model.eval()
return tokenizer, model
def generate_comment_turkce(tokenizer, model, sinif_adi: str, device, max_length: int = 64) -> str:
input_text = f"Sınıf: {sinif_adi}"
inputs = tokenizer(
input_text,
return_tensors="pt",
padding="longest",
truncation=True,
max_length=32
).to(device)
out_ids = model.generate(
**inputs,
max_length=max_length,
do_sample=True,
top_k=50,
top_p=0.95,
no_repeat_ngram_size=2,
early_stopping=True
)
comment = tokenizer.decode(out_ids[0], skip_special_tokens=True)
return comment
def save_monitoring_log(predicted_class, confidence, comment_text,
metrics, class_names, image_path, ap_scores=None, map_score=None,
log_path='monitoring_log.txt'):
os.makedirs(os.path.dirname(log_path) or '.', exist_ok=True)
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
img_name = os.path.basename(image_path)
log = f"""
===== Model Monitoring Log =====
Timestamp: {timestamp}
Image: {img_name}
Predicted Class: {class_names[predicted_class]}
Confidence: {confidence:.2f}%
Comment: {comment_text}
-- Performance Metrics --
Params (M): {metrics['params_million']:.2f}
FLOPs (B): {metrics['flops_billion']:.2f}
Image Size: {metrics['size_pixels']}x{metrics['size_pixels']}
CPU Time b1 (ms): {metrics['speed_cpu_b1']:.2f}
V100 Time b1 (ms): {metrics['speed_v100_b1']:.2f}
V100 Time b32 (ms): {metrics['speed_cpu_b32']:.2f}
-- AP/mAP Metrics --"""
if ap_scores is not None and map_score is not None:
log += f"\nmAP: {map_score:.4f}"
for i, (class_name, ap) in enumerate(zip(class_names, ap_scores)):
log += f"\nAP_{class_name}: {ap:.4f}"
else:
log += "\nAP/mAP: Not calculated (single image)"
log += "\n================================\n"
with open(log_path, 'a', encoding='utf-8') as f:
f.write(log)
def main():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
])
class_names = [
'Alzheimer Disease',
'Mild Alzheimer Risk',
'Moderate Alzheimer Risk',
'Very Mild Alzheimer Risk',
'No Risk',
'Parkinson Disease'
]
model = SimpleCNN(model_type='c', num_classes=len(class_names)).to(device) # Model tipine göre "model_type" değişkeni "f, c, q" olarak değiştirilebilir. / The ‘model_type’ variable can be changed to ‘f, c, q’ according to the model type.
model_path = 'Vbai/model/file/path'
try:
model.load_state_dict(torch.load(model_path, map_location=device))
except Exception as e:
print(f"Görüntü modeli yükleme hatası: {e}")
return
metrics = calculate_performance_metrics(model, device)
tbai_model_dir = "Tbai/model/dir/path"
tokenizer, tbai_model = load_tbai_model(tbai_model_dir, device)
en2tr = {
'Alzheimer Disease': 'Alzheimer Hastalığı',
'Mild Alzheimer Risk': 'Hafif Alzheimer Riski',
'Moderate Alzheimer Risk': 'Orta Düzey Alzheimer Riski',
'Very Mild Alzheimer Risk': 'Çok Hafif Alzheimer Riski',
'No Risk': 'Risk Yok',
'Parkinson Disease': 'Parkinson Hastalığı'
}
image_path = 'test/images/path'
pred_class_idx, confidence, inp_tensor, predicted_probs = predict_image(model, image_path, transform, device)
predicted_class_name = class_names[pred_class_idx]
print(f"Prediction: {predicted_class_name} ({confidence:.2f}%)")
print(f"Confidence: {confidence:.2f}%")
print(f"Params (M): {metrics['params_million']:.2f}")
print(f"FLOPs (B): {metrics['flops_billion']:.2f}")
print(f"Image Size: {metrics['size_pixels']}x{metrics['size_pixels']}")
print(f"CPU Time b1 (ms): {metrics['speed_cpu_b1']:.2f}")
print(f"V100 Time b1 (ms): {metrics['speed_v100_b1']:.2f}")
print(f"V100 Time b32 (ms): {metrics['speed_cpu_b32']:.2f}")
tr_class_name = en2tr.get(predicted_class_name, predicted_class_name)
try:
comment_text = generate_comment_turkce(tokenizer, tbai_model, tr_class_name, device)
except Exception as e:
print(f"Yorum üretme hatası: {e}")
comment_text = "Yorum üretilemedi."
print(f"\nComment (Tbai-DPA 1.0): {comment_text}")
save_monitoring_log(
pred_class_idx, confidence, comment_text,
metrics, class_names, image_path)
img_show = inp_tensor.squeeze(0).permute(1, 2, 0).cpu().numpy()
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
img_show = img_show * std + mean
img_show_clipped = np.clip(img_show, 0.0, 1.0)
plt.imshow(img_show_clipped)
plt.title(f'{predicted_class_name} — {confidence:.2f}%')
plt.axis('off')
plt.show()
if __name__ == '__main__':
main()