Exploring image filter parameters with blackbox optimization in Python Note

background

If the target function (problem) is like a black box, it is necessary to find its parameters.

It seems that it is called hyperparameter optimization (search) in the field of blackbox optimization and machine learning.

There are often requests for blackbox optimization in image processing as well.

For example, you want to match the appearance of the target oceanic image (instagram image) to make it an oceanic image even if you take a slightly unsightly photo. In this case, brightness or sepia/film filter. Search for filter parameters such as. (If you find it manually, it will melt infinitely and it will be hard)

This time, I will simplify the problem a little more and estimate the parameters of the blur with the image blurred by ImageMagick.

I will use RMSE for the error of the image for the time being (The more they match, the lower the value)

A note on optimizing blackbox functions in Python https://qiita.com/syoyo/items/6c33acb0fd475e651f2f

A note that runs an external program in Python and parses the resulting line https://qiita.com/syoyo/items/d13af423604192cee41c

Let's move ImageMagick with an external command and optimize it with benderopt, referring to.

Data and code

Before blurring

shimokita.jpeg

After blurring (-blur 27x20)

shimokita-blur.jpg

from benderopt import minimize
import numpy as np
import logging
import subprocess
import parse

logging.basicConfig(level=logging.DEBUG) # logging.INFO will print less information

blurred_filename = "shimokita-blur.jpg " # convert -blur 27x20
ref_filename = "shimokita.jpeg "

k_num_eval = 10000

def extract_result(lines):
    for line in lines:
        print(line.decode("utf-8"))
        ret = parse.parse("{:g} ({:g})", line.decode("utf-8"))
        if ret:
            return ret[0]

    raise RuntimeError("Failed to extract value from result.")

count = 0

def f(radius, sigma):
    global count

    count += 1

    print("run {} of {} ...".format(count, k_num_eval))
    tmp_filename = "shimokita-tmp.jpg "
    cmd = "convert {} -blur {}x{} {}".format(ref_filename, radius, sigma, tmp_filename)
    ret = subprocess.run(cmd, shell=True)


    # Compare two images using RMSE
    cmp_cmd = "compare -metric rmse {} {} null:".format(tmp_filename, blurred_filename)
    ret = subprocess.run(cmp_cmd, shell=True, capture_output=True)
    # `compare`(ImageMagick) outputs result into stderr, not stdout
    lines = ret.stderr.splitlines()

    val = extract_result(lines)

    return val


# We define the parameters we want to optimize:
optimization_problem_parameters = [
    {
        "name": "radius",
        "category": "uniform",
        "search_space": {
            "low": 0,
            "high": 100,
        }
    },
    {
        "name": "sigma",
        "category": "uniform",
        "search_space": {
            "low": 0,
            "high": 100,
        }
    }
]

# We launch the optimization
best_sample = minimize(f, optimization_problem_parameters, number_of_evaluation=k_num_eval)

print("radius", best_sample["radius"])
print("sigma", best_sample["sigma"])
print("err = ", f(best_sample["radius"], best_sample["sigma"]))

result

I turned it 10,000 times.

radius 27.993241302558445
sigma 19.957452459359814

err =  49.5512

Voila!

The radius is 1 different from 27 (true value) and 28 (estimated value), but the result is close to the true value.

shimokita-blur-estimated.jpg

Let's take the difference with idiff.

https://openimageio.readthedocs.io/en/release-2.2.8.0/idiff.html

diff.jpg

(The difference is 20 times. If the difference is the same size, it is visually almost black (= match))

jpg The effect of compression seems to be great ...

By the way, 100 times was not good at all, and 1000 times was reasonably close.

TODO

Recommended Posts

Exploring image filter parameters with blackbox optimization in Python Note
Tweet with image in Python
Image processing with Python 100 knock # 10 median filter
Easy image processing in Python with Pillow
Image processing with Python 100 knocks # 9 Gaussian filter
Python in optimization
A note on optimizing blackbox functions in Python
Create an image with characters in python (Japanese)
Filter List in Python
Image format in Python
Image processing with Python
[Python] Get the numbers in the graph image with OCR
Convert the image in .zip to PDF with Python
[Note] How to write QR code and description in the same image with python
Image processing with Python (Part 2)
Scraping with selenium in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Working with sounds in Python
Sorting image files with Python (2)
Scraping with Selenium in Python
[Note] Operate MongoDB with Python
Sorting image files with Python (3)
Image processing with Python (Part 1)
Scraping with Tor in Python
Image processing with Python (Part 3)
Image Processing Collection in Python
Solve optimization problems in Python
[Python] Image processing with scikit-image
Number recognition in images with Python
Cut out an image with python
Testing with random numbers in Python
[Python] Using OpenCV with Python (Image Filtering)
Implemented image segmentation in python (Union-Find)
[Note] Project Euler in Python (Problem 1-22)
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
[Python] Using OpenCV with Python (Image transformation)
FX Systre Parameter Optimization in Python
CSS parsing with cssutils in Python
MySQL-automatic escape of parameters in python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
(Note) Be careful with python argparse
Use rospy with virtualenv in Python3
[Note] Hello world output with python
Let's do image scraping with Python
Bayesian optimization very easy with Python
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Foreign Key in Python SQLite [Note]
Read files in parallel with Python
Password generation in texto with python
Find image similarity with Python + OpenCV
Use OpenCV with Python 3 in Window
Image processing with Python 100 knocks # 2 Grayscale
Until dealing with python in Atom
Get started with Python in Blender
Send image with python, save with php
Working with DICOM images in Python