Commit 223d125a authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): use dynamic adversarial cleaner

parent 4580e6c2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -1223,3 +1223,4 @@ fabric.properties
*.pt
/runs
/YOLOv8
.tests
 No newline at end of file
+0 −1983

File deleted.

Preview size limit exceeded, changes collapsed.

+0 −345

File deleted.

Preview size limit exceeded, changes collapsed.

+55 −26
Original line number Diff line number Diff line
@@ -11,59 +11,88 @@ Overview:
        :align: center

    .. note::
        This tool is inspired from `Github - lllyasviel/AdverseCleaner <https://github.com/lllyasviel/AdverseCleaner>`_.
        This tool is inspired from `Huggingface - mf666/mist-fucker <https://huggingface.co/spaces/mf666/mist-fucker>`_.
"""
import random

import cv2
import numpy as np
from PIL import Image
from cv2.ximgproc import guidedFilter

from ..data import load_image, ImageTyping
from ..data import load_image


def remove_adversarial_noise(image: ImageTyping, diameter: int = 5, sigma_color: float = 8.0,
                             sigma_space: float = 8.0, radius: int = 4, eps: float = 16.0) -> Image.Image:
def remove_adversarial_noise(
        image: Image.Image, diameter_min: int = 4, diameter_max: int = 6,
        sigma_color_min: float = 6.0, sigma_color_max: float = 10.0,
        sigma_space_min: float = 6.0, sigma_space_max: float = 10.0,
        radius_min: int = 3, radius_max: int = 6, eps_min: float = 16.0, eps_max: float = 24.0,
        b_iters: int = 64, g_iters: int = 8,
) -> Image.Image:
    """
    Remove adversarial noise from an image using bilateral and guided filtering.
    Remove adversarial noise from an image using random bilateral and guided filtering.

    This function applies bilateral filtering and guided filtering to reduce adversarial noise in the input image.
    This function applies random bilateral filtering and random guided filtering iteratively to reduce adversarial noise
    in the input image.

    :param image: The input image.
    :type image: ImageTyping
    :type image: Image.Image

    :param diameter_min: Minimum diameter for bilateral filtering.
    :type diameter_min: int, optional

    :param diameter_max: Maximum diameter for bilateral filtering.
    :type diameter_max: int, optional

    :param sigma_color_min: Minimum filter sigma in the color space for bilateral filtering.
    :type sigma_color_min: float, optional

    :param sigma_color_max: Maximum filter sigma in the color space for bilateral filtering.
    :type sigma_color_max: float, optional

    :param diameter: Diameter of each pixel neighborhood for bilateral filtering.
    :type diameter: int, optional
    :param sigma_space_min: Minimum filter sigma in the coordinate space for bilateral filtering.
    :type sigma_space_min: float, optional

    :param sigma_color: Filter sigma in the color space for bilateral filtering.
    :type sigma_color: float, optional
    :param sigma_space_max: Maximum filter sigma in the coordinate space for bilateral filtering.
    :type sigma_space_max: float, optional

    :param sigma_space: Filter sigma in the coordinate space for bilateral filtering.
    :type sigma_space: float, optional
    :param radius_min: Minimum radius for guided filtering.
    :type radius_min: int, optional

    :param radius: Radius of Guided Filter.
    :type radius: float, optional
    :param radius_max: Maximum radius for guided filtering.
    :type radius_max: int, optional

    :param eps: Guided Filter regularization term.
    :type eps: int, optional
    :param eps_min: Minimum guided filter regularization term.
    :type eps_min: float, optional

    :param eps_max: Maximum guided filter regularization term.
    :type eps_max: float, optional

    :param b_iters: Number of iterations for bilateral filtering.
    :type b_iters: int, optional

    :param g_iters: Number of iterations for guided filtering.
    :type g_iters: int, optional

    :return: Image with adversarial noise removed.
    :rtype: Image.Image

    Examples::
        >>> from imgutils.restore import remove_adversarial_noise
        >>>
        >>> remove_adversarial_noise('adversarial_input.png')
    """
    image = load_image(image, mode='RGB', force_background='white')
    img = np.array(image).astype(np.float32)
    y = img.copy()

    # Apply bilateral filtering iteratively
    for _ in range(64):
    # Apply random bilateral filtering iteratively
    for _ in range(b_iters):
        diameter = random.randint(diameter_min, diameter_max)
        sigma_color = random.uniform(sigma_color_min, sigma_color_max)
        sigma_space = random.uniform(sigma_space_min, sigma_space_max)
        y = cv2.bilateralFilter(y, diameter, sigma_color, sigma_space)

    # Apply guided filtering iteratively
    for _ in range(4):
    # Apply random guided filtering iteratively
    for _ in range(g_iters):
        radius = random.randint(radius_min, radius_max)
        eps = random.uniform(eps_min, eps_max)
        y = guidedFilter(img, y, radius, eps)

    # Clip the values and convert back to uint8 for PIL Image
−1.11 KiB (741 KiB)
Loading image diff...