Coder Social home page Coder Social logo

allenpandas / imagecorruptions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from crazyvertigo/imagecorruptions

1.0 1.0 0.0 4.98 MB

Python Implementation of the paper : Benchmarking Neural Network Robustness to Common Corruptions and Surface Variations.

License: Apache License 2.0

Python 100.00%
image-augmentation image-augmentation-preprocessing image-corruption image-corruptions imagecorruption unseen-perturbations

imagecorruptions's Introduction

imagecorruptions

image corruptions

Introduction

简体中文 | English

This repository was forked from CrazyVertigo/imagecorruptions and is maintained by Allenpandas who has made optimizations and adjustments based on the original repository's code. If you have any questions, feel free to submit a pull request 🤝, or contact me 📮. This package provides a set of corruptions that can be applied to images in order to benchmark the robustness of neural networks. These corruptions are not meant to be used as training data augmentation but rather to test the networks against unseen perturbations. For more information have a look at the paper on the original corruption package by Hendrycks and Dietterich: Benchmarking Neural Network Robustness to Common Corruptions and Surface Variations.

Notice: This repository folked from CrazyVertigo/imagecorruptions , and CrazyVertigo/imagecorruptions folked from bethgelab/imagecorruptions.

Installation and Usage

This package is pip installable via pip3 install imagecorruptions.

An example of how to use the corruption function is given below, and you can also find corresponding code in the examples.py file in the root directory.

from PIL import Image
import numpy as np
from imagecorruptions import corrupt
import os
import random


def apply_corruption(input_image_path, output_folder, corruption_list=None):
    # 创建输出目录
    os.makedirs(output_folder, exist_ok=True)
    # 读取图像并将PIL图像转换为NumPy数组
    image = Image.open(input_image_path)
    image_array = np.array(image)
    # 随机选择corruption效果
    corruption_name = random.choice(corruption_list)
    # 执行corruption
    corrupted_image_array = corrupt(image_array, corruption_name=corruption_name, severity=1)
    # 转换处理后的图像数组为 PIL 图像对象
    corrupted_image = Image.fromarray(corrupted_image_array)
    # 拼接输出图像的文件名和路径
    output_image_path = os.path.join(output_folder, os.path.basename(input_image_path))
    # 保存输出图像
    corrupted_image.save(output_image_path)


def apply_corruption_to_folder(input_folder, output_folder, corruption_list=None):
    for filename in os.listdir(input_folder):
        # 获取目录下所有的.jpg和png图像
        if filename.endswith(".jpg") or filename.endswith(".png"):
            input_image_path = os.path.join(input_folder, filename)
            # 逐一进行corruption
            apply_corruption(input_image_path, output_folder, corruption_list)


if __name__ == '__main__':
    input_folder = ""  # 原图像目录
    output_folder = ""  # 输出图像目录
    # 支持的corruption效果
    custom_corruption_list = [
        'gaussian_noise', 'shot_noise', 'impulse_noise', 'defocus_blur',
        'motion_blur', 'zoom_blur', 'snow', 'fog', 'contrast', 'elastic_transform',
        'pixelate', 'jpeg_compression', 'speckle_noise', 'spatter'
    ]
    apply_corruption_to_folder(input_folder, output_folder, custom_corruption_list)

Notice: You can set input_folder as the directory for the original images and output_folder as the directory for the resulting images.

Now, the currently supported corruption methods include the following: gaussian_noise(高斯噪声)、shot_noise(抖动噪声)、impulse_noise(脉冲噪声)、defocus_blur(虚焦模糊)、motion_blur(运动模糊)、zoom_blur(缩放模糊)、snow(雪花)、fog(雾)、contrast(对比度)、elastic_transform(弹性变换)、pixelate(像素化)、jpeg_compression(JPEG压缩)、speckle_noise(斑点噪声)、spatter(飞溅)

Changelog

0.1.0-alpha was released in 19/12/2023:

  • Organize and optimize the original code, add the examples.py and README.md .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.