-
Notifications
You must be signed in to change notification settings - Fork 1
/
inferbmfromRGBP.py
345 lines (301 loc) · 12.7 KB
/
inferbmfromRGBP.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#test end to end benchmark data test
from email.mime import image
import sys, os
from sklearn.inspection import partial_dependence
import torch
import argparse
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
import torchvision.transforms.functional as transforms
import cv2
from torch.autograd import Variable
from torch.utils import data
from tqdm import tqdm
import time
import matplotlib.pyplot as plt
import torchvision
from utils import find4contour,compute_boundary
from models import get_model
from loaders import get_loader
from utils import convert_state_dict
#print(torch.__version__)
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#DEVICE = torch.device('cpu')
t1=torch.arange(-1,1,2/128)
grid_x, grid_y = torch.meshgrid(t1, t1)
originalgrid=torch.cat([grid_y.unsqueeze(-1),grid_x.unsqueeze(-1)],dim=-1).to(torch.float32)
originalgrid=originalgrid.permute(2,0,1).unsqueeze(0).to(DEVICE)
def gaussian(M, std, sym=True):
if M < 1:
return np.array([])
if M == 1:
return np.ones(1, 'd')
odd = M % 2
if not sym and not odd:
M = M + 1
n = np.arange(0, M) - (M - 1.0) / 2.0
sig2 = 2 * std * std
w = np.exp(-n ** 2 / sig2)
if not sym and not odd:
w = w[:-1]
kernel_2d=np.outer(w,w)
return kernel_2d/np.sum(kernel_2d)
weights = torch.from_numpy(gaussian(3,1.5)).to(torch.float)
weights = torch.tensor([[0.0778, 0.1233, 0.0778],
[0.1233, 0.1953, 0.1233],
[0.0778, 0.1233, 0.0778]])
print(weights)
weights = weights.view(1, 1, 3, 3).repeat(1, 1, 1, 1).to(DEVICE)
def tight_crop(im, fm):
# different tight crop
msk=((fm[:,:,0]!=0)&(fm[:,:,1]!=0)&(fm[:,:,2]!=0)).to(torch.int8)
#print((msk).nonzero())
y, x = (msk).nonzero()[:,0],(msk).nonzero()[:,1]
minx = torch.min(x)
maxx = torch.max(x)
miny = torch.min(y)
maxy = torch.max(y)
im = im[miny : maxy + 1, minx : maxx + 1, :]
fm = fm[miny : maxy + 1, minx : maxx + 1, :]
im=im*fm
return im, fm
def tight_crop_4(im, fm):
# different tight crop
print(fm.shape)
msk=((fm[:,0,:,:]!=0)&(fm[:,1,:,:]!=0)&(fm[:,2,:,:]!=0)).to(torch.int8)
print((msk).nonzero())
y, x = (msk).nonzero()[:,1],(msk).nonzero()[:,2]
minx = torch.min(x)
maxx = torch.max(x)
miny = torch.min(y)
maxy = torch.max(y)
im = im[:,:,miny : maxy + 1, minx : maxx + 1]
#fm = fm[:,:,miny : maxy + 1, minx : maxx + 1]
return im, fm
def smooth2D(img,weights,pad='None'):
if pad=='constant':
img=F.pad(img,(1,1,1,1,0,0,0,0))
elif pad=='replicate':
img=F.pad(img,(1,1,1,1),mode='replicate') # 0.4564 10.1820
elif pad=='reflect':
img=F.pad(img,(1,1,1,1),mode='reflect')
return F.conv2d(img, weights)
def unwarp(img, bm):
w,h=img.shape[2],img.shape[3]
#w,h=1920,1080
bm=bm.squeeze(0)
bm = bm.permute(1,2,0).detach().cpu().numpy()
#print(bm[:,:,0].shape)
bm0=cv2.blur(bm[:,:,0],(3,3))
bm1=cv2.blur(bm[:,:,1],(3,3))
bm0=cv2.resize(bm0,(h,w))
bm1=cv2.resize(bm1,(h,w))
bm=np.stack([bm0,bm1],axis=-1)
bm=np.expand_dims(bm,0)
bm=torch.from_numpy(bm).double()
res = F.grid_sample(input=img, grid=(bm))
#res = res[0].numpy().transpose((1, 2, 0))
return res
def convertimg(img):
img = img.astype(float) / 255.0
img = img.transpose((2, 0, 1))
img = np.expand_dims(img, 0)
img = torch.from_numpy(img).to(torch.float).to(DEVICE)
return img
def unwarp_fast_nobmsmooth(img, bm):
img=convertimg(img)
h,w=img.shape[2],img.shape[3]
#h,w=1080,1920
#print(w,h)
#m = nn.ReplicationPad2d((1, 1, 1, 1))
#bm=m(bm)
#bm = bm.permute(0,2,3,1)
#bm0=cv2.blur(bm[:,:,0],(3,3))
#bm1=cv2.blur(bm[:,:,1],(3,3))
#print(bm[:,:1,:,:].shape)
#bm0=smooth2D(bm[:,:1,:,:],weights,pad='replicate')
#bm1=smooth2D(bm[:,1:,:,:],weights,pad='replicate')
#print(bm0.shape)
#bm0=F.interpolate(bm0,(h,w),mode='bilinear',align_corners=True)
#bm1=F.interpolate(bm1,(h,w),mode='bilinear',align_corners=True)
bm=F.interpolate(bm,(h,w),mode='bilinear',align_corners=True)
#bm=torch.cat([bm0,bm1],dim=1)
bm=bm.permute(0,2,3,1)
def unwarp_fast(img, bm):
img=convertimg(img)
h,w=img.shape[2],img.shape[3]
#h,w=1080,1920
#print(w,h)
#m = nn.ReplicationPad2d((1, 1, 1, 1))
#bm=m(bm)
#bm = bm.permute(0,2,3,1)
#bm0=cv2.blur(bm[:,:,0],(3,3))
#bm1=cv2.blur(bm[:,:,1],(3,3))
#print(bm[:,:1,:,:].shape)
bm0=smooth2D(bm[:,:1,:,:],weights,pad='replicate')
bm1=smooth2D(bm[:,1:,:,:],weights,pad='replicate')
#print(bm0.shape)
bm0=F.interpolate(bm0,(h,w),mode='bilinear',align_corners=True)
bm1=F.interpolate(bm1,(h,w),mode='bilinear',align_corners=True)
#bm=F.interpolate(bm,(h,w),mode='bilinear',align_corners=True)
bm=torch.cat([bm0,bm1],dim=1)
bm=bm.permute(0,2,3,1)
res = F.grid_sample(input=img, grid=bm)
return res
def unwarp_nosmooth(img, bm):
h,w=img.shape[2],img.shape[3]
bm=F.interpolate(bm,(h,w),mode='bilinear',align_corners=True)
bm=bm.permute(0,2,3,1)
res = F.grid_sample(input=img, grid=bm)
return res
def test(args,img_path,fname,prob_constraint=False):
originalstart=time.time()
#prob_model_file_name = os.path.split(args.prob_model_path)[1]
#prob_model_name = prob_model_file_name[:prob_model_file_name.find('_',2)]
boundary_model_name='u2net'
#prob_model_name='mobilevit_xxsandfcn_skip_probfromalb'
prob_model_name='mobilevit_sandfcn_skip_prob_sign'
#bm_model_file_name = os.path.split(args.bm_model_path)[1]
#bm_model_name = bm_model_file_name[:bm_model_file_name.find('_')]
#bm_model_name='mobilevit_sandfcn_skip'
bm_model_name='mobilevit_sandfcn_skipRGBD'
#bm_model_name='mobilevit_sandfcn'
#bm_model_name='bm0false'
#bm_model_name='mobilevit_sanddeeplab3'
#print(bm_model_name)
boundary_n_classes = 1
prob_n_classes = 1
bm_n_classes = 2
boundary_img_size=(256,256)
prob_img_size=(256,256)
bm_img_size=(128,128)
# Setup image
print("Read Input Image from : {}".format(img_path))
imgorg = cv2.imread(img_path) #BGR
#imgorg=cv2.resize(imgorg,(1920,1080))
h,w,_=imgorg.shape
imgorgbgr=cv2.resize(imgorg, prob_img_size)
imgbgr = np.expand_dims(imgorgbgr, 0)/255.0
imgbgr = torch.from_numpy(imgbgr).float().permute(0,3,1,2)
imgorgrgb = cv2.cvtColor(imgorg, cv2.COLOR_BGR2RGB)
imgrgb = cv2.resize(imgorgrgb, prob_img_size)
#img = img[:, :, ::-1]
imgrgb = imgrgb.astype(float) / 255.0
imgrgb = imgrgb.transpose((2, 0, 1)) #CHW
imgcanbeinputtobm_torch=torch.from_numpy(imgrgb).float().unsqueeze(0).to(DEVICE) #1 3 256 256
# Predict
prob_model = get_model(prob_model_name, prob_n_classes, in_channels=3)
bm_model = get_model(bm_model_name, bm_n_classes, in_channels=4, img_size=bm_img_size)
boundary_model = get_model(boundary_model_name, boundary_n_classes, in_channels=3)
if DEVICE.type == 'cpu':
prob_state = convert_state_dict(torch.load(args.prob_model_path, map_location='cpu')['model_state'])
boundary_state = convert_state_dict(torch.load(args.boundary_model_path, map_location='cpu')['model_state'])
bm_state = convert_state_dict(torch.load(args.bm_model_path, map_location='cpu')['model_state'])
#print(bm_state)
else:
prob_state = convert_state_dict(torch.load(args.prob_model_path)['model_state'])
boundary_state = convert_state_dict(torch.load(args.boundary_model_path, map_location='cpu')['model_state'])
bm_state = convert_state_dict(torch.load(args.bm_model_path)['model_state'])
#print(bm_state)
boundary_model.load_state_dict(boundary_state)
boundary_model.eval()
prob_model.load_state_dict(prob_state)
prob_model.eval()
bm_model.load_state_dict(bm_state)
bm_model.eval()
boundary_model.to(DEVICE)
prob_model.to(DEVICE)
bm_model.to(DEVICE)
images = Variable(imgbgr).to(DEVICE)
#print(images.shape)
#imgorg=convertimg(imgorg)
with torch.no_grad():
start=time.time()
boundary_outputs = boundary_model(images)
time1=time.time()
#print(time1-start)
#maxpool=nn.MaxPool2d(3,1,1)
pred_boundary = boundary_outputs[0]
#pred_boundary=maxpool(pred_boundary)
#pred_boundary = smooth2D(pred_boundary,weights,pad='constant') # 1,1,256,256
#pred_boundary=pred_boundary.squeeze(0).squeeze(0)
mskorg=(pred_boundary>0.5).to(torch.float32)#1,1,256,256
mskindex=mskorg.nonzero()
extracted_img=torch.mul(mskorg.repeat(1,3,1,1),imgcanbeinputtobm_torch) # 1,3,256,256
pmap= prob_model(extracted_img)
#pmap = smooth2D(pmap,weights,pad='constant') # 1,1,256,256
#cv2.imwrite("testdmap.png",pred_prob.squeeze(0).squeeze(0).cpu().numpy()*255)
#maxd=torch.max(pred_prob)
#pred_prob = smooth2D(pred_prob,weights,pad='constant') #1,1,256,256
#mind=torch.min(pred_prob[mskindex[:,0],mskindex[:,1],mskindex[:,2],mskindex[:,3]])
#pred_prob=mskorg*pred_prob
#final_msk=(pred_prob>0).to(torch.float32)
#pred_prob=final_msk*((pred_prob+0.675)/(0.675+maxd))
time2=time.time()
#print(time2-time1)
bm_input=torch.cat([extracted_img,pmap],dim=1)
bm_input=F.interpolate(bm_input,bm_img_size,mode='bilinear',align_corners=True)
#print(bm_input.shape)
#cv2.imwrite("testimg.png",bm_input[0,0:3,:,:].permute(1,2,0).cpu().numpy()*255)
outp_bm=bm_model(bm_input)+originalgrid
#outp_bm[0,:,0,1:]=up
#print(outp_bm.shape)
# Save the output
#resizedmsk=F.interpolate(msk_org3.permute(2,0,1).unsqueeze(0),(h,w)).to(DEVICE)
#print(resizedmsk.shape)
#imgorg,_=tight_crop_4(imgorg,resizedmsk)
#print(imgorg.shape)
if not os.path.exists(args.out_path) and args.show:
os.makedirs(args.out_path)
outp=os.path.join(args.out_path,fname)
uwpred=unwarp_fast(imgorg,outp_bm)
finish=time.time()
uwpred = uwpred[0].cpu().numpy().transpose((1, 2, 0))
#uwpred=F.grid_sample(bm_input,outp_bm.permute(0,2,3,1))[0].permute(1,2,0).detach().cpu().numpy()
if args.show:
cv2.imwrite(outp,uwpred*255)
print(finish-originalstart)
torch.cuda.empty_cache()
return finish-originalstart
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Params')
parser.add_argument('--boundary_model_path', nargs='?', type=str, default='',
help='Path to the saved boundary model')
parser.add_argument('--prob_model_path', nargs='?', type=str, default='',
help='Path to the saved prob model')
parser.add_argument('--bm_model_path', nargs='?', type=str, default='',
help='Path to the saved bm model')
parser.add_argument('--img_path', nargs='?', type=str, default='/disk2/sinan/crop/',
help='Path of the input image')
parser.add_argument('--out_path', nargs='?', type=str, default='/disk2/sinan/mymodelresult/testjoint_rgbp_sign0',
help='Path of the output unwarped image')
parser.add_argument('--show', dest='show', action='store_true',
help='Show the input image and output unwarped')
parser.set_defaults(show=False)
args = parser.parse_args()
totaltime=0
totalnbofimg=0
for fname in os.listdir(args.img_path):
if '.jpg' in fname or '.JPG' in fname or '.png' in fname:
img_path=os.path.join( args.img_path,fname)
totalnbofimg+=1
totaltime+=test(args,img_path,fname,prob_constraint=False)
print(totaltime/totalnbofimg)
#CUDA_VISIBLE_DEVICES=1 python inferbmfromRGBP.py --boundary_model_path /home/sinan/DewarpNet-master/checkpoints-joint_rgb_sign0p_boundary/u2net_89_0.04828382447361946_u2net_joint_rgbp_norecon_best_model.pkl --prob_model_path /home/sinan/DewarpNet-master/checkpoints-joint_rgb_sign0_prob/mobilevit_sandfcn_skip_prob_sign_89_0.039962654523551466_u2net_joint_rgbp_norecon_best_model.pkl --bm_model_path /home/sinan/DewarpNet-master/checkpoints-joint_rgb_sign0p_flow/mobilevit_sandfcn_skipRGBD_89_0.012501356787979603_u2net_joint_rgbp_norecon_best_model.pkl --show
"""
def smooth1D(img, sigma):
n = int(sigma*(2*torch.log(1000))**0.5)
x = torch.arange(-n, n+1)
filter = torch.exp((x**2)/-2/(sigma**2))
allOneMat = torch.ones(img.shape[0:2])
resultImg1 = nn.Conv1d(img, filter, 1, torch.float32, 'constant', 0, 0)
resultAllOne = nn.Conv1d(allOneMat, filter, 1,
torch.float32, 'constant', 0, 0)
img_smoothed = resultImg1/resultAllOne
return img_smoothed
def smooth2D(img, sigma):
smoothed1D = smooth1D(img, sigma)
img_smoothed = (smooth1D(smoothed1D.T, sigma)).T
return img_smoothed """