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

Add Gaussian Noise Operation #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Augmentor/Operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1617,3 +1617,21 @@ def perform_operation(self, image):
will expect an image of type PIL.Image)
"""
return self.function_name(image, **self.function_arguments)

class GaussianNoise(Operation):
"""
The class `:class noise` is used to perfrom random noise on images passed
Smellly marked this conversation as resolved.
Show resolved Hide resolved
to its :func:`perform_operation` function.
"""
def __init__(self, probability, mean, std):
Operation.__init__(self, probability)
self.mean = mean
self.std = std

def perform_operation(self, image):
w, h = image.size
c = len(image.getbands())

noise = np.random.normal(self.mean, self.std, (h, w, c))

return Image.fromarray(np.uint8(np.array(image) + noise))
23 changes: 23 additions & 0 deletions Augmentor/Pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,3 +1350,26 @@ def random_erasing(self, probability, rectangle_area):
raise ValueError("The rectangle_area must be between 0.1 and 1.")
else:
self.add_operation(RandomErasing(probability=probability, rectangle_area=rectangle_area))

def noise(self, probability, mean=0, std=0.3):
"""
The class `:class noise` is used to perfrom gaussian noise on images passed
to its :func:`perform_operation` function.

Its purpose is to make models robust to adversarial learning, by
randomly adding gassian noise to original images, which human could not
tell the difference between them.

:param probability: A value between 0 and 1 representing the
probability that the operation should be performed.
:param mean: The mean value of the gaussian distribution.
:param std: The std value of the gaussian distribution.
:return: None
"""
if not 0 < probability <= 1:
raise ValueError(Pipeline._probability_error_text)
elif std <= 0:
raise ValueError('The standard deviation must larger than 0.')
else:
self.add_operation(GaussianNoise(probability=probability, mean=mean, std=std))

2 changes: 1 addition & 1 deletion Augmentor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

__author__ = """Marcus D. Bloice"""
__email__ = '[email protected]'
__version__ = '0.1.10'
__version__ = '0.1.11'

__all__ = ['Pipeline']
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ p.flip_left_right(probability=0.8)
p.flip_top_bottom(probability=0.3)
p.crop_random(probability=1, percentage_area=0.5)
p.resize(probability=1.0, width=120, height=120)
p.noise(probability=1.0)
# or p.noise(probability=1.0, mean=0, std=0.5)
```

Once you have added the operations you require, you can sample images from this pipeline:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='Augmentor',
packages=['Augmentor'],
version='0.1.10',
version='0.1.11',
description='Image augmentation library for Machine Learning',
long_description='Image augmentation library for Machine Learning',
license='MIT',
Expand Down