-
Notifications
You must be signed in to change notification settings - Fork 19.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use a custom pooling function for a model? #2816
Comments
For this particular case, you can just do a max pool of the negation of the If you want other pooling there is the images2neibs that you could use: On Wed, May 25, 2016 at 7:26 PM, matthewmok notifications@github.com
|
Thank you for your reply! Would you mind to provide more technical details on the implementation? I am not sure how to change the input in the tensor in the max pooling layer... Currently, my neural net is set up as follow:
Thanks! |
Use lambda layer to customize your own layer. def min_max_pool2d(x):
max_x = K.pool2d(x, pool_size=(2, 2), strides=(2, 2))
min_x = -K.pool2d(-x, pool_size=(2, 2), strides=(2, 2))
return K.concatenate([max_x, min_x], axis=1) # concatenate on channel
def min_max_pool2d_output_shape(input_shape):
shape = list(input_shape)
shape[1] *= 2
return tuple(shape)
# replace maxpooling layer
cnn.add(Lambda(min_max_pool2d, output_shape=min_max_pool2d_output_shape)) Please try it. |
Thank you for the suggestion! It works except I have to change the output_shape dimension a little since when I was using a 2-by-2 stride, the output width and height will become half.
Thank you once again and please correct me if you catch something is wrong! |
@matthewmok You are correct, I forget about the stride. LOL |
Thank you for your help! |
To make this work for both backends with Keras 2, it has to be
|
I tried to use it instead of max pooling but i have error |
I am new to Keras and trying to implement a convolution neural network for predicting handwritten digits in the MNIST dataset using the theano backend.
I tried with having a MaxPooling2D layer in my model and it gives a good result. I am planning to try on a pooling method that will give the max value and the min value on the feature map (essentially doing two poolings on the feature map). I realized that Keras uses pool2d function from theano and they don't have a implementation of a min pooling.
I am wondering is there any way to implement a custom pooling layer in Keras just like using a custom objective function?
Thanks!
The text was updated successfully, but these errors were encountered: