Given an arbitrary function I'd like to find input to the function that minimizes it using nevergrad. The input is a (32,32) numpy array (or image) with values in the range (0,1).
Let's say our function is the contrast of the image. The contrast is minimized if all pixels have the same value. This is the code that I have so far.
import nevergrad
import numpy as np
def contrast(X):
vmin, vmax = float(np.min(X)), float(np.max(X))
num = vmax - vmin
denom = vmax + vmin
if denom == 0:
return 0
else:
return num / denom
shape = (32,32)
x0 = np.random.uniform(size=shape)
parametrization = nevergrad.p.Array(init=x0, lower=0.0, upper=1.0)
optimizer = nevergrad.optimizers.NGOpt(parametrization=parametrization, budget=10000)
res = optimizer.minimize(contrast)
print(contrast(res.value))
On my machine, this code takes about 45 seconds, and produces an image with a contrast value of around 0.95. Any idea how to tune this code in order to make it find a value closer to the minimum? Is nevergrad the right tool for the job?