Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# this is a quick implementation of
# BUT! This is kind of dirty. Lots of hard coding. 
import numpy as np
import math
from chainer import cuda, Function, FunctionSet, gradient_check, Variable, optimizers
import chainer.functions as Fu
from chainer.functions import caffe
import chainer
import matplotlib.pyplot as plt
from scipy.misc import imread, imresize, imsave
use_gpu = False
def readimage(filename):
img = imread(filename)
img = imresize(img,[224, 224])
img = np.transpose(img,(2,0,1))
img = img.reshape((1,3,224,224))
p_data = np.ascontiguousarray(img,dtype=np.float32)
return Variable(cuda.to_gpu(p_data) if use_gpu else p_data)
def reshape2(conv1_1):[1][2]*[3]
return chainer.functions.reshape(conv1_1,(k,pixels))
# save the image x
def save_x(img,filename='output.png'):
img = img.reshape((3,224,224))
img = np.transpose(img,(1,2,0))
def forward(x, p, a):
conv1_1, conv2_1, conv3_1, conv4_1,conv5_1, = func(inputs={'data': x}, outputs=['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'])
conv1_1F,conv2_1F, conv3_1F, conv4_1F,conv5_1F, = [ reshape2(x) for x in [conv1_1,conv2_1, conv3_1, conv4_1,conv5_1]]
conv1_1G,conv2_1G, conv3_1G, conv4_1G,conv5_1G, = [ Fu.matmul(x, x, transa=False, transb=True) for x in [conv1_1F,conv2_1F, conv3_1F, conv4_1F,conv5_1F]]
conv1_1,conv2_1, conv3_1, conv4_1,conv5_1, = func(inputs={'data': p}, outputs=['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'])
conv1_1P,conv2_1P, conv3_1P, conv4_1P,conv5_1P, = [ reshape2(x) for x in [conv1_1,conv2_1, conv3_1, conv4_1,conv5_1]]
L_content = Fu.mean_squared_error(conv4_1F,conv4_1P)/2
conv1_1,conv2_1, conv3_1, conv4_1,conv5_1, = func(inputs={'data': a}, outputs=['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'])
conv1_1A0,conv2_1A0, conv3_1A0, conv4_1A0,conv5_1A0, = [ reshape2(x) for x in [conv1_1,conv2_1, conv3_1, conv4_1,conv5_1]]
conv1_1A,conv2_1A, conv3_1A, conv4_1A,conv5_1A, = [ Fu.matmul(x, x, transa=False, transb=True) for x in [conv1_1A0,conv2_1A0, conv3_1A0, conv4_1A0,conv5_1A0]]
#caution! the deviding number is hard coding!
#this part is correspnding to equation (4) in the original paper
#to check the current N and M, run the following
#[ for x in [conv1_1F,conv2_1F, conv3_1F, conv4_1F,conv5_1F]]
L_style = (Fu.mean_squared_error(conv1_1G,conv1_1A)/(4*64*64*50176*50176)
+ Fu.mean_squared_error(conv2_1G,conv2_1A)/(4*128**128*12544*12544)
+ Fu.mean_squared_error(conv3_1G,conv3_1A)/(4*256*256*3136*3136)
+ Fu.mean_squared_error(conv4_1G,conv4_1A)/(4*512*512*784*784)\
)/4 # this is equal weighting of E_l
ratio = 0.0001 #alpha/beta
loss = ratio*L_content + L_style
return loss
if use_gpu:
print('initializing gpu')
cuda.init(0)# is GPU ID!!
print('loading images')
p=readimage('input/tuebingen.jpg')#read a content image
a=readimage('input/starry_night.jpg')#read a style image
x = Variable(cuda.to_gpu(x_data) if use_gpu else x_data)
x = readimage('input/tuebingen.jpg') # if you want to start from a exsiting image
print('loading network')
#download a pretraind caffe model from here:
func = caffe.CaffeFunction('VGG_ILSVRC_19_layers.caffemodel')#it takes some time.
if use_gpu:
print('moving net to gpu')
#optimize x(=image) with adam
#note we use numpy for optimization
v=np.zeros_like(cuda.to_cpu( if use_gpu else np.zeros_like(
print('starting gradient descent')
for epoch in xrange(10000):
grad = cuda.to_cpu(x.grad.copy()) if use_gpu else x.grad.copy()
t +=1
m = beta1*m + (1-beta1)*grad
v = beta2*v + (1-beta2)*(grad*grad)
v_hat=v/(1-np.power(beta2, t))
if use_gpu: -= cuda.to_gpu( alpha * m_hat / (np.sqrt(v_hat) + eps) )#back it to cuda
else: -= ( alpha * m_hat / (np.sqrt(v_hat) + eps) )
with open(savedir+'/log.txt', 'a') as f:
savename = savedir+'/frames'+str(epoch)+'.png'
if use_gpu:
save_x(, savename)
# #optimize x(=image) with momment
# momentum= 0.9
# lr=100
# v=np.zeros_like(
# for epoch in xrange(10000):
# loss=forward(x,p,a)
# loss.backward()
# grad=x.grad.copy()
# v *= momentum
# v -= lr * grad
# += v
# with open(savedir+'/log.txt', 'a') as f:
# f.write(str(epoch+315)+','+str(','+str(np.linalg.norm(x.grad))+'\n')
# savename = savedir+'/imge'+str(epoch+315)+'.png'
# save_x(,savename)

This comment has been minimized.

Copy link

apple2373 commented Sep 6, 2015

Hi, I refined the code. According to your advise.Thanks!

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.