Skip to content

SemanticSegmentor breaks when using some stride shapes #1071

@Jiaqi-Lv

Description

@Jiaqi-Lv

For example, using the grandqc tissue mask model with SemanticSegmentor:
If using the default stride shape which is (256,256), everything works fine
Code:

from pathlib import Path
from tiatoolbox.models.engine.semantic_segmentor import SemanticSegmentor

OUTPUT_DIR = Path("/media/u1910100/data/overlays/test") # where .db files will be saved
MODEL = "grandqc_tissue_detection"
BATCH_SIZE = 16
NUM_WORKERS = 4
DEVICE = "cuda"  # use "cpu" if no GPU is available
# ---------------------------------------------------------------------------
segmentor = SemanticSegmentor(
    model=MODEL,
    batch_size=BATCH_SIZE,
    num_workers=NUM_WORKERS,
)
all_slides = ["/media/u1910100/data/slides/TCGA-AO-A0J2-01Z-00-DX1.7C9FEC7B-6040-4C58-9563-D10C0D7AC72E.svs"]
results = segmentor.run(
    images=all_slides,
    patch_mode=False,
    auto_get_mask=False,
    output_type="annotationstore",
    save_dir=OUTPUT_DIR,
    overwrite=True,
    device=DEVICE,
)
print(f"Done. Annotation stores saved to {OUTPUT_DIR}")

Output:

(tiatoolbox-dev) u1910100@u1910100-Precision-3660:/media/u1910100/data/GitHub/tiatoolbox$ python test.py 
|2026-05-28|16:38:32.581| [INFO] HTTP Request: HEAD https://huggingface.co/TIACentre/GrandQC_Tissue_Detection/resolve/main/grandqc_tissue_detection.pth "HTTP/1.1 302 Found"
|2026-05-28|16:38:32.658| [WARNING] GPU is not compatible with torch.compile. Compatible GPUs include NVIDIA V100, A100, and H100. Speedup numbers may be lower than expected.
|2026-05-28|16:38:32.720| [INFO] When providing multiple whole slide images, the outputs will be saved and the locations of outputs will be returned to the calling function when `run()` finishes successfully.
Processing WSIs:   0%|                                                                                                                                           | 0/1 [00:00<?, ?it/s|2026-05-28|16:38:35.926| [INFO] Saving predictions as annotationstore in .db format.                                                                                                   
                                                                                                                                                                                      |2026-05-28|16:38:35.982| [WARNING] Invalid geometry found, fix using buffer().                                                                                   | 0/1 [00:00<?, ?it/s]
                                                                                                                                                                                      |2026-05-28|16:38:36.149| [INFO] Output file saved at /media/u1910100/data/overlays/test/TCGA-AO-A0J2-01Z-00-DX1.7C9FEC7B-6040-4C58-9563-D10C0D7AC72E.db.                               
Done. Annotation stores saved to /media/u1910100/data/overlays/test

If I change the stride shape to a different value, such as (64, 64), it does't work anymore.
Code:

results = segmentor.run(
    images=all_slides,
    patch_mode=False,
    auto_get_mask=False,
    output_type="annotationstore",
    save_dir=OUTPUT_DIR,
    overwrite=True,
    device=DEVICE,
    stride_shape=(64, 64)
)

Error:

(tiatoolbox-dev) u1910100@u1910100-Precision-3660:/media/u1910100/data/GitHub/tiatoolbox$ python test.py 
|2026-05-28|16:40:01.248| [INFO] HTTP Request: HEAD https://huggingface.co/TIACentre/GrandQC_Tissue_Detection/resolve/main/grandqc_tissue_detection.pth "HTTP/1.1 302 Found"
|2026-05-28|16:40:01.296| [WARNING] GPU is not compatible with torch.compile. Compatible GPUs include NVIDIA V100, A100, and H100. Speedup numbers may be lower than expected.
|2026-05-28|16:40:01.364| [INFO] When providing multiple whole slide images, the outputs will be saved and the locations of outputs will be returned to the calling function when `run()` finishes successfully.
Traceback (most recent call last):                                                                                                                                                     
  File "/media/u1910100/data/GitHub/tiatoolbox/test.py", line 27, in <module>                                                                                                          
    results = segmentor.run(
              ^^^^^^^^^^^^^^
  File "/media/u1910100/data/GitHub/tiatoolbox/tiatoolbox/models/engine/semantic_segmentor.py", line 1098, in run
    return super().run(
           ^^^^^^^^^^^^
  File "/media/u1910100/data/GitHub/tiatoolbox/tiatoolbox/models/engine/patch_predictor.py", line 668, in run
    return super().run(
           ^^^^^^^^^^^^
  File "/media/u1910100/data/GitHub/tiatoolbox/tiatoolbox/models/engine/engine_abc.py", line 1820, in run
    return self._run_wsi_mode(
           ^^^^^^^^^^^^^^^^^^^
  File "/media/u1910100/data/GitHub/tiatoolbox/tiatoolbox/models/engine/engine_abc.py", line 1652, in _run_wsi_mode
    raw_predictions = self.infer_wsi(
                      ^^^^^^^^^^^^^^^
  File "/media/u1910100/data/GitHub/tiatoolbox/tiatoolbox/models/engine/semantic_segmentor.py", line 616, in infer_wsi
    raw_predictions["probabilities"] = merge_vertical_chunkwise(
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/media/u1910100/data/GitHub/tiatoolbox/tiatoolbox/models/engine/semantic_segmentor.py", line 1470, in merge_vertical_chunkwise
    curr_chunk[-overlap:] += next_chunk[:overlap]
ValueError: operands could not be broadcast together with shapes (64,1989,2) (448,1989,2) (64,1989,2)

It seems that this error only comes up with some specific stride shapes, for example if I use a stride of (400x400), it works fine:

results = segmentor.run(
    images=all_slides,
    patch_mode=False,
    auto_get_mask=False,
    output_type="annotationstore",
    save_dir=OUTPUT_DIR,
    overwrite=True,
    device=DEVICE,
    stride_shape=(400, 400)
)
(tiatoolbox-dev) u1910100@u1910100-Precision-3660:/media/u1910100/data/GitHub/tiatoolbox$ python test.py 
|2026-05-28|16:48:47.804| [INFO] HTTP Request: HEAD https://huggingface.co/TIACentre/GrandQC_Tissue_Detection/resolve/main/grandqc_tissue_detection.pth "HTTP/1.1 302 Found"
|2026-05-28|16:48:47.852| [WARNING] GPU is not compatible with torch.compile. Compatible GPUs include NVIDIA V100, A100, and H100. Speedup numbers may be lower than expected.
|2026-05-28|16:48:47.935| [INFO] When providing multiple whole slide images, the outputs will be saved and the locations of outputs will be returned to the calling function when `run()` finishes successfully.
Processing WSIs:   0%|                                                                                                                              | 0/1 [00:00<?, ?it/s|2026-05-28|16:48:49.904| [INFO] Saving predictions as annotationstore in .db format.                                                                                      
                                                                                                                                                                         |2026-05-28|16:48:49.941| [WARNING] Invalid geometry found, fix using buffer().                                                                      | 0/1 [00:00<?, ?it/s]
                                                                                                                                                                         |2026-05-28|16:48:50.058| [INFO] Output file saved at /media/u1910100/data/overlays/test/TCGA-AO-A0J2-01Z-00-DX1.7C9FEC7B-6040-4C58-9563-D10C0D7AC72E.db.                  
Done. Annotation stores saved to /media/u1910100/data/overlays/test

...
(Stride shape of 128x128 also doesn't work)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions