-
Notifications
You must be signed in to change notification settings - Fork 21
/
test.py
79 lines (70 loc) · 2.81 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
import torch
import os
import numpy as np
from datasets.crowd import Crowd
from models.vgg_c import vgg19_trans
import argparse
import math
args = None
def parse_args():
parser = argparse.ArgumentParser(description='Test ')
parser.add_argument('--data-dir', default='JHU_Train_Val_Test',
help='training data directory')
parser.add_argument('--save-dir', default='model/model.pth',
help='model directory')
parser.add_argument('--device', default='0', help='assign device')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.device.strip() # set vis gpu
datasets = Crowd(os.path.join(args.data_dir, 'test'), 512, 8, is_gray=False, method='val')
dataloader = torch.utils.data.DataLoader(datasets, 1, shuffle=False,
num_workers=8, pin_memory=False)
device = torch.device('cuda')
model = vgg19_trans()
model.to(device)
model.eval()
model.load_state_dict(torch.load(args.save_dir, device))
epoch_minus = []
for inputs, count, name in dataloader:
inputs = inputs.to(device)
b, c, h, w = inputs.shape
h, w = int(h), int(w)
assert b == 1, 'the batch size should equal to 1 in validation mode'
input_list = []
if h >= 3584 or w >= 3584:
h_stride = int(math.ceil(1.0 * h / 3584))
w_stride = int(math.ceil(1.0 * w / 3584))
h_step = h // h_stride
w_step = w // w_stride
for i in range(h_stride):
for j in range(w_stride):
h_start = i * h_step
if i != h_stride - 1:
h_end = (i + 1) * h_step
else:
h_end = h
w_start = j * w_step
if j != w_stride - 1:
w_end = (j + 1) * w_step
else:
w_end = w
input_list.append(inputs[:, :, h_start:h_end, w_start:w_end])
with torch.set_grad_enabled(False):
pre_count = 0.0
for idx, input in enumerate(input_list):
output = model(input)[0]
pre_count += torch.sum(output)
res = count[0].item() - pre_count.item()
epoch_minus.append(res)
else:
with torch.set_grad_enabled(False):
outputs = model(inputs)[0]
res = count[0].item() - torch.sum(outputs).item()
epoch_minus.append(res)
epoch_minus = np.array(epoch_minus)
mse = np.sqrt(np.mean(np.square(epoch_minus)))
mae = np.mean(np.abs(epoch_minus))
log_str = 'mae {}, mse {}'.format(mae, mse)
print(log_str)