-
Notifications
You must be signed in to change notification settings - Fork 0
/
confmatrixkmu.py
146 lines (119 loc) · 5.11 KB
/
confmatrixkmu.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
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
from __future__ import print_function
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import transforms as transforms
import argparse
import itertools
import os
from torch.autograd import Variable
import matplotlib.pyplot as plt
import numpy as np
from models.build import EfficientViT_M2
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
import timm
from KMU import KMU
from models import combmodel1
from torchvision import models
from models import *
parser = argparse.ArgumentParser(description='PyTorch MKUFED shuffvit-DFER Training')
parser.add_argument('--dataset', type=str, default='shufflnetw1', help='CNN architecture')
parser.add_argument('--model', type=str, default='efficientViT', help='CNN architecture')
opt = parser.parse_args()
device = "cuda:0" if torch.cuda.is_available() else "cpu"
#cut_size = 44
transforms_vaild = torchvision.transforms.Compose([
torchvision.transforms.ToPILImage(),
torchvision.transforms.Resize((224,)),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225,))
])
def plot_confusion_matrix(cm, classes,
normalize=True,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title, fontsize=16)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label', fontsize=18)
plt.xlabel('Predicted label', fontsize=18)
plt.tight_layout()
class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sadness', 'Surprise']
# Model
if opt.model == 'efficientViT':
#pretrained model
net = EfficientViT_M4(pretrained='efficientvit_m4')
num_features = net.head.l.in_features
net.head.l = nn.Linear(num_features, 6)
if opt.model == 'Ourmodel':
num_classes = 6 # Adjust based on your task
#net = combmodel2.CombinedModel(num_classes)
net = shufflenetw.CombinedModel(num_classes)
#net = shufflenetwcc.CombinedModel(num_classes)
#net = efficientvitw.CombinedModel(num_classes)
#net = efficientvitwcc.CombinedModel(num_classes)
correct = 0
total = 0
all_target = []
for i in range(10):
print("%d fold" % (i+1))
path = os.path.join(opt.dataset + '_' + opt.model, '%d' %(i+1))
checkpoint = torch.load(os.path.join(path, 'Test_model.t7'))
net.load_state_dict(checkpoint['net'])
#net.cuda()
net.to(device)
net.eval()
testset = KMU(split = 'Testing', fold = i+1, transform=transforms_vaild)
testloader = torch.utils.data.DataLoader(testset, batch_size=128, shuffle=False)
for batch_idx, (inputs, targets) in enumerate(testloader):
inputs, targets = inputs.to(device), targets.to(device)
inputs, targets = Variable(inputs, volatile=True), Variable(targets)
outputs = net(inputs)
#outputs = net(inputs.to('cuda:1'))
# avg over crops
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
if batch_idx == 0 and i == 0:
all_predicted = predicted
all_targets = targets
else:
all_predicted = torch.cat((all_predicted, predicted), 0)
all_targets = torch.cat((all_targets, targets), 0)
acc = 100. * correct / total
print("accuracy: %0.3f" % acc)
# Compute confusion matrix
matrix = confusion_matrix(all_targets.data.cpu().numpy(), all_predicted.cpu().numpy())
np.set_printoptions(precision=2)
# Print classification report
print('Classification Report:\n', classification_report(all_targets.data.cpu().numpy(), all_predicted.cpu().numpy(), target_names=class_names))
# Plot normalized confusion matrix
plt.figure(figsize=(10, 8))
plot_confusion_matrix(matrix, classes=class_names, normalize=True,
title= 'Confusion Matrix (Accuracy: %0.3f%%)' %acc)
#plt.show()
plt.savefig(os.path.join(opt.dataset + '_' + opt.model, 'lightmtrcmobilevit.png'))
plt.close()