Reading pyramidal czi files in python

Hi Forum,

I need some advice on how to proceed on loading (large) pyramidal microscopy data for deep learning in Python. This is my scenario:

Training: I opened and annotated some czi images in QuPath and use the Tile Exporter to create some 4k (.tif) tiles with raw data and respective annotation. I do the training not at full resolution (20k x 20k, 0.44µm pixel size), but at a lower resolution level ( 3k x 3k, 1.8µm pixel size). I read the .tif tiles with python bio-formats.

Inference: To apply my model to all raw images, I have to open the czi images, downsample them to the correct resolution and run them through my model. I currently use python bioformats for this task, as it appears to be the only python loader that supports pyramidal image data - and I would like to avoid loading the whole image at full resolution, if possible.

Here’s the catch: Using bioformats to read the tiles is massively slower than other python packages (e.g. tifffile, cv2, PIL), and hence a huge bottleneck for the training process. On the other hand, using other readers for the training messes up the data types/color scaling badly so that my model doesn’t work at all in the inference mode. But since bio-formats appears to be the only python package that supports reading pyramidal czi images, I’m tied to using it in this step. The way out is either digging deeper into the statistics and data type conversion of different file readers, ooor simply using the same reader during testing and inference.

TL;DR: I’m looking for a python image reader that’s fast (i.e. faster than bioformats) and supports pyramidal czi images.

Thanks in advance!
Johannes

Hi @EL_Pollo_Diablo,

I think what you are looking for is this:

It is based on our c++ library libCZI which can be found here:

2 Likes

Hi @sebi06 ,

thanks for the quick reply! I tried to use aicsimageio already, but I wasn’t aware that the czi pyramidal format is actually a chunked data format - the mosaic reading function is exactly what I need.

Thanks!

Edit: The aicsimageio image reader is (without having measured) clearly superior to bio-formats in terms of speed. Nice.

Hi @EL_Pollo_Diablo,

aicsimageio is using aicspylibczi “under the hood” but so far explicit states that for mosaic CZI images the aicspylibczi should be used directly.

Be aware that you might still be carefule, because reading a whole CZI mosaic at once can be challenging … :-). But one can also read arbitrary ROIs from a CZI or even subblocks.

Hi @sebi06 ,

I think I finally also found the root problem for my above-question. I now use aicsimageio to import tif files

    img = AICSImage(img_path)
    image = img.get_image_dask_data("YXC", S=0, T=0, Z=0).compute()  # returns dask array

and aicspylibczi to import the czi files for inference:

    czi = aicspylibczi.CziFile(filename)
    image = czi.read_mosaic(C = 0, scale_factor=image_pixsize/target_pixsize)

…and I found the problem to persist: The model’s prediction is close to nonsense and can only distinguish sample/background.

Turns out, that the order of the color channels is inverted (BGR instead of RGB) if I use aicspylibczi for image import.
Here’s a sample of the same image region as imported from the tif tile (left) and cropped from the whole czi-image file (right):

from_tile from_image

Am I using either of the packages incorrectly? Or could this be a glitch in one of the packages?

Hi @EL_Pollo_Diablo,

ZEN is using BRG(A) colorspace (no a glitch).

But es specially when it comes to train networks etc. you can make use of our CZMODEL package which offers a dedicated processing layer to deal with BGR → RGB topic.

“While ANN models are often trained on images in RGB(A) space, the ZEN infrastructure requires models inside a CZModel to expect inputs in BGR(A) color space. This toolbox offers pre-processing layers to convert the color space before passing the input to the model to be actually deployed. The following code shows how to add a RGB to BGR conversion layer to a model and set its spatial input dimensions to 512x512.”

Have a look here: czmodel · PyPI

Maybe this can help you. Otherwise you have to just keep this in mind.

2 Likes