-
Notifications
You must be signed in to change notification settings - Fork 87
/
eval.py
191 lines (138 loc) · 5.81 KB
/
eval.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
import os
import cv2
import shutil
import numpy as np
import skimage
from skimage import io
from skimage import transform as tf
import argparse
import torch
import torch.nn as nn
from torch.autograd import Variable
from torchvision import transforms
from collections import OrderedDict
from modelGeoNet import GeoNet
# For parsing commandline arguments
parser = argparse.ArgumentParser()
parser.add_argument("--imgPath", type=str, default='E:\\IMG_7325.jpeg', help='input image path')
parser.add_argument("--modelPath", type=str, default='E:\\model.pkl', help='pre-trained model path')
parser.add_argument("--saveImgPath", type=str, default='E:\\IMG_7325.png', help='resized image path')
parser.add_argument("--saveFlowPath", type=str, default='E:\\IMG_7325.npy', help='saved flows path')
args = parser.parse_args()
def resizeImg(imgPath, H, W):
'''
resize while keeping the aspect ratio and then crop the image to a given shape (H, W)
'''
img = io.imread(imgPath)
h, w = img.shape[0:2]
if h > w:
ratio = float(h)/float(w)
if (ratio > float(H)/float(W)):
img = skimage.transform.resize(img, (int(ratio*W), W), order=1)
else:
img = skimage.transform.resize(img, (H, int(H/ratio)), order=1)
yc = int(img.shape[0]/2)
xc = int(img.shape[1]/2)
img = img[yc - int(H/2):yc + int(H/2), xc - int(W/2):xc + int(W/2)]
else:
ratio = float(w)/float(h)
if (ratio > float(H)/float(W)):
img = skimage.transform.resize(img, (W, int(W*ratio)), order=1)
else:
img = skimage.transform.resize(img, (int(H/ratio), H), order=1)
yc = int(img.shape[0]/2)
xc = int(img.shape[1]/2)
img = img[yc - int(W/2):yc + int(W/2), xc - int(H/2):xc + int(H/2)]
return img
def padImg(img):
'''
pad image twice.
The first padding is to make sure the patches cover all image regions.
The second padding is used for cropping the global patch.
'''
H = img.shape[0]
W = img.shape[1]
globalFct = 4
patchRes = 256
ovlp = int(patchRes * 0.25)
padH = (int((H - patchRes)/(patchRes - ovlp) + 1) * (patchRes - ovlp) + patchRes) - H
padW = (int((W - patchRes)/(patchRes - ovlp) + 1) * (patchRes - ovlp) + patchRes) - W
padding = int(patchRes * (globalFct - 1) / 2.0)
padImg = cv2.copyMakeBorder(img, 0, padH, 0, padW, cv2.BORDER_REPLICATE)
padImg = cv2.copyMakeBorder(padImg, padding, padding, padding, padding, cv2.BORDER_REPLICATE)
return padImg
def cropToPatch(img):
'''
crop the image to local and global patches
'''
H = img.shape[0]
W = img.shape[1]
globalFct = 4
patchRes = 256
ovlp = int(patchRes * 0.25)
padding = int(patchRes * (globalFct - 1) / 2.0)
cropH = patchRes
cropW = patchRes
ynum = int((H - (globalFct - 1) * cropH - cropH)/(cropH - ovlp)) + 1
xnum = int((W - (globalFct - 1) * cropW - cropW)/(cropW - ovlp)) + 1
totalLocal = np.zeros((ynum, xnum, patchRes, patchRes, 3), dtype=np.uint8)
totalGloba = np.zeros((ynum, xnum, 256, 256, 3), dtype=np.uint8)
for j in range(0, ynum):
for i in range(0, xnum):
x = int(padding + i * (cropW - ovlp))
y = int(padding + j * (cropH - ovlp))
totalLocal[j, i] = img[y:int(y + patchRes), x:int(x + patchRes)]
gx = int(x - padding)
gy = int(y - padding)
globalpatch = img[gy:int(gy + globalFct * patchRes), gx:int(gx + globalFct * patchRes)]
globalpatch = skimage.transform.resize(globalpatch, (256, 256)) * 255.0
totalGloba[j, i] = globalpatch
return totalLocal, totalGloba
def testRealFlow(modelPath, localPatch, globalPatch):
'''
estimate the flows
'''
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
model = GeoNet([1, 1, 1, 1, 1])
if torch.cuda.is_available():
model = model.cuda()
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
model.load_state_dict(torch.load(modelPath))
else:
state_dict = torch.load(modelPath)
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:]
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
model.eval()
ynum = localPatch.shape[0]
xnum = localPatch.shape[1]
scal = localPatch.shape[2]
totalFlow = np.zeros((ynum, xnum, 2, scal, scal), dtype = np.float32)
for j in range(0, ynum):
for i in range(0, xnum):
temp_localPatch = localPatch[j, i]
temp_globaPatch = globalPatch[j, i]
temp_localPatch = transform(temp_localPatch)
temp_globaPatch = transform(temp_globaPatch)
if torch.cuda.is_available():
temp_localPatch = temp_localPatch.cuda()
temp_globaPatch = temp_globaPatch.cuda()
temp_localPatch = temp_localPatch.view(1,3,scal,scal)
temp_globaPatch = temp_globaPatch.view(1,3,256,256)
temp_localPatch = Variable(temp_localPatch)
temp_globaPatch = Variable(temp_globaPatch)
flow_output = model(temp_localPatch, temp_globaPatch)
u = flow_output.data.cpu().numpy()[0][0]
v = flow_output.data.cpu().numpy()[0][1]
totalFlow[j,i,0] = u
totalFlow[j,i,1] = v
return totalFlow
img = resizeImg(args.imgPath, H = 2000, W = 1500)
io.imsave(args.saveImgPath, img)
img = padImg(img)
totalLocalPatch, totalGlobaPatch = cropToPatch(img)
totalFlow = testRealFlow(args.modelPath, totalLocalPatch, totalGlobaPatch)
np.save(args.saveFlowPath, totalFlow)