forked from chenhsuanlin/inverse-compositional-STN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
170 lines (162 loc) · 7.92 KB
/
data.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
import numpy as np
import scipy.linalg
import os,time
import tensorflow as tf
import warp
# load MNIST data
def loadMNIST(fname):
if not os.path.exists(fname):
# download and preprocess MNIST dataset
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
trainData,validData,testData = {},{},{}
trainData["image"] = mnist.train.images.reshape([-1,28,28]).astype(np.float32)
validData["image"] = mnist.validation.images.reshape([-1,28,28]).astype(np.float32)
testData["image"] = mnist.test.images.reshape([-1,28,28]).astype(np.float32)
trainData["label"] = mnist.train.labels.astype(np.float32)
validData["label"] = mnist.validation.labels.astype(np.float32)
testData["label"] = mnist.test.labels.astype(np.float32)
os.makedirs(os.path.dirname(fname))
np.savez(fname,train=trainData,valid=validData,test=testData)
MNIST = np.load(fname)
trainData = MNIST["train"].item()
validData = MNIST["valid"].item()
testData = MNIST["test"].item()
return trainData,validData,testData
# generate training batch
def genPerturbations(opt):
with tf.name_scope("genPerturbations"):
X = np.tile(opt.canon4pts[:,0],[opt.batchSize,1])
Y = np.tile(opt.canon4pts[:,1],[opt.batchSize,1])
dX = tf.random_normal([opt.batchSize,4])*opt.warpScale["pert"] \
+tf.random_normal([opt.batchSize,1])*opt.warpScale["trans"]
dY = tf.random_normal([opt.batchSize,4])*opt.warpScale["pert"] \
+tf.random_normal([opt.batchSize,1])*opt.warpScale["trans"]
O = np.zeros([opt.batchSize,4],dtype=np.float32)
I = np.ones([opt.batchSize,4],dtype=np.float32)
# fit warp parameters to generated displacements
if opt.warpType == "affine":
J = np.concatenate([np.stack([X,Y,I,O,O,O],axis=-1),
np.stack([O,O,O,X,Y,I],axis=-1)],axis=1)
dXY = tf.expand_dims(tf.concat(1,[dX,dY]),-1)
dpBatch = tf.matrix_solve_ls(J,dXY)
elif opt.warpType == "homography":
A = tf.concat([tf.stack([X,Y,I,O,O,O,-X*(X+dX),-Y*(X+dX)],axis=-1),
tf.stack([O,O,O,X,Y,I,-X*(Y+dY),-Y*(Y+dY)],axis=-1)],1)
b = tf.expand_dims(tf.concat([X+dX,Y+dY],1),-1)
dpBatch = tf.matrix_solve_ls(A,b)
dpBatch -= tf.to_float(tf.reshape([1,0,0,0,1,0,0,0],[1,8,1]))
dpBatch = tf.reduce_sum(dpBatch,reduction_indices=-1)
dpMtrxBatch = warp.vec2mtrxBatch(dpBatch,opt)
return dpBatch
# evaluation on validation/test sets
def evaluate(data,imageRawBatch,pInitBatch,labelBatch,prediction,sess,opt):
dataN = len(data["image"])
batchN = int(np.ceil(float(dataN)/opt.batchSize))
accurateCount = 0
for b in range(batchN):
# use some dummy data (0) as batch filler if necesaary
if b!=batchN-1:
realIdx = np.arange(opt.batchSize*b,opt.batchSize*(b+1))
else:
realIdx = np.arange(opt.batchSize*b,dataN)
idx = np.zeros([opt.batchSize],dtype=int)
idx[:len(realIdx)] = realIdx
batch = {
imageRawBatch: data["image"][idx],
labelBatch: data["label"][idx],
}
predictionBatch = sess.run(prediction,feed_dict=batch)
accurateCount += predictionBatch[:len(realIdx)].sum()
accuracy = float(accurateCount)/dataN
return accuracy
# generate batch of warped images from batch (bilinear interpolation)
def imageWarpIm(imageBatch,pMtrxBatch,opt,name=None):
with tf.name_scope("ImWarp"):
imageBatch = tf.expand_dims(imageBatch,-1)
batchSize = tf.shape(imageBatch)[0]
imageH,imageW = opt.H,opt.H
H,W = opt.H,opt.W
warpGTmtrxBatch = tf.tile(tf.expand_dims(opt.warpGTmtrx,0),[batchSize,1,1])
transMtrxBatch = tf.matmul(warpGTmtrxBatch,pMtrxBatch)
# warp the canonical coordinates
X,Y = np.meshgrid(np.linspace(-1,1,W),np.linspace(-1,1,H))
XYhom = tf.transpose(tf.stack([X.reshape([-1]),Y.reshape([-1]),np.ones([X.size])],axis=1))
XYhomBatch = tf.tile(tf.expand_dims(XYhom,0),[batchSize,1,1])
XYwarpHomBatch = tf.matmul(transMtrxBatch,tf.to_float(XYhomBatch))
XwarpHom,YwarpHom,ZwarpHom = tf.split(XYwarpHomBatch,3,1)
Xwarp = tf.reshape(XwarpHom/ZwarpHom,[batchSize,H,W])
Ywarp = tf.reshape(YwarpHom/ZwarpHom,[batchSize,H,W])
# get the integer sampling coordinates
Xfloor,Xceil = tf.floor(Xwarp),tf.ceil(Xwarp)
Yfloor,Yceil = tf.floor(Ywarp),tf.ceil(Ywarp)
XfloorInt,XceilInt = tf.to_int32(Xfloor),tf.to_int32(Xceil)
YfloorInt,YceilInt = tf.to_int32(Yfloor),tf.to_int32(Yceil)
imageIdx = tf.tile(tf.reshape(tf.range(batchSize),[batchSize,1,1]),[1,H,W])
imageVec = tf.reshape(imageBatch,[-1,tf.shape(imageBatch)[3]])
imageVecOutside = tf.concat([imageVec,tf.zeros([1,tf.shape(imageBatch)[3]])],0)
idxUL = (imageIdx*imageH+YfloorInt)*imageW+XfloorInt
idxUR = (imageIdx*imageH+YfloorInt)*imageW+XceilInt
idxBL = (imageIdx*imageH+YceilInt)*imageW+XfloorInt
idxBR = (imageIdx*imageH+YceilInt)*imageW+XceilInt
idxOutside = tf.fill([batchSize,H,W],batchSize*imageH*imageW)
def insideIm(Xint,Yint):
return (Xint>=0)&(Xint<imageW)&(Yint>=0)&(Yint<imageH)
idxUL = tf.where(insideIm(XfloorInt,YfloorInt),idxUL,idxOutside)
idxUR = tf.where(insideIm(XceilInt,YfloorInt),idxUR,idxOutside)
idxBL = tf.where(insideIm(XfloorInt,YceilInt),idxBL,idxOutside)
idxBR = tf.where(insideIm(XceilInt,YceilInt),idxBR,idxOutside)
# bilinear interpolation
Xratio = tf.reshape(Xwarp-Xfloor,[batchSize,H,W,1])
Yratio = tf.reshape(Ywarp-Yfloor,[batchSize,H,W,1])
ImUL = tf.to_float(tf.gather(imageVecOutside,idxUL))*(1-Xratio)*(1-Yratio)
ImUR = tf.to_float(tf.gather(imageVecOutside,idxUR))*(Xratio)*(1-Yratio)
ImBL = tf.to_float(tf.gather(imageVecOutside,idxBL))*(1-Xratio)*(Yratio)
ImBR = tf.to_float(tf.gather(imageVecOutside,idxBR))*(Xratio)*(Yratio)
ImWarpBatch = ImUL+ImUR+ImBL+ImBR
ImWarpBatch = tf.identity(ImWarpBatch,name=name)
return ImWarpBatch
# generate batch of warped images from batch (bilinear interpolation)
def ImWarpIm(ImBatch,pMtrxBatch,opt,name=None):
with tf.name_scope("ImWarp"):
batchSize = tf.shape(ImBatch)[0]
H,W = opt.H,opt.W
warpGTmtrxBatch = tf.tile(tf.expand_dims(opt.warpGTmtrx,0),[batchSize,1,1])
transMtrxBatch = tf.matmul(warpGTmtrxBatch,pMtrxBatch)
# warp the canonical coordinates
X,Y = np.meshgrid(np.linspace(-1,1,W),np.linspace(-1,1,H))
XYhom = tf.transpose(tf.stack([X.reshape([-1]),Y.reshape([-1]),np.ones([X.size])],axis=1))
XYhomBatch = tf.tile(tf.expand_dims(XYhom,0),[batchSize,1,1])
XYwarpHomBatch = tf.matmul(transMtrxBatch,tf.to_float(XYhomBatch))
XwarpHom,YwarpHom,ZwarpHom = tf.split(XYwarpHomBatch,3,1)
Xwarp = tf.reshape(XwarpHom/ZwarpHom,[batchSize,H,W])
Ywarp = tf.reshape(YwarpHom/ZwarpHom,[batchSize,H,W])
# get the integer sampling coordinates
Xfloor,Xceil = tf.floor(Xwarp),tf.ceil(Xwarp)
Yfloor,Yceil = tf.floor(Ywarp),tf.ceil(Ywarp)
XfloorInt,XceilInt = tf.to_int32(Xfloor),tf.to_int32(Xceil)
YfloorInt,YceilInt = tf.to_int32(Yfloor),tf.to_int32(Yceil)
ImIdx = tf.tile(tf.reshape(tf.range(batchSize),[batchSize,1,1]),[1,H,W])
ImVecBatch = tf.reshape(ImBatch,[-1,tf.shape(ImBatch)[3]])
ImVecBatchOutside = tf.concat([ImVecBatch,tf.zeros([1,tf.shape(ImBatch)[3]])],0)
idxUL = (ImIdx*H+YfloorInt)*W+XfloorInt
idxUR = (ImIdx*H+YfloorInt)*W+XceilInt
idxBL = (ImIdx*H+YceilInt)*W+XfloorInt
idxBR = (ImIdx*H+YceilInt)*W+XceilInt
idxOutside = tf.fill([batchSize,H,W],batchSize*H*W)
def insideIm(Xint,Yint):
return (Xint>=0)&(Xint<W)&(Yint>=0)&(Yint<H)
idxUL = tf.where(insideIm(XfloorInt,YfloorInt),idxUL,idxOutside)
idxUR = tf.where(insideIm(XceilInt,YfloorInt),idxUR,idxOutside)
idxBL = tf.where(insideIm(XfloorInt,YceilInt),idxBL,idxOutside)
idxBR = tf.where(insideIm(XceilInt,YceilInt),idxBR,idxOutside)
# bilinear interpolation
Xratio = tf.reshape(Xwarp-Xfloor,[batchSize,H,W,1])
Yratio = tf.reshape(Ywarp-Yfloor,[batchSize,H,W,1])
ImUL = tf.to_float(tf.gather(ImVecBatchOutside,idxUL))*(1-Xratio)*(1-Yratio)
ImUR = tf.to_float(tf.gather(ImVecBatchOutside,idxUR))*(Xratio)*(1-Yratio)
ImBL = tf.to_float(tf.gather(ImVecBatchOutside,idxBL))*(1-Xratio)*(Yratio)
ImBR = tf.to_float(tf.gather(ImVecBatchOutside,idxBR))*(Xratio)*(Yratio)
ImWarpBatch = ImUL+ImUR+ImBL+ImBR
ImWarpBatch = tf.identity(ImWarpBatch,name=name)
return ImWarpBatch