-
Notifications
You must be signed in to change notification settings - Fork 10
/
FSC_test_cross(zero-shot).py
416 lines (331 loc) · 15.9 KB
/
FSC_test_cross(zero-shot).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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import argparse
import datetime
import json
import numpy as np
import os
import time
from pathlib import Path
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
import scipy.ndimage as ndimage
import pandas as pd
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
from torch.utils.data import Dataset
import torchvision
from torchvision import transforms
import torchvision.transforms.functional as TF
import timm
assert "0.4.5" <= timm.__version__ <= "0.4.9" # version check
import util.misc as misc
import models_mae_cross
def get_args_parser():
parser = argparse.ArgumentParser('MAE pre-training', add_help=False)
parser.add_argument('--batch_size', default=1, type=int,
help='Batch size per GPU (effective batch size is batch_size * accum_iter * # gpus')
parser.add_argument('--epochs', default=1, type=int)
parser.add_argument('--accum_iter', default=1, type=int,
help='Accumulate gradient iterations (for increasing the effective batch size under memory constraints)')
# Model parameters
parser.add_argument('--model', default='mae_vit_base_patch16', type=str, metavar='MODEL',
help='Name of model to train')
parser.add_argument('--mask_ratio', default=0.5, type=float,
help='Masking ratio (percentage of removed patches).')
parser.add_argument('--norm_pix_loss', action='store_true',
help='Use (per-patch) normalized pixels as targets for computing loss')
parser.set_defaults(norm_pix_loss=False)
# Optimizer parameters
parser.add_argument('--weight_decay', type=float, default=0.05,
help='weight decay (default: 0.05)')
parser.add_argument('--lr', type=float, default=None, metavar='LR',
help='learning rate (absolute lr)')
parser.add_argument('--blr', type=float, default=1e-3, metavar='LR',
help='base learning rate: absolute_lr = base_lr * total_batch_size / 256')
parser.add_argument('--min_lr', type=float, default=0., metavar='LR',
help='lower lr bound for cyclic schedulers that hit 0')
parser.add_argument('--warmup_epochs', type=int, default=10, metavar='N',
help='epochs to warmup LR')
# Dataset parameters
parser.add_argument('--data_path', default='/GPFS/data/changliu/FSC147/', type=str,
help='dataset path')
parser.add_argument('--anno_file', default='annotation_FSC147_384.json', type=str,
help='annotation json file')
parser.add_argument('--data_split_file', default='Train_Test_Val_FSC_147.json', type=str,
help='data split json file')
parser.add_argument('--im_dir', default='images_384_VarV2', type=str,
help='images directory')
parser.add_argument('--output_dir', default='./Image',
help='path where to save, empty for no saving')
parser.add_argument('--log_dir', default='./Image',
help='path where to tensorboard log')
parser.add_argument('--device', default='cuda',
help='device to use for training / testing')
parser.add_argument('--seed', default=0, type=int)
parser.add_argument('--resume', default='./output_fim6_dir/checkpoint-0.pth',
help='resume from checkpoint')
parser.add_argument('--start_epoch', default=0, type=int, metavar='N',
help='start epoch')
parser.add_argument('--num_workers', default=10, type=int)
parser.add_argument('--pin_mem', action='store_true',
help='Pin CPU memory in DataLoader for more efficient (sometimes) transfer to GPU.')
parser.add_argument('--no_pin_mem', action='store_false', dest='pin_mem')
parser.set_defaults(pin_mem=True)
# distributed training parameters
parser.add_argument('--world_size', default=1, type=int,
help='number of distributed processes')
parser.add_argument('--local_rank', default=-1, type=int)
parser.add_argument('--dist_on_itp', action='store_true')
parser.add_argument('--dist_url', default='env://',
help='url used to set up distributed training')
return parser
os.environ["CUDA_LAUNCH_BLOCKING"] = '1'
class TestData(Dataset):
def __init__(self):
self.img = data_split['test']
self.img_dir = im_dir
def __len__(self):
return len(self.img)
def __getitem__(self, idx):
im_id = self.img[idx]
anno = annotations[im_id]
bboxes = anno['box_examples_coordinates']
dots = np.array(anno['points'])
image = Image.open('{}/{}'.format(im_dir, im_id))
image.load()
W, H = image.size
new_H = 384
new_W = 16 * int((W / H * 384) / 16)
scale_factor = float(new_W) / W
image = transforms.Resize((new_H, new_W))(image)
Normalize = transforms.Compose([transforms.ToTensor()])
image = Normalize(image)
rects = list()
for bbox in bboxes:
x1 = int(bbox[0][0] * scale_factor)
y1 = bbox[0][1]
x2 = int(bbox[2][0] * scale_factor)
y2 = bbox[2][1]
rects.append([y1, x1, y2, x2])
boxes = list()
cnt = 0
for box in rects:
cnt += 1
if cnt > 3:
break
box2 = [int(k) for k in box]
y1, x1, y2, x2 = box2[0], box2[1], box2[2], box2[3]
bbox = image[:, y1:y2 + 1, x1:x2 + 1]
bbox = transforms.Resize((64, 64))(bbox)
boxes.append(bbox.numpy())
boxes = np.array(boxes)
boxes = torch.Tensor(boxes)
# Only for visualisation purpose, no need for ground truth density map indeed.
gt_map = np.zeros((image.shape[1], image.shape[2]), dtype='float32')
for i in range(dots.shape[0]):
gt_map[min(new_H - 1, int(dots[i][1]))][min(new_W - 1, int(dots[i][0] * scale_factor))] = 1
gt_map = ndimage.gaussian_filter(gt_map, sigma=(1, 1), order=0)
gt_map = torch.from_numpy(gt_map)
gt_map = gt_map * 60
sample = {'image': image, 'dots': dots, 'boxes': boxes, 'pos': rects, 'gt_map': gt_map, 'name': im_id}
return sample['image'], sample['dots'], sample['boxes'], sample['pos'], sample['gt_map'], sample['name']
def main(args):
misc.init_distributed_mode(args)
print('job dir: {}'.format(os.path.dirname(os.path.realpath(__file__))))
print("{}".format(args).replace(', ', ',\n'))
device = torch.device(args.device)
# fix the seed for reproducibility
seed = args.seed + misc.get_rank()
torch.manual_seed(seed)
np.random.seed(seed)
cudnn.benchmark = True
dataset_test = TestData()
print(dataset_test)
if True: # args.distributed:
num_tasks = misc.get_world_size()
global_rank = misc.get_rank()
sampler_test = torch.utils.data.DistributedSampler(
dataset_test, num_replicas=num_tasks, rank=global_rank, shuffle=True
)
print("Sampler_test = %s" % str(sampler_test))
else:
sampler_test = torch.utils.data.RandomSampler(dataset_test)
data_loader_test = torch.utils.data.DataLoader(
dataset_test, sampler=sampler_test,
batch_size=1,
num_workers=args.num_workers,
pin_memory=args.pin_mem,
drop_last=False,
)
# define the model
model = models_mae_cross.__dict__[args.model](norm_pix_loss=args.norm_pix_loss)
model.to(device)
model_without_ddp = model
# print("Model = %s" % str(model_without_ddp))
if args.distributed:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu], find_unused_parameters=True)
model_without_ddp = model.module
misc.load_model_FSC(args=args, model_without_ddp=model_without_ddp)
print(f"Start testing.")
start_time = time.time()
# test
epoch = 0
model.eval()
metric_logger = misc.MetricLogger(delimiter=" ")
header = 'Epoch: [{}]'.format(epoch)
print_freq = 20
# some parameters in training
train_mae = 0
train_rmse = 0
pred_cnt = 0
gt_cnt = 0
loss_array = []
gt_array = []
pred_arr = []
name_arr = []
for data_iter_step, (samples, gt_dots, boxes, pos, gt_map, im_name) in \
enumerate(metric_logger.log_every(data_loader_test, print_freq, header)):
samples = samples.to(device, non_blocking=True)
gt_dots = gt_dots.to(device, non_blocking=True).half()
boxes = boxes.to(device, non_blocking=True)
pos = pos
_, _, h, w = samples.shape
r_cnt = 0
s_cnt = 0
for rect in pos:
r_cnt += 1
if r_cnt > 3:
break
if rect[2] - rect[0] < 10 and rect[3] - rect[1] < 10:
s_cnt += 1
if s_cnt >= 100:
r_images = []
r_images.append(TF.crop(samples[0], 0, 0, int(h / 3), int(w / 3)))
r_images.append(TF.crop(samples[0], int(h / 3), 0, int(h / 3), int(w / 3)))
r_images.append(TF.crop(samples[0], 0, int(w / 3), int(h / 3), int(w / 3)))
r_images.append(TF.crop(samples[0], int(h / 3), int(w / 3), int(h / 3), int(w / 3)))
r_images.append(TF.crop(samples[0], int(h * 2 / 3), 0, int(h / 3), int(w / 3)))
r_images.append(TF.crop(samples[0], int(h * 2 / 3), int(w / 3), int(h / 3), int(w / 3)))
r_images.append(TF.crop(samples[0], 0, int(w * 2 / 3), int(h / 3), int(w / 3)))
r_images.append(TF.crop(samples[0], int(h / 3), int(w * 2 / 3), int(h / 3), int(w / 3)))
r_images.append(TF.crop(samples[0], int(h * 2 / 3), int(w * 2 / 3), int(h / 3), int(w / 3)))
pred_cnt = 0
for r_image in r_images:
r_image = transforms.Resize((h, w))(r_image).unsqueeze(0)
density_map = torch.zeros([h, w])
density_map = density_map.to(device, non_blocking=True)
start = 0
prev = -1
with torch.no_grad():
while start + 383 < w:
output, = model(r_image[:, :, :, start:start + 384], boxes, 3)
output = output.squeeze(0)
b1 = nn.ZeroPad2d(padding=(start, w - prev - 1, 0, 0))
d1 = b1(output[:, 0:prev - start + 1])
b2 = nn.ZeroPad2d(padding=(prev + 1, w - start - 384, 0, 0))
d2 = b2(output[:, prev - start + 1:384])
b3 = nn.ZeroPad2d(padding=(0, w - start, 0, 0))
density_map_l = b3(density_map[:, 0:start])
density_map_m = b1(density_map[:, start:prev + 1])
b4 = nn.ZeroPad2d(padding=(prev + 1, 0, 0, 0))
density_map_r = b4(density_map[:, prev + 1:w])
density_map = density_map_l + density_map_r + density_map_m / 2 + d1 / 2 + d2
prev = start + 383
start = start + 128
if start + 383 >= w:
if start == w - 384 + 128:
break
else:
start = w - 384
pred_cnt += torch.sum(density_map / 60).item()
else:
density_map = torch.zeros([h, w])
density_map = density_map.to(device, non_blocking=True)
start = 0
prev = -1
with torch.no_grad():
while start + 383 < w:
output, = model(samples[:, :, :, start:start + 384], boxes, 0)
output = output.squeeze(0)
b1 = nn.ZeroPad2d(padding=(start, w - prev - 1, 0, 0))
d1 = b1(output[:, 0:prev - start + 1])
b2 = nn.ZeroPad2d(padding=(prev + 1, w - start - 384, 0, 0))
d2 = b2(output[:, prev - start + 1:384])
b3 = nn.ZeroPad2d(padding=(0, w - start, 0, 0))
density_map_l = b3(density_map[:, 0:start])
density_map_m = b1(density_map[:, start:prev + 1])
b4 = nn.ZeroPad2d(padding=(prev + 1, 0, 0, 0))
density_map_r = b4(density_map[:, prev + 1:w])
density_map = density_map_l + density_map_r + density_map_m / 2 + d1 / 2 + d2
prev = start + 383
start = start + 128
if start + 383 >= w:
if start == w - 384 + 128:
break
else:
start = w - 384
pred_cnt = torch.sum(density_map / 60).item()
e_cnt = 0
cnt = 0
for rect in pos:
cnt += 1
if cnt > 3:
break
e_cnt += torch.sum(density_map[rect[0]:rect[2] + 1, rect[1]:rect[3] + 1] / 60).item()
gt_cnt = gt_dots.shape[1]
cnt_err = abs(pred_cnt - gt_cnt)
train_mae += cnt_err
train_rmse += cnt_err ** 2
print(f'{data_iter_step}/{len(data_loader_test)}: pred_cnt: {pred_cnt}, gt_cnt: {gt_cnt}, error: {cnt_err}, AE: {cnt_err}, SE: {cnt_err ** 2}, id: {im_name[0]}')
loss_array.append(cnt_err)
gt_array.append(gt_cnt)
pred_arr.append(round(pred_cnt))
name_arr.append(im_name[0])
# compute and save images
pred = density_map.unsqueeze(0)
pred = torch.cat((pred, torch.zeros_like(pred), torch.zeros_like(pred)))
fig = samples[0] + pred / 2
fig = torch.clamp(fig, 0, 1)
pred_img = Image.new(mode="RGB", size=(w, h), color=(0, 0, 0))
draw = ImageDraw.Draw(pred_img)
draw.text((w-50, h-50), str(round(pred_cnt)), (255, 255, 255))
pred_img = np.array(pred_img).transpose((2, 0, 1))
pred_img = torch.tensor(np.array(pred_img), device=device) + pred
full = torch.cat((samples[0], fig, pred_img), -1)
torchvision.utils.save_image(fig, (os.path.join(args.output_dir, f'vis_{im_name[0]}')))
torchvision.utils.save_image(pred_img, (os.path.join(args.output_dir, f'pred_{im_name[0]}')))
torchvision.utils.save_image(full, (os.path.join(args.output_dir, f'full_{im_name[0]}')))
torch.cuda.synchronize()
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
log_stats = {'MAE': train_mae / (len(data_loader_test)),
'RMSE': (train_rmse / (len(data_loader_test))) ** 0.5}
print('Current MAE: {:5.2f}, RMSE: {:5.2f} '.format(train_mae / (len(data_loader_test)), (
train_rmse / (len(data_loader_test))) ** 0.5))
if args.output_dir and misc.is_main_process():
with open(os.path.join(args.output_dir, "log.txt"), mode="a", encoding="utf-8") as f:
f.write(json.dumps(log_stats) + "\n")
plt.scatter(gt_array, loss_array)
plt.xlabel('Ground Truth')
plt.ylabel('Error')
plt.savefig(os.path.join(args.output_dir, 'test_stat.png'))
df = pd.DataFrame(data={'time': np.arange(data_iter_step+1)+1, 'name': name_arr, 'prediction': pred_arr})
df.to_csv(os.path.join(args.output_dir, f'results.csv'), index=False)
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
print('Testing time {}'.format(total_time_str))
if __name__ == '__main__':
args = get_args_parser()
args = args.parse_args()
# load data
data_path = Path(args.data_path)
anno_file = data_path / args.anno_file
data_split_file = data_path / args.data_split_file
im_dir = data_path / args.im_dir
with open(anno_file) as f:
annotations = json.load(f)
with open(data_split_file) as f:
data_split = json.load(f)
if args.output_dir:
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
main(args)