File size: 1,957 Bytes
9339e0c |
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 |
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import numpy as np
def testModel(model, test_features, test_labels, dataset, num_frames=32):
"""
"Test the LSTM model on the test set."
"""
model.eval()
test_loss = 0.0
test_total = 0
test_correct = 0
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
criterion = nn.CrossEntropyLoss()
test_dataset = TensorDataset(test_features, test_labels)
test_loader = DataLoader(test_dataset, batch_size=num_frames, shuffle=False)
predicted_labels = []
all_outputs = []
with torch.no_grad():
for batch_x, batch_y in test_loader:
batch_x, batch_y = batch_x.to(device), batch_y.to(device)
outputs = model(batch_x)
loss = criterion(outputs, batch_y.long())
test_loss += loss.item()
_, predicted = torch.max(outputs, 1)
predicted_labels.extend(predicted.cpu().numpy())
# Store softmax probabilities
softmax_outputs = torch.nn.functional.softmax(outputs, dim=1)
all_outputs.append(softmax_outputs.cpu().numpy())
test_correct += (predicted == batch_y).sum().item()
test_total += batch_y.size(0)
y_pred = predicted_labels
y_pred_proba = np.vstack(all_outputs) # Convert list of arrays to a single numpy array
test_loss /= len(test_loader)
test_accuracy = 100 * test_correct / test_total
# Print final testing results
print(f'Test Loss: {test_loss:.4f}, Test Accuracy: {test_accuracy:.2f}%')
# Save the test results to file .txt
with open(f'./benchmarks/{dataset}/benchmark.txt', 'a') as f:
f.write(f"Test Loss: {test_loss:.4f}, Test Accuracy: {test_accuracy:.2f}%\n")
return y_pred, y_pred_proba
|