mmagic.visualization
¶
Package Contents¶
Classes¶
Visualize multiple images by concatenation. |
|
Visualization backend for Pavi. |
|
Tensorboard visualization backend class. |
|
MMagic visualization backend class. It can write image, config, scalars, |
|
Wandb visualization backend for MMagic. |
|
MMagic Visualizer. |
- class mmagic.visualization.ConcatImageVisualizer(fn_key: str, img_keys: Sequence[str], pixel_range={}, bgr2rgb=False, name: str = 'visualizer', *args, **kwargs)[源代码]¶
Bases:
mmengine.visualization.Visualizer
Visualize multiple images by concatenation.
This visualizer will horizontally concatenate images belongs to different keys and vertically concatenate images belongs to different frames to visualize.
- Image to be visualized can be:
torch.Tensor or np.array
Image sequences of shape (T, C, H, W)
Multi-channel image of shape (1/3, H, W)
Single-channel image of shape (C, H, W)
- 参数
fn_key (str) – key used to determine file name for saving image. Usually it is the path of some input image. If the value is dir/basename.ext, the name used for saving will be basename.
img_keys (str) – keys, values of which are images to visualize.
pixel_range (dict) – min and max pixel value used to denormalize images, note that only float array or tensor will be denormalized, uint8 arrays are assumed to be unnormalized.
bgr2rgb (bool) – whether to convert the image from BGR to RGB.
name (str) – name of visualizer. Default: ‘visualizer’.
**kwargs (*args and) – Other arguments are passed to Visualizer. # noqa
- add_datasample(data_sample: mmagic.structures.DataSample, step=0) None ¶
Concatenate image and draw.
- 参数
input (torch.Tensor) – Single input tensor from data_batch.
data_sample (DataSample) – Single data_sample from data_batch.
output (DataSample) – Single prediction output by model.
step (int) – Global step value to record. Default: 0.
- class mmagic.visualization.PaviVisBackend(save_dir: str, exp_name: Optional[str] = None, labels: Optional[str] = None, project: Optional[str] = None, model: Optional[str] = None, description: Optional[str] = None)[源代码]¶
Bases:
mmengine.visualization.BaseVisBackend
Visualization backend for Pavi.
- property experiment: VisBackend¶
Return the experiment object associated with this visualization backend.
- _init_env()¶
Init save dir.
- add_image(name: str, image: numpy.array, step: int = 0, **kwargs) None ¶
Record the image to Pavi.
- 参数
name (str) – The image identifier.
image (np.ndarray) – The image to be saved. The format should be RGB. Default to None.
step (int) – Global step value to record. Default to 0.
- add_scalar(name: str, value: Union[int, float, torch.Tensor, numpy.ndarray], step: int = 0, **kwargs) None ¶
Record the scalar data to Pavi.
- 参数
name (str) – The scalar identifier.
value (int, float, torch.Tensor, np.ndarray) – Value to save.
step (int) – Global step value to record. Default to 0.
- add_scalars(scalar_dict: dict, step: int = 0, file_path: Optional[str] = None, **kwargs) None ¶
Record the scalars to Pavi.
The scalar dict will be written to the default and specified files if
file_path
is specified.- 参数
scalar_dict (dict) – Key-value pair storing the tag and corresponding values. The value must be dumped into json format.
step (int) – Global step value to record. Default to 0.
file_path (str, optional) – The scalar’s data will be saved to the
file_path
file at the same time if thefile_path
parameter is specified. Default to None.
- class mmagic.visualization.TensorboardVisBackend(save_dir: str)[源代码]¶
Bases:
mmengine.visualization.TensorboardVisBackend
Tensorboard visualization backend class.
It can write images, config, scalars, etc. to a tensorboard file.
实际案例
>>> from mmengine.visualization import TensorboardVisBackend >>> import numpy as np >>> vis_backend = TensorboardVisBackend(save_dir='temp_dir') >>> img = np.random.randint(0, 256, size=(10, 10, 3)) >>> vis_backend.add_image('img', img) >>> vis_backend.add_scaler('mAP', 0.6) >>> vis_backend.add_scalars({'loss': 0.1,'acc':0.8}) >>> cfg = Config(dict(a=1, b=dict(b1=[0, 1]))) >>> vis_backend.add_config(cfg)
- 参数
save_dir (str) – The root directory to save the files produced by the backend.
- add_image(name: str, image: numpy.array, step: int = 0, **kwargs)¶
Record the image to Tensorboard. Additional support upload gif files.
- 参数
name (str) – The image identifier.
image (np.ndarray) – The image to be saved. The format should be RGB.
step (int) – Useless parameter. Wandb does not need this parameter. Default to 0.
- class mmagic.visualization.VisBackend(save_dir: str, img_save_dir: str = 'vis_image', config_save_file: str = 'config.py', scalar_save_file: str = 'scalars.json', ceph_path: Optional[str] = None, delete_local_image: bool = True)[源代码]¶
Bases:
mmengine.visualization.BaseVisBackend
MMagic visualization backend class. It can write image, config, scalars, etc. to the local hard disk and ceph path. You can get the drawing backend through the experiment property for custom drawing.
实际案例
>>> from mmagic.visualization import VisBackend >>> import numpy as np >>> vis_backend = VisBackend(save_dir='temp_dir', >>> ceph_path='s3://temp-bucket') >>> img = np.random.randint(0, 256, size=(10, 10, 3)) >>> vis_backend.add_image('img', img) >>> vis_backend.add_scalar('mAP', 0.6) >>> vis_backend.add_scalars({'loss': [1, 2, 3], 'acc': 0.8}) >>> cfg = Config(dict(a=1, b=dict(b1=[0, 1]))) >>> vis_backend.add_config(cfg)
- 参数
save_dir (str) – The root directory to save the files produced by the visualizer.
img_save_dir (str) – The directory to save images. Default to ‘vis_image’.
config_save_file (str) – The file name to save config. Default to ‘config.py’.
scalar_save_file (str) – The file name to save scalar values. Default to ‘scalars.json’.
ceph_path (Optional[str]) – The remote path of Ceph cloud storage. Defaults to None.
delete_local (bool) – Whether delete local after uploading to ceph or not. If
ceph_path
is None, this will be ignored. Defaults to True.
- property experiment: VisBackend¶
Return the experiment object associated with this visualization backend.
- _init_env()¶
Setup env for VisBackend.
- add_config(config: mmengine.config.Config, **kwargs) None ¶
Record the config to disk.
- 参数
config (Config) – The Config object
- add_image(name: str, image: numpy.array, step: int = 0, **kwargs) None ¶
Record the image to disk.
- 参数
name (str) – The image identifier.
image (np.ndarray) – The image to be saved. The format should be RGB. Default to None.
step (int) – Global step value to record. Default to 0.
- add_scalar(name: str, value: Union[int, float, torch.Tensor, numpy.ndarray], step: int = 0, **kwargs) None ¶
Record the scalar data to disk.
- 参数
name (str) – The scalar identifier.
value (int, float, torch.Tensor, np.ndarray) – Value to save.
step (int) – Global step value to record. Default to 0.
- add_scalars(scalar_dict: dict, step: int = 0, file_path: Optional[str] = None, **kwargs) None ¶
Record the scalars to disk.
The scalar dict will be written to the default and specified files if
file_path
is specified.- 参数
scalar_dict (dict) – Key-value pair storing the tag and corresponding values. The value must be dumped into json format.
step (int) – Global step value to record. Default to 0.
file_path (str, optional) – The scalar’s data will be saved to the
file_path
file at the same time if thefile_path
parameter is specified. Default to None.
- _dump(value_dict: dict, file_path: str, file_format: str) None ¶
dump dict to file.
- 参数
value_dict (dict) – The dict data to saved.
file_path (str) – The file path to save data.
file_format (str) – The file format to save data.
- _upload(path: str, delete_local=False) None ¶
Upload file at path to remote.
- 参数
path (str) – Path of file to upload.
- class mmagic.visualization.WandbVisBackend(save_dir: str, init_kwargs: Optional[dict] = None, define_metric_cfg: Union[dict, list, None] = None, commit: Optional[bool] = True, log_code_name: Optional[str] = None, watch_kwargs: Optional[dict] = None)[源代码]¶
Bases:
mmengine.visualization.WandbVisBackend
Wandb visualization backend for MMagic.
- _init_env()¶
Setup env for wandb.
- add_image(name: str, image: numpy.array, step: int = 0, **kwargs)¶
Record the image to wandb. Additional support upload gif files.
- 参数
name (str) – The image identifier.
image (np.ndarray) – The image to be saved. The format should be RGB.
step (int) – Useless parameter. Wandb does not need this parameter. Default to 0.
- class mmagic.visualization.Visualizer(name='visualizer', vis_backends: Optional[List[Dict]] = None, save_dir: Optional[str] = None)[源代码]¶
Bases:
mmengine.visualization.Visualizer
MMagic Visualizer.
- 参数
name (str) – Name of the instance. Defaults to ‘visualizer’.
vis_backends (list, optional) – Visual backend config list. Defaults to None.
save_dir (str, optional) – Save file dir for all storage backends. If it is None, the backend storage will not save any data.
Examples:
>>> # Draw image >>> vis = Visualizer() >>> vis.add_datasample( >>> 'random_noise', >>> gen_samples=torch.rand(2, 3, 10, 10), >>> gt_samples=dict(imgs=torch.randn(2, 3, 10, 10)), >>> gt_keys='imgs', >>> vis_mode='image', >>> n_rows=2, >>> step=10)
- static _post_process_image(image: torch.Tensor) torch.Tensor ¶
Post process images.
- 参数
image (Tensor) – Image to post process. The value range of image should be in [0, 255] and the channel order should be BGR.
- 返回
Image in RGB color order.
- 返回类型
Tensor
- static _get_n_row_and_padding(samples: Tuple[dict, torch.Tensor], n_row: Optional[int] = None) Tuple[int, Optional[torch.Tensor]] ¶
Get number of sample in each row and tensor for padding the empty position.
- 参数
samples (Tuple[dict, Tensor]) – Samples to visualize.
n_row (int, optional) – Number of images displayed in each row of. If not passed, n_row will be set as
int(sqrt(batch_size))
.
- 返回
- Number of sample in each row and tensor
for padding the empty position.
- 返回类型
Tuple[int, Optional[int]]
- _vis_gif_sample(gen_samples: mmagic.utils.typing.SampleList, target_keys: Union[str, List[str], None], n_row: int) numpy.ndarray ¶
Visualize gif samples.
- 参数
gen_samples (SampleList) – List of data samples to visualize
target_keys (Union[str, List[str], None]) – Keys of the visualization target in data samples.
n_rows (int, optional) – Number of images in one row.
- 返回
The visualization results.
- 返回类型
np.ndarray
- _vis_image_sample(gen_samples: mmagic.utils.typing.SampleList, target_keys: Union[str, List[str], None], n_row: int) numpy.ndarray ¶
Visualize image samples.
- 参数
gen_samples (SampleList) – List of data samples to visualize
target_keys (Union[str, List[str], None]) – Keys of the visualization target in data samples.
color_order (str) – The color order of the passed images.
target_mean (Sequence[Union[float, int]]) – The target mean of the visualization results.
target_std (Sequence[Union[float, int]]) – The target std of the visualization results.
n_rows (int, optional) – Number of images in one row.
- 返回
The visualization results.
- 返回类型
np.ndarray
- _get_vis_data_by_key(sample: mmagic.structures.DataSample, key: str) torch.Tensor ¶
Get tensor in
DataSample
by the given key.- 参数
sample (DataSample) – Input data sample.
key (str) – Name of the target tensor.
- 返回
Tensor from the data sample.
- 返回类型
Tensor
- add_datasample(name: str, *, gen_samples: Sequence[mmagic.structures.DataSample], target_keys: Optional[Tuple[str, List[str]]] = None, vis_mode: Optional[str] = None, n_row: Optional[int] = None, show: bool = False, wait_time: int = 0, step: int = 0, **kwargs) None ¶
Draw datasample and save to all backends.
If GT and prediction are plotted at the same time, they are displayed in a stitched image where the left image is the ground truth and the right image is the prediction.
If
show
is True, all storage backends are ignored, and the images will be displayed in a local window.- 参数
name (str) – The image identifier.
gen_samples (List[DataSample]) – Data samples to visualize.
vis_mode (str, optional) – Visualization mode. If not passed, will visualize results as image. Defaults to None.
n_rows (int, optional) – Number of images in one row. Defaults to None.
color_order (str) – The color order of the passed images. Defaults to ‘bgr’.
target_mean (Sequence[Union[float, int]]) – The target mean of the visualization results. Defaults to 127.5.
target_std (Sequence[Union[float, int]]) – The target std of the visualization results. Defaults to 127.5.
show (bool) – Whether to display the drawn image. Default to False.
wait_time (float) – The interval of show (s). Defaults to 0.
step (int) – Global step value to record. Defaults to 0.
- add_image(name: str, image: numpy.ndarray, step: int = 0, **kwargs) None ¶
Record the image. Support input kwargs.
- 参数
name (str) – The image identifier.
image (np.ndarray, optional) – The image to be saved. The format should be RGB. Default to None.
step (int) – Global step value to record. Default to 0.