-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTest.py
128 lines (87 loc) · 4.21 KB
/
Test.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
'''
testing on MS-CVNet
'''
from MSCVNets import *
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
#-----------------DEVICE CONFIGURATION--------------------------
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if torch.cuda.is_available():
print ('GPU is true')
print('cuda version: {}'.format(torch.version.cuda))
else:
print('CPU is true')
# hyperparameters
PATH = './models/class10/fullmodel_434Ep_0.9979Acc.pth' ## model path
batch_size = 100
num_classes = 10
#----------------DataLoader-----------------
train_dataset = scipy.io.loadmat('./Complex_MSTAR/data_SOC/class10/train/2equilibrium/data_train_64.mat')
test_dataset = scipy.io.loadmat('./Complex_MSTAR/data_SOC/class10/test/2equilibrium/data_test_64.mat')
traindata_r = train_dataset['data_r']
traindata_i = train_dataset['data_i']
trainlabel = train_dataset['label'].squeeze() ## label必须是一维向量
testdata_r = test_dataset['data_r']
testdata_i = test_dataset['data_i']
testlabel = test_dataset['label'].squeeze()
train_dataset = MyDataset(img_r=traindata_r, img_i=traindata_i, label=trainlabel, transform=transforms.ToTensor())
test_dataset = MyDataset(img_r=testdata_r, img_i=testdata_i, label=testlabel, transform=transforms.ToTensor())
print('real train data size: {} \nimaginary train data size: {}' \
.format(train_dataset.img_r.shape[0], train_dataset.img_i.shape[0]))
print('real test data size: {} \nimaginary test data size: {}' \
.format(test_dataset.img_r.shape[0], test_dataset.img_i.shape[0]))
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=False)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
#----------------model loading-----------------
model = torch.load(PATH)
# criterion = nn.NLLLoss()
criterion = nn.CrossEntropyLoss()
#----------------testing-----------------
model.eval()
with torch.no_grad():
correct = 0
total = 0
total_loss = 0
label = []
label_pre = []
for image_r, image_i, target in train_loader:
image_r = image_r.to(device)
image_i = image_i.to(device)
target = target.to(device)
output = model(image_r, image_i)
loss = criterion(output, target)
total_loss += loss.item()
_, predicted = torch.max(output.data, 1)
total += target.size(0)
correct += (predicted == target).sum().item()
label.extend(target.data.cpu().numpy())
label_pre.extend(predicted.data.cpu().numpy())
print('correct number : {}, train data number : {}, train Accuracy: {}, trian_loss: {:.4f}'.format(correct, total, 100*correct/total, total_loss))
matrix = confusion_matrix(label,label_pre)
print('############ confusion matrix ########### \n', matrix)
# scipy.io.savemat('./results/confusion_matrix.mat',{'confusion_matrix':matrix,'label':label, 'label_predict':label_pre})
with torch.no_grad():
correct = 0
total = 0
total_loss = 0
label = []
label_pre = []
for image_r, image_i, target in test_loader:
image_r = image_r.to(device)
image_i = image_i.to(device)
target = target.to(device)
output = model(image_r, image_i)
loss = criterion(output, target)
total_loss += loss.item()
_, predicted = torch.max(output.data, 1)
total += target.size(0)
correct += (predicted == target).sum().item()
label.extend(target.data.cpu().numpy())
label_pre.extend(predicted.data.cpu().numpy())
print('correct number : {}, test data number : {}, test Accuracy: {}, test loss: {:.4f}'.format(correct, total, 100*correct/total, total_loss))
# label = np.asarray(label)
# label_pre = np.asarray(label_pre)
matrix = confusion_matrix(label,label_pre)
print('############ confusion matrix ########### \n', matrix)
# scipy.io.savemat('./results/confusion_matrix_class3.mat',{'confusion_matrix':matrix,'label':label, 'label_predict':label_pre})