項目總結四:神經風格遷移項目(Art generation with Neural Style Transfer)

一、項目介紹python

神經風格轉換 (NST) 是深部學習中最有趣的技術之一。它合併兩個圖像, 即 內容圖像 C(content image) 和 樣式圖像S(style image), 以生成圖像 G(generated image)。生成的圖像 G 將圖像 C 的 內容與圖像S的 樣式組合在一塊兒。算法

 

二、模型數組

利用遷移學習的技巧,模型採用預訓練的VGG19網絡。預訓練的模型來自 MatConvNet. http://www.vlfeat.org/matconvnet/pretrained/ 。 模型結構以下:網絡

(1)模型結構示例圖:函數

(2)本項目用的VGG19網絡的結構學習

{'input': <tf.Variable 'Variable:0' shape=(1, 300, 400, 3) dtype=float32_ref>,
 'conv1_1': <tf.Tensor 'Relu:0' shape=(1, 300, 400, 64) dtype=float32>, 
 'conv1_2': <tf.Tensor 'Relu_1:0' shape=(1, 300, 400, 64) dtype=float32>,
 'avgpool1': <tf.Tensor 'AvgPool:0' shape=(1, 150, 200, 64) dtype=float32>,
 'conv2_1': <tf.Tensor 'Relu_2:0' shape=(1, 150, 200, 128) dtype=float32>, 
 'conv2_2': <tf.Tensor 'Relu_3:0' shape=(1, 150, 200, 128) dtype=float32>, 
 'avgpool2': <tf.Tensor 'AvgPool_1:0' shape=(1, 75, 100, 128) dtype=float32>, 
 'conv3_1': <tf.Tensor 'Relu_4:0' shape=(1, 75, 100, 256) dtype=float32>, 
 'conv3_2': <tf.Tensor 'Relu_5:0' shape=(1, 75, 100, 256) dtype=float32>, 
 'conv3_3': <tf.Tensor 'Relu_6:0' shape=(1, 75, 100, 256) dtype=float32>, 
 'conv3_4': <tf.Tensor 'Relu_7:0' shape=(1, 75, 100, 256) dtype=float32>,
 'avgpool3': <tf.Tensor 'AvgPool_2:0' shape=(1, 38, 50, 256) dtype=float32>,
 'conv4_1': <tf.Tensor 'Relu_8:0' shape=(1, 38, 50, 512) dtype=float32>, 
 'conv4_2': <tf.Tensor 'Relu_9:0' shape=(1, 38, 50, 512) dtype=float32>, 
 'conv4_3': <tf.Tensor 'Relu_10:0' shape=(1, 38, 50, 512) dtype=float32>, 
 'conv4_4': <tf.Tensor 'Relu_11:0' shape=(1, 38, 50, 512) dtype=float32>, 
 'avgpool4': <tf.Tensor 'AvgPool_3:0' shape=(1, 19, 25, 512) dtype=float32>, 
 'conv5_1': <tf.Tensor 'Relu_12:0' shape=(1, 19, 25, 512) dtype=float32>, 
 'conv5_2': <tf.Tensor 'Relu_13:0' shape=(1, 19, 25, 512) dtype=float32>,
 'conv5_3': <tf.Tensor 'Relu_14:0' shape=(1, 19, 25, 512) dtype=float32>,
 'conv5_4': <tf.Tensor 'Relu_15:0' shape=(1, 19, 25, 512) dtype=float32>,
 'avgpool5': <tf.Tensor 'AvgPool_4:0' shape=(1, 10, 13, 512) dtype=float32>}

  

三、成本函數優化

(1)內容代價函數blog

  • 首先把圖片由3D volume展開爲2D matrix,以下圖:

  • 計算內容代價函數。分別以G和S兩圖片做爲輸入時,若是神經網絡某一層的激活值類似,那麼就意味着兩個圖片的內容類似。

   

(2)風格代價函數圖片

  • 首先計算某一層的Gram矩陣:

     

  • 計算風格代價函數。分別以G和S兩圖片做爲輸入時,若是神經網絡某一層的各個通道之間激活值相關係數高,那麼就意味着兩個圖片的內容類似。

     

  • 實際上,若是你對各層都使用風格代價函數,會讓結果變得更好。計算公式以下:

      

  • 把內容代價函數和風格代價函數組合到一塊兒,就獲得了代價函數:

       

 

四、模型優化算法與訓練目標get

# define optimizer (1 line)
optimizer = tf.train.AdamOptimizer(2.0)
 
# define train_step (1 line)
train_step = optimizer.minimize(J)

 

五、輸入輸出數據

  • 輸入數據:content_image、style_image、generated_image
  • 輸出數據:generated_image

 

六、總結

  • Neural Style Transfer is an algorithm that given a content image C and a style image S can generate an artistic image
  • It uses representations (hidden layer activations) based on a pretrained ConvNet.
  • The content cost function is computed using one hidden layer's activations.
  • The style cost function for one layer is computed using the Gram matrix of that layer's activations. The overall style cost function is obtained using several hidden layers.
  • Optimizing the total cost function results in synthesizing new images.
相關文章
相關標籤/搜索