-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTesting_ONED_CNN_original.py
126 lines (104 loc) · 5.04 KB
/
Testing_ONED_CNN_original.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
# _ _ _____ _ _ ____ ____ ____ _ _
# | \ | |_ _| | | | | _ \/ ___|| _ \ | | __ _| |__
# | \| | | | | | | | | | | \___ \| |_) | | | / _` | '_ \
# | |\ | | | | |_| | | |_| |___) | __/ | |__| (_| | |_) |
# |_| \_| |_| \___/ |____/|____/|_| |_____\__,_|_.__/
#
import numpy as np
import torch
from torch import nn
from torch.utils.data import DataLoader
from MyDataLoader import MyNoiseDataset
from Bcolors import bcolors
from sklearn.metrics import classification_report
BATCH_SIZE = 250
#------------------------------------------------------------
def create_data_loader(train_data, batch_size):
train_dataloader = DataLoader(train_data, batch_size)
return train_dataloader
#-------------------------------------------------------------
# Function : load_weigth_for_model()
# Loading the weights to model from pre-trained coefficients
#-------------------------------------------------------------
def load_weigth_for_model(model, pretrained_path, device):
model_dict = model.state_dict()
pretrained_dict = torch.load(pretrained_path,map_location= device)
for k, v in model_dict.items():
model_dict[k] = pretrained_dict[k]
model.load_state_dict(model_dict)
#-------------------------------------------------------------
# Function : validate_single_epoch()
# Testing the accuracy of the trained modle in test dataset.
#-------------------------------------------------------------
def validate_single_epoch(model, eva_data_loader, loss_fn, device, return_prdict_vector=None):
if return_prdict_vector == None:
eval_loss = 0
eval_acc = 0
model.eval()
i = 0
for input, target in eva_data_loader:
input, target = input.to(device), target.to(device)
i += 1
# Calculating the loss value
prediction = model(input)
loss = loss_fn(prediction,target)
# recording the validating loss and accuratcy
eval_loss += loss.item()
_, pred = prediction.max(1)
num_correct = (pred == target).sum().item()
acc = num_correct / input.shape[0]
eval_acc += acc
print(f"Validat loss : {eval_loss/i}" + f" Validat accuracy : {eval_acc/i}")
return eval_acc/i
else:
eval_loss = 0
eval_acc = 0
model.eval()
i = 0
for input, target in eva_data_loader:
input, target = input.to(device), target.to(device)
i += 1
# Calculating the loss value
prediction = model(input)
loss = loss_fn(prediction,target)
# recording the validating loss and accuratcy
eval_loss += loss.item()
_, pred = prediction.max(1)
num_correct = (pred == target).sum().item()
acc = num_correct / input.shape[0]
eval_acc += acc
if i == 1:
prdic_vector = torch.clone(pred.cpu())
targe_vector = torch.clone(target.cpu())
else:
prdic_vector = torch.cat((prdic_vector,pred.cpu()))
targe_vector = torch.cat((targe_vector,target.cpu()))
print(f"Validat loss : {eval_loss/i}" + f" Validat accuracy : {eval_acc/i}")
return eval_acc/i, prdic_vector, targe_vector
#----------------------------------------------------------------------------------------
# Function : Testing the accuracy of the trained model in the testing set.
#----------------------------------------------------------------------------------------
def Test_model_accuracy_original(TESTING_DATASET_FILE=None, MODLE_CLASS=None, MODLE_PTH=None, Report =None):
File_sheet = 'Index.csv'
testing_dataset = MyNoiseDataset(TESTING_DATASET_FILE, File_sheet)
testing_loader = create_data_loader(testing_dataset,int(BATCH_SIZE/10))
# construct model and assign it to device
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
print(f"Using {device}")
feed_forward_net = MODLE_CLASS().to(device)
# loading coefficients
load_weigth_for_model(model=feed_forward_net, pretrained_path=MODLE_PTH,device=device)
# testing model
loss_fn = nn.CrossEntropyLoss()
if Report == None:
accuracy = validate_single_epoch(feed_forward_net, testing_loader, loss_fn, device)
else:
accuracy, y_pred, y_true = validate_single_epoch(feed_forward_net, testing_loader, loss_fn, device, return_prdict_vector=True)
target_names = ['A0', 'B0', 'B1','C0', 'C1', 'C2', 'C3','D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7']
print(bcolors.RED + '<<====================Classification report=========================>>' + bcolors.ENDC)
print(classification_report(y_true, y_pred, target_names=target_names))
print(bcolors.RED +'<<==========================End=====================================>>' + bcolors.ENDC)
return accuracy