日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PyTorch进行神经风格转换/迁移(Neural-Transfer:图像风格迁移)

發布時間:2025/3/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PyTorch进行神经风格转换/迁移(Neural-Transfer:图像风格迁移) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

文章目錄

  • 前言
  • 1.介紹
  • 2. 基本原理
  • 3 準備工作
  • 4 加載素材

1.介紹

本教程主要講解如何實現由Leon A. Gatys,Alexander S. Ecker和Matthias Bethge提出的 Neural-Style 算法。Neural-Style或者叫Neural-Transfer,可以讓你運用新的風格將你指定的圖片進行重構。這個算法將使用兩張圖片,一張圖片作為風格提供者,一張圖片作為內容提供者,另外生成一張圖片內容與內容圖片相似,但圖片風格的和風格圖片相似的新圖片。

2. 基本原理

其實現原理非常特別,展現了人類思維的巧妙性,
我們定義兩個優化指標:
1,一個用于內容D_C;
2,一個用于風格D_S。
D_C度量兩張圖片內容上的區別,而D_S用來測量兩張圖片風格的區別。
然后,我們生成第三張圖片,并優化這張圖片,使其與內容圖片的內容差別和風格圖片的風格差別最小化。
現在,原理講完了,開始實現吧,首先,我們導入必要的包。

3 準備工作

首先是導入以下的包:

  • torch, torch.nn(使用PyTorch進行風格轉換必不可少的包)
  • numpy (矩陣處理必須用)
  • torch.optim (高效的梯度下降)
  • PIL, PIL.Image, matplotlib.pyplot (加載和展示圖片)
  • torchvision.transforms (將PIL圖片轉換成張量)
  • torchvision.models (訓練或加載預訓練模型)
  • copy (對模型進行深度拷貝;系統包)
  • import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from PIL import Image import matplotlib.pyplot as plt import torchvision.transforms as transforms import torchvision.models as models import copy import warnings warnings.filterwarnings("ignore")

    接著是一個比較關鍵的步驟,確定是GPU還是CPU來運行神經網絡。雖然,在GPU上運行可以加速,但有的電腦上沒有GPU。
    我們可以使用torch.cuda.is_available()來判斷是否有可用的GPU。

    if torch,cuda.is_available():device=torch.device("cuda") else:device=torch.device("cpu")

    4 加載素材

    接下來是導入提供風格和內容的圖片。
    導入圖片也意味著對圖片進行預處理,原始的PIL圖片的屬性值介于0到255之間,但是當轉換成torch張量時,它們的值被壓縮到0到1之間,另外,圖片分辨率也會被調整到520。一個重要的細節是,注意torch庫中的神經網絡用來訓練的張量的值為0到1之間。如果你嘗試將0到255的張量圖片加載到神經網絡,然后激活的特征映射將不能偵測到目標內容和風格。然而,Caffe庫中的預訓練網絡用來訓練的張量值為0到255之間的圖片。

    這是一個下載本教程需要用到的圖片的鏈接: picasso.jpg 和 dancing.jpg。下載這兩張圖片并且將它們添加到你當前工作目錄。
    如果嫌麻煩,可以關注公眾號,

    發送neuralstyle,自動推送資源集成包。
    設置圖片預處理程序

    # desired size of the output image imsize = 512 if torch.cuda.is_available() else 128 # use small size if no gpuloader = transforms.Compose([transforms.Resize(imsize), # scale imported imagetransforms.ToTensor()]) # transform it into a torch tensordef image_loader(image_name):image = Image.open(image_name)# fake batch dimension required to fit network's input dimensionsimage = loader(image).unsqueeze(0) #添加一個0維度 batch 適應網絡輸入return image.to(device, torch.float)style_img = image_loader("picasso.jpg") content_img = image_loader("dancing.jpg")assert style_img.size() == content_img.size(), \"we need to import style and content images of the same size"

    因為tensor是四維的不能直接展示,所以,我們創建一個imshow函數,重新將圖片轉換成標準三維數據來展示,這樣也可以讓我們確認圖像是否被正確加載。

    unloader = transforms.ToPILImage() # reconvert into PIL imageplt.ion()def imshow(tensor, title=None):image = tensor.cpu().clone() # we clone the tensor to not do changes on itimage = image.squeeze(0) # remove the fake batch dimension 去掉0維度image = unloader(image)plt.imshow(image)if title is not None:plt.title(title)plt.pause(0.001) # pause a bit so that plots are updatedplt.figure() imshow(style_img, title='Style Image')plt.figure() imshow(content_img, title='Content Image')

    正確加載的話,運行到這里,可以看到這個。

    總結

    以上是生活随笔為你收集整理的PyTorch进行神经风格转换/迁移(Neural-Transfer:图像风格迁移)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。