gaussian.py 5.53 KB
Newer Older
dingchang's avatar
dingchang committed
1
# Copyright (c) OpenMMLab. All rights reserved.
2
3
from typing import List, Tuple

4
5
import numpy as np
import torch
6
from torch import Tensor
7
8


9
def gaussian_2d(shape: Tuple[int, int], sigma: float = 1) -> np.ndarray:
10
11
12
    """Generate gaussian map.

    Args:
13
14
        shape (Tuple[int]): Shape of the map.
        sigma (float): Sigma to generate gaussian map.
15
16
17
18
19
20
21
22
23
24
25
26
27
            Defaults to 1.

    Returns:
        np.ndarray: Generated gaussian map.
    """
    m, n = [(ss - 1.) / 2. for ss in shape]
    y, x = np.ogrid[-m:m + 1, -n:n + 1]

    h = np.exp(-(x * x + y * y) / (2 * sigma * sigma))
    h[h < np.finfo(h.dtype).eps * h.max()] = 0
    return h


28
29
30
31
def draw_heatmap_gaussian(heatmap: Tensor,
                          center: Tensor,
                          radius: int,
                          k: int = 1) -> Tensor:
32
33
34
    """Get gaussian masked heatmap.

    Args:
35
36
        heatmap (Tensor): Heatmap to be masked.
        center (Tensor): Center coord of the heatmap.
37
        radius (int): Radius of gaussian.
38
        k (int): Multiple of masked_gaussian. Defaults to 1.
39
40

    Returns:
41
        Tensor: Masked heatmap.
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    """
    diameter = 2 * radius + 1
    gaussian = gaussian_2d((diameter, diameter), sigma=diameter / 6)

    x, y = int(center[0]), int(center[1])

    height, width = heatmap.shape[0:2]

    left, right = min(x, radius), min(width - x, radius + 1)
    top, bottom = min(y, radius), min(height - y, radius + 1)

    masked_heatmap = heatmap[y - top:y + bottom, x - left:x + right]
    masked_gaussian = torch.from_numpy(
        gaussian[radius - top:radius + bottom,
                 radius - left:radius + right]).to(heatmap.device,
                                                   torch.float32)
    if min(masked_gaussian.shape) > 0 and min(masked_heatmap.shape) > 0:
        torch.max(masked_heatmap, masked_gaussian * k, out=masked_heatmap)
    return heatmap


63
64
def gaussian_radius(det_size: Tuple[Tensor, Tensor],
                    min_overlap: float = 0.5) -> Tensor:
65
66
67
    """Get radius of gaussian.

    Args:
68
69
        det_size (Tuple[Tensor]): Size of the detection result.
        min_overlap (float): Gaussian_overlap. Defaults to 0.5.
70
71

    Returns:
72
        Tensor: Computed radius.
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    """
    height, width = det_size

    a1 = 1
    b1 = (height + width)
    c1 = width * height * (1 - min_overlap) / (1 + min_overlap)
    sq1 = torch.sqrt(b1**2 - 4 * a1 * c1)
    r1 = (b1 + sq1) / 2

    a2 = 4
    b2 = 2 * (height + width)
    c2 = (1 - min_overlap) * width * height
    sq2 = torch.sqrt(b2**2 - 4 * a2 * c2)
    r2 = (b2 + sq2) / 2

    a3 = 4 * min_overlap
    b3 = -2 * min_overlap * (height + width)
    c3 = (min_overlap - 1) * width * height
    sq3 = torch.sqrt(b3**2 - 4 * a3 * c3)
    r3 = (b3 + sq3) / 2
    return min(r1, r2, r3)
ChaimZhu's avatar
ChaimZhu committed
94
95


96
97
98
99
100
def get_ellip_gaussian_2D(heatmap: Tensor,
                          center: List[int],
                          radius_x: int,
                          radius_y: int,
                          k: int = 1) -> Tensor:
ChaimZhu's avatar
ChaimZhu committed
101
102
103
104
105
    """Generate 2D ellipse gaussian heatmap.

    Args:
        heatmap (Tensor): Input heatmap, the gaussian kernel will cover on
            it and maintain the max value.
106
        center (List[int]): Coord of gaussian kernel's center.
ChaimZhu's avatar
ChaimZhu committed
107
108
        radius_x (int): X-axis radius of gaussian kernel.
        radius_y (int): Y-axis radius of gaussian kernel.
109
        k (int): Coefficient of gaussian kernel. Defaults to 1.
ChaimZhu's avatar
ChaimZhu committed
110
111
112
113
114
115

    Returns:
        out_heatmap (Tensor): Updated heatmap covered by gaussian kernel.
    """
    diameter_x, diameter_y = 2 * radius_x + 1, 2 * radius_y + 1
    gaussian_kernel = ellip_gaussian2D((radius_x, radius_y),
116
117
                                       sigma_x=diameter_x // 6,
                                       sigma_y=diameter_y // 6,
ChaimZhu's avatar
ChaimZhu committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
                                       dtype=heatmap.dtype,
                                       device=heatmap.device)

    x, y = int(center[0]), int(center[1])
    height, width = heatmap.shape[0:2]

    left, right = min(x, radius_x), min(width - x, radius_x + 1)
    top, bottom = min(y, radius_y), min(height - y, radius_y + 1)

    masked_heatmap = heatmap[y - top:y + bottom, x - left:x + right]
    masked_gaussian = gaussian_kernel[radius_y - top:radius_y + bottom,
                                      radius_x - left:radius_x + right]
    out_heatmap = heatmap
    torch.max(
        masked_heatmap,
        masked_gaussian * k,
        out=out_heatmap[y - top:y + bottom, x - left:x + right])

    return out_heatmap


139
140
141
142
143
def ellip_gaussian2D(radius: Tuple[int, int],
                     sigma_x: int,
                     sigma_y: int,
                     dtype: torch.dtype = torch.float32,
                     device: str = 'cpu') -> Tensor:
ChaimZhu's avatar
ChaimZhu committed
144
145
146
    """Generate 2D ellipse gaussian kernel.

    Args:
147
        radius (Tuple[int]): Ellipse radius (radius_x, radius_y) of gaussian
ChaimZhu's avatar
ChaimZhu committed
148
149
150
            kernel.
        sigma_x (int): X-axis sigma of gaussian function.
        sigma_y (int): Y-axis sigma of gaussian function.
151
152
153
154
        dtype (torch.dtype): Dtype of gaussian tensor.
            Defaults to torch.float32.
        device (str): Device of gaussian tensor.
            Defaults to 'cpu'.
ChaimZhu's avatar
ChaimZhu committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

    Returns:
        h (Tensor): Gaussian kernel with a
            ``(2 * radius_y + 1) * (2 * radius_x + 1)`` shape.
    """
    x = torch.arange(
        -radius[0], radius[0] + 1, dtype=dtype, device=device).view(1, -1)
    y = torch.arange(
        -radius[1], radius[1] + 1, dtype=dtype, device=device).view(-1, 1)

    h = (-(x * x) / (2 * sigma_x * sigma_x) - (y * y) /
         (2 * sigma_y * sigma_y)).exp()
    h[h < torch.finfo(h.dtype).eps * h.max()] = 0

    return h