forked from RishengDeng/mvi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
283 lines (239 loc) · 9.56 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
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
import os
import argparse
import logging
import numpy as np
from sklearn.metrics import roc_auc_score
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torch.nn.functional as F
import torchvision.datasets as datasets
import torchvision.models as models
from torch.utils.data import Dataset, DataLoader, Sampler
from torch.utils.tensorboard import SummaryWriter
from model import Resnet18, Resnet50, DilatedResnet, Attention, DRN22, DRN22_test, \
MulRes18, MulRes18Att
from data import SinglePhase, transforms, TwoPhases
from utils import AverageMeter, accuracy_binary, stack3array
parser = argparse.ArgumentParser(description='MVI test')
parser.add_argument('data', metavar='DIR',
help='path to dataset')
parser.add_argument('--workers', default=4, type=int, metavar='N',
help='number of data loading workers (default: 4)')
parser.add_argument('-b', '--batch_size', default=1, type=int, metavar='N',
help='number of data loads into the model (default: 1)')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to the checkpoint (default: none)')
parser.add_argument('--gpu', default=0, type=int,
help='GPU id to use')
parser.add_argument('--scale', default=0.05, type=float,
help='scale factor range for augmentation.')
parser.add_argument('--angle', default=15, type=int,
help='rotation angle range in degrees for augmentation.')
parser.add_argument('--lr', '--learning_rate', default=0.01, type=float,
metavar='LR', help='initial learning rate', dest='lr')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum')
parser.add_argument('--wd', '--weight_decay', default=1e-4, type=float,
metavar='W', help='weight decay (default: 1e-4)',
dest='weight_decay')
args = parser.parse_args()
date = '0220'
path = os.path.dirname(__file__)
logs = os.path.join(path, 'testlogs', date)
if not os.path.exists(logs):
os.makedirs(logs)
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
log_path = os.path.join(logs, 'ev1_attention_res18_1') + '.log'
handler = logging.FileHandler(log_path, mode='w')
formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
def main():
torch.cuda.set_device(args.gpu)
print('Use GPU: {} to test'.format(args.gpu))
# model = DRN22_test()
# model = Resnet18()
model = MulRes18Att()
model = model.cuda(args.gpu)
criterion = nn.CrossEntropyLoss()
criterion = criterion.cuda(args.gpu)
optimizer = torch.optim.SGD(
model.parameters(),
args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay
)
if args.resume:
resume_path = args.resume + 'art_pv_attention_111' + '.pth.tar'
if os.path.isfile(resume_path):
print('==> loading checkpoint {}'.format(resume_path))
checkpoint = torch.load(resume_path)
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
else:
print('==> no checkpoint found at {}'.format(args.resume))
# test_dir = os.path.join(args.data, 'bbox_npy', 'dl')
# test_dir = os.path.join(args.data, 'val')
test_dir = os.path.join(args.data, 'bbox_npy')
# test_dataset = SinglePhase(
test_dataset = TwoPhases(
test_dir,
image_size=224,
# transforms=transforms(scale=args.scale, angle=args.angle, flip_prob=0.5)
)
test_loader = DataLoader(
test_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers,
pin_memory=True
)
model.eval()
id_label = {}
id_slice_sum = {}
id_slice_num = {}
total_0 = 0
total_acc = 0
acc0 = 0
acc1 = 0
total_num = 0
total_acc0 = 0
total_acc1 = 0
total_0 = 0
# with torch.no_grad():
# for step, (data, target, id_num) in enumerate(test_loader):
# data = data.cuda(args.gpu, non_blocking=True)
# target = target.cuda(args.gpu, non_blocking=True)
# output, vector = model(data)
# # print(vector.shape)
# # print(vector)
# for (a, b, c) in zip(vector, target, id_num):
# a = a.cpu().numpy()
# b = b.cpu().numpy()
# if c not in id_slice_num:
# id_slice_num[c] = 1
# id_slice_sum[c] = a
# id_label[c] = b
# else:
# id_slice_num[c] += 1
# id_slice_sum[c] += a
# path = '/home/drs/Desktop/DL_feature'
# for key in id_label:
# average_vector = id_slice_sum[key] / id_slice_num[key]
# # print(average_vector.shape)
# # print(type(average_vector))
# # print(average_vector)
# np.save((os.path.join(path, key) + '_dl.npy'), average_vector)
with torch.no_grad():
for step, (data, target, id_num) in enumerate(test_loader):
data = data.cuda(args.gpu, non_blocking=True)
art_data = data[:, :3, :, :].cuda(args.gpu, non_blocking=True)
pv_data = data[:, 3:, :, :].cuda(args.gpu, non_blocking=True)
target = target.cuda(args.gpu, non_blocking=True)
# output = model(data)
output = model(art_data, pv_data)
probability = F.softmax(output, dim=1)
predict = torch.argmax(probability, dim=1)
# for (a, b, c) in zip(probability, target, id_num):
# a = a.cpu().numpy()
# b = b.cpu().numpy()
# if c not in id_slice_num:
# id_slice_num[c] = 1
# id_slice_sum[c] = a
# id_label[c] = b
# else:
# id_slice_num[c] += 1
# id_slice_sum[c] += a
for (a, b, c) in zip(probability, target, id_num):
a = a.cpu().numpy()
b = b.cpu().numpy()
if c not in id_slice_sum:
id_slice_num[c] = 1
temp = []
temp.append(a)
id_slice_sum[c] = temp
id_label[c] = b
else:
id_slice_num[c] += 1
temp = id_slice_sum[c]
temp.append(a)
id_slice_sum[c] = temp
y_true = []
y_score = []
y_score_1 = []
y_pre = []
y_predict = []
y_probability = []
y_name = []
# for key in id_label:
# average_prob = id_slice_sum[key] / id_slice_num[key]
# y_true.append(id_label[key])
# y_score.append(average_prob)
# y_score_1.append(average_prob[1])
# a = np.argmax(average_prob)
# y_pre.append(a)
# b = id_label[key]
# if b == 0:
# total_0 += 1
# if a == 0:
# total_acc += 1
# acc0 += 1
# elif b == 1:
# if a == 1:
# total_acc += 1
# acc1 += 1
for key in id_label:
slice_sum = sum(id_slice_sum[key])
average_prob = slice_sum / id_slice_num[key]
y_true.append(id_label[key])
y_score.append(average_prob[1])
a = np.argmax(average_prob)
b = id_label[key]
y_predict.append(a)
y_probability.append(id_slice_sum[key])
y_name.append(key)
if a == 0 and b == 0:
total_acc0 += 1
total_0 += 1
elif a == 1 and b == 1:
total_acc1 += 1
elif a == 1 and b == 0:
total_0 += 1
total_num = len(id_label)
total_1 = total_num - total_0
# accuracy = total_acc / total_num
y_score = np.array(y_score)
# y_score_1 = np.array(y_score_1)
y_true = np.array(y_true)
# y_pre = np.array(y_pre)
# auc = roc_auc_score(y_true, y_score_1)
# sensitivity = acc1 / total_1
# specificity = acc0 / total_0
total_acc = total_acc0 + total_acc1
accuracy = total_acc / total_num
auc = roc_auc_score(y_true, y_score)
sensitivity = total_acc1 / total_1
specificity = total_acc0 / total_0
print(total_0, total_1, total_acc0, total_acc1)
ppv = total_acc1 / (total_0 - total_acc0 + total_acc1)
npv = total_acc0 / (total_1 - total_acc1 + total_acc0)
f1_score = 2 / ((1 / sensitivity) + (1 / ppv))
logger.info(
'number:{}, acc0:{}, acc1:{}, accuracy:{:.3f}, auc:{:.3f}, sen:{:.3f}, spe:{:.3f}, ppv:{:.3f}, npv:{:.3f}, f1:{:.3f}'.format(
total_num, total_acc0, total_acc1, accuracy, auc, sensitivity, specificity, ppv, npv, f1_score
)
)
txt_path = os.path.splitext(log_path)[0] + '.txt'
with open(txt_path, 'a+') as f:
print('name:\n', y_name, file=f)
print('true:\n', y_true, file=f)
print('predict:\n', y_predict, file=f)
print('probability:\n', y_probability, file=f)
print('\n', file=f)
# logger.info(y_score, y_pre, y_true)
if __name__ == "__main__":
main()