Skip to content
New issue

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

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

Already on GitHub? # to your account

Add Elastic Deformation for KPL #163

Open
innat opened this issue Mar 6, 2022 · 14 comments
Open

Add Elastic Deformation for KPL #163

innat opened this issue Mar 6, 2022 · 14 comments

Comments

@innat
Copy link
Contributor

innat commented Mar 6, 2022

This type of augmentation is mostly sued in 2D and 3D image augmentation, especially in medical imaging.

- Source.

TensorFlow (DeepMind): How to use the On-the-Fly Elastic Deformation for TensorFlow

@innat
Copy link
Contributor Author

innat commented Apr 20, 2022

Some starters in python. https://gist.github.com/fmder/e28813c1e8721830ff9c

import numpy as np
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter

def elastic_transform(image, alpha, sigma, random_state=None):
    if random_state is None:
        random_state = np.random.RandomState(None)

    shape = image.shape
    dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma, mode="constant", cval=0) * alpha
    dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma, mode="constant", cval=0) * alpha
    dz = np.zeros_like(dx)
    x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
    indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1)), np.reshape(z, (-1, 1))
    distored_image = map_coordinates(image, indices, order=1, mode='reflect')
    return distored_image.reshape(image.shape)
img = cv2.imread('sample.png')[:,:,::-1]
out = elastic_transform(img, alpha=1000, sigma=7)

download

@Tony363
Copy link

Tony363 commented Apr 23, 2022

@innat is there a style guide or API method guide in which i should implement this feature to? Like an overall API architecture design? I hope to follow up on this and look into what i can do about this after my midterms, after 2-3 days. @bhack mentioned to like at tensorflow_addons implementation. It can't be as simple as reimplementing it to KerasCV API right?

@innat
Copy link
Contributor Author

innat commented Apr 24, 2022

is there a style guide or API method guide in which i should implement this feature to? Like an overall API architecture design?

I'm not sure what you're looking for. For style guide or API method guide, is this sufficient? For implementation references, you can follow the following reference implementation.

@Tony363
Copy link

Tony363 commented Apr 24, 2022

I was just thinking if there are any strict coding style I should follow or any KerasCV API design pattern i should keep in mind during implementing the different object classes and methods. I think this is perfect ! I'll be proactive, keep being curious and continue to communicate ~

@Luvideria
Copy link

Hello, I recently posted an issue on tensorflow addons: tensorflow/addons#2733

I created a repo containing and explaining a working implementation of the catmull-rom (quite similar to Lanczos-2) interpolation as well as a comparison and a test
https://github.com/Luvideria/tensorflow-dense-warp-catmull-rom

It behaves just like dense_image_warp.py but uses a better interpolation scheme. I have no idea if it is up to the standards of these repositories. I use it in my research, and it works well enough.

@innat
Copy link
Contributor Author

innat commented Jun 29, 2022

@Luvideria I didn't try dense_image_wrap from tensorflow addons. Do you think that function or your catmull-ran address this feature request (elastic deformation)?

@Luvideria
Copy link

@Luvideria I didn't try dense_image_wrap from tensorflow addons. Do you think that function or your catmull-rom address this feature request (elastic deformation)?

I am not 100% sure. My use case is not elastic deformation but motion vector reprojection.

Earlier @bhack mentionned dense_image_warp which may or may not make fully sense for the specific use case of this thread.

I think that the dense_image can help with the last stage of elastic deformation. Dense_image_warp needs a list of sample points, which means the transform has to be computed by other means. Inside dense_image_warp it's like this:

# flatten the input list
       interpolated = interpolate_catmull_rom(image, query_points_flattened)
       interpolated = tf.reshape(interpolated, [batch_size, height, width, channels])

It's just a list of sample points.

In any case, I think that this dense image warp with catmull rom (or higher order, and not necessarily my implementation) deserves to be integrated in one way or another. I can eventually post this somewhere else where you deem it appropriate. I already mentioned it in tensorflow_addons: tensorflow/addons#2733
This seemed appropriate at the time as the original dense_image_warp comes from there.

@bhack
Copy link
Contributor

bhack commented Jul 15, 2022

I think that the dense_image can help with the last stage of elastic deformation. Dense_image_warp needs a list of sample points, which means the transform has to be computed by other means.

If we see tensorlayer elastic_transform and elastic_tranform_multi it makes sense.

https://tensorlayer.readthedocs.io/en/v2.2.4/_modules/tensorlayer/prepro.html#elastic_transform

@Luvideria Probably you could open a new ticket in this repo for porting/refactoring the dense_image_warp from TF Addons eventually with your new interpolation.

Then we could use it for build up the Elastic Deformation (as Tensorlayer done with scipy map_coordinates).

@innat
Copy link
Contributor Author

innat commented Jul 24, 2022

@Tony363 @Luvideria
Here is another tf implementation reference for elastic transform. Placing it here in case you're interested to contribute.

https://github.com/hirune924/imgaug-tf/blob/main/imgaugtf/augmentations.py#L253-L269

@innat
Copy link
Contributor Author

innat commented Jul 25, 2022

@Luvideria
Could you please follow @bhack's previous comment? It's needed to port tfa..dense_image_warp layer (with your addition) first as elastic transform might depend on it.

@Luvideria
Copy link

@Luvideria Could you please follow @bhack's previous comment? It's needed to port tfa..dense_image_warp layer (with your addition) first as elastic transform might depend on it.

done

Copy link

This issue is stale because it has been open for 180 days with no activity. It will be closed if no further activity occurs. Thank you.

@sachinprasadhs
Copy link
Collaborator

Thanks for reporting the issue! We have consolidated the development of KerasCV into the new KerasHub package, which supports image, text, and multi-modal models. Please read keras-team/keras-hub#1831. KerasHub will support all the core functionality of KerasCV.

KerasHub can be installed with !pip install -U keras-hub. Documentation and guides are available at keras.io/keras_hub.

With our focus shifted to KerasHub, we are not planning any further development or releases in KerasCV. If you encounter a KerasCV feature that is missing from KerasHub, or would like to propose an addition to the library, please file an issue with KerasHub.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

6 participants