Skip to content

Instantly share code, notes, and snippets.



Last active Feb 23, 2018
What would you like to do?
AI Chainer fast neural style
# 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(
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.