Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slice layer #3055

Merged
merged 3 commits into from
Feb 15, 2025
Merged

Slice layer #3055

merged 3 commits into from
Feb 15, 2025

Conversation

kSkip
Copy link
Contributor

@kSkip kSkip commented Feb 9, 2025

I am training a U-Net using the architecture from U-Net: Convolutional Networks for Biomedical Image Segmentation. It requires cropping the tensors from the encoding stages to concatenate with the outputs of the decoding stages. I created the slice layer to implement this.

AFAICK, there are no layers in dlib that accommodate this. However, I probably should have asked before creating this one.

Some of my considerations:

  • It is similar to extract, but instead it copies non-contiguous regions to perform a 3D crop. This is both different behavior and performance.
  • I could have used an alias_tensor, similarly to extract, by aliasing every row. The CPU implementation may have been similar performance doing this. However, this is not ideal with CUDA because it requires a kernel invocation for every row. Since kernels on the same stream execute serially, the GPU throughput would not be saturated. So, I overloaded copy_tensor to copy non-contiguous rows with a single kernel call.
  • I don't know if the public interface is the best, but it works for my case. I'm happy to hear any suggestions or improvements so this can be useful to others.

Thanks for taking a look!

Copy link
Owner

@davisking davisking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice yeah this seems like a useful thing to have :D

const tensor& src,
size_t sk, size_t snr, size_t snc,
size_t k, size_t nr, size_t nc
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add docs for this (like you see for the other functions in this file :) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I forgot that. Added the doc string. Thanks!

@davisking davisking merged commit e8b6c05 into davisking:master Feb 15, 2025
10 checks passed
@davisking
Copy link
Owner

Thanks for the PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants