nn.SpatialPeriodicPadding

75 views
Skip to first unread message

Alexander Weiss

unread,
Dec 12, 2016, 8:48:55 PM12/12/16
to torch7
I'm working with images that have periodic boundary conditions, meaning that objects wrap around from the right boundary of the image to the left.  It would be really great if there were an nn.SpatialPeriodicPadding module so that I could insert periodic convolutions directly into a neural network.  If anyone has already created such a module or is willing to create one, that would be extremely useful.

On the other hand, I would be happy to create such a module myself if anyone can provide me with some guidelines for inserting the new module into torch.  It looks like it would be pretty easy to just modify the existing code for nn.SpatialReflectionPadding or nn.SpatialReplicationPadding, but I really don't understand the inner workings of the torch code well enough to then incorporate that code into the torch library.

Alexander Weiss

unread,
Dec 12, 2016, 9:05:39 PM12/12/16
to torch7
To partially address my own post, it looks like guidelines for creating new nn modules already exists:

Jeffrey Hartmann

unread,
Dec 13, 2016, 11:43:55 AM12/13/16
to torch7 on behalf of Alexander Weiss
My suggestion is that you take a look at some torch modules that are distributed out of tree.  One I used recently as a template for work I was doing was the spatial transformers module.  It is a good example of a standalone loadable neural network module with both CUDA and CPU implementations: https://github.com/qassemoquab/stnbhwd

Making a loadable library independent of torch will help get you more familiar with the inner workings of torch.  After you have built a standalone library, it should be much easier for you to convert that to a pull request against the main torch project.

Best,
-Jeff

On Mon, Dec 12, 2016 at 8:48 PM, Alexander Weiss via torch7 <torch7+APn2wQcsaR8bcOeppHpjYu30P...@googlegroups.com> wrote:
I'm working with images that have periodic boundary conditions, meaning that objects wrap around from the right boundary of the image to the left.  It would be really great if there were an nn.SpatialPeriodicPadding module so that I could insert periodic convolutions directly into a neural network.  If anyone has already created such a module or is willing to create one, that would be extremely useful.

On the other hand, I would be happy to create such a module myself if anyone can provide me with some guidelines for inserting the new module into torch.  It looks like it would be pretty easy to just modify the existing code for nn.SpatialReflectionPadding or nn.SpatialReplicationPadding, but I really don't understand the inner workings of the torch code well enough to then incorporate that code into the torch library.

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+unsubscribe@googlegroups.com.
To post to this group, send email to tor...@googlegroups.com.
Visit this group at https://groups.google.com/group/torch7.
For more options, visit https://groups.google.com/d/optout.

Alexander Weiss

unread,
Dec 13, 2016, 6:47:10 PM12/13/16
to torch7
@Jeff:  Thanks for the suggestion.  I took a look at the spatial transformer code, but the CMakeLists.txt was too scary for me.  Honestly, I've always been confused by CMake, but linking all the torch libraries by hand would probably be even worse.  For this reason, I decided to just add my implementation of SpatialPeriodicPadding directly into the full torch library.

I've gotten things working on the CPU side, but I'm having trouble with the GPU.  My strategy was to look for every reference to SpatialReflectionPadding within the torch library, and just replicate it for SpatialPeriodicPadding.  Unfortunately, I must have missed something.  Here's what I did:

1)  Created SpatialPeriodicPadding.lua, SpatialPeriodicPadding.c, SpatialPeriodicPadding.cu
2)  Placed SpatialPeriodicPadding.lua into torch/extra/nn and torch/install/share/lua/5.1/nn  (I don't know what the difference is between these two directories.)
3)  Placed SpatialPeriodicPadding.c into torch/extra/nn/lib/THNN/generic
4)  Placed SpatialPeriodicPadding.cu into torch/extra/cunn/lib/THCUNN

5)  Modified torch/extra/nn/lib/THNN/THNN.h
6)  Modified torch/extra/cunn/THCUNN_h.lua
7)  Modified torch/extra/nn/lib/THNN/init.c
8)  Modified torch/extra/cunn/lib/THCUNN/THCUNN.h

9)  From within torch/extra/nn, ran:  luarocks make rocks/nn-scm-1.rockspec
10)  From within torch/extra/cunn, ran:  luarocks make rocks/cunn-scm-1.rockspec

Unlike for SpatialReflectionPadding, it looks like no .cu.o files were created for SpatialPeriodicPadding.  From within the Torch REPL, I can access nn.SpatialPeriodicPadding() and use it to forward float and double tensors.  I can also convert it to a cuda module:  nn.SpatialPeriodicPading():cuda().  However, I can't forward cuda tensors.  Here's the error:

torch/install/share/lua/5.1/nn/SpatialPeriodicPadding.lua:14: attempt to call field 'SpatialPeriodicPadding_updateOutput' (a nil value)



On Tuesday, December 13, 2016 at 11:43:55 AM UTC-5, Jeffrey Hartmann wrote:
My suggestion is that you take a look at some torch modules that are distributed out of tree.  One I used recently as a template for work I was doing was the spatial transformers module.  It is a good example of a standalone loadable neural network module with both CUDA and CPU implementations: https://github.com/qassemoquab/stnbhwd

Making a loadable library independent of torch will help get you more familiar with the inner workings of torch.  After you have built a standalone library, it should be much easier for you to convert that to a pull request against the main torch project.

Best,
-Jeff
On Mon, Dec 12, 2016 at 8:48 PM, Alexander Weiss via torch7 <torch7+APn2wQcsaR8bcOeppHpjYu30PfpJTwn3luLMBUUyH0EVaRyRp1q7UJHGY@googlegroups.com> wrote:
I'm working with images that have periodic boundary conditions, meaning that objects wrap around from the right boundary of the image to the left.  It would be really great if there were an nn.SpatialPeriodicPadding module so that I could insert periodic convolutions directly into a neural network.  If anyone has already created such a module or is willing to create one, that would be extremely useful.

On the other hand, I would be happy to create such a module myself if anyone can provide me with some guidelines for inserting the new module into torch.  It looks like it would be pretty easy to just modify the existing code for nn.SpatialReflectionPadding or nn.SpatialReplicationPadding, but I really don't understand the inner workings of the torch code well enough to then incorporate that code into the torch library.

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+un...@googlegroups.com.

Alexander Weiss

unread,
Dec 15, 2016, 10:58:14 AM12/15/16
to torch7
When I ran luarocks make rocks/cunn-scm-1.rockspec, I thought it had completed without error.  However, maybe I was wrong about that.  Upon running it again, I now get the following error:

CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:108 (message):
  Could NOT find CUDA (missing: CUDA_INCLUDE_DIRS CUDA_CUDART_LIBRARY) (found
  suitable version "7.5", minimum required is "6.5")

I get this error regardless of whether or not I include my modifications to the torch library.  On the other hand, when I install cunn using luarocks install cunn, there are no errors.  What am I doing wrong?

Jeffrey Hartmann

unread,
Dec 15, 2016, 11:16:12 AM12/15/16
to torch7 on behalf of Alexander Weiss
luarocks install cunn will not use the exact same version as you have on disk, it fetches from what is published on the rocks server.  You might do a git pull to make sure you are using the latest and greatest, perhaps this is an already resolved issue.

If not you will have to figure out why cmake is not finding things properly.  You can pass some arguments to have it print debug information to the console (I always google for the arguments don't remember them off hand), that would be the first step of what you would have to do to figure it out if just updating the code doesn't fix things.

I still believe that building an out of tree module is an easier path as a first step for a new developer to torch.  There should be minimal cmake changes required (I just pulled in the cmake code from cunn to find the local device into my module).  The stn package I mentioned just compiles init.c and init.cu and you #include the code you need from there.  You really shouldn't have to do much at all.

If you think it would be helpful I can maybe look at building a template for an out of tree module and putting it on github this weekend.

Best,
-Jeff


On Mon, Dec 12, 2016 at 8:48 PM, Alexander Weiss via torch7 <torch7+APn2wQcsaR8bcOeppHpjYu30PfpJTwn3luLMBUUyH0EVaRyRp1q7UJ...@googlegroups.com> wrote:
I'm working with images that have periodic boundary conditions, meaning that objects wrap around from the right boundary of the image to the left.  It would be really great if there were an nn.SpatialPeriodicPadding module so that I could insert periodic convolutions directly into a neural network.  If anyone has already created such a module or is willing to create one, that would be extremely useful.

On the other hand, I would be happy to create such a module myself if anyone can provide me with some guidelines for inserting the new module into torch.  It looks like it would be pretty easy to just modify the existing code for nn.SpatialReflectionPadding or nn.SpatialReplicationPadding, but I really don't understand the inner workings of the torch code well enough to then incorporate that code into the torch library.

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+un...@googlegroups.com.
To post to this group, send email to tor...@googlegroups.com.
Visit this group at https://groups.google.com/group/torch7.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+unsubscribe@googlegroups.com.

Alexander Weiss

unread,
Dec 15, 2016, 11:27:22 AM12/15/16
to torch7
@Jeff:  I hesitate to tell you how to spend your weekend, but if you do create such a template, I think that would be a great service to not just me but the whole Torch community.  You should definitely add a link to the Torch wiki, whenever you have time to complete it.



On Thursday, December 15, 2016 at 11:16:12 AM UTC-5, Jeffrey Hartmann wrote:
luarocks install cunn will not use the exact same version as you have on disk, it fetches from what is published on the rocks server.  You might do a git pull to make sure you are using the latest and greatest, perhaps this is an already resolved issue.

If not you will have to figure out why cmake is not finding things properly.  You can pass some arguments to have it print debug information to the console (I always google for the arguments don't remember them off hand), that would be the first step of what you would have to do to figure it out if just updating the code doesn't fix things.

I still believe that building an out of tree module is an easier path as a first step for a new developer to torch.  There should be minimal cmake changes required (I just pulled in the cmake code from cunn to find the local device into my module).  The stn package I mentioned just compiles init.c and init.cu and you #include the code you need from there.  You really shouldn't have to do much at all.

If you think it would be helpful I can maybe look at building a template for an out of tree module and putting it on github this weekend.

Best,
-Jeff
Message has been deleted

Alexander Weiss

unread,
Jan 6, 2017, 11:47:48 AM1/6/17
to torch7
I did eventually get this working.  The problem was, as Jeff stated above, that I was not working with the most recent nn/cunn code.  I was using luarocks install rather than cloning from the github repo.

Since nn.SpatialPeriodicPadding is a general purpose module, I would like to add it to the official torch library.  I've never contributed to a project of this size, so I would really appreciate some help.  Do I need to separately fork nn and cunn, make the modifications on each, and then submit two separate pull requests?
Reply all
Reply to author
Forward
0 new messages