seung-lab / connected-components-3d

Connected components on discrete and continuous multilabel 3D & 2D images. Handles 26, 18, and 6 connected variants; periodic boundaries (4, 8, & 6)
GNU Lesser General Public License v3.0
356 stars 42 forks source link

Any way to make this GPU Compatible? #122

Closed aymuos15 closed 2 months ago

aymuos15 commented 3 months ago

Maybe a naive question, but -> It becomes a bit tedious detaching and reattaching stuff when this is required within a pipeline. Is there any way this can be done through something like Pytorch?

william-silversmith commented 3 months ago

There are GPU algorithms for CCL but they are based on a totally different algorithm. The one I've used here is based on a complex decision tree (meaning branch after branch) which would murder a GPU.

It's kind of an interesting challenge though. I'll think about it. I don't know PyTorch well. Are there no GPU CCL algos for it already?

On Wed, May 15, 2024, 10:22 AM Soumya Snigdha Kundu < @.***> wrote:

Maybe a naive question, but -> It becomes a bit tedious detaching and reattaching stuff when this is required within a pipeline. Is there any way this can be done through something like Pytorch?

— Reply to this email directly, view it on GitHub https://github.com/seung-lab/connected-components-3d/issues/122, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATGQSKFMHO5E55FKW2VHBDZCNVSTAVCNFSM6AAAAABHYJATEOVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI4TQMBYGQYTSOI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

william-silversmith commented 3 months ago

I found this:

https://github.com/zsef123/Connected_components_PyTorch

This lab has the fastest CCL implementations point blank for binary images last I checked.

On Wed, May 15, 2024, 10:29 AM William Silversmith < @.***> wrote:

There are GPU algorithms for CCL but they are based on a totally different algorithm. The one I've used here is based on a complex decision tree (meaning branch after branch) which would murder a GPU.

It's kind of an interesting challenge though. I'll think about it. I don't know PyTorch well. Are there no GPU CCL algos for it already?

On Wed, May 15, 2024, 10:22 AM Soumya Snigdha Kundu < @.***> wrote:

Maybe a naive question, but -> It becomes a bit tedious detaching and reattaching stuff when this is required within a pipeline. Is there any way this can be done through something like Pytorch?

— Reply to this email directly, view it on GitHub https://github.com/seung-lab/connected-components-3d/issues/122, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATGQSKFMHO5E55FKW2VHBDZCNVSTAVCNFSM6AAAAABHYJATEOVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI4TQMBYGQYTSOI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

aymuos15 commented 3 months ago

There are a few but each has their own issue.

https://github.com/prittt/YACCLAB - not in pytroch https://github.com/zsef123/Connected_components_PyTorch.git - found this to be a bit buggy.

Could not find anything else really


Just saw your message, guess Ill just continue with that for the time being.

william-silversmith commented 3 months ago

Just out of curiosity, what variation of CCL are you attempting to apply? If I get into GPU algorithms, I just want to know what people are interested in doing so I apply my energy in the right direction.

aymuos15 commented 3 months ago

The one which comes closest to what is in this library. I personally mostly use: 4-connected CCL of binary image.

Thank you for even considering it :) I will reopen the issue then for the time being.

aymuos15 commented 2 months ago

My advisor actually suggested a very nice workaround for this (at least for my own requirements)

import torch
import cupy as cp
from cucim.skimage import measure as cucim_measure

def get_connected_components(img, connectivity=None):

    img_cupy = cp.asarray(img)
    labeled_img, num_features = cucim_measure.label(img_cupy, connectivity=connectivity, return_num=True)
    labeled_img_torch = torch.as_tensor(labeled_img, device=img.device)

    return labeled_img_torch, num_features

Mostly inspired from here: https://github.com/Project-MONAI/MONAI/blob/59a7211070538586369afd4a01eca0a7fe2e742e/monai/transforms/utils.py#L1061-L1107

Made a small repo for it :) https://github.com/aymuos15/GPU-Connected-Components-Pytorch