Skip to content

Instantly share code, notes, and snippets.

@sylhare
Last active February 23, 2018 19:11
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 sylhare/20134391079699a42813b9315da74c96 to your computer and use it in GitHub Desktop.
Save sylhare/20134391079699a42813b9315da74c96 to your computer and use it in GitHub Desktop.
AI Chainer fast neural style
https://github.com/yusuketomoto/chainer-fast-neuralstyle
https://medium.com/element-ai-research-lab/stabilizing-neural-style-transfer-for-video-62675e203e42?lipi=urn%3Ali%3Apage%3Ad_flagship3_feed%3B%2Fi1%2BFDQ2S4mFUrkbZOoRFQ%3D%3D
# This loss component captures the difference between the content of our source image and the content of our stylized image
L_feat = lambda_f * F.mean_squared_error(Variable(feature[2].data), feature_hat[2])
# Establish a variable to hold our style-losses
L_style = Variable(xp.zeros((), dtype=np.float32))
# Loop over style components, accumulating loss
for f, f_hat, g_s in zip(feature, feature_hat, gram_s):
L_style += lambda_s * F.mean_squared_error(gram_matrix(f_hat), Variable(g_s.data))
# Add a loss term for the total variance in the output image
L_tv = lambda_tv * total_variation(y)
L = L_feat + L_style + L_tv
# The 'popping' noise is the difference in resulting stylizations
# from two images that are very similar. Minimizing it results
# in a much more stable stylization that can be applied to video.
# Small changes in the input result in small changes in the output.
if noise_count:
L_pop = lambda_noise * F.mean_squared_error(y, noisy_y)
L = L_feat + L_style + L_tv + L_pop
noiseimg = xp.zeros((3, image_size, image_size), dtype=xp.float32)
# prepare a noise image
for ii in range(noise_count):
xx = random.randrange(image_size)
yy = random.randrange(image_size)
noiseimg[0][yy][xx] += random.randrange(-noise_range, noise_range)
noiseimg[1][yy][xx] += random.randrange(-noise_range, noise_range)
noiseimg[2][yy][xx] += random.randrange(-noise_range, noise_range)
# add the noise image to the source image
noisy_x = x.copy()
noisy_x = noisy_x + noiseimg
noisy_x = Variable(noisy_x)
noisy_y = model(noisy_x)
lambda_noise = 1000
noise_range = 30
noise_count = 1000
https://gist.github.com/archydeberker/4e2722825c62685a5dd5b60eb879cb91#file-cfns_loss-py
https://gist.github.com/archydeberker/d568b15803ac2924740690f17e0ddb4a#file-style_stabilization_loss-py
https://gist.github.com/archydeberker/0a571f5f380995a2ed42d21f646149c9#file-add_image_noise-py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment