Visualizer — mmengine 0.11.0rc0 documentation (original) (raw)

class mmengine.visualization.Visualizer(name='visualizer', image=None, vis_backends=None, save_dir=None, fig_save_cfg={'frameon': False}, fig_show_cfg={'frameon': False})[source]

MMEngine provides a Visualizer class that uses the Matplotliblibrary as the backend. It has the following functions:

All the basic drawing methods support chain calls, which is convenient for overlaydrawing and display. Each downstream algorithm library can inheritVisualizer and implement the add_datasample logic. For example,DetLocalVisualizer in MMDetection inherits from Visualizerand implements functions, such as visual detection boxes, instance masks, and semantic segmentation maps in the add_datasample interface.

Parameters:

Examples

Basic info methods

vis = Visualizer() vis.set_image(image) vis.get_image() vis.show()

Basic drawing methods

vis = Visualizer(image=image) vis.draw_bboxes(np.array([0, 0, 1, 1]), edge_colors='g') vis.draw_bboxes(bbox=np.array([[1, 1, 2, 2], [2, 2, 3, 3]]), edge_colors=['g', 'r']) vis.draw_lines(x_datas=np.array([1, 3]), y_datas=np.array([1, 3]), colors='r', line_widths=1) vis.draw_lines(x_datas=np.array([[1, 3], [2, 4]]), y_datas=np.array([[1, 3], [2, 4]]), colors=['r', 'r'], line_widths=[1, 2]) vis.draw_texts(text='MMEngine', position=np.array([2, 2]), colors='b') vis.draw_texts(text=['MMEngine','OpenMMLab'], position=np.array([[2, 2], [5, 5]]), colors=['b', 'b']) vis.draw_circles(circle_coord=np.array([2, 2]), radius=np.array[1]) vis.draw_circles(circle_coord=np.array([[2, 2], [3, 5]), radius=np.array[1, 2], colors=['g', 'r']) square = np.array([[0, 0], [100, 0], [100, 100], [0, 100]]) vis.draw_polygons(polygons=square, edge_colors='g') squares = [np.array([[0, 0], [100, 0], [100, 100], [0, 100]]), np.array([[0, 0], [50, 0], [50, 50], [0, 50]])] vis.draw_polygons(polygons=squares, edge_colors=['g', 'r']) vis.draw_binary_masks(binary_mask, alpha=0.6) heatmap = vis.draw_featmap(featmap, img, channel_reduction='select_max') heatmap = vis.draw_featmap(featmap, img, channel_reduction=None, topk=8, arrangement=(4, 2)) heatmap = vis.draw_featmap(featmap, img, channel_reduction=None, topk=-1)

chain calls

vis.draw_bboxes().draw_texts().draw_circle().draw_binary_masks()

Backend related methods

vis = Visualizer(vis_backends=[dict(type='LocalVisBackend')], save_dir='temp_dir') cfg = Config(dict(a=1, b=dict(b1=[0, 1]))) vis.add_config(cfg) image=np.random.randint(0, 256, size=(10, 10, 3)).astype(np.uint8) vis.add_image('image',image) vis.add_scaler('mAP', 0.6) vis.add_scalars({'loss': 0.1,'acc':0.8})

inherit

class DetLocalVisualizer(Visualizer): def add_datasample(self, name, image: np.ndarray, gt_sample: Optional['BaseDataElement'] = None, pred_sample: Optional['BaseDataElement'] = None, draw_gt: bool = True, draw_pred: bool = True, show: bool = False, wait_time: int = 0, step: int = 0) -> None: pass

add_config(config, **kwargs)[source]

Record the config.

Parameters:

config (Config) – The Config object.

add_datasample(name, image, data_sample=None, draw_gt=True, draw_pred=True, show=False, wait_time=0, step=0)[source]

Draw datasample.

Parameters:

Return type:

None

add_graph(model, data_batch, **kwargs)[source]

Record the model graph.

Parameters:

Return type:

None

add_image(name, image, step=0)[source]

Record the image.

Parameters:

Return type:

None

add_scalar(name, value, step=0, **kwargs)[source]

Record the scalar data.

Parameters:

Return type:

None

add_scalars(scalar_dict, step=0, file_path=None, **kwargs)[source]

Record the scalars’ data.

Parameters:

Return type:

None

close()[source]

Close an opened object.

Return type:

None

property dataset_meta_: dict | None_

Meta info of the dataset.

Type:

Optional[dict]

draw_bboxes(bboxes, edge_colors='g', line_styles='-', line_widths=2, face_colors='none', alpha=0.8)[source]

Draw single or multiple bboxes.

Parameters:

Return type:

Visualizer

draw_binary_masks(binary_masks, colors='g', alphas=0.8)[source]

Draw single or multiple binary masks.

Parameters:

Return type:

Visualizer

draw_circles(center, radius, edge_colors='g', line_styles='-', line_widths=2, face_colors='none', alpha=0.8)[source]

Draw single or multiple circles.

Parameters:

Return type:

Visualizer

static draw_featmap(featmap, overlaid_image=None, channel_reduction='squeeze_mean', topk=20, arrangement=(4, 5), resize_shape=None, alpha=0.5)[source]

Draw featmap.

Parameters:

Returns:

RGB image.

Return type:

np.ndarray

draw_lines(x_datas, y_datas, colors='g', line_styles='-', line_widths=2)[source]

Draw single or multiple line segments.

Parameters:

Return type:

Visualizer

draw_points(positions, colors='g', marker=None, sizes=None)[source]

Draw single or multiple points.

Parameters:

draw_polygons(polygons, edge_colors='g', line_styles='-', line_widths=2, face_colors='none', alpha=0.8)[source]

Draw single or multiple bboxes.

Parameters:

Return type:

Visualizer

draw_texts(texts, positions, font_sizes=None, colors='g', vertical_alignments='top', horizontal_alignments='left', font_families='sans-serif', bboxes=None, font_properties=None)[source]

Draw single or multiple text boxes.

Parameters:

Return type:

Visualizer

get_backend(name)[source]

Get vis backend by name.

Parameters:

name (str) – The name of vis backend

Returns:

The vis backend.

Return type:

BaseVisBackend

get_image()[source]

Get the drawn image. The format is RGB.

Returns:

the drawn image which channel is RGB.

Return type:

np.ndarray

classmethod get_instance(name, **kwargs)[source]

Make subclass can get latest created instance byVisualizer.get_current_instance().

Downstream codebase may need to get the latest created instance without knowing the specific Visualizer type. For example, mmdetection builds visualizer in runner and some component which cannot access runner wants to get latest created visualizer. In this case, the component does not know which type of visualizer has been built and cannot get target instance. Therefore, Visualizeroverrides the get_instance() and its subclass will register the created instance to _instance_dict additionally.get_current_instance() will return the latest created subclass instance.

Examples

class DetLocalVisualizer(Visualizer): def init(self, name): super().init(name)

visualizer1 = DetLocalVisualizer.get_instance('name1') visualizer2 = Visualizer.get_current_instance() visualizer3 = DetLocalVisualizer.get_current_instance() assert id(visualizer1) == id(visualizer2) == id(visualizer3)

Parameters:

name (str) – Name of instance.

Returns:

Corresponding name instance.

Return type:

object

set_image(image)[source]

Set the image to draw.

Parameters:

image (np.ndarray) – The image to draw.

Return type:

None

show(drawn_img=None, win_name='image', wait_time=0.0, continue_key=' ', backend='matplotlib')[source]

Show the drawn image.

Parameters:

Return type:

None