forked from chen-judge/SKT
-
Notifications
You must be signed in to change notification settings - Fork 21
/
test.py
175 lines (137 loc) · 5.37 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
import sys
import os
import warnings
from models.model_vgg import CSRNet as CSRNet_vgg
from models.model_student_vgg import CSRNet as CSRNet_student
from utils import save_checkpoint
from utils import cal_para, crop_img_patches
import torch
import torch.nn as nn
from torch.autograd import Variable
from torchvision import datasets, transforms
import json
import numpy as np
import argparse
import json
import dataset
import time
parser = argparse.ArgumentParser(description='PyTorch CSRNet')
parser.add_argument('test_json', metavar='TEST',
help='path to test json')
parser.add_argument('--dataset', '-d', default='Shanghai', type=str,
help='Shanghai/UCF')
parser.add_argument('--checkpoint', '-c', metavar='CHECKPOINT', default=None, type=str,
help='path to the checkpoint')
parser.add_argument('--version', '-v', default=None, type=str,
help='vgg/quarter_vgg')
parser.add_argument('--transform', '-t', default=True, type=str,
help='1x1 conv transform')
parser.add_argument('--batch', default=1, type=int,
help='batch size')
parser.add_argument('--gpu', metavar='GPU', default='0', type=str,
help='GPU id to use.')
args = parser.parse_args()
def main():
global args, best_prec1
args.batch_size = 1
args.workers = 4
args.seed = time.time()
if args.transform == 'false':
args.transform = False
with open(args.test_json, 'r') as outfile:
test_list = json.load(outfile)
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
torch.cuda.manual_seed(args.seed)
if args.version == 'vgg':
print 'VGG'
model = CSRNet_vgg(pretrained=False)
print model
cal_para(model)
elif args.version == 'quarter_vgg':
print 'quarter_VGG'
model = CSRNet_student(ratio=4, transform=args.transform)
print model
cal_para(model) # including 1x1conv transform layer that can be removed
else:
raise NotImplementedError()
model = model.cuda()
if args.checkpoint:
if os.path.isfile(args.checkpoint):
print("=> loading checkpoint '{}'".format(args.checkpoint))
checkpoint = torch.load(args.checkpoint)
if args.transform is False:
# remove 1x1 conv para
for k in checkpoint['state_dict'].keys():
if k[:9] == 'transform':
del checkpoint['state_dict'][k]
model.load_state_dict(checkpoint['state_dict'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.checkpoint, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.checkpoint))
if args.dataset == 'UCF':
test_ucf(test_list, model)
else:
test(test_list, model)
def test(test_list, model):
print('begin test')
test_loader = torch.utils.data.DataLoader(
dataset.listDataset(test_list,
transform=transforms.Compose([
transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
train=False),
shuffle=False,
batch_size=args.batch_size)
model.eval()
mae = 0
mse = 0
for i, (img, target) in enumerate(test_loader):
img = img.cuda()
img = Variable(img)
with torch.no_grad():
output = model(img)
mae += abs(output.data.sum() - target.sum().type(torch.FloatTensor).cuda())
mse += (output.data.sum() - target.sum().type(torch.FloatTensor).cuda()).pow(2)
N = len(test_loader)
mae = mae / N
mse = torch.sqrt(mse / N)
print(' * MAE {mae:.3f} \t * MSE {mse:.3f}'
.format(mae=mae, mse=mse))
def test_ucf(test_list, model):
print 'begin test'
test_loader = torch.utils.data.DataLoader(
dataset.listDataset(test_list,
transform=transforms.Compose([
transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
train=False,
dataset='ucf_test',
),
shuffle=False,
batch_size=1)
model.eval()
mae = 0
mse = 0
for i, (img, target) in enumerate(test_loader):
img = img.cuda()
img = Variable(img)
people = 0
img_patches = crop_img_patches(img, size=512)
for patch in img_patches:
with torch.no_grad():
sub_output = model(patch)
people += sub_output.data.sum()
error = people - target.sum().type(torch.FloatTensor).cuda()
mae += abs(error)
mse += error.pow(2)
N = len(test_loader)
mae = mae / N
mse = torch.sqrt(mse / N)
print(' * MAE {mae:.3f} \t * MSE {mse:.3f}'
.format(mae=mae, mse=mse))
return mae, mse
if __name__ == '__main__':
main()