Skip to content

Speed up mean_filter2d with depthwise_conv2d #235

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 10 commits into from
May 31, 2019

Conversation

WindQAQ
Copy link
Member

@WindQAQ WindQAQ commented May 3, 2019

Closes #234. Will create another PR to clean up median_filter2d.

@WindQAQ WindQAQ requested a review from a team as a code owner May 3, 2019 08:41
Copy link
Member

@facaiy facaiy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pad_left = (filter_width - 1) // 2
pad_right = filter_width - 1 - pad_left
paddings = [[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]
return tf.pad(image, paddings, mode=mode, constant_values=constant_values)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add an unit test for even shape, say (4, 4), and make sure the result is the same with scipy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we import scipy for test here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could add a test case to check whether the output of explicitly zero padding is the same with the one of native "SAME" padding supported by depthwise_conv2d if we really do not want to compare the result produced by other packages.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Tzu-Wei, perhaps we don't have to import scipy. Can we rename test_reflect_padding to test_reflect_padding_with_3x3_filter, and create an new test_reflect_padding_with_4x4_filter in the same way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Facai, after a rough test, I found the padding method (or the anchor of even-sized kernel) of tensorflow seems different from the one of scipy. Here is the testing notbook. Take 4x4 filter for example. The explicit (zero) padding could yield the same result with implicit SAME padding supported by depthwise_conv2d. However, I have to adjust origin argument in scipy's implement to (-1, -1) so that it could produce the same result. In this case, should we follow the scipy's implementation or match the implicit padding in tensorflow?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW,
scipy's reflect padding => (c b a | a b c d | d c b)
TF's reflect padding => (d c b | a b c d | c b a)
TF's reflect padding = scipy's mirror padding
scipy's reflect padding = TF's symmetric padding

Seems that they have different meaning...
https://www.tensorflow.org/api_docs/python/tf/pad

Copy link
Member

@facaiy facaiy May 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Tzu-Wei,

  1. If it's not easy to be compatible with scipy's implementation, we can give out all details about how we calculate it in the document.
  2. As for padding mode, I think tf.pad keeps consistent with np.pad. And similarity, we can either keep compatible with scipy, or write a detailed document.

Both are fine for me. cc @seanpmorgan Sean, what's your opinion?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure :-) In either way, it should be more docs there so let me write some details in this version first.

@facaiy
Copy link
Member

facaiy commented May 5, 2019

@Mainak431 Mainak, as the author, would you like to take a look? Thanks.

@facaiy facaiy added the image label May 5, 2019
@Mainak431
Copy link
Contributor

@WindQAQ. I think we should only take 4D tensor. If anybody wants to process a single image. He/she should reshape the tensor to (1, image-shape). This pattern is followed in many tf operations that supports both single and batch processing. I think that trend should be followed here too.

@Mainak431
Copy link
Contributor

Yes. Image operations support this. But, many other operations i have come through does not support this. It seems there is no rule for the same. Ok.

@Mainak431
Copy link
Contributor

Mainak431 commented May 5, 2019

Can you please try your function with conv2d layer, activation layer, dropout and other basic layers by creating a small tf architecture. And ensure that it is working as expected even in the training of nn. A colab notebook will be helpful. As, the batch processing operation aims at direct usage of this in the nn architecture. Thanks.

@WindQAQ
Copy link
Member Author

WindQAQ commented May 5, 2019

@WindQAQ. I think we should only take 4D tensor. If anybody wants to process a single image. He/she should reshape the tensor to (1, image-shape). This pattern is followed in many tf operations that supports both single and batch processing. I think that trend should be followed here too.

Ok, @facaiy What's your thought on this. I'd vote for that the input can be either 3-D or 4-D.

Can you please try your function with conv2d layer, activation layer, dropout and other basic layers by creating a small tf architecture. And ensure that it is working as expected even in the training of nn. A colab notebook will be helpful. As, the batch processing operation aims at direct usage of this in the nn architecture. Thanks.

I'll run a toy model on MNIST dataset when I'm back to the office.

@Mainak431
Copy link
Contributor

Just out of curiosity, what happens to a 2D grayscale image. Batch of this kind of images will look like a 3D image. I think either this should be explicitly mentioned in the documentation. Or we should consider taking only 4D tensors.

@WindQAQ
Copy link
Member Author

WindQAQ commented May 5, 2019

Just out of curiosity, what happens to a 2D grayscale image. Batch of this kind of images will look like a 3D image. I think either this should be explicitly mentioned in the documentation. Or we should consider taking only 4D tensors.

I think we have already mentioned this in the documentation :-) Users have to make sure that the last axis is the channel axis as the one in the core TF.

Args:
image: Either a 3-D `Tensor` of shape `[height, width, channels]`,
or a 4-D `Tensor` of shape `[batch_size, height, width, channels]`.

@Mainak431
Copy link
Contributor

Mainak431 commented May 5, 2019 via email

@facaiy
Copy link
Member

facaiy commented May 7, 2019

Sorry for the delay, @WindQAQ @Mainak431

I think we should keep consistency with tf.image: function works on either a single image (image is a 3-D Tensor), or a batch of images (image is a 4-D Tensor).

@facaiy
Copy link
Member

facaiy commented May 23, 2019

@WindQAQ Tzu-Wei, could you update the PR when time allows :-)

@WindQAQ
Copy link
Member Author

WindQAQ commented May 23, 2019

Thanks for reminding, facai. I'm currently workingon this :-)

@WindQAQ
Copy link
Member Author

WindQAQ commented May 24, 2019

@facaiy, hi facai, I make some changes:

  1. Avoid loss of precision
  2. Refactor test cases according to https://github.com/tensorflow/tensorflow/blob/b14c390fc869b63fd2b1a4e6f4477ce410b9383e/tensorflow/python/kernel_tests/conv_ops_3d_test.py
  3. Add a test case with channels of None

As for even-sized kernel test, should we use or import scipy for test here? Thanks for the suggestion!

@facaiy facaiy self-assigned this May 31, 2019
Copy link
Member

@facaiy facaiy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to merge the PR, and then we can fix all unsolved comments, if any, in the following PRs.

@facaiy facaiy merged commit e49361b into tensorflow:master May 31, 2019
@WindQAQ WindQAQ deleted the speed_up_mean_filter2d branch May 31, 2019 09:40
@bot-of-gabrieldemarmiesse

@Mainak431

You are owner of some files modified in this pull request.
Would you kindly review the changes whenever you have the time to?
Thank you very much.

@seanpmorgan
Copy link
Member

You are owner of some files modified in this pull request.
Would you kindly review the changes whenever you have the time to?
Thank you very much.

@gabrieldemarmiesse this doesn't happen often but wondering if you see any reason this happens?

@gabrieldemarmiesse
Copy link
Member

It's the first time I hear about it. Do you have other examples so I can see if there is a pattern?

In the yaml, the only condition starting the bot is:

on:
  pull_request:
    types: [opened]

So that makes me quite perplex that something happens after a PR is closed.

@gabrieldemarmiesse
Copy link
Member

Sorry @seanpmorgan I've seriously no idea what causes this. If that happens too many time I'll take a much longer look.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Speed up mean_filter2d with depthwise convolution
8 participants