File size: 13,691 Bytes
186701e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# Copyright (c) OpenMMLab. All rights reserved.
import math
from typing import List, Union
import mmcv
import numpy as np
import torch
from mmdet.structures.bbox import HorizontalBoxes
from mmdet.visualization import DetLocalVisualizer
from mmdet.visualization.palette import _get_adaptive_scales, get_palette
from mmengine.structures import InstanceData
from torch import Tensor
from mmyolo.registry import VISUALIZERS
@VISUALIZERS.register_module()
class YOLOAssignerVisualizer(DetLocalVisualizer):
"""MMYOLO Detection Assigner Visualizer.
This class is provided to the `assigner_visualization.py` script.
Args:
name (str): Name of the instance. Defaults to 'visualizer'.
"""
def __init__(self, name: str = 'visualizer', *args, **kwargs):
super().__init__(name=name, *args, **kwargs)
# need priors_size from config
self.priors_size = None
def draw_grid(self,
stride: int = 8,
line_styles: Union[str, List[str]] = ':',
colors: Union[str, tuple, List[str],
List[tuple]] = (180, 180, 180),
line_widths: Union[Union[int, float],
List[Union[int, float]]] = 1):
"""Draw grids on image.
Args:
stride (int): Downsample factor of feature map.
line_styles (Union[str, List[str]]): The linestyle
of lines. ``line_styles`` can have the same length with
texts or just single value. If ``line_styles`` is single
value, all the lines will have the same linestyle.
Reference to
https://matplotlib.org/stable/api/collections_api.html?highlight=collection#matplotlib.collections.AsteriskPolygonCollection.set_linestyle
for more details. Defaults to ':'.
colors (Union[str, tuple, List[str], List[tuple]]): The colors of
lines. ``colors`` can have the same length with lines or just
single value. If ``colors`` is single value, all the lines
will have the same colors. Reference to
https://matplotlib.org/stable/gallery/color/named_colors.html
for more details. Defaults to (180, 180, 180).
line_widths (Union[Union[int, float], List[Union[int, float]]]):
The linewidth of lines. ``line_widths`` can have
the same length with lines or just single value.
If ``line_widths`` is single value, all the lines will
have the same linewidth. Defaults to 1.
"""
assert self._image is not None, 'Please set image using `set_image`'
# draw vertical lines
x_datas_vertical = ((np.arange(self.width // stride - 1) + 1) *
stride).reshape((-1, 1)).repeat(
2, axis=1)
y_datas_vertical = np.array([[0, self.height - 1]]).repeat(
self.width // stride - 1, axis=0)
self.draw_lines(
x_datas_vertical,
y_datas_vertical,
colors=colors,
line_styles=line_styles,
line_widths=line_widths)
# draw horizontal lines
x_datas_horizontal = np.array([[0, self.width - 1]]).repeat(
self.height // stride - 1, axis=0)
y_datas_horizontal = ((np.arange(self.height // stride - 1) + 1) *
stride).reshape((-1, 1)).repeat(
2, axis=1)
self.draw_lines(
x_datas_horizontal,
y_datas_horizontal,
colors=colors,
line_styles=line_styles,
line_widths=line_widths)
def draw_instances_assign(self,
instances: InstanceData,
retained_gt_inds: Tensor,
not_show_label: bool = False):
"""Draw instances of GT.
Args:
instances (:obj:`InstanceData`): gt_instance. It usually
includes ``bboxes`` and ``labels`` attributes.
retained_gt_inds (Tensor): The gt indexes assigned as the
positive sample in the current prior.
not_show_label (bool): Whether to show gt labels on images.
"""
assert self.dataset_meta is not None
classes = self.dataset_meta['classes']
palette = self.dataset_meta['palette']
if len(retained_gt_inds) == 0:
return self.get_image()
draw_gt_inds = torch.from_numpy(
np.array(
list(set(retained_gt_inds.cpu().numpy())), dtype=np.int64))
bboxes = instances.bboxes[draw_gt_inds]
labels = instances.labels[draw_gt_inds]
if not isinstance(bboxes, Tensor):
bboxes = bboxes.tensor
edge_colors = [palette[i] for i in labels]
max_label = int(max(labels) if len(labels) > 0 else 0)
text_palette = get_palette(self.text_color, max_label + 1)
text_colors = [text_palette[label] for label in labels]
self.draw_bboxes(
bboxes,
edge_colors=edge_colors,
alpha=self.alpha,
line_widths=self.line_width)
if not not_show_label:
positions = bboxes[:, :2] + self.line_width
areas = (bboxes[:, 3] - bboxes[:, 1]) * (
bboxes[:, 2] - bboxes[:, 0])
scales = _get_adaptive_scales(areas)
for i, (pos, label) in enumerate(zip(positions, labels)):
label_text = classes[
label] if classes is not None else f'class {label}'
self.draw_texts(
label_text,
pos,
colors=text_colors[i],
font_sizes=int(13 * scales[i]),
bboxes=[{
'facecolor': 'black',
'alpha': 0.8,
'pad': 0.7,
'edgecolor': 'none'
}])
def draw_positive_assign(self,
grid_x_inds: Tensor,
grid_y_inds: Tensor,
class_inds: Tensor,
stride: int,
bboxes: Union[Tensor, HorizontalBoxes],
retained_gt_inds: Tensor,
offset: float = 0.5):
"""
Args:
grid_x_inds (Tensor): The X-axis indexes of the positive sample
in current prior.
grid_y_inds (Tensor): The Y-axis indexes of the positive sample
in current prior.
class_inds (Tensor): The classes indexes of the positive sample
in current prior.
stride (int): Downsample factor of feature map.
bboxes (Union[Tensor, HorizontalBoxes]): Bounding boxes of GT.
retained_gt_inds (Tensor): The gt indexes assigned as the
positive sample in the current prior.
offset (float): The offset of points, the value is normalized
with corresponding stride. Defaults to 0.5.
"""
if not isinstance(bboxes, Tensor):
# Convert HorizontalBoxes to Tensor
bboxes = bboxes.tensor
# The PALETTE in the dataset_meta is required
assert self.dataset_meta is not None
palette = self.dataset_meta['palette']
x = ((grid_x_inds + offset) * stride).long()
y = ((grid_y_inds + offset) * stride).long()
center = torch.stack((x, y), dim=-1)
retained_bboxes = bboxes[retained_gt_inds]
bbox_wh = retained_bboxes[:, 2:] - retained_bboxes[:, :2]
bbox_area = bbox_wh[:, 0] * bbox_wh[:, 1]
radius = _get_adaptive_scales(bbox_area) * 4
colors = [palette[i] for i in class_inds]
self.draw_circles(
center,
radius,
colors,
line_widths=0,
face_colors=colors,
alpha=1.0)
def draw_prior(self,
grid_x_inds: Tensor,
grid_y_inds: Tensor,
class_inds: Tensor,
stride: int,
feat_ind: int,
prior_ind: int,
offset: float = 0.5):
"""Draw priors on image.
Args:
grid_x_inds (Tensor): The X-axis indexes of the positive sample
in current prior.
grid_y_inds (Tensor): The Y-axis indexes of the positive sample
in current prior.
class_inds (Tensor): The classes indexes of the positive sample
in current prior.
stride (int): Downsample factor of feature map.
feat_ind (int): Index of featmap.
prior_ind (int): Index of prior in current featmap.
offset (float): The offset of points, the value is normalized
with corresponding stride. Defaults to 0.5.
"""
palette = self.dataset_meta['palette']
center_x = ((grid_x_inds + offset) * stride)
center_y = ((grid_y_inds + offset) * stride)
xyxy = torch.stack((center_x, center_y, center_x, center_y), dim=1)
device = xyxy.device
if self.priors_size is not None:
xyxy += self.priors_size[feat_ind][prior_ind].to(device)
else:
xyxy += torch.tensor(
[[-stride / 2, -stride / 2, stride / 2, stride / 2]],
device=device)
colors = [palette[i] for i in class_inds]
self.draw_bboxes(
xyxy,
edge_colors=colors,
alpha=self.alpha,
line_styles='--',
line_widths=math.ceil(self.line_width * 0.3))
def draw_assign(self,
image: np.ndarray,
assign_results: List[List[dict]],
gt_instances: InstanceData,
show_prior: bool = False,
not_show_label: bool = False) -> np.ndarray:
"""Draw assigning results.
Args:
image (np.ndarray): The image to draw.
assign_results (list): The assigning results.
gt_instances (:obj:`InstanceData`): Data structure for
instance-level annotations or predictions.
show_prior (bool): Whether to show prior on image.
not_show_label (bool): Whether to show gt labels on images.
Returns:
np.ndarray: the drawn image which channel is RGB.
"""
img_show_list = []
for feat_ind, assign_results_feat in enumerate(assign_results):
img_show_list_feat = []
for prior_ind, assign_results_prior in enumerate(
assign_results_feat):
self.set_image(image)
h, w = image.shape[:2]
# draw grid
stride = assign_results_prior['stride']
self.draw_grid(stride)
# draw prior on matched gt
grid_x_inds = assign_results_prior['grid_x_inds']
grid_y_inds = assign_results_prior['grid_y_inds']
class_inds = assign_results_prior['class_inds']
prior_ind = assign_results_prior['prior_ind']
offset = assign_results_prior.get('offset', 0.5)
if show_prior:
self.draw_prior(grid_x_inds, grid_y_inds, class_inds,
stride, feat_ind, prior_ind, offset)
# draw matched gt
retained_gt_inds = assign_results_prior['retained_gt_inds']
self.draw_instances_assign(gt_instances, retained_gt_inds,
not_show_label)
# draw positive
self.draw_positive_assign(grid_x_inds, grid_y_inds, class_inds,
stride, gt_instances.bboxes,
retained_gt_inds, offset)
# draw title
if self.priors_size is not None:
base_prior = self.priors_size[feat_ind][prior_ind]
else:
base_prior = [stride, stride, stride * 2, stride * 2]
prior_size = (base_prior[2] - base_prior[0],
base_prior[3] - base_prior[1])
pos = np.array((20, 20))
text = f'feat_ind: {feat_ind} ' \
f'prior_ind: {prior_ind} ' \
f'prior_size: ({prior_size[0]}, {prior_size[1]})'
scales = _get_adaptive_scales(np.array([h * w / 16]))
font_sizes = int(13 * scales)
self.draw_texts(
text,
pos,
colors=self.text_color,
font_sizes=font_sizes,
bboxes=[{
'facecolor': 'black',
'alpha': 0.8,
'pad': 0.7,
'edgecolor': 'none'
}])
img_show = self.get_image()
img_show = mmcv.impad(img_show, padding=(5, 5, 5, 5))
img_show_list_feat.append(img_show)
img_show_list.append(np.concatenate(img_show_list_feat, axis=1))
# Merge all images into one image
# setting axis is to beautify the merged image
axis = 0 if len(assign_results[0]) > 1 else 1
return np.concatenate(img_show_list, axis=axis)
|