-
Notifications
You must be signed in to change notification settings - Fork 17
/
test.py
254 lines (191 loc) · 7.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
from __future__ import division
import warnings
from Networks.models import base_patch16_384_token, base_patch16_384_gap
import torch.nn as nn
from torchvision import transforms
import dataset
import math
from utils import save_checkpoint, setup_seed
import torch
import os
import logging
import nni
from nni.utils import merge_parameter
from config import return_args, args
import numpy as np
from image import load_data
warnings.filterwarnings('ignore')
import time
setup_seed(args.seed)
logger = logging.getLogger('mnist_AutoML')
def main(args):
if args['dataset'] == 'ShanghaiA':
train_file = './npydata/ShanghaiA_train.npy'
test_file = './npydata/ShanghaiA_test.npy'
elif args['dataset'] == 'ShanghaiB':
train_file = './npydata/ShanghaiB_train.npy'
test_file = './npydata/ShanghaiB_test.npy'
elif args['dataset'] == 'UCF_QNRF':
train_file = './npydata/qnrf_train.npy'
test_file = './npydata/qnrf_test.npy'
elif args['dataset'] == 'JHU':
train_file = './npydata/jhu_train.npy'
test_file = './npydata/jhu_val.npy'
elif args['dataset'] == 'NWPU':
train_file = './npydata/nwpu_train.npy'
test_file = './npydata/nwpu_val.npy'
with open(train_file, 'rb') as outfile:
train_list = np.load(outfile).tolist()
with open(test_file, 'rb') as outfile:
val_list = np.load(outfile).tolist()
print(len(train_list), len(val_list))
os.environ['CUDA_VISIBLE_DEVICES'] = args['gpu_id']
if args['model_type'] == 'token':
model = base_patch16_384_token(pretrained=True)
elif args['model_type'] == 'gap':
model = base_patch16_384_gap(pretrained=True)
model = nn.DataParallel(model, device_ids=[0])
model = model.cuda()
criterion = nn.L1Loss(size_average=False).cuda()
optimizer = torch.optim.Adam(
[ #
{'params': model.parameters(), 'lr': args['lr']},
], lr=args['lr'], weight_decay=args['weight_decay'])
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[300], gamma=0.1, last_epoch=-1)
print(args['pre'])
# args['save_path'] = args['save_path'] + str(args['rdt'])
print(args['save_path'])
if not os.path.exists(args['save_path']):
os.makedirs(args['save_path'])
if args['pre']:
if os.path.isfile(args['pre']):
print("=> loading checkpoint '{}'".format(args['pre']))
checkpoint = torch.load(args['pre'])
model.load_state_dict(checkpoint['state_dict'], strict=False)
args['start_epoch'] = checkpoint['epoch']
args['best_pred'] = checkpoint['best_prec1']
else:
print("=> no checkpoint found at '{}'".format(args['pre']))
torch.set_num_threads(args['workers'])
print(args['best_pred'], args['start_epoch'])
test_data = pre_data(val_list, args, train=False)
'''inference'''
prec1 = validate(test_data, model, args)
print(' * best MAE {mae:.3f} '.format(mae=args['best_pred']))
def pre_data(train_list, args, train):
print("Pre_load dataset ......")
data_keys = {}
count = 0
for j in range(len(train_list)):
Img_path = train_list[j]
fname = os.path.basename(Img_path)
img, gt_count = load_data(Img_path, args, train)
blob = {}
blob['img'] = img
blob['gt_count'] = gt_count
blob['fname'] = fname
data_keys[count] = blob
count += 1
'''for debug'''
# if j> 10:
# break
return data_keys
def train(Pre_data, model, criterion, optimizer, epoch, args, scheduler):
losses = AverageMeter()
batch_time = AverageMeter()
data_time = AverageMeter()
train_loader = torch.utils.data.DataLoader(
dataset.listDataset(Pre_data, args['save_path'],
shuffle=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
train=True,
batch_size=args['batch_size'],
num_workers=args['workers'],
args=args),
batch_size=args['batch_size'], drop_last=False)
args['lr'] = optimizer.param_groups[0]['lr']
print('epoch %d, processed %d samples, lr %.10f' % (epoch, epoch * len(train_loader.dataset), args['lr']))
model.train()
end = time.time()
for i, (fname, img, gt_count) in enumerate(train_loader):
data_time.update(time.time() - end)
img = img.cuda()
out1 = model(img)
gt_count = gt_count.type(torch.FloatTensor).cuda().unsqueeze(1)
# print(out1.shape, kpoint.shape)
loss = criterion(out1, gt_count)
losses.update(loss.item(), img.size(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
batch_time.update(time.time() - end)
end = time.time()
if i % args['print_freq'] == 0:
print('4_Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
.format(
epoch, i, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses))
scheduler.step()
def validate(Pre_data, model, args):
print('begin test')
batch_size = 1
test_loader = torch.utils.data.DataLoader(
dataset.listDataset(Pre_data, args['save_path'],
shuffle=False,
transform=transforms.Compose([
transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
args=args, train=False),
batch_size=1)
model.eval()
mae = 0.0
mse = 0.0
visi = []
index = 0
for i, (fname, img, gt_count) in enumerate(test_loader):
img = img.cuda()
if len(img.shape) == 5:
img = img.squeeze(0)
if len(img.shape) == 3:
img = img.unsqueeze(0)
with torch.no_grad():
out1 = model(img)
count = torch.sum(out1).item()
gt_count = torch.sum(gt_count).item()
mae += abs(gt_count - count)
mse += abs(gt_count - count) * abs(gt_count - count)
if i % 15 == 0:
print('{fname} Gt {gt:.2f} Pred {pred}'.format(fname=fname[0], gt=gt_count, pred=count))
mae = mae * 1.0 / (len(test_loader) * batch_size)
mse = math.sqrt(mse / (len(test_loader)) * batch_size)
nni.report_intermediate_result(mae)
print(' \n* MAE {mae:.3f}\n'.format(mae=mae), '* MSE {mse:.3f}'.format(mse=mse))
return mae
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
if __name__ == '__main__':
tuner_params = nni.get_next_parameter()
logger.debug(tuner_params)
params = vars(merge_parameter(return_args, tuner_params))
print(params)
main(params)