CellProfiling / HPA-Cell-Segmentation

Apache License 2.0
95 stars 26 forks source link
  1. The =cellsegmentator= module which implements a class =CellSegmentator= to make running segmentation from Python code easier.
  2. A =utils= module which contain a couple of post-processing functions that is good to use when working with images from the Human Protein Cell Atlas.

You can verify the installation has worked by running

+begin_example

python -c 'import hpacellseg'

+end_example

in a terminal.

If you want to use anaconda/miniconda (env tested on a100 gpu):

Note that it can be nice to run the pip steps in the install script yourself, trying them with the --dry-run flag to make sure they won't overwrite the desired torch and numpy versions. The torch and cuda versions should be changed for the needed setup, we just can't have torch higher than 1.9.1. Using =conda search pytorch= you can find the right property string to add after the version number in the environment yaml.

It is located in ~hpacellseg.cellsegmentator~.

*** Arguments

*** Methods The two segmentation functions are

Note that both these functions assume that all input images are of the same shape!!

** Post processing The two available post-processing functions are located in the ~hpacellseg.utils~ module. They are:

** Example usage

+begin_src python

import hpacellseg.cellsegmentator as cellsegmentator from hpacellseg.utils import label_cell, label_nuclei from imageio import imwrite

Assuming that there are images in the current folder with the

following names.

images = [ ["microtubules_one.tif", "microtubules_two.tif"], ["endoplasmic_reticulum_one.tif", "endoplasmic_reticulum_two.tif"], ["nuclei_one.tif", "nuclei_two.tif"] ] NUC_MODEL = "./nuclei-model.pth" CELL_MODEL = "./cell-model.pth" segmentator = cellsegmentator.CellSegmentator( NUC_MODEL, CELL_MODEL, scale_factor=0.25, device="cuda",

NOTE: setting padding=True seems to solve most issues that have been encountered

  #       during our single cell Kaggle challenge.
  padding=False,
  multi_channel_model=True,

)

For nuclei: taking in nuclei channels as inputs

nuc_segmentations = segmentator.pred_nuclei(images[2])

For full cells: taking in 3 channels as inputs

cell_segmentations = segmentator.pred_cells(images)

post-processing nuclei mask

nuclei_mask = label_nuclei(nuc_segmentations[0])

post-processing nuclei and cell mask

for i, (nuc_segmentation, cell_segmentation) in enumerate(zip(nuc_segmentations, cell_segmentations)): nuclei_mask, cell_mask = label_cell(nuc_segmentation, cell_segmentation)

Save these masks in local folder

  imwrite(f"nucleimask_{i}.png", nuclei_mask)
  imwrite(f"cellmask_{i}.png", cell_mask)

+end_src