有几种方法可以在 PyTorch 中执行可区分的裁剪。
让我们以 2D 为例:
>>> x1, y1, x2, y2 = torch.randint(0, 9, (4,))
(tensor(7), tensor(3), tensor(5), tensor(6))
>>> x = torch.randint(0, 100, (9,9), dtype=float, requires_grad=True)
tensor([[18., 34., 28., 41., 1., 14., 77., 75., 23.],
[62., 33., 64., 41., 16., 70., 47., 45., 19.],
[20., 69., 5., 51., 1., 16., 20., 63., 52.],
[51., 25., 8., 30., 40., 67., 41., 27., 33.],
[36., 6., 95., 53., 69., 84., 51., 42., 71.],
[46., 72., 88., 82., 71., 75., 86., 36., 15.],
[66., 19., 58., 50., 91., 28., 7., 83., 4.],
[94., 50., 34., 34., 92., 45., 48., 97., 76.],
[80., 34., 19., 13., 77., 77., 51., 15., 13.]], dtype=torch.float64,
requires_grad=True)
给定x1,x2(分别是y1,y2 高度维度(分别是宽度维度)上的补丁索引边界。您可以使用组合获得对应的坐标网格torch.arange 和 torch.meshgrid 的:
>>> sorted_range = lambda a, b: torch.arange(a, b) if b >= a else torch.arange(b, a)
>>> xi, yi = sorted_range(x1, x2), sorted_range(y1, y2)
(tensor([3, 4, 5, 6]), tensor([5]))
>>> i, j = torch.meshgrid(xi, yi)
(tensor([[3],
[4],
[5],
[6]]),
tensor([[5],
[5],
[5],
[5]]))
使用该设置,您可以提取和替换 x 的补丁。
-
您可以通过直接索引x 来提取补丁:
>>> patch = x[i, j].reshape(len(xi), len(yi))
tensor([[67.],
[84.],
[75.],
[28.]], dtype=torch.float64, grad_fn=<ViewBackward>)
这是用于说明目的的掩码:
tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=torch.float64,
grad_fn=<IndexPutBackward>)
-
您可以将x 中的值替换为使用torch.Tensor.index_put 对补丁进行某些转换的结果:
>>> values = 2*patch
tensor([[134.],
[168.],
[150.],
[ 56.]], dtype=torch.float64, grad_fn=<MulBackward0>)
>>> x.index_put(indices=(i, j), values=values)
tensor([[ 18., 34., 28., 41., 1., 14., 77., 75., 23.],
[ 62., 33., 64., 41., 16., 70., 47., 45., 19.],
[ 20., 69., 5., 51., 1., 16., 20., 63., 52.],
[ 51., 25., 8., 30., 40., 134., 41., 27., 33.],
[ 36., 6., 95., 53., 69., 168., 51., 42., 71.],
[ 46., 72., 88., 82., 71., 150., 86., 36., 15.],
[ 66., 19., 58., 50., 91., 56., 7., 83., 4.],
[ 94., 50., 34., 34., 92., 45., 48., 97., 76.],
[ 80., 34., 19., 13., 77., 77., 51., 15., 13.]],
dtype=torch.float64, grad_fn=<IndexPutBackward>)