MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / plot_2d_or_3d_image

Function plot_2d_or_3d_image

monai/visualize/img2tensorboard.py:146–223  ·  view source on GitHub ↗

Plot 2D or 3D image on the TensorBoard, 3D image will be converted to GIF image. Note: Plot 3D or 2D image(with more than 3 channels) as separate images. And if writer is from TensorBoardX, data has 3 channels and `max_channels=3`, will plot as RGB video. Args: data

(
    data: NdarrayTensor | list[NdarrayTensor],
    step: int,
    writer: SummaryWriter | SummaryWriterX,
    index: int = 0,
    max_channels: int = 1,
    frame_dim: int = -3,
    max_frames: int = 24,
    tag: str = "output",
)

Source from the content-addressed store, hash-verified

144
145
146def plot_2d_or_3d_image(
147 data: NdarrayTensor | list[NdarrayTensor],
148 step: int,
149 writer: SummaryWriter | SummaryWriterX,
150 index: int = 0,
151 max_channels: int = 1,
152 frame_dim: int = -3,
153 max_frames: int = 24,
154 tag: str = "output",
155) -> None:
156 """Plot 2D or 3D image on the TensorBoard, 3D image will be converted to GIF image.
157
158 Note:
159 Plot 3D or 2D image(with more than 3 channels) as separate images.
160 And if writer is from TensorBoardX, data has 3 channels and `max_channels=3`, will plot as RGB video.
161
162 Args:
163 data: target data to be plotted as image on the TensorBoard.
164 The data is expected to have 'NCHW[D]' dimensions or a list of data with `CHW[D]` dimensions,
165 and only plot the first in the batch.
166 step: current step to plot in a chart.
167 writer: specify TensorBoard or TensorBoardX SummaryWriter to plot the image.
168 index: plot which element in the input data batch, default is the first element.
169 max_channels: number of channels to plot.
170 frame_dim: if plotting 3D image as GIF, specify the dimension used as frames,
171 expect input data shape as `NCHWD`, default to `-3` (the first spatial dim)
172 max_frames: if plot 3D RGB image as video in TensorBoardX, set the FPS to `max_frames`.
173 tag: tag of the plotted image on TensorBoard.
174 """
175 data_index = data[index]
176 # as the `d` data has no batch dim, reduce the spatial dim index if positive
177 frame_dim = frame_dim - 1 if frame_dim > 0 else frame_dim
178
179 d: np.ndarray = (
180 data_index.detach().cpu().numpy() if isinstance(data_index, torch.Tensor) else np.asarray(data_index)
181 )
182
183 if d.ndim == 2:
184 d = rescale_array(d, 0, 1) # type: ignore
185 dataformats = "HW"
186 writer.add_image(f"{tag}_{dataformats}", d, step, dataformats=dataformats)
187 return
188
189 if d.ndim == 3:
190 if d.shape[0] == 3 and max_channels == 3: # RGB
191 dataformats = "CHW"
192 writer.add_image(f"{tag}_{dataformats}", d, step, dataformats=dataformats)
193 return
194 dataformats = "HW"
195 for j, d2 in enumerate(d[:max_channels]):
196 d2 = rescale_array(d2, 0, 1)
197 writer.add_image(f"{tag}_{dataformats}_{j}", d2, step, dataformats=dataformats)
198 return
199
200 if d.ndim >= 4:
201 spatial = d.shape[-3:]
202 d = d.reshape([-1] + list(spatial))
203 d_chans = d.shape[0] # type: ignore

Callers 5

__call__Method · 0.90
run_training_testFunction · 0.90
test_tb_imageMethod · 0.90
test_tbx_imageMethod · 0.90
test_tbx_videoMethod · 0.90

Calls 3

rescale_arrayFunction · 0.90
minFunction · 0.85
add_animated_gifFunction · 0.85

Tested by 4

run_training_testFunction · 0.72
test_tb_imageMethod · 0.72
test_tbx_imageMethod · 0.72
test_tbx_videoMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…