Skip to content

Commit

Permalink
test that openslide.OpenSlide is available (#199)
Browse files Browse the repository at this point in the history
* test that openslide.OpenSlide is available

* do not bound sphinx version

* use PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 for macos test

* add WSINFER_FORCE_CPU env var

This forces CPU device, even if CUDA or MPS are available. The main use
case is for testing on github actions, where I faced failures with MPS.
  • Loading branch information
kaczmarj authored Nov 11, 2023
1 parent d505734 commit 45c4eed
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
mkdir slides && cd slides
wget -q https://openslide.cs.cmu.edu/download/openslide-testdata/Aperio/JP2K-33003-1.svs
cd ..
wsinfer run --wsi-dir slides/ --results-dir results/ --model breast-tumor-resnet34.tcga-brca
WSINFER_FORCE_CPU=1 wsinfer run --wsi-dir slides/ --results-dir results/ --model breast-tumor-resnet34.tcga-brca
test -f results/run_metadata_*.json
test -f results/patches/JP2K-33003-1.h5
test -f results/model-outputs-csv/JP2K-33003-1.csv
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ dev = [
]
docs = [
"pydata-sphinx-theme",
"sphinx<6.0.0",
"sphinx",
"sphinx-autoapi",
"sphinx-click",
"sphinx-copybutton",
Expand Down
10 changes: 9 additions & 1 deletion wsinfer/modellib/run_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
from __future__ import annotations

import os
from pathlib import Path
from typing import TYPE_CHECKING
from typing import cast as type_cast
Expand Down Expand Up @@ -93,7 +94,14 @@ def run_inference(
model.eval()

# Set the device.
if torch.cuda.is_available():
# Use CPU if env var specifies. Prefer checking if this is false, because
# if the var is set to almost anything (other than 0, False, f), then it
# should be true.
# This env var was introduced mainly for continuous integration tests that
# failed on apple silicon in github actions. Forcing to cpu avoids failures.
if os.getenv("WSINFER_FORCE_CPU", "0").lower() not in {"0", "f", "false"}:
device = torch.device("cpu")
elif torch.cuda.is_available():
device = torch.device("cuda")
if torch.cuda.device_count() > 1:
model = torch.nn.DataParallel(model)
Expand Down
4 changes: 4 additions & 0 deletions wsinfer/wsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
try:
import openslide

# Test that OpenSlide object exists. If it doesn't, an error will be thrown and
# caught. For some reason, it is possible that openslide-python can be installed
# but the OpenSlide object (and other openslide things) are not available.
openslide.OpenSlide
HAS_OPENSLIDE = True
logger.debug("Imported openslide")
except Exception as err:
Expand Down

0 comments on commit 45c4eed

Please sign in to comment.