图像风格迁移技术(论文复现)
from keras import backend as K
# 获取原始图像和参考风格图像 不变设置为常量
target_image = (preprocess_image(target_image_path))
style_reference_image = (preprocess_image(style_reference_image_path))
# 目标生成图像占位符(可变)默认为float32
combination_image = ((1, img_height, img_width, 3))
# 合并为一个批量(这里是为了符合VGG19的批次(3,width,height,3)
input_tensor = ([target_image,
style_reference_image,
combination_image], axis=0)
# 官方文档 /api/applications/vgg/#vgg19-function
model = vgg19.VGG19(input_tensor=input_tensor,
weights='imagenet',
include_top=False)
print('Model loaded.')
print(())
outputs_dict = dict([(, ) for layer in ])
# 内容格式的层激活
content_layer = 'block5_conv4'
# 风格的CNN层激活
style_layers = ['block1_conv1',
'block2_conv1',
'block3_conv1',
'block4_conv1',
'block5_conv1']
outputs_dict
from import fmin_l_bfgs_b
# from import imsavefrom import
import time
result_prefix = 'style_transfer_result'
iterations = 10 # 更深的轮次 效果越深(风格迁移越强)
# Run scipy-based optimization (L-BFGS) over the pixels of the generated image
# so as to minimize the neural style loss.
# This is our initial state: the target image.
# Note that `.fmin_l_bfgs_b` can only process flat vectors.
x = preprocess_image(target_image_path)
x = ()
for i in range(iterations):
print('Start of iteration', i)
start_time = ()
x, min_val, info = fmin_l_bfgs_b(, x,
fprime=, maxfun=20)
print('Current loss value:', min_val)
# Save current generated image
img = ().reshape((img_height, img_width, 3))
img = deprocess_image(img)
fname = result_prefix + '_at_iteration_%' % i
.save_img(fname, img)
end_time = ()
print('Image saved as', fname)
print('Iteration %d completed in %ds' % (i, end_time - start_time))