Skip to content

Commit aac82f5

Browse files
author
Eric Kerfoot
committed
Trying a better way of getting length
Signed-off-by: Eric Kerfoot <eric.kerfoot@gmail>
1 parent 10d8c9a commit aac82f5

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

monai/engines/workflow.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import warnings
1515
from collections.abc import Callable, Iterable, Sequence
16-
from typing import TYPE_CHECKING, Any
16+
from typing import TYPE_CHECKING, Any, Sized
1717

1818
import torch
1919
import torch.distributed as dist
@@ -133,12 +133,9 @@ def __init__(
133133
def set_sampler_epoch(engine: Engine) -> None:
134134
sampler.set_epoch(engine.state.epoch)
135135

136-
# if the epoch_length isn't given, attempt to get it from the length of the data loader
137-
if epoch_length is None:
138-
try:
139-
epoch_length = len(data_loader)
140-
except TypeError: # raised when data_loader has an iterable dataset with no length, or is some other type
141-
pass # deliberately leave epoch_length as None
136+
# if the epoch_length isn't given, attempt to get it from the length of the data loader
137+
if epoch_length is None and isinstance(data_loader.dataset, Sized):
138+
epoch_length = len(data_loader.dataset)
142139

143140
# set all sharable data for the workflow based on Ignite engine.state
144141
self.state: Any = State(

0 commit comments

Comments
 (0)