-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMousePainDetectionSystem.py
More file actions
253 lines (201 loc) · 9.41 KB
/
MousePainDetectionSystem.py
File metadata and controls
253 lines (201 loc) · 9.41 KB
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
import json
import os
from matplotlib import pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import transforms
from PIL import Image
from MouseDataLoader import MousePainDataLoader
from MouseEyeDataset import MouseEyeDataset
from SimpleYOLO import SimpleYOLO, SimpleYOLOLoss
from PainClassifier import PainClassifier
class MousePainDetectionSystem:
def __init__(self, device='cuda' if torch.cuda.is_available() else 'cpu'):
"""
Initialize the detection system. Handles data loading, model training, and inference.
Args:
device: 'cuda' for GPU, 'cpu' for CPU
"""
self.device = device
self.data_loader = MousePainDataLoader()
self.yolo_model = SimpleYOLO(num_classes=1).to(device)
self.pain_classifier = PainClassifier(num_classes=3).to(device)
self.classification_loss = nn.CrossEntropyLoss()
self.yolo_optimizer = torch.optim.Adam(self.yolo_model.parameters(), lr=1e-4)
self.classifier_optimizer = torch.optim.Adam(self.pain_classifier.parameters(), lr=1e-4)
def prepare_data(self, batch_size=8):
# prepare datasets and data loaders
all_samples = self.data_loader.get_all_samples()
if not all_samples:
print("No samples found")
return
# for detection, training on images with bounding boxes
self.detection_dataset = MouseEyeDataset(all_samples, img_size=416, mode='detection')
# for classification, training on images with pain levels
self.classification_dataset = MouseEyeDataset(all_samples, img_size=224, mode='classification')
self.det_loader = DataLoader(self.detection_dataset, batch_size=batch_size, shuffle=True)
self.cls_loader = DataLoader(self.classification_dataset, batch_size=batch_size, shuffle=True)
print(f"Prepared datasets:")
print(f"\tDetection: {len(self.detection_dataset)} samples")
print(f"\tClassification: {len(self.classification_dataset)} samples")
def train_yolo(model, train_loader, device, num_epochs=10):
"""Trenuj model YOLO z właściwą funkcją straty"""
print("=== TRENING YOLO ===")
os.makedirs("metrics", exist_ok=True)
os.makedirs("metrics/plots", exist_ok=True)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
criterion = SimpleYOLOLoss()
all_losses = []
batch_losses = []
for epoch in range(num_epochs):
model.train()
total_loss = 0
epoch_losses = []
print(f"YOLO Epoch {epoch+1}/{num_epochs}")
for batch_idx, (images, targets) in enumerate(train_loader):
images, targets = images.to(device), targets.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
total_loss += loss.item()
epoch_losses.append(loss.item())
batch_losses.append(loss.item())
if batch_idx % 5 == 0:
print(f"\tBatch {batch_idx+1}, Loss: {loss.item():.4f}")
avg_loss = total_loss / len(train_loader)
all_losses.append(avg_loss)
print(f"YOLO Epoch {epoch+1} - Avg Loss: {avg_loss:.4f}")
metrics = {
"epoch_losses": all_losses,
"batch_losses": batch_losses
}
with open("metrics/yolo_metrics.json", "w") as f:
json.dump(metrics, f)
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.plot(range(1, num_epochs+1), all_losses, "o-")
plt.title("YOLO: Loss per epoch")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.grid(True)
plt.subplot(1, 2, 2)
plt.plot(batch_losses[:100])
plt.title("YOLO: Training loss per batch (first 100)")
plt.xlabel("Batch")
plt.ylabel("Loss")
plt.grid(True)
plt.tight_layout()
plt.savefig("metrics/plots/yolo_training_loss.png")
plt.close()
torch.save(model.state_dict(), "yolo_model.pth")
print("Model YOLO saved as 'yolo_model.pth'")
return all_losses
def train_classifier(model, train_loader, val_loader, device, num_epochs=10):
"""Train the pain classifier"""
print("=== TRAINING CLASSIFIER ===")
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
best_acc = 0.0
train_losses = []
train_accuracies = []
val_accuracies = []
for epoch in range(num_epochs):
model.train()
epoch_train_loss = 0
train_correct = 0
train_total = 0
for images, labels in train_loader:
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
epoch_train_loss += loss.item()
_, predicted = torch.max(outputs, 1)
train_total += labels.size(0)
train_correct += (predicted == labels).sum().item()
epoch_train_loss /= len(train_loader)
epoch_train_acc = 100 * train_correct / train_total
model.eval()
val_correct = 0
val_total = 0
with torch.no_grad():
for images, labels in val_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs, 1)
val_total += labels.size(0)
val_correct += (predicted == labels).sum().item()
epoch_val_acc = 100 * val_correct / val_total
train_losses.append(epoch_train_loss)
train_accuracies.append(epoch_train_acc)
val_accuracies.append(epoch_val_acc)
print(f"Classifier Epoch {epoch+1}/{num_epochs}")
print(f"\tTrain Loss: {epoch_train_loss:.4f}")
print(f"\tTrain Acc: {epoch_train_acc:.2f}%, Val Acc: {epoch_val_acc:.2f}%")
if epoch_val_acc > best_acc:
best_acc = epoch_val_acc
torch.save(model.state_dict(), "best_classifier.pth")
metrics = {
"train_losses": train_losses,
"train_accuracies": train_accuracies,
"val_accuracies": val_accuracies,
"best_val_accuracy": best_acc
}
with open("metrics/classifier_metrics.json", "w") as f:
json.dump(metrics, f)
epochs = range(1, num_epochs+1)
plt.figure()
plt.plot(epochs, train_losses, "bo-")
plt.title("Classifier: Training Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.grid(True)
plt.savefig("metrics/plots/classifier_loss.png")
plt.close()
plt.figure()
plt.plot(epochs, train_accuracies, "bo-", label="Training")
plt.plot(epochs, val_accuracies, "ro-", label="Validation")
plt.title("Classifier: Accuracy")
plt.xlabel("Epoch")
plt.ylabel("Accuracy (%)")
plt.legend()
plt.grid(True)
plt.savefig("metrics/plots/classifier_accuracy.png")
plt.close()
print(f"Best accuracy of classifier: {best_acc:.2f}%")
return metrics
def predict_pain_level(self, image_path):
"""Predict pain level for an image"""
try:
image = Image.open(image_path).convert('RGB')
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])
])
input_tensor = transform(image).unsqueeze(0).to(self.device)
self.pain_classifier.eval()
with torch.no_grad():
output = self.pain_classifier(input_tensor)
probabilities = F.softmax(output, dim=1)
predicted_class = torch.argmax(probabilities, dim=1).item()
return predicted_class, probabilities.cpu().numpy()[0]
except Exception as e:
print(f"Error predicting for {image_path}: {e}")
return None, None
def save_models(self, classifier_path='pain_classifier.pth'):
torch.save(self.pain_classifier.state_dict(), classifier_path)
print(f"Model saved in: {classifier_path}")
def load_models(self, classifier_path='pain_classifier.pth'):
if os.path.exists(classifier_path):
self.pain_classifier.load_state_dict(torch.load(classifier_path, map_location=self.device))
print(f"Model loaded from: {classifier_path}")
else:
print(f"Model couldn't be found: {classifier_path}")