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

ndimage.convolve should not use default mode 'reflect' #1

Open
blownhither opened this issue Aug 29, 2020 · 0 comments
Open

ndimage.convolve should not use default mode 'reflect' #1

blownhither opened this issue Aug 29, 2020 · 0 comments

Comments

@blownhither
Copy link

blownhither commented Aug 29, 2020

Problem

im1 = ndimage.convolve(im, np.expand_dims(np.stack([tf1]*3, axis=-1), axis=0))

With the stacking, convolution happens between an image of [B, H, W, 3] and kernels of [1, 3, 3, 3]. Note that both operands have 3 channels. And the default mode of ndimage.convolve is 'reflect'. Reflect is weird as detailed in this link, and would behave like this when both operands have 3 elements on some dimension (use 1d array for illustration):

ndimage.convolve([1, 2, 3], [1, 1, 1]) == array([4, 6, 8])
# if 3 channels are convoluted against 3 channels
ndimage.convolve([R, G, B], [1, 1, 1]) == array([R+R+G, R+G+B, G+B+B])  
# [B, G, R | R, G, B | B, G, R]
#      ^ [1, 1, 1]

This is not doing edge detection consistently over all channels, or over each channel. It is a weird combination that involves things like [R+R+G].

Proposal

If I understand it correctly, it would make more sense if we adopt one of these:

  • use per-channel edge detection
im1 = ndimage.convolve(im, tf1[:, :, :, np.newaxis])      # kernel [1, 3, 3, 1], no wrapping
  • use all-channel edge detection
im1 = ndimage.convolve(im, np.expand_dims(np.stack([tf1]*3, axis=-1), axis=0), mode='wrap')     # wrap as [r,b,g,r,g,b] seqeuence
  • edge detection on luminance

More

This seems to be happening for TF operations as well. May change padding='SAME' to 'VALID'. It is very unlikely that this would affect training though.

im1 = tf.nn.conv2d(im, tf1, strides=[1, 1, 1, 1], padding='SAME')

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

1 participant