tueimage / gryds

A Python package for geometric transformations of images for data augmentation in deep learning
https://tue-image.nl
GNU General Public License v3.0
63 stars 14 forks source link

How to embedding the transformation into deep neural layers #5

Open ShuchaoPangUNSW opened 3 years ago

ShuchaoPangUNSW commented 3 years ago

Hi Koen,

   I have read your some papers about bspline transformation for medical image analysis. However, I'm still not sure how to change the bspline transformation operations into a neural network layer for training together. Can you show me how to do that? I sent emails to your student address, but it failed. I think you changed your email address.

Thanks a lot.

Yours sincerely, Shuchao

keppenhof commented 3 years ago

Hi Shuchao,

The Gryds transformations are not suitable for use inside neural networks because they are based on numpy and scipy code. They cannot be used in backpropagation during training.

If you want to have a deformable transformation inside your network that can be trained, I suggest looking at spatial transformer networks for the deep learning framework of your choice:

I hope this helps you a bit further. Best regards,

Koen

ShuchaoPangUNSW commented 3 years ago

Hi Koen,

       Thank you very much for your kind reply.

       Yes, you’re right. The Gryds transformation version is prepared to augment data for deep learning. If I want to use bspline transformation for building a new network, as you said I have to write a bspline transformation layer. Actually, I had built a model based on STN, however, as you know, the STN is a affine transformation, not a bspline transformation. So, the results said it is only a global transformation. So, If I want to have a local transformation, I have to build a new STN layer using like bspline transformation.

      Based on your great work, i.e., Fast contour propagation for MR-guided prostate radiotherapy using convolutional neural networks, I found you exploited this bspline transformation based layer and trained it together with other CNN layers, so I send you this email to ask for your experience.

      Can you help with it? Thanks a lot.

Yours sincerely, Shuchao

From: Koen @.> Sent: Monday, 5 July 2021 3:54 PM To: tueimage/gryds @.> Cc: Shuchao Pang @.>; Author @.> Subject: Re: [tueimage/gryds] How to embedding the transformation into deep neural layers (#5)

Hi Shuchao,

The Gryds transformations are not suitable for use inside neural networks because they are based on numpy and scipy code. They cannot be used in backpropagation during training.

If you want to have a deformable transformation inside your network that can be trained, I suggest looking at spatial transformer networks for the deep learning framework of your choice:

I hope this helps you a bit further. Best regards,

Koen

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/tueimage/gryds/issues/5#issuecomment-873822828, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AR4S7XMBGZNLMOEAQTSWBOTTWFCJDANCNFSM47ZYTHAQ.

keppenhof commented 2 years ago

In the paper you mentioned we actually did not use a B-spline transformation inside the network, we used a dense deformation field of the same dimensions as the image. The network in the paper outputs that vector field as an N_dim x N_x x N_y tensor (the green block in figure 2) and then uses that to reinterpolate the image. Sidenote: the network is trained on B-spline transformations but no B-spline transform is done inside the network.

Maybe a 2D example to make it more clear:

Let's say we have an image I(x) of shape N_x x N_y and a vector field u(x) with a vector for every pixel in that image, i.e. something that can be represented by a 2 x N_x x N_y tensor. You can write an interpolation layer that reinterpolates the image based on the vector field, i.e. it makes a new (transformed) image J(x) = I(x + u(x)).

For the paper we used Theano (which is no longer supported), but for instance PyTorch has a function for this: torchvision.transforms.functional_tensor._apply_grid_transform(image, vector_field). I'm sure something similar can be done in TensorFlow.