-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
111 lines (75 loc) · 4.21 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
import numpy as np
import os,sys,math
import argparse
from tqdm import tqdm
from einops import rearrange, repeat
import torch.nn as nn
import torch
from torch.utils.data import DataLoader
import torch.nn.functional as F
from ptflops import get_model_complexity_info
import time
import scipy.io as sio
from utils.loader import get_validation_data
import utils
from model import Network
from skimage import img_as_float32, img_as_ubyte
from ptflops import get_model_complexity_info
from thop import profile
parser = argparse.ArgumentParser(description='TESTING CODE ECCV WORKSHOP SUBMISSION PAPER ID 25')
parser.add_argument('--input_dir', default='./testing_data/',
type=str, help='Directory of validation images')
parser.add_argument('--result_dir', default='./results/restored/',
type=str, help='Directory for results')
parser.add_argument('--weights', default='./log/models/model_best.pth',
type=str, help='Path to weights')
parser.add_argument('--gpus', default='0', type=str, help='CUDA_VISIBLE_DEVICES')
parser.add_argument('--arch', default='Network', type=str, help='arch')
parser.add_argument('--batch_size', default=1, type=int, help='Batch size for dataloader')
parser.add_argument('--save_images', action='store_true', help='Save denoised images in result directory')
parser.add_argument('--embed_dim', type=int, default=16, help='number of data loading workers')
parser.add_argument('--win_size', type=int, default=8, help='number of data loading workers')
parser.add_argument('--token_projection', type=str,default='linear', help='linear/conv token projection')
parser.add_argument('--token_mlp', type=str,default='leff', help='ffn/leff token mlp')
# args for vit
parser.add_argument('--vit_dim', type=int, default=256, help='vit hidden_dim')
parser.add_argument('--vit_depth', type=int, default=12, help='vit depth')
parser.add_argument('--vit_nheads', type=int, default=8, help='vit hidden_dim')
parser.add_argument('--vit_mlp_dim', type=int, default=512, help='vit mlp_dim')
parser.add_argument('--vit_patch_size', type=int, default=16, help='vit patch_size')
parser.add_argument('--global_skip', action='store_true', default=False, help='global skip connection')
parser.add_argument('--local_skip', action='store_true', default=False, help='local skip connection')
parser.add_argument('--vit_share', action='store_true', default=False, help='share vit module')
parser.add_argument('--train_ps', type=int, default=128, help='patch size of training sample')
args = parser.parse_args()
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus
utils.mkdir(args.result_dir)
test_dataset = get_validation_data(args.input_dir)
test_loader = DataLoader(dataset=test_dataset, batch_size=1, shuffle=False, num_workers=0, drop_last=False)
model_restoration= utils.get_arch(args)
pytorch_total_params = sum(p.numel() for p in model_restoration.parameters() if p.requires_grad)
print("PARAMETERS ARE::::::::::::::",pytorch_total_params)
utils.load_checkpoint(model_restoration,args.weights)
print("===>Testing using weights: ", args.weights)
model_restoration.cuda()
model_restoration.eval()
def expand2square(timg,factor=16.0):
_, _, h, w = timg.size()
X = int(math.ceil(max(h,w)/float(factor))*factor)
img = torch.zeros(1,3,X,X).type_as(timg) # 3, h,w
mask = torch.zeros(1,1,X,X).type_as(timg)
img[:,:, ((X - h)//2):((X - h)//2 + h),((X - w)//2):((X - w)//2 + w)] = timg
mask[:,:, ((X - h)//2):((X - h)//2 + h),((X - w)//2):((X - w)//2 + w)].fill_(1.0)
return img, mask
with torch.no_grad():
psnr_val_rgb = []
ssim_val_rgb = []
for ii, data_test in enumerate(tqdm(test_loader), 0):
rgb_gt = data_test[0].numpy().squeeze().transpose((1,2,0))
rgb_noisy, mask = expand2square(data_test[1].cuda(), factor=128)
filenames = data_test[2]
rgb_restored = model_restoration(rgb_noisy, 1 - mask)
rgb_restored = torch.masked_select(rgb_restored,mask.bool()).reshape(1,3,rgb_gt.shape[0],rgb_gt.shape[1])
rgb_restored = torch.clamp(rgb_restored,0,1).cpu().numpy().squeeze().transpose((1,2,0))
utils.save_img(os.path.join(args.result_dir,filenames[0]), img_as_ubyte(rgb_restored))