-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathvalidate.py
77 lines (57 loc) · 2.47 KB
/
validate.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
import torch
import time
import sys
from utils.util import *
from utils.save import *
def validate(args, val_loader, model, criterion, epoch):
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
log = Log()
model.eval()
end = time.time()
# we may have ten d in data
for i, (data, target, paths) in enumerate(val_loader):
if args.gpu is not None:
data = data.cuda()
target = target.cuda()
# compute output
for idx, d in enumerate(data[0]): # data [batchsize, 10_crop, 3, 448, 448]
d = d.unsqueeze(0) # d [1, 3, 448, 448]
output1, output2, output3, _ = model(d)
output = output1 + output2 + 0.1 * output3
# measure accuracy and record loss
prec1, prec5 = accuracy(output, target, topk=(1, 5))
top1.update(prec1[0], 1)
top5.update(prec5[0], 1)
print('DFL-CNN <==> Test <==> Img:{} No:{} Top1 {:.3f} Top5 {:.3f}'.format(i, idx, prec1.cpu().numpy()[0], prec5.cpu().numpy()[0]))
print('DFL-CNN <==> Test Total <==> Top1 {:.3f}% Top5 {:.3f}%'.format(top1.avg, top5.avg))
log.save_test_info(epoch, top1, top5)
return top1.avg
def validate_simple(args, val_loader, model, criterion, epoch):
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
log = Log()
model.eval()
end = time.time()
# we may have ten d in data
for i, (data, target, paths) in enumerate(val_loader):
if args.gpu is not None:
data = data.cuda()
target = target.cuda()
# compute output
for idx, d in enumerate(data): # data [batchsize, 10_crop, 3, 448, 448]
d = d.unsqueeze(0) # d [1, 3, 448, 448]
output1, output2, output3, _ = model(d)
output = output1 + output2 + 0.1 * output3
# measure accuracy and record loss
prec1, prec5 = accuracy(output, target, topk=(1, 5))
top1.update(prec1[0], 1)
top5.update(prec5[0], 1)
print('DFL-CNN <==> Test <==> Img:{} Top1 {:.3f} Top5 {:.3f}'.format(i, prec1.cpu().numpy()[0], prec5.cpu().numpy()[0]))
print('DFL-CNN <==> Test Total <==> Top1 {:.3f}% Top5 {:.3f}%'.format(top1.avg, top5.avg))
log.save_test_info(epoch, top1, top5)
return top1.avg