-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
border_mode='same' and/or custom zero padding for convolutions #2118
Comments
👍 Agreed that this would be an excellent addition to Theano in light of how common 'same' convolutions are in recent papers. |
I agree it would be good, but I don't have the time. If you want to use If you use directly GpuCorrMM, do it support it? There parameter is there, On Fri, Sep 19, 2014 at 1:07 AM, Madison May notifications@github.com
|
Ideally it would be available in all implementations so I can use whatever is fastest :) I don't think I have the skills to implement this on the GPU (or the time to learn them, to be honest), which is why I made this issue instead of a pull request. But now it's here, so hopefully someone with the same needs and the right skill set will see it! |
Actually if you want to do it for GpuDnnConv, you would have to skill for it :) It's just a matter of computing the right amount of padding to pass in the descriptor. |
That would mean I have to use GpuDnnConv directly though, right? I wouldn't be able to rely on the optimization to swap it in because I like how I can currently try every implementation (legacy, cudnn, conv_gemm and conv_fft) just by setting a flag (well, except cuda-convnet), so in the long term I'd love to see this implemented everywhere. But I guess it's a good first step, I'll look into it if I find some time. I believe I saw that conv_gemm already supports something similar, but it's called So there are a number of ways this could be implemented:
I guess the latter is probably the cleanest approach, but |
Just a quick comment, same is used by scipy convolution. we must have the I also like to have a good "cond2d() + opt" interface. But it will need |
Some implementations include a different algorithm for Now a general problem is that
I think it's not necessary to have the legacy @abergeron, @nouiz: What do you think about extending the
The latter two options abuse I feel reasonably acquainted with the code to work on that (not now, but next month). |
I'm ok with this extension of border_mode. I agree, we need to keep compatibility with numpy semantic, but we can extend it. We can put in the documentation that other system call it pad. I'll probably keep the pad parameter in the op that use it, to don't break user. Just make sure that if both pad and border_mode are provided, they must be equivalent. Otherwise raise an error. |
Yes, I think the different wrappers of existing convolution implementations should provide whatever their implementations offer and not be forced to mimic the |
A simple way to emulate border_mode=same is to simply access a subset of the conv2D output. E.g., for square filter you could define this module:
|
Yes, but that trigger extra computation. It would be better to not do that. On Fri, Sep 11, 2015 at 6:01 PM, jwingit notifications@github.com wrote:
|
border mode is done for some time already. We probably won't implement border_mode=same, so closing. |
Here is how I do this at the moment, but with theano 0.10.0 cuda backend is going to removed. I wonder if there is a reason why same won't be supported in theano using the libgpuarray backed? |
same should work with the new back end. If not, make sure to update Theano
to version 0.9rc3 and update libgpuarray to 0.6.2. if the problem persist,
open a new issue with the full error message.
Le mar. 7 mars 2017 14:33, Ragav Venkatesan <notifications@github.com> a
écrit :
… ` if border_mode == 'same': # this is used typically by VGG.
_out_height = image_shape[2]
_out_width = image_shape[3]
x = (filter_shape[2] - 1) // 2
y = (filter_shape[3] - 1) // 2
border_mode = (x,y)
from theano.sandbox import cuda
if not cuda.dnn.dnn_available():
raise Exception ("cuDNN is needed for this type of convolution.")
else:
if subsample[0] == 1 and subsample[1] == 1:
self.out = cuda.dnn.dnn_conv (
img = input ,
kerns = filters,
border_mode= border_mode,
conv_mode = 'cross'
)
else:
self.out = cuda.dnn.dnn_conv (
img = input ,
kerns = filters,
border_mode= border_mode,
subsample = subsample,
conv_mode = 'cross'
)`
Here is how I do this at the moment, but with theano 0.10.0 cuda backend
is going to removed. I wonder if there is a reason why same won't be
supported in theano using the libgpuarray backed?
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#2118 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AALC-4RRCGMV9bZUd12qrfQw6twnib7_ks5rjbEigaJpZM4Cjful>
.
|
Looks like you'd just want to use |
In the "same" mode (zero padding) it would be nice to have something like "same donut" and "same cylinder" to choose "top/bottom" ("cylinder-vertical") of "side to side" ("cylinder-horizontal") and instead of zeros it would "roll" the extremes of the images ... For example the top zeros would be the values on the bottom of the image. This would be nice in multivariables time-series problems , where I have better results than using LSTM ... (Instead of a CNN looking to 2D images it looks into a 2D map of several values through time ). The kernel map (CNN "neuron") instead of making correlations with "zeros", while working on the edge of the multivariable time-serie map, it would be nice to make correlations with with the variables from top to bottom... Well it is difficult to explain ... Where can I see the source code of keras tensorflow where they put zeros around the image ? so I can make a new version for me to put other things instead of zeros ? |
#5827 is about that feature request. |
On CUDA GPUs, the actual implementation is usually in cuDNN, which is a closed-source library provided by Nvidia. Libraries will usually have fall-back implementations that you could modify (often based on im2col and gemm), but are a bit slower. The easiest workaround will be to pad your network input (with wraparound) and then use unpadded ("valid") convolutions throughout. |
yes nice ideia ... I will build some custom layer with using keras lambda function to transform the maps and include the lines on top and bottom of the feature with the values of the bottom and top lines of the feature to create an horizontal cylinder ... according with the size of the filter used in the next layer (half of it...). Nice Ideia ! thanks . this way the kernel (cnn neuron) will travel the features maps like in the asteroid game :-) what goes up appear down :-) in a multivariable time series it will correlate all variables with all ... and not with zeros (the ones on the top and bottom) |
Several convolution implementations (cuda-convnet, cudnn, ...) provide a way to implicitly pad the input with a border of zeros. This mechanism is then used to implement the various 'border modes' (i.e. full, valid, same).
Currently Theano's
theano.tensor.nnet.conv2d
only allows to set theborder_mode
, and as far as I can tell all current implementations only supportvalid
andfull
, but notsame
.The main advantage of
border_mode='same'
is that the output feature maps are the same size as the input, so this makes it a lot easier to design architectures with lots of stacked layers.Implementing a
same
convolution in Theano is of course possible by doing the padding manually and then using avalid
convolution, or by using afull
convolution and then slicing the output. But neither of these methods is optimal.Considering that
same
convolutions are pretty commonly used now in state of the art networks (Krizhevky's 2012 ImageNet entry, VGG, GoogLeNet, ...), it would be useful to have proper support for this in Theano as well.The easiest way to achieve this would be to make
theano.tensor.nnet.conv2d
supportborder_mode='same'
. The most flexible way would be to explicitly expose apad
argument, like cuda-convnet and cudnn. The most convenient way would be to support both :)I don't really have any code to contribute unfortunately, but I wanted to bring this up in case someone else is interested in this and wants to work on it.
TODO:
The text was updated successfully, but these errors were encountered: