-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Add reflect, symmetric and edge padding #460
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
Conversation
fmassa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome, thanks a lot!
Apart from some doc changes, I think it would be great if you could add some very simple tests for it.
Something simple, for example with a dummy image like
a = torch.zeros(10, 10, 3)
a[:, :5] = 255and check that the padded versions behave as expected. What do you think?
| this is the padding for the left, top, right and bottom borders | ||
| respectively. | ||
| fill: Pixel fill value. Default is 0. If a tuple of | ||
| length 3, it is used to fill R, G, B channels respectively. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torchvision/transforms/functional.py
Outdated
| fill: Pixel fill value for constant fill. Default is 0. If a tuple of | ||
| length 3, it is used to fill R, G, B channels respectively. | ||
| padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
fmassa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome, thanks a lot for the contribution!
| # First 6 elements of leftmost edge in the middle of the image, values are in order: | ||
| # edge_pad, edge_pad, edge_pad, constant_pad, constant value added to leftmost edge, 0 | ||
| edge_middle_slice = np.asarray(edge_padded_img).transpose(2, 0, 1)[0][17][:6] | ||
| assert np.all(edge_middle_slice == np.asarray([200, 200, 200, 200, 255, 0])) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
* [RNN-T] bucketing sampler fix drop random samples, instead of replacing them with longes sequences * remove samples without repetitions




Added reflect, symmetric and edge padding options.
This option was requested by #400. PR #213 was sent to include the same functionality but still awaiting response from the author since Sep 20, 2017.
The new padding options also work the same way with how it is for the constant one.
If
paddingis an int: it pads all sides.if
paddingis a tuple with two elements: it pads left/right and top/bottom respectively.if
paddingis a tuple with four elements: it pads left, top, right, bottom borders respectively.It also does not break or change the behavior of existing code.
In #213 @alykhantejani suggested that the function should be
pad(img, padding, padding_mode='constant', fill=0)instead ofpad(img, padding, fill=0, padding_mode='constant')because when thepadding_modeis not constant, fill is irrelevant/not used. This is correct, but doing so might break existing code for some people so I did not change the positions.Below are some tests:

RGB test image:
pad(img, 20, padding_mode = 'reflect')pad(img, (0, 50), padding_mode = 'edge')pad(img, (1, 10, 20, 30), padding_mode = 'symmetric')Grayscale test image:

pad(img, (5,10,20,50), padding_mode = 'symmetric')