-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevalute.py
More file actions
328 lines (263 loc) · 12 KB
/
evalute.py
File metadata and controls
328 lines (263 loc) · 12 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
from matplotlib import pyplot as plt
from sklearn.metrics import accuracy_score, make_scorer, precision_score,recall_score,f1_score
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.metrics import classification_report
from sklearn.model_selection import cross_validate
from torch.utils.data import random_split
from torch.utils.data import DataLoader, Subset
from skorch.helper import SliceDataset
from sklearn.model_selection import KFold
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torchvision.datasets
import torch.optim as optim
from cnnModel import CNN
from cnnModel import CNNV1
from cnnModel import CNNV2
from train import train
# Load model paths (replace with actual paths on your system or provide relative paths)
# Replace with your own model path
loadModelPath = "YOUR_MODEL_PATH_HERE" # Example: ./Models/model_main
# loadModelPath = "YOUR_MODEL_PATH_HERE" # Example: ./Models/modelv1
# loadModelPath = "YOUR_MODEL_PATH_HERE" # Example: ./Models/modelv2
# Trying to mitigate bias (replace with actual model path)
loadModelPath = "YOUR_MODEL_PATH_HERE" # Example: ./Models/modelBias
# loadModelPath = "YOUR_MODEL_PATH_HERE" # Example: ./Models/modelBias_best.pth
# Best fit models (replace with actual model path)
# loadModelPath = "YOUR_MODEL_PATH_HERE" # Example: ./Models/model_best.pth
# loadModelPath = "YOUR_MODEL_PATH_HERE" # Example: ./Models/modelv1_best.pth
# loadModelPath = "YOUR_MODEL_PATH_HERE" # Example: ./Models/modelv2_best.pth
modelPath1 = "Your_Model_PATH_HERE" #main
# Dataset paths (replace with actual dataset path on your system or use relative paths)
# Original dataset
# dataPath = "YOUR_DATASET_PATH_HERE" # Example: ./Dataset/train
# Bias mitigation datasets (replace with actual dataset path or use relative paths)
# Age-related bias
# dataPath = "YOUR_DATASET_PATH_HERE" # Example: ./Dataset/age-bias-train/middle
# dataPath = "YOUR_DATASET_PATH_HERE" # Example: ./Dataset/age-bias-train/senior
# dataPath = "YOUR_DATASET_PATH_HERE" # Example: ./Dataset/age-bias-train/young
# Gender-related bias
# dataPath = "YOUR_DATASET_PATH_HERE" # Example: ./Dataset/gender/Female
dataPath = "YOUR_DATASET_PATH_HERE" # Example: ./Dataset/gender/Male
"""Evaluation with Confusion Matrix"""
def evaluate(cnnModel,testLoader, classes ):
#Evaluation with Test Data
cnnModel.eval()
yTest = []
yPredict = []
with torch.no_grad():
for images, labels in testLoader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
yTest.extend(labels.detach().cpu().numpy())
yPredict.extend(predicted.detach().cpu().numpy())
#Printing report out for recall,precision, f1-score and accuracy of model
print(classification_report(yTest, yPredict))
#accuracy for test
print("Accuracy for {} Dataset: {}%" .format("Test" , round(accuracy_score(yTest,yPredict)*100,2)))
#confusion matrix
ConfusionMatrixDisplay.from_predictions(yTest, yPredict, display_labels=classes)
plt.title('Confusion Matrix for {} Dataset'.format('Test'))
plt.show()
"""K-fold Cross-Validate with k = 10 folds"""
def trainKFold(model, trainLoader, valLoader, criterion, optimizer, device, num_epochs, patience = 5):
#Early stopping variables
bestValLoss = float('inf')
patience = 5
noImprovementCount = 0
#Best fit variables
#best-fit save path
# bestModel = modelPath + "_best.pth"
#training model
#training loop
for epoch in range(num_epochs):
#training phase
model.train()
trainCorrect = 0
trainTotal = 0
trainLoss = 0.0
for i, (images,labels) in enumerate(trainLoader):
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
#train loss
trainLoss += loss.item() * images.size(0)
#train accuarcy
_, trainPredicted = torch.max(outputs.data, 1)
trainTotal += labels.size(0)
trainCorrect += (trainPredicted == labels).sum().item()
#training accuracy and loss for each epoch
trainAccuracy = trainCorrect/trainTotal
trainLoss /= len(trainLoader.dataset)
#validating train model and save best fit model
model.eval()
valLoss = 0.0
valCorrect = 0
valTotal = 0
for images, labels in valLoader:
with torch.no_grad():
outputs = model(images)
loss = criterion(outputs, labels)
#val loss
valLoss += loss.item() * images.size(0)
#val accuracy
_, predicted = torch.max(outputs.data, 1)
valTotal += labels.size(0)
valCorrect += (predicted == labels).sum().item()
#validation results for each epoch
valLoss /= len(valLoader.dataset)
valAccuracy = valCorrect/valTotal
#print results for each epoch
print(f"Epoch [{epoch+1}/{num_epochs}], Train Loss: {trainLoss:.4f}, Validation Loss: {valLoss:.4f}, "
f"Training Accuracy: {trainAccuracy * 100:.2f}%, Validation Accuracy: {valAccuracy*100:.2f}%")
#best fit checking
if valLoss < bestValLoss:
bestValLoss = valLoss
#save best-fit model
# torch.save(model.state_dict(), bestModel)
noImprovementCount = 0
else:
noImprovementCount += 1
#Early stopping
if noImprovementCount > patience:
print("*** Early Stopping Happened! ****")
break
# #saving the final model
# torch.save(model.state_dict(), modelPath)
print("\n ++++++ Training Complete!! +++++ ")
#Takes a long time to compute
def kFoldCrossValidation(cnnModel,trainData,valLoader,k = 10):
#Hyperparameters
num_epochs = 10 #minimum of 10 iteration
kf = KFold(n_splits=k, shuffle = True, random_state = 0)
# #store the macro metrics
macroAccuracy = 0.0
macroPrecision = 0.0
macroRecall = 0.0
macroF1 = 0.0
#micro metric
microAccuracy = 0.0
microPrecision = 0.0
microRecall = 0.0
microF1 = 0.0
#train
cnnModel.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 0.001)
#k-fold loop
for fold, (trainIndex, _) in enumerate(kf.split(trainData),1): #start it at 1
print("===========================================")
print(f"Fold {fold}/ {k}:")
print("========================================")
trainSet = Subset(trainData, trainIndex) #creates subset of train data for eack k fold
trainLoader = DataLoader(trainSet, batch_size=32, shuffle=True, num_workers=2)
#train the model
trainKFold(cnnModel, trainLoader,valLoader,criterion,optimizer,device, num_epochs)
#evalute
cnnModel.eval()
valLoss = 0.0
valCorrect = 0
valTotal = 0
yTrue = []
yPredict = []
with torch.no_grad():
for images, labels in valLoader:
images,labels = images.to(device), labels.to(device)
outputs = cnnModel(images)
loss = criterion(outputs, labels)
#val loss
valLoss += loss.item() * images.size(0)
#val accuracy
_, predicted = torch.max(outputs.data, 1)
valTotal += labels.size(0)
valCorrect += (predicted == labels).sum().item()
yTrue.extend(labels.cpu().numpy())
yPredict.extend(predicted.cpu().numpy())
#Metrics Calculations Macro
accuracy = accuracy_score(yTrue, yPredict)
precision = precision_score (yTrue, yPredict, average='macro')
recall = recall_score (yTrue, yPredict, average='macro')
f1 = f1_score (yTrue, yPredict, average='macro')
#printing k-fold results
#Macro
print("Macro Values: ")
print(f"Validation Loss {valLoss / len (valLoader.dataset):.4f}, Accuracy: {accuracy *100:.2f}%, "
f"Precision: {precision:.2f}, Recall: {recall:.2f}, F1-Score: {f1:.2f}")
#Metrics Calculations micro
accuracyMic = accuracy_score(yTrue, yPredict)
precisionMic = precision_score (yTrue, yPredict, average='micro')
recallMic = recall_score (yTrue, yPredict, average='micro')
f1Mic = f1_score (yTrue, yPredict, average='micro')
#printing k-fold results
#Macro
print("\nMicro Values: ")
print(f"Validation Loss {valLoss / len (valLoader.dataset):.4f}, Accuracy: {accuracyMic *100:.2f}%, "
f"Precision: {precisionMic:.2f}, Recall: {recallMic:.2f}, F1-Score: {f1Mic:.2f}")
#Total fold metrics
macroAccuracy += accuracy
macroPrecision += precision
macroRecall += recall
macroF1 += f1
microAccuracy += accuracyMic
microPrecision += precisionMic
microRecall += recallMic
microF1 += f1Mic
#average values of total folds
averageMacroAccuracy = macroAccuracy/k
averageMacroPrecision = macroPrecision/k
averageMacroRecall = macroRecall/k
averageMacroF1 = macroF1/k
averageMicroAccuracy = microAccuracy/k
averageMicroPrecision = microPrecision/k
averageMicroRecall = microRecall/k
averageMicroF1 = microF1/k
#print K-fold cross validation
print(f"\nK-Fold Cross Validation Average Results for {k} Folds")
print("===============================================")
print("Macro Averages: ")
print(f"Accuracy: {averageMacroAccuracy *100:.2f}%, Precision: {averageMacroPrecision:.2f}, "
f"Recall: {averageMacroRecall:.2f}, F1-Score: {averageMacroF1:.2f}")
print("\nMicro Average Values: ")
print(f"Accuracy: {averageMicroAccuracy *100:.2f}%, Precision: {averageMicroPrecision:.2f}, "
f"Recall: {averageMicroRecall:.2f}, F1-Score: {averageMicroF1:.2f}")
#To run python script
if __name__ == "__main__":
#Set random seed to be the same each time to help with reproducability
torch.manual_seed(0)
np.random.seed(0)
#transform
transform = transforms.Compose(
[transforms.Grayscale(num_output_channels=1), #Images are in grayscaled
transforms.Resize((48,48)),#image size 48 x 48
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
#getting dataset for model to be evaluated on
dataset = torchvision.datasets.ImageFolder(dataPath, transform=transform)
#randomely spliting the datset into training and testing (70% for training, 20 % for testing and 10% for validation)
m = len(dataset)
trainSize = int(0.7*m)
testSize = int(0.1*m)
valSize = m - trainSize - testSize
trainData, testData, valData = random_split(dataset, [trainSize,testSize,valSize])
#Data Loader
#allow random order for loading data (shuffle = true) and use 2 subprocess to load data
trainLoader = DataLoader(trainData, batch_size=32, shuffle=True, num_workers=2)
testLoader = DataLoader(testData, batch_size=32, shuffle=False, num_workers=2)
valLoader = DataLoader(valData, batch_size=32, shuffle=False, num_workers=2)
#checking if user has cuda to be able to use GPU instead of CPU for training
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#yTrain = np.array ([y for x, y,in iter (trainData)])
classes = ("angry", "neutral", "engaged", "surpise") #classes for classification
#model = CNNV1() #variant 1
#model = CNNV2() #variant 2
model = CNN()
model.load_state_dict(torch.load(loadModelPath))
evaluate(model,testLoader,classes)
#kFoldCrossValidation(model,trainData,valLoader, k=10)