-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
559 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include imagenet_c/frost/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# ImageNet-C Corruption Functions | ||
|
||
With this package, it is possible to corrupt an image with ImageNet-C corruptions. | ||
These functions are exposed with the function ```corrupt```. | ||
|
||
Try | ||
``` | ||
from imagenet_c import corrupt | ||
corrupt(<image>, corruption_number=0) | ||
``` | ||
|
||
This function ```corrupt``` is | ||
``` | ||
corruption_tuple = (gaussian_noise, shot_noise, impulse_noise, defocus_blur, | ||
glass_blur, motion_blur, zoom_blur, snow, frost, fog, | ||
brightness, contrast, elastic_transform, pixelate, jpeg_compression, | ||
speckle_noise, gaussian_blur, spatter, saturate) | ||
corruption_dict = {corr_func.__name__: corr_func for corr_func in corruption_tuple} | ||
def corrupt(x, severity=1, corruption_name=None, corruption_number=-1): | ||
""" | ||
:param x: image to corrupt; a 224x224x3 numpy array in [0, 255] | ||
:param severity: strength with which to corrupt x; an integer in [0, 5] | ||
:param corruption_name: specifies which corruption function to call; | ||
must be one of 'gaussian_noise', 'shot_noise', 'impulse_noise', 'defocus_blur', | ||
'glass_blur', 'motion_blur', 'zoom_blur', 'snow', 'frost', 'fog', | ||
'brightness', 'contrast', 'elastic_transform', 'pixelate', 'jpeg_compression', | ||
'speckle_noise', 'gaussian_blur', 'spatter', 'saturate'; | ||
the last four are validation functions | ||
:param corruption_number: the position of the corruption_name in the above list; | ||
an integer in [0, 18]; useful for easy looping; 15, 16, 17, 18 are validation corruption numbers | ||
:return: the image x corrupted by a corruption function at the given severity; same shape as input | ||
""" | ||
if corruption_name: | ||
x_corrupted = corruption_dict[corruption_name](Image.fromarray(x), severity) | ||
elif corruption_number == -1: | ||
x_corrupted = corruption_tuple[corruption_number](Image.fromarray(x), severity) | ||
else: | ||
raise ValueError("Either corruption_name or corruption_number must be passed") | ||
return np.uint8(x_corrupted) | ||
``` | ||
|
||
## Citation | ||
|
||
If you find this useful in your research, please consider citing: | ||
|
||
@article{hendrycks2018robustness, | ||
title={Benchmarking Neural Network Robustness to Common Corruptions and Perturbations}, | ||
author={Dan Hendrycks and Thomas Dietterich}, | ||
journal={arXiv preprint arXiv:1807.01697}, | ||
year={2018} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import numpy as np | ||
from PIL import Image | ||
from .corruptions import * | ||
|
||
corruption_tuple = (gaussian_noise, shot_noise, impulse_noise, defocus_blur, | ||
glass_blur, motion_blur, zoom_blur, snow, frost, fog, | ||
brightness, contrast, elastic_transform, pixelate, jpeg_compression, | ||
speckle_noise, gaussian_blur, spatter, saturate) | ||
|
||
corruption_dict = {corr_func.__name__: corr_func for corr_func in corruption_tuple} | ||
|
||
|
||
def corrupt(x, severity=1, corruption_name=None, corruption_number=-1): | ||
""" | ||
:param x: image to corrupt; a 224x224x3 numpy array in [0, 255] | ||
:param severity: strength with which to corrupt x; an integer in [0, 5] | ||
:param corruption_name: specifies which corruption function to call; | ||
must be one of 'gaussian_noise', 'shot_noise', 'impulse_noise', 'defocus_blur', | ||
'glass_blur', 'motion_blur', 'zoom_blur', 'snow', 'frost', 'fog', | ||
'brightness', 'contrast', 'elastic_transform', 'pixelate', 'jpeg_compression', | ||
'speckle_noise', 'gaussian_blur', 'spatter', 'saturate'; | ||
the last four are validation functions | ||
:param corruption_number: the position of the corruption_name in the above list; | ||
an integer in [0, 18]; useful for easy looping; 15, 16, 17, 18 are validation corruption numbers | ||
:return: the image x corrupted by a corruption function at the given severity; same shape as input | ||
""" | ||
|
||
if corruption_name: | ||
x_corrupted = corruption_dict[corruption_name](Image.fromarray(x), severity) | ||
elif corruption_number != -1: | ||
x_corrupted = corruption_tuple[corruption_number](Image.fromarray(x), severity) | ||
else: | ||
raise ValueError("Either corruption_name or corruption_number must be passed") | ||
|
||
return np.uint8(x_corrupted) |
Oops, something went wrong.