-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_confusion_matrix.py
56 lines (38 loc) · 1.37 KB
/
get_confusion_matrix.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
import torch
from utils import get_confusion_matrix, plot_confusion_matrix
from dataset import DynamicSolarPanelSoilingDataset
from torchvision import transforms
from torch.utils.data import DataLoader
import numpy as np
from sklearn.metrics import confusion_matrix
device = torch.device('cuda:0')
ds = DynamicSolarPanelSoilingDataset(16, "Solar_Panel_Soiling_Image_dataset\\PanelImages", every=1, format='PNG', transform=transforms.ToTensor())
model = torch.load('classifier_checkpoints\\ShortConv_16_5\\MODEL36.pt').to(device)
dataloader = DataLoader(ds, 64)
predictions = []
labels = []
with torch.no_grad():
for i, (img, true, irradiance) in enumerate(dataloader):
img = img.to(device)
irradiance = irradiance.to(device)
true = true.tolist()
for t in true:
labels.append(t)
preds = model(img, irradiance)
preds = torch.softmax(preds, dim=1)
preds = torch.argmax(preds, dim=1)
preds = preds.cpu().tolist()
for p in preds:
predictions.append(p)
predictions = np.array(predictions)
labels = np.array(labels)
print(predictions.shape)
print(labels.shape)
torch.save(predictions, 'preds.pt')
torch.save(labels, 'labels.pt')
"""
predictions = torch.load("preds.pt")
labels = torch.load("labels.pt")
"""
cf = get_confusion_matrix(predictions, labels, per=True)
plot_confusion_matrix(cf)