CS231n assignment3 Q4 Style Transfer

"Image Style Transfer Using Convolutional Neural Networks" (Gatys et al., CVPR 2015).

复现这一篇论文中的代码

loss由三部分组成,内容loss,风格loss,正则化loss,其中风格loss使用gram矩阵。

Content loss

def content_loss(content_weight, content_current, content_original):
    """
    Compute the content loss for style transfer.

    Inputs:
    - content_weight: scalar constant we multiply the content_loss by.
    - content_current: features of the current image, Tensor with shape [1, height, width, channels]
    - content_target: features of the content image, Tensor with shape [1, height, width, channels]

    Returns:
    - scalar content loss
    """
    # tf.squared_difference(x,y,name=None) 返回的是(x-y)(x-y)
    return content_weight * tf.reduce_sum(tf.squared_difference(content_current,content_original))

Style loss

def gram_matrix(features, normalize=True):
    """
    Compute the Gram matrix from features.

    Inputs:
    - features: Tensor of shape (1, H, W, C) giving features for
      a single image.
    - normalize: optional, whether to normalize the Gram matrix
        If True, divide the Gram matrix by the number of neurons (H * W * C)

    Returns:
    - gram: Tensor of shape (C, C) giving the (optionally normalized)
      Gram matrices for the input image.
    """
    features = tf.transpose(features,[0,3,1,2])
    shape = tf.shape(features)
    features = tf.reshape(features,(shape[0],shape[1],-1))
    transpose_features = tf.transpose(features,[0,2,1])
    result = tf.matmul(features,transpose_features)
    if normalize:
        result = tf.div(result,tf.cast(shape[0] * shape[1] * shape[2] * shape[3],tf.float32))
    return result

def style_loss(feats, style_layers, style_targets, style_weights):
    """
    Computes the style loss at a set of layers.

    Inputs:
    - feats: list of the features at every layer of the current image, as produced by
      the extract_features function.
    - style_layers: List of layer indices into feats giving the layers to include in the
      style loss.
    - style_targets: List of the same length as style_layers, where style_targets[i] is
      a Tensor giving the Gram matrix of the source style image computed at
      layer style_layers[i].
    - style_weights: List of the same length as style_layers, where style_weights[i]
      is a scalar giving the weight for the style loss at layer style_layers[i].

    Returns:
    - style_loss: A Tensor containing the scalar style loss.
    """
    # Hint: you can do this with one for loop over the style layers, and should
    # not be very much code (~5 lines). You will need to use your gram_matrix function.
    style_losses = 0
    for i in range(len(style_layers)):
        cur_index = style_layers[i]
        cur_feat = feats[cur_index]
        cur_weight = style_weights[i]
        cur_style_target = style_targets[i] #已经是一个gram矩阵了
        grammatrix = gram_matrix(cur_feat) #计算当前层的特征图的gram矩阵
        style_losses += cur_weight * tf.reduce_sum(tf.squared_difference(grammatrix,cur_style_target))
    return style_losses

Total-variation regularization

def tv_loss(img, tv_weight):
    """
    Compute total variation loss.

    Inputs:
    - img: Tensor of shape (1, H, W, 3) holding an input image.
    - tv_weight: Scalar giving the weight w_t to use for the TV loss.

    Returns:
    - loss: Tensor holding a scalar giving the total variation loss
      for img weighted by tv_weight.
    """
    # Your implementation should be vectorized and not require any loops!
    shape = tf.shape(img)
    img_row_before = tf.slice(img,[0,0,0,0],[-1,shape[1]-1,-1,-1])
    img_row_after = tf.slice(img,[0,1,0,0],[-1,shape[1]-1,-1,-1])
    img_col_before = tf.slice(img,[0,0,0,0],[-1,-1,shape[2]-1,-1])
    img_col_after = tf.slice(img,[0,0,1,0],[-1,-1,shape[2]-1,-1])
    result = tv_weight * (tf.reduce_sum(tf.squared_difference(img_row_before,img_row_after)) +
                          tf.reduce_sum(tf.squared_difference(img_col_before,img_col_after)))
    return result

原文地址:https://www.cnblogs.com/bernieloveslife/p/10224313.html

时间: 2024-11-08 23:50:45

CS231n assignment3 Q4 Style Transfer的相关文章

谈谈图像的style transfer(二)

总说 主要从几个方面来进行说明吧 - 加快transfer的速度 - 让transfer的效果看起来更加visual-pleasing - 其他的一些方面 - 用GAN来做 加快style stransfer 谈谈图像的Style Transfer(一) 这里写了 Neural style以及fast neural style. 在超越fast style transfer--任意风格图和内容图0.1秒出结果已经可以将转换做到实时.这篇的一个主要的问题是耗费的大部分时间是在提取隐藏层的patch

图像风格转换(Image style transfer)

图像风格转换是最近新兴起的一种基于深度学习的技术,它的出现一方面是占了卷积神经网络的天时,卷积神经网络所带来的对图像特征的高层特征的抽取使得风格和内容的分离成为了可能.另一方面则可能是作者的灵感,内容的表示是卷积神经网络所擅长,但风格却不是,如何保持内容而转换风格则是本文所要讲述的. 本篇属于论文阅读笔记系列.论文即[1]. 引入 风格转换属于纹理转换问题,纹理转换问题在之前采用的是一些非参方法,通过一些专有的固定的方法来渲染. 传统的方法的问题在于只能提取底层特征而非高层抽象特征.随着CNN的

风格迁移(2)-Fast Style Transfer

X为输入图片 fw 风格迁移的网络 yc就是X ys是风格后的图片 y帽为输入图片X经过fw 风格迁移的网络生成的图片 y帽在内容上与yc相类似,在风格上与ys相类似. Fast Style Transfer的训练步骤如下: 1 输入一张图片x到fw中得到结果y帽 2 将y帽与yc输入到loss network(VGG-16)中,计算它的relu3_3的输出,并计算它们的均方误差作为content loss 3 将y帽与ys输入到loss network(VGG-16)中,计算它的relu1_2

OpenCv dnn模块扩展研究(1)--style transfer

一.opencv的示例模型文件 使用Torch模型[OpenCV对各种模型兼容并包,起到胶水作用], 下载地址: fast_neural_style_eccv16_starry_night.t7 http://cs.stanford.edu/people/jcjohns/fast-neural-style/models/eccv16/starry_night.t7 和 fast_neural_style_instance_norm_feathers.t7 http://cs.stanford.e

Neural Style Transfer

风格转移 这几天看了一篇风格转移的论文,这里大致介绍下论文的内容,并且推到下论文中出现的公式. 基本思想 有两张图片,我们关注一张图片的内容(Content)记为C,一张图片的风格(Style)记为S,想要生成一张图片包含C的内容和S的风格,记为G. 那么如何获取图片的C和S那?论文使用训练好的VGG net解决这一问题. 文中使用VGG net的中间层来表示C:对于一张input_image,中间某一输出层shape为$heighttimes width times channel$,将其re

Perceptual losses for real-time style transfer and super-resolution(by_xiao jian)

Perceptual losses for real-time style transfer and super-resolution,2016 ECCV https://cs.stanford.edu/people/jcjohns/eccv16/ 官方源码Torch:https://github.com/jcjohnson/fast-neural-style 其他程序Tensorflow:https://github.com/lengstrom/fast-style-transfer,实现细节

CS231n assignment2 Q4 Convolutional Networks

终于来到了卷积网络 首先完成最基本的前向传播: def conv_forward_naive(x, w, b, conv_param): """ A naive implementation of the forward pass for a convolutional layer. The input consists of N data points, each with C channels, height H and width W. We convolve each

CS231n assignment3 Q5 Generative Adversarial Networks

LeakyReLU def leaky_relu(x, alpha=0.01): """Compute the leaky ReLU activation function. Inputs: - x: TensorFlow Tensor with arbitrary shape - alpha: leak parameter for leaky ReLU Returns: TensorFlow Tensor with the same shape as x "&qu

CS231n:

Bayesian Hyperparameter Optimization is a whole area of research devoted to coming up with algorithms that try to more efficiently navigate the space of hyperparameters. The core idea is to appropriately balance the exploration - exploitation trade-o