Skip to content
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

SpatialDepthWiseConvolution swaps height and width #1307

Open
diovisgood opened this issue Feb 25, 2018 · 3 comments
Open

SpatialDepthWiseConvolution swaps height and width #1307

diovisgood opened this issue Feb 25, 2018 · 3 comments

Comments

@diovisgood
Copy link

diovisgood commented Feb 25, 2018

I've met a problem with SpatialDepthWiseConvolution
When I pass image, say 4x5 (height=4, width=5), this layer outputs image 5x4.
Proof code:

-- Make model:
local model = nn.SpatialDepthWiseConvolution(3, 1, 3, 3, 1, 1, 1, 1)
-- Generate input: batch = 2, nChannels = 3, height = 4, width = 5
local X = torch.randn(2, 3, 4, 5)
print('X:\n'..tostring(X))
-- Pass input through SpatialDepthWiseConvolution
local Yhat = model:forward(X)
print('Yhat:\n'..tostring(Yhat))

This script outputs:

X: ...
[torch.DoubleTensor of size 2x3x4x5]

Yhat: ...
[torch.DoubleTensor of size 2x3x5x4]

This breaks all the sequence of NN, and model does not work as intended!
Also, this contradicts to documentation, that says:

The most noticiable difference is the output dimension of SpatialConvolution is nOutputPlane x oheight x owidth, while for SpatialDepthWiseConvolution it is (nOutputPlane x nInputPlane) x oheight x owidth.

@diovisgood
Copy link
Author

Unfortunately, I have no time to dig into C-code of THNN implementation of SpatialDepthWiseConvolution.
There is a way to emulate this module using containers Concat and Parallel along with standard SpatialConvolution module:

  local depth_wise_conv = nn.Concat(2)
  for o = 1, nOutputPlane do
    local out = nn.Parallel(2, 2)
    for i = 1, nInputPlane do
      local seq = nn.Sequential()
      local conv = nn.SpatialConvolution(1, 1, kW, kH, dW, dH, pW, pH):noBias()
      conv.weight:copy(weight[o][i])
      seq:add( nn.Reshape(1, inputHeight, inputWidth) )
      seq:add( conv )
      out:add( seq )
    end
    depth_wise_conv:add( out )
  end

But I've spend some time and created a module in pure lua that performs spatial depth wise convolution and (I hope) works as intended. You may find source code and unit tests here:
SpatialDepthWiseConvolution in torch (lua)

I'll be waiting for someone to fix this problem, because low-level implementation should be faster!

@gkanno
Copy link

gkanno commented Jun 4, 2019

How about these ?
gkanno@05da433
gkanno@54b9525

(Are you waiting now ?)

@diovisgood
Copy link
Author

Thank you!
Though I had to migrate to PyTorch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants