def lecun_lcn(input, kernel_size=9, threshold=1e-4, use_divisor=False):
"""
Yann LeCun's local contrast normalization
Orginal code in Theano by: Guillaume Desjardins
:param input:
:param kernel_size:
:param threshold:
:param use_divisor:
:return:
"""
input_shape = (input.shape[0], 1, input.shape[1], input.shape[2])
input = input.reshape(input_shape).astype(floatX)
X = T.tensor4(dtype=floatX)
filter_shape = (1, 1, kernel_size, kernel_size)
filters = gaussian_filter(kernel_size).reshape(filter_shape)
filters = shared(_asarray(filters, dtype=floatX), borrow=True)
convout = conv2d(input=X,
filters=filters,
input_shape=input.shape,
filter_shape=filter_shape,
border_mode='half')
new_X = X - convout
if use_divisor:
# Scale down norm of kernel_size x kernel_size patch
sum_sqr_XX = conv2d(input=T.sqr(T.abs_(new_X)),
filters=filters,
input_shape=input.shape,
filter_shape=filter_shape,
border_mode='half')
denom = T.sqrt(sum_sqr_XX)
per_img_mean = denom.mean(axis=[2, 3])
divisor = T.largest(per_img_mean.dimshuffle(0, 1, 'x', 'x'), denom)
divisor = T.maximum(divisor, threshold)
new_X = new_X / divisor
new_X = new_X.dimshuffle(0, 2, 3, 1)
new_X = new_X.flatten(ndim=3)
f = function([X], new_X)
return f(input)
评论列表
文章目录