Augmentor icon indicating copy to clipboard operation
Augmentor copied to clipboard

Using Augmentor with TensorFlow

Open pronot opened this issue 7 years ago • 10 comments

How can i use Augmentor on the fly with tensorflow? There's an example with Keras. Can you provide an example with tensor flow too?

pronot avatar Sep 29 '17 11:09 pronot

Hi there, as I do not have TensorFlow currently installed on my main dev machine, I can't really add this functionality at present. However, it is something I definitely want to add, so I will take a look at doing this some time soon. I can't make any promises regarding when this might be unfortunately. That said, I'd be happy to take a look at a PR you might want to make!

mdbloice avatar Sep 29 '17 12:09 mdbloice

you may like miniconda to setup tensorflow with cpu / very simple to get up and running. https://gist.github.com/johndpope/187b0dd996d16152ace2f842d43e3990

johndpope avatar Oct 03 '17 13:10 johndpope

Hello @mdbloice

Do you have some hints to get started?

benjaminbarbe avatar Dec 05 '17 15:12 benjaminbarbe

@benjaminbarbe If I have correctly understood your question, I guess that there is no too much difference in case of tensorflow. At least, when you're writing a training loop by hand, like in this snippet (probably has typos, but I guess you've got the idea):

from Augmentor.Operations import RotateRange
from PIL import Image

op = RotateRange(probability=0.5, max_left_rotation=30, max_right_rotation=30)

with tf.Session() as session:
    tf.global_variables_initialized().run()
    for epoch in range(n_epochs):
        for x_batch, y_batch in training_batches:
            pil_images = [Image.fromarray(x) for x in x_batch]
            augmented = op.perform_operation(pil_images)
            arrays = [np.asarray(img, dtype=float) for img in augmented]
            sess.run([train_op], feed_dict={x: x_batch, y: y_batch})

Probably there are some helpful utils in the library to make this process more simple. Though if you want to augment images right on GPU, then you need something more involved.

devforfu avatar Apr 08 '18 10:04 devforfu

Tensorflow has the standard data processing API. I'm interested to interface Augmentor with the API.

As I can see, Augmentor is implemented entirely in Python. It should be possible to use tf.py_func to wrap Augmentor's functionality and provide data mappers that can be used with tf.data.Dataset.

@mdbloice, I'm not very familiar with Augmentor, so what would be a good point to start? Also, can Augmentor process images on a GPU?

alshedivat avatar Oct 02 '18 14:10 alshedivat

I came across this repo the other week - https://github.com/tueimage/SE2CNN it will build a roto-translation covariant convolutional network with tensorflow. (so you don't need to add extra images to training augmentation - the neural net will allow invariants in rotation / translation) https://arxiv.org/pdf/1804.03393

johndpope avatar Oct 02 '18 15:10 johndpope

fyi - Jonas Adler @adler-j has created a tensorflow example for deforming images with his own function. There's a tensorflow sample. Should be able to cherry pick / glue tensorflow code https://github.com/adler-j/tfdeform

johndpope avatar Oct 13 '18 11:10 johndpope

Hi @alshedivat, sorry for the delay in replying, work duties have kept me from doing any work on Augmentor for the past few weeks. Actually, what I am working on next is proper support for TensorFlow. Right now, using Augmentor with Keras should work, and PyTorch should also work. Next step is to get Augmentor working with TF properly without workarounds. Expect it in the next release (although I cannot say for sure when that might be, unfortunately).

mdbloice avatar Oct 22 '18 07:10 mdbloice

@mdbloice, cool, thanks for your reply! Do you have a roadmap for TF support? I'm pretty familiar with TF and it's Dataset API, so happy to contribute if that accelerates development.

Also, it looks like Augmentor doesn't use opencv, does it? Another image augmentation library shows that using highly optimized opencv gives best throughput.

alshedivat avatar Oct 22 '18 19:10 alshedivat

HI @alshedivat, no I do not really have a roadmap right now for TF support, I am only able to work on Augmentor when time allows. And it's not really predictable when that might be, so I cannot say for sure when I might be able to work on new features or bugs unfortunately!

As for OpenCV, no I do not use it because it is a rather large dependency that I wanted to avoid. The library you linked to looks good though! I think with multi-threading the throughput in Augmentor is pretty good.

As for contributing, that would be great - I presume TF expects a generator of some kind, and if you check out the code for the keras_generator() function, you'll see how it works:

https://github.com/mdbloice/Augmentor/blob/5a8f6d6080ecbaa20af1a7bf2824f7fdbfd0ab94/Augmentor/Pipeline.py#L456-L543

If you can write a function like tf_generator or something similar that would output images in the format that is expected by TensorFlow, then I think that would be all there is to it! Let me know what you think.

mdbloice avatar Oct 23 '18 07:10 mdbloice