keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.98k stars 19.47k forks source link

how to normalize input for each patch during convolution #4741

Closed HC-2016 closed 7 years ago

HC-2016 commented 7 years ago

I wanna to normalize (substract mean, divide standard deviation) input in each kernel during convolution. This means a affine transformation before convolution for each kernel. How can I achieve it ? Any idea?

keunwoochoi commented 7 years ago

Something like this? So you can implement a simple layer that does

all_dims=[2, 3] # if theano. Otherwise [1, 2]
mean = K.mean(x, axis=all_dims, keepdims=True)
std = K.std(x, axis=all_dims, keepdims=True)
x = (x - mean) / (std + epsilon)

and put it before a conv layer.

HC-2016 commented 7 years ago

@keunwoochoi Thank you for your attention! It seems that I have to give more details about my problem. For example, Input: (1,224, 224) kernel: (64,5,5) stride: 1 During the calculation of the first feature map, I'd like to do the following operations for each position:

feaMap[0, 0, 0] = conv( (Input[0, 0:5, 0:5] -  mean)/std, kernel[0, :, :] )
feaMap[0, 0, 1] = conv( (Input[0, 0:5, 1:6] -  mean)/std, kernel[0, :, :] )
feaMap[0, 0, 2] = conv( (Input[0, 0:5, 2:7] -  mean)/std, kernel[0, :, :] )

....... where mean is a fix matrix of shape (1, 5, 5), std is a fix matrix of shape (1, 5, 5).

It is great cost if I write my own code to extract each patch, do normalization then conduct convolution. So I just want to edit the conv function to add "substract mean", "divide standard deviation" operation. But it seems difficult to edit the conv function to achieve my goal.

keunwoochoi commented 7 years ago

That sounds like LRN or LCN - local response (contrast) normalization. Is it? Then you can probably find the LRN layer for old version of keras. There used be one, later was removed.

HC-2016 commented 7 years ago

@keunwoochoi They are not the same problems. Someone advise "you could probably copy / edit the CorrMM version of convolutions, and add the normalization of patches after the call to im2col. You may actually have to reverse the role of image and kernel for that. " But it seems difficult......

keunwoochoi commented 7 years ago

Um yeah, is it then ‘Local Response Standardization’ instead of ‘Local Response Normalization’? Isn’t it a mapping of the input slice for each convolution operation? If so, I still think peeking LRN code would help.

On 20Dec 2016, at 20:27, HC-2016 notifications@github.com wrote:

@keunwoochoi https://github.com/keunwoochoi They are not the same problems. Someone advise "you could probably copy / edit the CorrMM version of convolutions, and add the normalization of patches after the call to im2col. You may actually have to reverse the role of image and kernel for that. " But it seems difficult......

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/fchollet/keras/issues/4741#issuecomment-268408718, or mute the thread https://github.com/notifications/unsubscribe-auth/APZ8xWlI3SAFtXNgUMMjtmSRa0quBc9Wks5rKICfgaJpZM4LPFzL.

liangbright commented 7 years ago

@HC-2016 Have you implemented this ?

HC-2016 commented 7 years ago

@liangbright No. To implement this I have to modify the im2col function like Deformable Convolutional Networks.

stale[bot] commented 7 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.