-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
177 lines (130 loc) · 3.93 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
#from Python
import time
import csv
import os
import math
import numpy as np
import sys
from shutil import copyfile
#from Pytorch
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torchvision import datasets
from torchvision import transforms
from torchvision.utils import save_image
import torch.nn.utils as torch_utils
from torch.optim.lr_scheduler import StepLR
#from this project
from data_loader import get_loader
import data_loader as dl
import VisionOP
import model
import param as p
import utils
import pytorch_ssim
#local function
def to_var(x):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x)
def denorm(x):
out = (x + 1) / 2
return out.clamp(0, 1)
def norm(x):
out = (x - 0.5) * 2
return out.clamp(-1,1)
################ Hyper Parameters ################
# VERSION
version = '2019-12-19(LPGnet-with-LRblock)'
subversion = '1_1'
# data Set
dataSetName = p.dataSetName
dataSetMode = p.dataSetMode
dataPath = p.dataPath
maxDataNum = p.maxDataNum #in fact, 4500
batchSize = p.batchSize
MaxCropWidth = p.MaxCropWidth
MinCropWidth = p.MinCropWidth
MaxCropHeight = p.MaxCropHeight
MinCropHeight = p.MinCropHeight
# model
NOF = p.NOF
# train
MaxEpoch = p.MaxEpoch
learningRate = p.learningRate
# save
numberSaveImage = p.numberSaveImage
############################################
############################################
############################################
print("")
print(" _____ ______ _______ _____ _ _ ________ __ ")
print(" | __ \\| ____|__ __|_ _| \\ | | ____\\ \\ / / ")
print(" | |__) | |__ | | | | | \\| | |__ \\ V / ")
print(" | _ /| __| | | | | | . ` | __| > < ")
print(" | | \\ \\| |____ | | _| |_| |\\ | |____ / . \\")
print(" |_| \\_\\______| |_| |_____|_| \\_|______/_/ \\_\\ ")
print("")
print("Retinex model")
print("main Version : " + version)
print("sub Version : " + subversion)
print("")
############################################
############################################
torch.backends.cudnn.benchmark = True
# system setting
MODE = sys.argv[1]
dataSetMode = 'test'
dataPath = './data/test/'
data_loader = get_loader(dataPath,MaxCropWidth,MinCropWidth,MaxCropHeight,MinCropHeight,batchSize,dataSetName,dataSetMode)
#init model
Retinex = model.LMSN()
Retinex = nn.DataParallel(Retinex).cuda()
#model load
startEpoch = 0
print("Load below models")
if (MODE != 'n'):
checkpoint_rt = torch.load('./data/model/Retinex' + '.pkl')
Retinex.load_state_dict(checkpoint_rt['model'])
print("All model loaded.")
def psnr(input, target):
#print(torch.min(input))
#print(torch.max(input))
input = torch.abs(input - target).cuda()
MSE = torch.mean(input * input)
PSNR = 10 * math.log10(1 / MSE)
return PSNR
#a = time.perf_counter()
for epoch in range(0, 1):
# ============= Train Retinex & Adjust module =============#
finali = 0
ssim = 0
psnr2 = 0
torch.set_grad_enabled(False)
# rt_scheduler.step(epoch)
#total_time = 0
j=0
avg_in = 0
avg_out = 0
for i, (images) in enumerate(data_loader):
b,c,h,w_ = images.size()
w = int(w_/2)
if i == 0:
total_time = 0
with torch.no_grad():
torch.cuda.synchronize()
Input = to_var(images).contiguous()
if i >= 0:
a = time.perf_counter()
Scale1,Scale2,Scale3,res2,res3 = Retinex(Input)
olda = a
a = time.perf_counter()
total_time = total_time + a - olda
print('%d/500, time: %.5f sec ' % ((j+1),total_time / (j+1)), end="\n")
j=j+1
else:
Scale1,Scale2,Scale3,res2,res3 = Retinex(Input)
save_image(Scale3.data, './data/result/%d.png' % (i + 1))