Skip to content

Conversation

@utkuozbulak
Copy link
Contributor

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 padding is an int: it pads all sides.
if padding is a tuple with two elements: it pads left/right and top/bottom respectively.
if padding is 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 of
pad(img, padding, fill=0, padding_mode='constant') because when the padding_mode is 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:
test_image

pad(img, 20, padding_mode = 'reflect')
20reflect

pad(img, (0, 50), padding_mode = 'edge')
0-50-edge

pad(img, (1, 10, 20, 30), padding_mode = 'symmetric')
1-10-20-30

Grayscale test image:
cat

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

5-10-20-50-sym

Copy link
Member

@fmassa fmassa left a 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] = 255

and 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.

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.

@utkuozbulak
Copy link
Contributor Author

utkuozbulak commented Apr 4, 2018

Inspired from what you suggested as the test case Included the following test:

1- Create an empty tensor (image) with zeros
image

2- Change leftmost column to one.
image

3- Pad image with a constant value with a length of 1
image

4- Pad image with edge, symmetric or reflect (3 different cases, below only illustrated symmetric version)
image

Check the values of leftmost middle crop with the length of six (highlighted in the box above), if it is indeed symmetric. Then check the shape of the matrix if it adds up to what we are expecting (Here, I also test if the padding is a tuple instead of a single value in different versions of padding).

Copy link
Member

@fmassa fmassa left a 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!

@fmassa fmassa merged commit f124509 into pytorch:master Apr 4, 2018
# 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.

rajveerb pushed a commit to rajveerb/vision that referenced this pull request Nov 30, 2023
* [RNN-T] bucketing sampler fix

drop random samples, instead of replacing them with longes sequences

* remove samples without repetitions
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

Successfully merging this pull request may close these issues.

3 participants