|
| 1 | +# imagecorruptions |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +**[中文](./README.zh-CN.md)** | **[English](./README.md)** |
| 6 | + |
| 7 | +这个存储库是从 [CrazyVertigo/imagecorruptions](https://github.com/CrazyVertigo/imagecorruptions) 派生出来的,目前由本人 [Allenpandas](https://github.com/Allenpandas) 维护,本代码在原始存储库的代码的基础上进行了优化和调整。如果您有任何疑问,欢迎随时提交 [pull request](https://github.com/Allenpandas/imagecorruptions/pulls) 🤝,或 [联系我 ](https://github.com/users/follow?target=Allenpandas)📮。这个仓库代码提供了一组可以应用于图像的损坏,以便对神经网络的鲁棒性进行基准测试。这些破坏不是用来增强训练数据的,而是用来测试网络对看不见的扰动的抵抗能力。欲了解更多信息,请参阅 Hendrycks 和 Dietterich 关于 image corruption的论文:[Benchmarking Neural Network Robustness to Common Corruptions and Surface Variations](https://arxiv.org/abs/1807.01697)。 |
| 8 | + |
| 9 | +**注意:** 这个仓库来源于 [CrazyVertigo/imagecorruptions](https://github.com/CrazyVertigo/imagecorruptions) , 且仓库 [CrazyVertigo/imagecorruptions](https://github.com/CrazyVertigo/imagecorruptions) 来源于 [bethgelab/imagecorruptions](https://github.com/bethgelab/imagecorruptions). |
| 10 | + |
| 11 | +## Installation and Usage |
| 12 | +通过pip安装所需要的依赖包: `pip3 install imagecorruptions`. |
| 13 | + |
| 14 | +下面给出了如何使用的示例,您也可以在根目录下的`examples.py`文件中找到相应的代码。 |
| 15 | + |
| 16 | +```python |
| 17 | +from PIL import Image |
| 18 | +import numpy as np |
| 19 | +from imagecorruptions import corrupt |
| 20 | +import os |
| 21 | +import random |
| 22 | + |
| 23 | + |
| 24 | +def apply_corruption(input_image_path, output_folder, corruption_list=None): |
| 25 | + # 创建输出目录 |
| 26 | + os.makedirs(output_folder, exist_ok=True) |
| 27 | + # 读取图像并将PIL图像转换为NumPy数组 |
| 28 | + image = Image.open(input_image_path) |
| 29 | + image_array = np.array(image) |
| 30 | + # 随机选择corruption效果 |
| 31 | + corruption_name = random.choice(corruption_list) |
| 32 | + # 执行corruption |
| 33 | + corrupted_image_array = corrupt(image_array, corruption_name=corruption_name, severity=1) |
| 34 | + # 转换处理后的图像数组为 PIL 图像对象 |
| 35 | + corrupted_image = Image.fromarray(corrupted_image_array) |
| 36 | + # 拼接输出图像的文件名和路径 |
| 37 | + output_image_path = os.path.join(output_folder, os.path.basename(input_image_path)) |
| 38 | + # 保存输出图像 |
| 39 | + corrupted_image.save(output_image_path) |
| 40 | + |
| 41 | + |
| 42 | +def apply_corruption_to_folder(input_folder, output_folder, corruption_list=None): |
| 43 | + for filename in os.listdir(input_folder): |
| 44 | + # 获取目录下所有的.jpg和png图像 |
| 45 | + if filename.endswith(".jpg") or filename.endswith(".png"): |
| 46 | + input_image_path = os.path.join(input_folder, filename) |
| 47 | + # 逐一进行corruption |
| 48 | + apply_corruption(input_image_path, output_folder, corruption_list) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == '__main__': |
| 52 | + input_folder = "" # 原图像目录 |
| 53 | + output_folder = "" # 输出图像目录 |
| 54 | + # 支持的corruption效果 |
| 55 | + custom_corruption_list = [ |
| 56 | + 'gaussian_noise', 'shot_noise', 'impulse_noise', 'defocus_blur', |
| 57 | + 'motion_blur', 'zoom_blur', 'snow', 'fog', 'contrast', 'elastic_transform', |
| 58 | + 'pixelate', 'jpeg_compression', 'speckle_noise', 'spatter' |
| 59 | + ] |
| 60 | + apply_corruption_to_folder(input_folder, output_folder, custom_corruption_list) |
| 61 | + |
| 62 | +``` |
| 63 | +**注意:** 您需要将 `input_folder` 设置为原始图像的存放目录,将 `output_folder` 设置为输出图像的存放目录。 |
| 64 | + |
| 65 | +目前支持的corruption方法有以下几类:gaussian_noise(高斯噪声)、shot_noise(抖动噪声)、impulse_noise(脉冲噪声)、defocus_blur(虚焦模糊)、motion_blur(运动模糊)、zoom_blur(缩放模糊)、snow(雪花)、fog(雾)、contrast(对比度)、elastic_transform(弹性变换)、pixelate(像素化)、jpeg_compression(JPEG压缩)、speckle_noise(斑点噪声)、spatter(飞溅) |
| 66 | + |
| 67 | +## Citation |
| 68 | + |
| 69 | +如果您使用 imagecorruptions 包,请考虑引用: |
| 70 | +``` |
| 71 | +@article{michaelis2019dragon, |
| 72 | + title={Benchmarking Robustness in Object Detection: Autonomous Driving when Winter is Coming}, |
| 73 | + author={Michaelis, Claudio and Mitzkus, Benjamin and Geirhos, Robert and Rusak, Evgenia and Bringmann, Oliver and Ecker, Alexander S. and Bethge, Matthias and Brendel, Wieland}, |
| 74 | + journal={arXiv preprint arXiv:1907.07484}, |
| 75 | + year={2019} |
| 76 | +} |
| 77 | +``` |
0 commit comments