-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest.py
143 lines (119 loc) · 5.54 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
# ------------------------------------------------------------------------------
# The code is from GLPDepth (https://github.com/vinvino02/GLPDepth).
# For non-commercial purpose only (research, evaluation etc).
# Shift window testing and flip testing is modified by Zigang Geng (zigang@mail.ustc.edu.cn).
# -----------------------------------------------------------------------------
import os
import cv2
import numpy as np
from collections import OrderedDict
import torch
from torch.utils.data import DataLoader
import torch.backends.cudnn as cudnn
import utils.logging as logging
import utils.metrics as metrics
from models.model import GLPDepth
from dataset.base_dataset import get_dataset
from configs.test_options import TestOptions
metric_name = ['d1', 'd2', 'd3', 'abs_rel', 'sq_rel', 'rmse', 'rmse_log',
'log10', 'silog']
def main():
# experiments setting
opt = TestOptions()
args = opt.initialize().parse_args()
print(args)
if args.gpu_or_cpu == 'gpu':
device = torch.device('cuda')
cudnn.benchmark = True
else:
device = torch.device('cpu')
if args.save_eval_pngs or args.save_visualize:
result_path = os.path.join(args.result_dir, args.exp_name)
logging.check_and_make_dirs(result_path)
print("Saving result images in to %s" % result_path)
if args.do_evaluate:
result_metrics = {}
for metric in metric_name:
result_metrics[metric] = 0.0
print("\n1. Define Model")
model = GLPDepth(args=args).to(device)
model_weight = torch.load(args.ckpt_dir)
if 'module' in next(iter(model_weight.items()))[0]:
model_weight = OrderedDict((k[7:], v) for k, v in model_weight.items())
model.load_state_dict(model_weight)
model.eval()
print("\n2. Define Dataloader")
if args.dataset == 'imagepath': # not for do_evaluate in case of imagepath
dataset_kwargs = {'dataset_name': 'ImagePath', 'data_path': args.data_path}
else:
dataset_kwargs = {'data_path': args.data_path, 'dataset_name': args.dataset,
'is_train': False}
test_dataset = get_dataset(**dataset_kwargs)
test_loader = DataLoader(test_dataset, batch_size=1, shuffle=False,
pin_memory=True)
print("\n3. Inference & Evaluate")
for batch_idx, batch in enumerate(test_loader):
input_RGB = batch['image'].to(device)
filename = batch['filename']
with torch.no_grad():
if args.shift_window_test:
bs, _, h, w = input_RGB.shape
assert w > h and bs == 1
interval_all = w - h
interval = interval_all // (args.shift_size-1)
sliding_images = []
sliding_masks = torch.zeros((bs, 1, h, w), device=input_RGB.device)
for i in range(args.shift_size):
sliding_images.append(input_RGB[..., :, i*interval:i*interval+h])
sliding_masks[..., :, i*interval:i*interval+h] += 1
input_RGB = torch.cat(sliding_images, dim=0)
if args.flip_test:
input_RGB = torch.cat((input_RGB, torch.flip(input_RGB, [3])), dim=0)
pred = model(input_RGB)
pred_d = pred['pred_d']
if args.flip_test:
batch_s = pred_d.shape[0]//2
pred_d = (pred_d[:batch_s] + torch.flip(pred_d[batch_s:], [3]))/2.0
if args.shift_window_test:
pred_s = torch.zeros((bs, 1, h, w), device=pred_d.device)
for i in range(args.shift_size):
pred_s[..., :, i*interval:i*interval+h] += pred_d[i:i+1]
pred_d = pred_s/sliding_masks
if args.do_evaluate:
depth_gt = batch['depth'].to(device)
pred_d, depth_gt = pred_d.squeeze(), depth_gt.squeeze()
pred_crop, gt_crop = metrics.cropping_img(args, pred_d, depth_gt)
computed_result = metrics.eval_depth(pred_crop, gt_crop)
for metric in metric_name:
result_metrics[metric] += computed_result[metric]
if args.save_eval_pngs:
save_path = os.path.join(result_path, filename[0])
if save_path.split('.')[-1] == 'jpg':
save_path = save_path.replace('jpg', 'png')
pred_d = pred_d.squeeze()
if args.dataset == 'nyudepthv2':
pred_d = pred_d.cpu().numpy() * 1000.0
cv2.imwrite(save_path, pred_d.astype(np.uint16),
[cv2.IMWRITE_PNG_COMPRESSION, 0])
else:
pred_d = pred_d.cpu().numpy() * 256.0
cv2.imwrite(save_path, pred_d.astype(np.uint16),
[cv2.IMWRITE_PNG_COMPRESSION, 0])
if args.save_visualize:
save_path = os.path.join(result_path, filename[0])
pred_d_numpy = pred_d.squeeze().cpu().numpy()
pred_d_numpy = (pred_d_numpy / pred_d_numpy.max()) * 255
pred_d_numpy = pred_d_numpy.astype(np.uint8)
pred_d_color = cv2.applyColorMap(pred_d_numpy, cv2.COLORMAP_RAINBOW)
cv2.imwrite(save_path, pred_d_color)
logging.progress_bar(batch_idx, len(test_loader), 1, 1)
if args.do_evaluate:
for key in result_metrics.keys():
result_metrics[key] = result_metrics[key] / (batch_idx + 1)
display_result = logging.display_result(result_metrics)
if args.kitti_crop:
print("\nCrop Method: ", args.kitti_crop)
print(display_result)
print("Done")
if __name__ == "__main__":
main()