Skip to content

Conversation

zoooo0820
Copy link
Contributor

@zoooo0820 zoooo0820 commented Apr 26, 2022

PR types

New features

PR changes

APIs

Describe

Add new API paddle.vision.transforms.RandomErasing and paddle.vision.transforms.erase. These two APIs are used to erase pixels from a random selected area in given image.

The DOC pr is : #4071

@paddle-bot-old
Copy link

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@@ -1297,6 +1298,144 @@ def _apply_image(self, img):
self.center, self.fill)


class RandomErasing(BaseTransform):
"""Erase the pixels in a rectangle region selected randomly. This transform only
supports Tensor.
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove "This transform only supports Tensor"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done,同时修改了class中其他文档的错误。

pil_img = Image.fromarray(np_img).convert('RGB')

F.erase(np_img, 10, 10, 5, 5, 0, inplace=True)
np.testing.assert_equal(np_img[12, 12, 0], 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

需要测试所有值的正确性,而非一个值。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

np.testing.assert_equal(np_img[12, 12, 0], 0)

pil_result = F.erase(pil_img, 10, 10, 5, 5, 0)
np.testing.assert_equal(np.array(pil_result)[12, 12, 0], 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

需要测试输入是paddle.Tensor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, 新增了Tensor类型输入的测试用例

else:
value = self.value
if value is not None and not (len(value) == 1 or
len(value) == img.shape[-3]):
Copy link
Contributor

Choose a reason for hiding this comment

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

这里img.shape[-3]是不是只是[c,h,w]的场景

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

tensor_img = paddle.randn((3, 28, 28), dtype='float32')

F.erase(np_img, 10, 10, 5, 5, 0, inplace=True)
np.testing.assert_equal(np_img[10:15, 10:15, :], 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

检查全图吧,检查局部并不完备

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

qingqing01
qingqing01 previously approved these changes Apr 28, 2022
Copy link
Contributor

@qingqing01 qingqing01 left a comment

Choose a reason for hiding this comment

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

LGTM.

Remember to add Chinese documents later.

Copy link
Collaborator

@sneaxiy sneaxiy left a comment

Choose a reason for hiding this comment

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

LG API

img_area = h * w
log_ratio = np.log(ratio)
for _ in range(10):
erase_area = np.random.uniform(*scale) * img_area
Copy link
Contributor

Choose a reason for hiding this comment

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

这里不使用paddle原生api,而大量使用numpy的原因是什么?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个函数的作用在于确定erase函数执行时的参数数值,用numpy或者python原生数值处理主要有以下几个原因:

  • 这里只需要数值/标量的结果即可,且不需要反向梯度。
  • erase函数本身同时包含了numpy / paddle两种backends的实现。paddle api是针对Tensor进行处理,在numpy backends的函数中,需要额外引入一些Tensor和numpy/scalar的转化步骤。

Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@qingqing01 qingqing01 merged commit 20286ae into PaddlePaddle:develop May 5, 2022
@zoooo0820 zoooo0820 deleted the random_erasing branch May 5, 2022 11:01
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.

6 participants