diff --git a/.github/workflows/pythonapp.yml b/.github/workflows/pythonapp.yml index 38c96b3b0d..4226ea7d99 100644 --- a/.github/workflows/pythonapp.yml +++ b/.github/workflows/pythonapp.yml @@ -105,6 +105,7 @@ jobs: python -m unittest -v env: QUICKTEST: True + PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION: python # https://github.com/Project-MONAI/MONAI/issues/4354 packaging: runs-on: ubuntu-latest @@ -189,6 +190,8 @@ jobs: ls -al python -m pip install -r requirements-dev.txt python -m unittest -v + env: + PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION: python # https://github.com/Project-MONAI/MONAI/issues/4354 build-docs: runs-on: ubuntu-latest diff --git a/docs/Makefile b/docs/Makefile index 1a3b22d020..d9a870064a 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -8,6 +8,9 @@ SPHINXBUILD ?= sphinx-build SOURCEDIR = source BUILDDIR = build +# https://github.com/Project-MONAI/MONAI/issues/4354 +export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION := python + # Put it first so that "make" without argument is like "make help". help: @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/monai/handlers/tensorboard_handlers.py b/monai/handlers/tensorboard_handlers.py index 0105e7c8ca..445e3e76ca 100644 --- a/monai/handlers/tensorboard_handlers.py +++ b/monai/handlers/tensorboard_handlers.py @@ -20,13 +20,12 @@ from monai.visualize import plot_2d_or_3d_image Events, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Events") +SummaryWriter, _ = optional_import("torch.utils.tensorboard", name="SummaryWriter") if TYPE_CHECKING: from ignite.engine import Engine - from torch.utils.tensorboard import SummaryWriter else: Engine, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Engine") - SummaryWriter, _ = optional_import("torch.utils.tensorboard", name="SummaryWriter") DEFAULT_TAG = "Loss" @@ -42,7 +41,7 @@ class TensorBoardHandler: """ - def __init__(self, summary_writer: Optional[SummaryWriter] = None, log_dir: str = "./runs"): + def __init__(self, summary_writer=None, log_dir: str = "./runs"): if summary_writer is None: self._writer = SummaryWriter(log_dir=log_dir) self.internal_writer = True @@ -82,13 +81,13 @@ class TensorBoardStatsHandler(TensorBoardHandler): def __init__( self, - summary_writer: Optional[SummaryWriter] = None, + summary_writer=None, log_dir: str = "./runs", iteration_log: bool = True, epoch_log: bool = True, - epoch_event_writer: Optional[Callable[[Engine, SummaryWriter], Any]] = None, + epoch_event_writer: Optional[Callable[[Engine, Any], Any]] = None, epoch_interval: int = 1, - iteration_event_writer: Optional[Callable[[Engine, SummaryWriter], Any]] = None, + iteration_event_writer: Optional[Callable[[Engine, Any], Any]] = None, iteration_interval: int = 1, output_transform: Callable = lambda x: x[0], global_epoch_transform: Callable = lambda x: x, @@ -179,7 +178,7 @@ def iteration_completed(self, engine: Engine) -> None: else: self._default_iteration_writer(engine, self._writer) - def _write_scalar(self, _engine: Engine, writer: SummaryWriter, tag: str, value: Any, step: int) -> None: + def _write_scalar(self, _engine: Engine, writer, tag: str, value: Any, step: int) -> None: """ Write scale value into TensorBoard. Default to call `SummaryWriter.add_scalar()`. @@ -194,7 +193,7 @@ def _write_scalar(self, _engine: Engine, writer: SummaryWriter, tag: str, value: """ writer.add_scalar(tag, value, step) - def _default_epoch_writer(self, engine: Engine, writer: SummaryWriter) -> None: + def _default_epoch_writer(self, engine: Engine, writer) -> None: """ Execute epoch level event write operation. Default to write the values from Ignite `engine.state.metrics` dict and @@ -216,7 +215,7 @@ def _default_epoch_writer(self, engine: Engine, writer: SummaryWriter) -> None: self._write_scalar(engine, writer, attr, getattr(engine.state, attr, None), current_epoch) writer.flush() - def _default_iteration_writer(self, engine: Engine, writer: SummaryWriter) -> None: + def _default_iteration_writer(self, engine: Engine, writer) -> None: """ Execute iteration level event write operation based on Ignite `engine.state.output` data. Extract the values from `self.output_transform(engine.state.output)`. @@ -295,7 +294,7 @@ class TensorBoardImageHandler(TensorBoardHandler): def __init__( self, - summary_writer: Optional[SummaryWriter] = None, + summary_writer=None, log_dir: str = "./runs", interval: int = 1, epoch_level: bool = True, diff --git a/monai/visualize/img2tensorboard.py b/monai/visualize/img2tensorboard.py index 0af05adf32..6dd7abcbf1 100644 --- a/monai/visualize/img2tensorboard.py +++ b/monai/visualize/img2tensorboard.py @@ -21,14 +21,13 @@ PIL, _ = optional_import("PIL") GifImage, _ = optional_import("PIL.GifImagePlugin", name="Image") SummaryX, _ = optional_import("tensorboardX.proto.summary_pb2", name="Summary") +SummaryWriter, _ = optional_import("torch.utils.tensorboard", name="SummaryWriter") SummaryWriterX, has_tensorboardx = optional_import("tensorboardX", name="SummaryWriter") if TYPE_CHECKING: from tensorboard.compat.proto.summary_pb2 import Summary - from torch.utils.tensorboard import SummaryWriter else: Summary, _ = optional_import("tensorboard.compat.proto.summary_pb2", name="Summary") - SummaryWriter, _ = optional_import("torch.utils.tensorboard", name="SummaryWriter") __all__ = ["make_animated_gif_summary", "add_animated_gif", "plot_2d_or_3d_image"] @@ -104,7 +103,7 @@ def make_animated_gif_summary( def add_animated_gif( - writer: SummaryWriter, + writer, tag: str, image_tensor: Union[np.ndarray, torch.Tensor], max_out: int = 3, @@ -136,7 +135,7 @@ def add_animated_gif( def plot_2d_or_3d_image( data: Union[NdarrayTensor, List[NdarrayTensor]], step: int, - writer: SummaryWriter, + writer, index: int = 0, max_channels: int = 1, frame_dim: int = -3, diff --git a/runtests.sh b/runtests.sh index 01d04dafd8..ec9493f6b2 100755 --- a/runtests.sh +++ b/runtests.sh @@ -14,6 +14,9 @@ # script for running all tests set -e +# FIXME: https://github.com/Project-MONAI/MONAI/issues/4354 +export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python + # output formatting separator="" blue=""