-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_prediction.py
224 lines (177 loc) · 6.79 KB
/
test_prediction.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
import os
import torch
import time
import argparse
import shutil
import numpy as np
import torch.backends.cudnn as cudnn
from core.utils import transforms as tf
from core import models
from core import datasets
from core.utils.optim import Optim
from core.utils.config import Config
from core.utils.eval import EvalPSNR
from core.ops.sync_bn_cupy.sync_bn_module import DataParallelwithSyncBN
import logging
from skimage.measure import compare_psnr as my_compare_psnr
from skimage.measure import compare_ssim
import cv2
# import utils.util as util
def my_compare_ssim(img1,img2):
ssim = compare_ssim(img1, img2, multichannel=True)
return ssim
best_PSNR = 0
def parse_args():
parser = argparse.ArgumentParser(description='Train Voxel Flow')
parser.add_argument('config', default='./configs/voxel-flow.py', help='config file path')
args = parser.parse_args()
return args
def main():
checkpoint_path = '/DATA/wangshen_data/UCF101/voxelflow_finetune_model_best.pth.tar'
global cfg, best_PSNR
args = parse_args()
cfg = Config.from_file(args.config)
str1 = ','.join(str(gpu) for gpu in cfg.device)
print(str1)
os.environ["CUDA_VISIBLE_DEVICES"] = str1
# os.environ["CUDA_VISIBLE_DEVICES"] = '0'
cudnn.benchmark = True
cudnn.fastest = True
if hasattr(datasets, cfg.dataset):
ds = getattr(datasets, cfg.dataset)
else:
raise ValueError('Unknown dataset ' + cfg.dataset)
model = getattr(models, cfg.model.name)(cfg.model).cuda()
cfg.train.input_mean = model.input_mean
cfg.train.input_std = model.input_std
cfg.test.input_mean = model.input_mean
cfg.test.input_std = model.input_std
input_mean = cfg.test.input_mean
input_std = cfg.test.input_std
# Data loading code
val_loader = torch.utils.data.DataLoader(
datasets.UCF101Test_NEW(cfg.test),
batch_size=1,
shuffle=False,
num_workers=0, #32,
pin_memory=True)
if os.path.isfile(checkpoint_path):
print(("=> loading checkpoint '{}'".format(checkpoint_path)))
checkpoint = torch.load(checkpoint_path)
model.load_state_dict(checkpoint['state_dict'], False)
else:
print(("=> no checkpoint found at '{}'".format(checkpoint_path)))
model = DataParallelwithSyncBN(
model, device_ids=range(len(cfg.device))).cuda()
# define loss function (criterion) optimizer and evaluator
criterion = torch.nn.MSELoss().cuda()
evaluator = EvalPSNR(255.0 / np.mean(cfg.test.input_std))
PSNR = validate(val_loader, model, criterion, evaluator, input_mean, input_std)
print(PSNR)
def flip(x, dim):
xsize = x.size()
dim = x.dim() + dim if dim < 0 else dim
x = x.view(-1, *xsize[dim:])
x = x.view(x.size(0), x.size(1),
-1)[:,
getattr(
torch.arange(x.size(1) - 1, -1, -1), ('cpu', 'cuda')[
x.is_cuda])().long(), :]
return x.view(xsize)
def validate(val_loader, model, criterion, evaluator, input_mean, input_std):
with torch.no_grad():
batch_time = AverageMeter()
losses = AverageMeter()
psnrs = AverageMeter()
ssims = AverageMeter()
evaluator.clear()
# switch to evaluate mode
model.eval()
re_std = [1/x for x in input_std]
re_mean = [-1*mean*std for mean, std in zip(input_mean, re_std)]
end = time.time()
for i, (input, target) in enumerate(val_loader):
target = target.cuda(async=True)
input_var = torch.autograd.Variable(input)
target_var = torch.autograd.Variable(target)
# compute output
output = model(input_var)
loss = criterion(output, target_var)
# measure accuracy and record loss
# convert to normal img: BGR, 0-255
pred = output.data.cpu().numpy()
gt = target.cpu().numpy()
evaluator(pred, gt)
losses.update(loss.item(), input.size(0))
pred = pred[0]
pred = np.transpose(pred, (1, 2, 0)) #CHW -> HWC, BGR
pred = tf.normalize(pred, re_mean, re_std) # 0-255
pred = pred.clip(0, 255.0)
pred = np.round(pred).astype(np.uint8)
gt = gt[0]
gt = np.transpose(gt, (1, 2, 0)) #CHW -> HWC, BGR
gt = tf.normalize(gt, re_mean, re_std) # 0-255
gt = gt.clip(0, 255.0)
gt = np.round(gt).astype(np.uint8)
# can save image
# cv2.imwrite('./img.png', pred)
psnr = my_compare_psnr(pred, gt)
ssim = my_compare_ssim(pred, gt)
psnrs.update(psnr, 1)
ssims.update(ssim, 1)
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
print( ('Test: [{0}/{1}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'PSNR {PSNR:.3f}\t'
'SSIM {SSIM:.3f}'.format(
i,
len(val_loader),
batch_time=batch_time,
loss=losses,
PSNR=evaluator.PSNR(),
SSIM=ssim))
)
print('Testing Results: '
'PSNR {PSNR:.3f} SSIM {SSIM:.3f} ({bestPSNR:.4f})\tLoss {loss.avg:.5f}'.format(
PSNR=evaluator.PSNR(),
SSIM=ssims.avg,
bestPSNR=max(evaluator.PSNR(), best_PSNR),
loss=losses))
return evaluator.PSNR()
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
if not cfg.output_dir:
return
if not os.path.exists(cfg.output_dir):
os.makedirs(cfg.output_dir)
filename = os.path.join(cfg.output_dir, '_'.join((cfg.snapshot_pref,
filename)))
torch.save(state, filename)
if is_best:
best_name = os.path.join(cfg.output_dir, '_'.join(
(cfg.snapshot_pref, 'model_best.pth.tar')))
shutil.copyfile(filename, best_name)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = None
self.avg = None
self.sum = None
self.count = None
def update(self, val, n=1):
if self.val is None:
self.val = val
self.sum = val * n
self.count = n
self.avg = self.sum / self.count
else:
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
if __name__ == '__main__':
main()