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

Method _default_iteration_print

monai/handlers/stats_handler.py:239–291  ·  view source on GitHub ↗

Execute iteration log operation based on Ignite `engine.state.output` data. Print the values from `self.output_transform(engine.state.output)`. Since `engine.state.output` is a decollated list and we replicated the loss value for every item of the decollated list, th

(self, engine: Engine)

Source from the content-addressed store, hash-verified

237 self.logger.info(out_str)
238
239 def _default_iteration_print(self, engine: Engine) -> None:
240 """
241 Execute iteration log operation based on Ignite `engine.state.output` data.
242 Print the values from `self.output_transform(engine.state.output)`.
243 Since `engine.state.output` is a decollated list and we replicated the loss value for every item
244 of the decollated list, the default behavior is to print the loss from `output[0]`.
245
246 Args:
247 engine: Ignite Engine, it can be a trainer, validator or evaluator.
248
249 """
250 loss = self.output_transform(engine.state.output)
251 if loss is None:
252 return # no printing if the output is empty
253
254 out_str = ""
255 if isinstance(loss, dict): # print dictionary items
256 for name in sorted(loss):
257 value = loss[name]
258 if not is_scalar(value):
259 warnings.warn(
260 "ignoring non-scalar output in StatsHandler,"
261 " make sure `output_transform(engine.state.output)` returns"
262 " a scalar or dictionary of key and scalar pairs to avoid this warning."
263 f" {name}:{type(value)}"
264 )
265 continue # not printing multi dimensional output
266 out_str += self.key_var_format.format(name, value.item() if isinstance(value, torch.Tensor) else value)
267 elif is_scalar(loss): # not printing multi dimensional output
268 out_str += self.key_var_format.format(
269 self.tag_name, loss.item() if isinstance(loss, torch.Tensor) else loss
270 )
271 else:
272 warnings.warn(
273 "ignoring non-scalar output in StatsHandler,"
274 " make sure `output_transform(engine.state.output)` returns"
275 " a scalar or a dictionary of key and scalar pairs to avoid this warning."
276 f" {type(loss)}"
277 )
278
279 if not out_str:
280 return # no value to print
281
282 num_iterations = engine.state.epoch_length
283 current_iteration = engine.state.iteration
284 if num_iterations is not None:
285 current_iteration = (current_iteration - 1) % num_iterations + 1
286 current_epoch = engine.state.epoch
287 num_epochs = engine.state.max_epochs
288
289 base_str = f"Epoch: {current_epoch}/{num_epochs}, Iter: {current_iteration}/{num_iterations} --"
290
291 self.logger.info(" ".join([base_str, out_str]))

Callers 1

iteration_completedMethod · 0.95

Calls 2

is_scalarFunction · 0.90
infoMethod · 0.80

Tested by

no test coverage detected