-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model_eval_folio.py
98 lines (81 loc) · 3.49 KB
/
Model_eval_folio.py
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
import torch
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.datasets import ImageFolder
from sklearn.metrics import classification_report
from DeepHybridnet import DeepHybridnet
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
def imshow(inp, title=None):
inp = inp.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.figure(figsize=(20, 5))
plt.imshow(inp)
if title is not None:
plt.title(title)
plt.pause(0.001)
def visualize_model_predictions(model, dataloader, device, num_images=6):
images_so_far = 0
plt.figure()
with torch.no_grad():
for i, (inputs, labels) in enumerate(dataloader):
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
for j in range(inputs.size()[0]):
images_so_far += 1
ax = plt.subplot(num_images//2, 2, images_so_far)
ax.axis('off')
ax.set_title(f'predicted: {dataloader.dataset.classes[preds[j]]}\ntrue: {dataloader.dataset.classes[labels[j]]}')
imshow(inputs.cpu().data[j])
if images_so_far == num_images:
return
def main():
transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
test_dataset = ImageFolder(root='G:/vineet/medicinal_plant/Code/V1/mendley_dataset/split_dataset/test', transform=transform)
print("loading medicinal dataset")
test_loader = DataLoader(dataset=test_dataset, batch_size=32, shuffle=False, num_workers=2)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
num_classes = 30
model = DeepHybridnet(num_classes=num_classes).to(device)
print("loading model Deep HybridNet")
model.load_state_dict(torch.load('G:/vineet/medicinal_plant/Code/V1/weights/model.pth', map_location=device))
model.eval()
visualize_model_predictions(model, test_loader, device, num_images=6)
correct = 0
total = 0
all_preds = []
all_labels = []
with torch.no_grad():
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
all_preds.extend(predicted.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
accuracy = 100 * correct / total
print(f'Accuracy of the model on the test images: {accuracy:.2f}%')
cm = confusion_matrix(all_labels, all_preds )
print("evaluating the model")
print(classification_report(all_labels, all_preds, target_names=test_dataset.classes))
cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
plt.figure(figsize=(25, 10))
sns.heatmap(cm_normalized, annot=True, fmt=".2f", cmap='Blues')
plt.title('Normalized Confusion Matrix')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.show()
if __name__ == '__main__':
main()