aleju / imgaug

Image augmentation for machine learning experiments.
http://imgaug.readthedocs.io
MIT License
14.37k stars 2.44k forks source link

How to random augment process some patch of one img? #492

Open Ronales opened 4 years ago

Ronales commented 4 years ago

Dear author,I want process one img with random area in this img,such as contrast ,brightness in random area in this img Instead of augment process over img,what should i do with this imgaug?Thanks very much!

aleju commented 4 years ago

If you only want to augment a subarea of an image, it would probably make most sense to just extract it, augment it and then re-insert it into the full image.

image = ...
image_sub = image[10:20, 15:25, :]
image_sub_aug = iaa.LinearContrast((0.5, 1.5))(image=image)
image[10:20, 15:25, :] = image_sub_aug
Ronales commented 4 years ago

I get it, thanks for your reply.by the way, can I use Gauss_blur to achieve image augment, which means augment Gaussian distribution instead of uniform distribution with certain mask in one image.

this is uniform distribution, can i use this tool to achieve similar Gaussian distribution augment performance? image

aleju commented 4 years ago

Do you mean that you want to have parameters that are following gaussian distributions? Something like MultiplyBrightness(factor) with factor ~ N(1.0, 0.5)? You can use the parameters in imgaug.parameters for that. E.g.

import imgaug.parameters as iap
aug = iaa.LinearContrast(iap.Normal(1.0, 0.5))

# or e.g.:
aug = iaa.LinearContrast(iap.TruncatedNormal(1.0, 0.5, low=1.0-0.5, high=1.0+0.5))
Ronales commented 4 years ago

I get it,my code is:

aug = iaa.Add((-50, 50), per_channel=0.1)
# image = ia.quokka(size=0.22)
image=imageio.imread(r"C:\Users\ycc\Desktop\voc\someimages_cocotest\2007_001630.jpg")
batches = [[image] * 8, [image] * 8]  # two batches of each three images

# augment in stochastic mode
images_stochastic = [aug.augment_images(batch) for batch in batches]

Another question,if I want to choose one or two augment result as my final augment in pytorch train,instead of all possible result of iaa.Add function,can i find out this centain value for my choose parameter.as you know,8 result means 8 different parameter.but i just want to choose little augment result.

image

otherwise,If I use imgaug tools to augment dataset in pytorch,Is it enough to just watch the tutorial in github? thanks for your reply! Maybe bothering you,I'm so sorry.

aleju commented 4 years ago

There is currently no easy way to select only the N smallest samples for augmentation. You would have to implement that yourself with something along the lines of

import imgaug.parameters as iap
import imgaug.random as iarandom

rng = iarandom.RNG(123456)

for image in images:
    samples = iap.Uniform(-50, 50).draw_samples(8, rng)
    samples = sorted(samples)[0:2]
    images_aug = [iaa.Add(sample, per_channel=0.1)(image=image) for sample in samples]

Didn't test it, but might work. It will probably be rather slow. Though if you take always the two smallest samples from a zero-centered symmetric uniform distribution, then that might be similar to just sampling from a poisson distribution (and then randomizing the sign of each sample). Something in the direction of iap.RandomSign(iap.Poisson(1)) might hence also work.

For the integration with pytorch there isn't yet a tutorial. Easiest way is probably to write your own training loop, load images yourself as numpy arrays and apply augmentations to them before transforming to pytorch tensors.

Ronales commented 4 years ago

I get it, thank you very much!