python Deep learning 学习笔记(10) (5)

demo

from keras.preprocessing.image import load_img, img_to_array import numpy as np from keras.applications import vgg19 from keras import backend as K from scipy.optimize import fmin_l_bfgs_b from scipy.misc import imsave import time def preprocess_image(image_path): img = load_img(image_path, target_size=(img_height, img_width)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg19.preprocess_input(img) return img def deprocess_image(x): # vgg19.preprocess_input 的作用是减去 ImageNet 的平均像素值, # 使其中心为 0。这里相当于 vgg19.preprocess_input 的逆操作 x[:, :, 0] += 103.939 x[:, :, 1] += 116.779 x[:, :, 2] += 123.68 # 将图像由 BGR 格式转换为 RGB 格式。这也是 # vgg19.preprocess_input 逆操作的一部分 x = x[:, :, ::-1] x = np.clip(x, 0, 255).astype('uint8') return x target_image_path = 'cat.jpg' style_reference_image_path = 'style.png' # 设置生成图像的尺寸 width, height = load_img(target_image_path).size img_height = 400 img_width = int(width * img_height / height) # 加载预训练的 VGG19 网络,并将其应用于三张图像 target_image = K.constant(preprocess_image(target_image_path)) style_reference_image = K.constant(preprocess_image(style_reference_image_path)) # 占位符用于保存生成图像 combination_image = K.placeholder((1, img_height, img_width, 3)) # 将三张图像合并为一个批量 input_tensor = K.concatenate([target_image, style_reference_image, combination_image], axis=0) model = vgg19.VGG19(input_tensor=input_tensor, weights='imagenet', include_top=False) print('Model loaded.') def content_loss(base, combination): """ 内容损失 :param base: :param combination: :return: """ return K.sum(K.square(combination - base)) def gram_matrix(x): features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1))) gram = K.dot(features, K.transpose(features)) return gram def style_loss(style, combination): """ 风格损失 :param style: :param combination: :return: """ S = gram_matrix(style) C = gram_matrix(combination) channels = 3 size = img_height * img_width return K.sum(K.square(S - C)) / (4. * (channels ** 2) * (size ** 2)) def total_variation_loss(x): """ 总变差损失 对生成的组合图像的像素进行操作, 促使生成图像具有空间连续性,从而避免结果过度像素化 也可以简单理解为正则化损失 :param x: :return: """ a = K.square(x[:, :img_height - 1, :img_width - 1, :] - x[:, 1:, :img_width - 1, :]) b = K.square(x[:, :img_height - 1, :img_width - 1, :] - x[:, :img_height - 1, 1:, :]) return K.sum(K.pow(a + b, 1.25)) # 定义最小化的最终损失 # 将层的名称映射为激活张量的字典 outputs_dict = dict([(layer.name, layer.output) for layer in model.layers]) content_layer = 'block5_conv2' style_layers = ['block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1', 'block5_conv1'] total_variation_weight = 1e-4 style_weight = 1. content_weight = 0.025 loss = K.variable(0.) layer_features = outputs_dict[content_layer] target_image_features = layer_features[0, :, :, :] combination_features = layer_features[2, :, :, :] loss += content_weight * content_loss(target_image_features, combination_features) for layer_name in style_layers: layer_features = outputs_dict[layer_name] style_reference_features = layer_features[1, :, :, :] combination_features = layer_features[2, :, :, :] sl = style_loss(style_reference_features, combination_features) loss += (style_weight / len(style_layers)) * sl loss += total_variation_weight * total_variation_loss(combination_image) # 使用 L-BFGS 算法进行优化,设置梯度下降过程 # 获取损失相对于生成图像的梯度 grads = K.gradients(loss, combination_image)[0] # 用于获取当前损失值和当前梯度值的函数 fetch_loss_and_grads = K.function([combination_image], [loss, grads]) class Evaluator(object): """ 这个类将 fetch_loss_and_grads 包 装起来,让你可以利用两个单独的方法 调用来获取损失和梯度,这是我们要使 用的 SciPy 优化器所要求的 """ def __init__(self): self.loss_value = None self.grads_values = None def loss(self, x): assert self.loss_value is None x = x.reshape((1, img_height, img_width, 3)) outs = fetch_loss_and_grads([x]) loss_value = outs[0] grad_values = outs[1].flatten().astype('float64') self.loss_value = loss_value self.grad_values = grad_values return self.loss_value def grads(self, x): assert self.loss_value is not None grad_values = np.copy(self.grad_values) self.loss_value = None self.grad_values = None return grad_values evaluator = Evaluator() # 使用 SciPy 的 L-BFGS 算法来运行梯度上升过程 # 风格迁移循环 result_prefix = 'my_result' iterations = 20 x = preprocess_image(target_image_path) # 将图像展平,因为 scipy.optimize.fmin_l_bfgs_b 只能处理展平的向量 x = x.flatten() for i in range(iterations): print('Start of iteration', i) start_time = time.time() x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x, fprime=evaluator.grads, maxfun=20) print('Current loss value:', min_val) # 保存当前的生成图像 img = x.copy().reshape((img_height, img_width, 3)) img = deprocess_image(img) fname = result_prefix + '_at_iteration_%d.png' % i imsave(fname, img) print('Image saved as', fname) end_time = time.time() print('Iteration %d completed in %ds' % (i, end_time - start_time))

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyjxjs.html