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

Function plot_engine_status

monai/utils/jupyter_utils.py:158–233  ·  view source on GitHub ↗

Plot the status of the given Engine with its logger. The plot will consist of a graph of loss values and metrics taken from the logger, and images taken from the `output` and `batch` members of `engine.state`. The images are converted to Numpy arrays suitable for input to `Axes.imshow`

(
    engine: Engine,
    logger: Any,
    title: str = "Training Log",
    yscale: str = "log",
    avg_keys: tuple[str] = (LOSS_NAME,),
    window_fraction: int = 20,
    image_fn: Callable[[str, torch.Tensor], Any] | None = tensor_to_images,
    fig: plt.Figure | None = None,
    selected_inst: int = 0,
)

Source from the content-addressed store, hash-verified

156
157
158def plot_engine_status(
159 engine: Engine,
160 logger: Any,
161 title: str = "Training Log",
162 yscale: str = "log",
163 avg_keys: tuple[str] = (LOSS_NAME,),
164 window_fraction: int = 20,
165 image_fn: Callable[[str, torch.Tensor], Any] | None = tensor_to_images,
166 fig: plt.Figure | None = None,
167 selected_inst: int = 0,
168) -> tuple[plt.Figure, list]:
169 """
170 Plot the status of the given Engine with its logger. The plot will consist of a graph of loss values and metrics
171 taken from the logger, and images taken from the `output` and `batch` members of `engine.state`. The images are
172 converted to Numpy arrays suitable for input to `Axes.imshow` using `image_fn`, if this is None then no image
173 plotting is done.
174
175 Args:
176 engine: Engine to extract images from
177 logger: MetricLogger to extract loss and metric data from
178 title: graph title
179 yscale: for metric plot, scale for y-axis compatible with `Axes.set_yscale`
180 avg_keys: for metric plot, tuple of keys in `graphmap` to provide running average plots for
181 window_fraction: for metric plot, what fraction of the graph value length to use as the running average window
182 image_fn: callable converting tensors keyed to a name in the Engine to a tuple of images to plot
183 fig: Figure object to plot into, reuse from previous plotting for flicker-free refreshing
184 selected_inst: index of the instance to show in the image plot
185
186 Returns:
187 Figure object (or `fig` if given), list of Axes objects for graph and images
188 """
189 if fig is not None:
190 fig.clf()
191 else:
192 fig = plt.Figure(figsize=(20, 10), tight_layout=True, facecolor="white")
193
194 graphmap: dict[str, list[float]] = {LOSS_NAME: logger.loss}
195 graphmap.update(logger.metrics)
196
197 imagemap: dict = {}
198 if image_fn is not None and engine.state is not None and engine.state.batch is not None:
199 for src in (engine.state.batch, engine.state.output):
200 label = "Batch" if src is engine.state.batch else "Output"
201 batch_selected_inst = selected_inst # selected batch index, set to 0 when src is decollated
202
203 # if the src object is a list of elements, ie. a decollated batch, select an element and keep it as
204 # a dictionary of tensors with a batch dimension added
205 if isinstance(src, list):
206 selected_dict = src[selected_inst] # select this element
207 batch_selected_inst = 0 # set the selection to be the single index in the batch dimension
208 # store each tensor that is interpretable as an image with an added batch dimension
209 src = {k: v[None] for k, v in selected_dict.items() if isinstance(v, torch.Tensor) and v.ndim >= 3}
210
211 # images will be generated from the batch item selected above only, or from the single item given as `src`
212
213 if isinstance(src, dict):
214 for k, v in src.items():
215 if isinstance(v, torch.Tensor) and v.ndim >= 4:

Callers

nothing calls this directly

Calls 2

plot_metric_imagesFunction · 0.85
updateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…