Skip to content

Instantly share code, notes, and snippets.

@kivantium
Created August 1, 2022 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kivantium/ae68c2cbfa3c2a0e8f5857e5984cfca5 to your computer and use it in GitHub Desktop.
Save kivantium/ae68c2cbfa3c2a0e8f5857e5984cfca5 to your computer and use it in GitHub Desktop.
Adversarial Exampleで作る"エロ画像" ソースコード(ライセンスはMIT-0です)
import copy
import i2v
import numpy as np
from PIL import Image
from skimage.transform import resize
image_size = 224
channel_size = 3
noise_size = 56
illust2vec = i2v.make_i2v_with_onnx(
"illust2vec_tag_ver200.onnx", "tag_list.json")
img_org = Image.open("/home/kivantium/initial.jpg")
img_org_resized = img_org.resize((image_size, image_size))
img_org_resized_np = np.array(img_org_resized, np.float32) / 255.0
def order_rating(rating):
for r in rating:
if r[0] == 'safe':
safe = r[1]
elif r[0] == 'questionable':
questionable = r[1]
elif r[0] == 'explicit':
explicit = r[1]
return safe, questionable, explicit
# image: numpy array (float)
def target_function(image_np):
c = 1.0
k = 20
image_pil = Image.fromarray(np.uint8(image_np * 255))
rating = illust2vec.estimate_plausible_tags([image_pil])[0]['rating']
safe, questionable, explicit = order_rating(rating)
f = max(np.log(safe + questionable) - np.log(explicit), -k)
return np.linalg.norm(image_np - img_org_resized_np) + c * f
# image: numpy array
# coordinate: 3-membered tuple
def grad_and_hessian(image, coordinate):
x, y, c = coordinate
h = 0.01
f = target_function(image)
perturbation = np.zeros((noise_size, noise_size, channel_size))
perturbation[y][x][c] = h
perturbation_resized = resize(perturbation, (image_size, image_size))
f1 = target_function(image + perturbation_resized)
f2 = target_function(image - perturbation_resized)
grad = (f1 - f2) / (2 * h)
hessian = (f1 - 2 * f + f2) / (h ** 2)
return grad, hessian
image = copy.deepcopy(img_org_resized_np)
eta = 0.001
it = 0
while True:
x, y = np.random.randint(noise_size, size=2)
c = np.random.randint(channel_size)
grad, hessian = grad_and_hessian(image, (x, y, c))
noise = np.zeros((noise_size, noise_size, channel_size))
if hessian <= 0.0:
noise[y, x, c] = -eta * grad
else:
noise[y, x, c] = -eta * grad / hessian
noise_resized = resize(noise, (image_size, image_size))
image += noise_resized
image_pil = Image.fromarray(np.uint8(image * 255))
rating = illust2vec.estimate_plausible_tags([image_pil])[0]['rating']
print("rating:", rating, "func value:", target_function(image))
if it % 100 == 0:
image_pil.save(f"adversarial{it}.jpg")
it += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment