-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathq.py
34 lines (26 loc) · 727 Bytes
/
q.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import numpy as np
from interface import BlackBox
def estimate_grad(x, n, sigma):
g = np.zeros_like(x).astype(float)
for i in range(n):
dx = np.random.normal(0, 1, x.shape)
g += (1 - bbox.calc(x + sigma * dx)) * dx
g -= (1 - bbox.calc(x - sigma * dx)) * dx
return g / 2 / n / sigma
sigma = 0.1
n_estimate = 130
n_iter = 100000
lr = 3
reg = 0.0012
bbox = BlackBox()
x = bbox.get_image().astype(float)
original = x.copy()
for i in range(n_iter):
grad = estimate_grad(x, n_estimate, sigma)
grad += reg * (x - original) / x.shape[0]
x -= lr * grad / np.linalg.norm(grad)
x = np.clip(x, 0, 255)
p = bbox.calc(x)
if p > 0.5:
break
print(*x.astype('uint8'))