def get_output_for(self, inputs, **kwargs):
unary, ref = inputs
N, _, H, W = ref.shape
yx = tt.cast(tt.stack(tt.mgrid[0:H, 0:W]), "float32")
grid = tt.alloc(yx[np.newaxis, :, :, :], N, 2, H, W)
stacked = tt.concatenate([grid, ref], axis=1)
def _bilateral(V, R):
o = tt.ones((1, V.shape[1], V.shape[2]), "float32")
norm = tt.sqrt(gaussian_filter(R, o, self.kstd_bf,
self.ref_dim)) + 1e-8
return gaussian_filter(R, V/norm, self.kstd_bf, self.ref_dim,
self.val_dim) / norm
def _step(prev_q, U, ref, normalize=True):
qbf = _bilateral(prev_q, ref,)
qsf = tt.nnet.conv2d(prev_q[np.newaxis, :, :, :],
self.W_spatial, border_mode="half")[0]
q_hat = -self.compat_bf * qbf + -self.compat_spatial * qsf
q_hat = U - q_hat
return softmax(q_hat, axis=0) if normalize else q_hat
def _inference(unary_i, ref_i):
U = tt.log(tt.clip(unary_i, 1e-5, 1))
prev_q = softmax(U, axis=0)
# This is faster than using scan.
for i in range(self.num_iter):
normalize = self.normalize_final_iter or i < self.num_iter-1
prev_q = _step(prev_q, U, ref_i, normalize)
return prev_q
return theano.scan(fn=_inference, sequences=[unary, stacked],
outputs_info=None)[0]
评论列表
文章目录