forked from HongguangZhang/DMPHN-cvpr19-master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDNet4_test.py
executable file
·207 lines (175 loc) · 10.6 KB
/
SDNet4_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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.optim.lr_scheduler import StepLR
import numpy as np
import os
import math
import argparse
import random
import models
import torchvision
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, datasets
from datasets import GoProDataset
import time
from PIL import Image
parser = argparse.ArgumentParser(description="Deep Multi-Patch Hierarchical Network")
parser.add_argument("-e","--epochs",type = int, default = 3000)
parser.add_argument("-se","--start_epoch",type = int, default = 0)
parser.add_argument("-b","--batchsize",type = int, default = 2)
parser.add_argument("-c","--cropsize",type = int, default = 256)
parser.add_argument("-l","--learning_rate", type = float, default = 0.0001)
parser.add_argument("-g","--gpu",type=int, default=0)
args = parser.parse_args()
#Hyper Parameters
METHOD = "SDNet4"
SAMPLE_DIR = "test_samples"
EXPDIR = "SDNet4_test_res"
LEARNING_RATE = args.learning_rate
EPOCHS = args.epochs
GPU = args.gpu
BATCH_SIZE = args.batchsize
CROP_SIZE = args.cropsize
def save_images(images, name):
filename = './test_results/' + EXPDIR + "/" + name
torchvision.utils.save_image(images, filename)
def weight_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(0.5 / n))
if m.bias is not None:
m.bias.data.zero_()
elif classname.find('BatchNorm') != -1:
m.weight.data.fill_(1)
m.bias.data.zero_()
elif classname.find('Linear') != -1:
n = m.weight.size(1)
m.weight.data.normal_(0, 0.01)
m.bias.data = torch.ones(m.bias.data.size())
def main():
print("init data folders")
encoder = {}
decoder = {}
encoder_optim = {}
decoder_optim = {}
encoder_scheduler = {}
decoder_scheduler = {}
for s in ['s1', 's2', 's3', 's4']:
encoder[s] = {}
decoder[s] = {}
encoder_optim[s] = {}
decoder_optim[s] = {}
encoder_scheduler[s] = {}
decoder_scheduler[s] = {}
for lv in ['lv1', 'lv2', 'lv3']:
encoder[s][lv] = models.Encoder()
decoder[s][lv] = models.Decoder()
encoder[s][lv].apply(weight_init).cuda(GPU)
decoder[s][lv].apply(weight_init).cuda(GPU)
encoder_optim[s][lv] = torch.optim.Adam(encoder[s][lv].parameters(),lr=LEARNING_RATE)
encoder_scheduler[s][lv] = StepLR(encoder_optim[s][lv],step_size=1000,gamma=0.1)
decoder_optim[s][lv] = torch.optim.Adam(decoder[s][lv].parameters(),lr=LEARNING_RATE)
decoder_scheduler[s][lv] = StepLR(decoder_optim[s][lv],step_size=1000,gamma=0.1)
if os.path.exists(str('./checkpoints/' + METHOD + "/encoder_" + s + "_" + lv + ".pkl")):
encoder[s][lv].load_state_dict(torch.load(str('./checkpoints/' + METHOD + "/encoder_" + s + "_" + lv + ".pkl")))
print("load encoder_" + s + "_" + lv + " successfully!")
if os.path.exists(str('./checkpoints/' + METHOD + "/decoder_" + s + "_" + lv + ".pkl")):
decoder[s][lv].load_state_dict(torch.load(str('./checkpoints/' + METHOD + "/decoder_" + s + "_" + lv + ".pkl")))
print("load decoder_" + s + "_" + lv + " successfully!")
if os.path.exists('./test_results/' + EXPDIR) == False:
os.system('mkdir ./test_results/' + EXPDIR)
iteration = 0.0
test_time = 0.0
for images_name in os.listdir(SAMPLE_DIR):
with torch.no_grad():
images = {}
feature = {}
residual = {}
for s in ['s1', 's2', 's3', 's4']:
feature[s] = {}
residual[s] = {}
images['lv1'] = transforms.ToTensor()(Image.open(SAMPLE_DIR + '/' + images_name).convert('RGB'))
images['lv1'] = Variable(images['lv1'] - 0.5).unsqueeze(0).cuda(GPU)
start = time.time()
H = images['lv1'].size(2)
W = images['lv1'].size(3)
images['lv2_1'] = images['lv1'][:,:,0:int(H/2),:]
images['lv2_2'] = images['lv1'][:,:,int(H/2):H,:]
images['lv3_1'] = images['lv2_1'][:,:,:,0:int(W/2)]
images['lv3_2'] = images['lv2_1'][:,:,:,int(W/2):W]
images['lv3_3'] = images['lv2_2'][:,:,:,0:int(W/2)]
images['lv3_4'] = images['lv2_2'][:,:,:,int(W/2):W]
s = 's1'
feature[s]['lv3_1'] = encoder[s]['lv3'](images['lv3_1'])
feature[s]['lv3_2'] = encoder[s]['lv3'](images['lv3_2'])
feature[s]['lv3_3'] = encoder[s]['lv3'](images['lv3_3'])
feature[s]['lv3_4'] = encoder[s]['lv3'](images['lv3_4'])
feature[s]['lv3_top'] = torch.cat((feature[s]['lv3_1'], feature[s]['lv3_2']), 3)
feature[s]['lv3_bot'] = torch.cat((feature[s]['lv3_3'], feature[s]['lv3_4']), 3)
residual[s]['lv3_top'] = decoder[s]['lv3'](feature[s]['lv3_top'])
residual[s]['lv3_bot'] = decoder[s]['lv3'](feature[s]['lv3_bot'])
feature[s]['lv2_1'] = encoder[s]['lv2'](images['lv2_1'] + residual[s]['lv3_top']) + feature[s]['lv3_top']
feature[s]['lv2_2'] = encoder[s]['lv2'](images['lv2_2'] + residual[s]['lv3_bot']) + feature[s]['lv3_bot']
feature[s]['lv2'] = torch.cat((feature[s]['lv2_1'], feature[s]['lv2_2']), 2)
residual[s]['lv2'] = decoder[s]['lv2'](feature[s]['lv2'])
feature[s]['lv1'] = encoder[s]['lv1'](images['lv1'] + residual[s]['lv2']) + feature[s]['lv2']
residual[s]['lv1'] = decoder[s]['lv1'](feature[s]['lv1'])
s = 's2'
ps = 's1'
feature[s]['lv3_1'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,0:int(H/2),0:int(W/2)])
feature[s]['lv3_2'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,0:int(H/2),int(W/2):W])
feature[s]['lv3_3'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,int(H/2):H,0:int(W/2)])
feature[s]['lv3_4'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,int(H/2):H,int(W/2):W])
feature[s]['lv3_top'] = torch.cat((feature[s]['lv3_1'], feature[s]['lv3_2']), 3) + feature[ps]['lv3_top']
feature[s]['lv3_bot'] = torch.cat((feature[s]['lv3_3'], feature[s]['lv3_4']), 3) + feature[ps]['lv3_bot']
residual[s]['lv3_top'] = decoder[s]['lv3'](feature[s]['lv3_top'])
residual[s]['lv3_bot'] = decoder[s]['lv3'](feature[s]['lv3_bot'])
feature[s]['lv2_1'] = encoder[s]['lv2'](residual[ps]['lv1'][:,:,0:int(H/2),:] + residual[s]['lv3_top']) + feature[s]['lv3_top'] + feature[ps]['lv2_1']
feature[s]['lv2_2'] = encoder[s]['lv2'](residual[ps]['lv1'][:,:,int(H/2):H,:] + residual[s]['lv3_bot']) + feature[s]['lv3_bot'] + feature[ps]['lv2_2']
feature[s]['lv2'] = torch.cat((feature[s]['lv2_1'], feature[s]['lv2_2']), 2)
residual[s]['lv2'] = decoder[s]['lv2'](feature[s]['lv2']) + residual['s1']['lv1']
feature[s]['lv1'] = encoder[s]['lv1'](residual[ps]['lv1'] + residual[s]['lv2']) + feature[s]['lv2'] + feature[ps]['lv1']
residual[s]['lv1'] = decoder[s]['lv1'](feature[s]['lv1'])
s = 's3'
ps = 's2'
feature[s]['lv3_1'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,0:int(H/2),0:int(W/2)])
feature[s]['lv3_2'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,0:int(H/2),int(W/2):W])
feature[s]['lv3_3'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,int(H/2):H,0:int(W/2)])
feature[s]['lv3_4'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,int(H/2):H,int(W/2):W])
feature[s]['lv3_top'] = torch.cat((feature[s]['lv3_1'], feature[s]['lv3_2']), 3) + feature[ps]['lv3_top']
feature[s]['lv3_bot'] = torch.cat((feature[s]['lv3_3'], feature[s]['lv3_4']), 3) + feature[ps]['lv3_bot']
residual[s]['lv3_top'] = decoder[s]['lv3'](feature[s]['lv3_top'])
residual[s]['lv3_bot'] = decoder[s]['lv3'](feature[s]['lv3_bot'])
feature[s]['lv2_1'] = encoder[s]['lv2'](residual[ps]['lv1'][:,:,0:int(H/2),:] + residual[s]['lv3_top']) + feature[s]['lv3_top'] + feature[ps]['lv2_1']
feature[s]['lv2_2'] = encoder[s]['lv2'](residual[ps]['lv1'][:,:,int(H/2):H,:] + residual[s]['lv3_bot']) + feature[s]['lv3_bot'] + feature[ps]['lv2_2']
feature[s]['lv2'] = torch.cat((feature[s]['lv2_1'], feature[s]['lv2_2']), 2)
residual[s]['lv2'] = decoder[s]['lv2'](feature[s]['lv2']) + residual['s1']['lv1']
feature[s]['lv1'] = encoder[s]['lv1'](residual[ps]['lv1'] + residual[s]['lv2']) + feature[s]['lv2'] + feature[ps]['lv1']
residual[s]['lv1'] = decoder[s]['lv1'](feature[s]['lv1'])
s = 's4'
ps = 's3'
feature[s]['lv3_1'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,0:int(H/2),0:int(W/2)])
feature[s]['lv3_2'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,0:int(H/2),int(W/2):W])
feature[s]['lv3_3'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,int(H/2):H,0:int(W/2)])
feature[s]['lv3_4'] = encoder[s]['lv3'](residual[ps]['lv1'][:,:,int(H/2):H,int(W/2):W])
feature[s]['lv3_top'] = torch.cat((feature[s]['lv3_1'], feature[s]['lv3_2']), 3) + feature[ps]['lv3_top']
feature[s]['lv3_bot'] = torch.cat((feature[s]['lv3_3'], feature[s]['lv3_4']), 3) + feature[ps]['lv3_bot']
residual[s]['lv3_top'] = decoder[s]['lv3'](feature[s]['lv3_top'])
residual[s]['lv3_bot'] = decoder[s]['lv3'](feature[s]['lv3_bot'])
feature[s]['lv2_1'] = encoder[s]['lv2'](residual[ps]['lv1'][:,:,0:int(H/2),:] + residual[s]['lv3_top']) + feature[s]['lv3_top'] + feature[ps]['lv2_1']
feature[s]['lv2_2'] = encoder[s]['lv2'](residual[ps]['lv1'][:,:,int(H/2):H,:] + residual[s]['lv3_bot']) + feature[s]['lv3_bot'] + feature[ps]['lv2_2']
feature[s]['lv2'] = torch.cat((feature[s]['lv2_1'], feature[s]['lv2_2']), 2)
residual[s]['lv2'] = decoder[s]['lv2'](feature[s]['lv2']) + residual['s1']['lv1']
feature[s]['lv1'] = encoder[s]['lv1'](residual[ps]['lv1'] + residual[s]['lv2']) + feature[s]['lv2'] + feature[ps]['lv1']
residual[s]['lv1'] = decoder[s]['lv1'](feature[s]['lv1'])
deblurred_image = residual[s]['lv1']
stop = time.time()
test_time += stop-start
print('RunTime:%.4f'%(stop-start), ' Average Runtime:%.4f'%(test_time/(iteration+1)))
save_images(deblurred_image.data + 0.5, images_name)
iteration += 1
if __name__ == '__main__':
main()