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
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()
def forward(self, inputs): return paddle.argmin(inputs)
def test_argmin_dtype_type(): data = paddle.static.data(name="test_argmin", shape=[10], dtype="float32") output = paddle.argmin(x=data, dtype=None)
def test_argmin_axis_type(): data = paddle.static.data(name="test_argmin", shape=[10], dtype="float32") output = paddle.argmin(x=data, axis=1.2)
def test_argmin_x_type(): x2 = [1, 2, 3] output = paddle.argmin(x=x2)
def forward(self, inputs): """ forward """ x = paddle.argmin(inputs, axis=self.axis, keepdim=self.keepdim) return x
def min(self, dim, keepdim=None): return pd_min(self, dim, keepdim), paddle.argmin(self, dim, keepdim)