示例#1
0
def group_pixels(ctr, offsets):
    """
    Gives each pixel in the image an instance id.

    Args:
        ctr (Tensor): A Tensor of shape [K, 2] where K is the number of center points. The order of second dim is (y, x).
        offsets (Tensor): A Tensor of shape [2, H, W] of raw offset output, where N is the batch size,
            for consistent, we only support N=1. The order of second dim is (offset_y, offset_x).

    Returns:
        Tensor: A Tensor of shape [1, H, W], ins_id is 1, 2, ...
    """
    height, width = offsets.shape[-2:]
    y_coord = paddle.arange(height, dtype=offsets.dtype).reshape([1, -1, 1])
    y_coord = paddle.concat([y_coord] * width, axis=2)
    x_coord = paddle.arange(width, dtype=offsets.dtype).reshape([1, 1, -1])
    x_coord = paddle.concat([x_coord] * height, axis=1)
    coord = paddle.concat([y_coord, x_coord], axis=0)

    ctr_loc = coord + offsets
    ctr_loc = ctr_loc.reshape((2, height * width)).transpose((1, 0))

    # ctr: [K, 2] -> [K, 1, 2]
    # ctr_loc = [H*W, 2] -> [1, H*W, 2]
    ctr = ctr.unsqueeze(1)
    ctr_loc = ctr_loc.unsqueeze(0)

    # distance: [K, H*W]
    distance = paddle.norm((ctr - ctr_loc).astype('float32'), axis=-1)

    # finds center with minimum distance at each location, offset by 1, to reserve id=0 for stuff
    instance_id = paddle.argmin(distance, axis=0).reshape(
        (1, height, width)) + 1

    return instance_id
示例#2
0
 def run(place):
     paddle.disable_static(place)
     np.random.seed(2021)
     numpy_input = (np.random.random(self.dims)).astype(self.dtype)
     tensor_input = paddle.to_tensor(numpy_input)
     numpy_output = np.argmin(numpy_input, axis=self.axis)
     paddle_output = paddle.argmin(tensor_input, axis=self.axis)
     self.assertEqual(np.allclose(numpy_output, paddle_output.numpy()),
                      True)
     paddle.enable_static()
示例#3
0
 def forward(self, inputs):
     return paddle.argmin(inputs)
示例#4
0
 def test_argmin_dtype_type():
     data = paddle.static.data(name="test_argmin",
                               shape=[10],
                               dtype="float32")
     output = paddle.argmin(x=data, dtype=None)
示例#5
0
 def test_argmin_axis_type():
     data = paddle.static.data(name="test_argmin",
                               shape=[10],
                               dtype="float32")
     output = paddle.argmin(x=data, axis=1.2)
示例#6
0
 def test_argmin_x_type():
     x2 = [1, 2, 3]
     output = paddle.argmin(x=x2)
示例#7
0
 def forward(self, inputs):
     """
     forward
     """
     x = paddle.argmin(inputs, axis=self.axis, keepdim=self.keepdim)
     return x
示例#8
0
def min(self, dim, keepdim=None):
    return pd_min(self, dim, keepdim), paddle.argmin(self, dim, keepdim)