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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【GANs入门】pytorch-GANs任务迁移-单个目标(数字的生成)

發(fā)布時間:2025/4/16 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【GANs入门】pytorch-GANs任务迁移-单个目标(数字的生成) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡述

之前認真學習了網(wǎng)上的一份,代碼做了很詳細的筆記。
【Gans入門】Pytorch實現(xiàn)Gans代碼詳解【70+代碼】

但是上面的任務只是畫一條在一定區(qū)間下的曲線。
這里對這個進行遷移,到可以進行圖像的生成。

圖像的很多數(shù)據(jù)都沒有,但是突然想到在sklearn上的digits是一個非常簡單的圖片。
這里我想到之前的一份筆記
sklearn學習(一)

這里會使用sklearn自帶的小數(shù)據(jù)來做訓練
目標是讓神經(jīng)網(wǎng)絡自己學會生成數(shù)字。

任務描述

為了讓神經(jīng)網(wǎng)絡操作更簡單。這里的輸入數(shù)據(jù)只會選擇特定數(shù)值的數(shù)字圖片數(shù)據(jù)。然后丟給對抗生成神經(jīng)網(wǎng)絡學習。讓其中的生成器學會如何生成手寫數(shù)字。

下面是選擇用數(shù)值1的生成過程

其實可以發(fā)現(xiàn)其實是有點這樣的感覺了。

下面的這個是讓它學習數(shù)字0的效果

可能是由于數(shù)字0的細節(jié)更粗糙一點,所以,可以發(fā)現(xiàn),我們認為這個0生成的更好。(數(shù)字1和數(shù)字4其實是有點像的,所以會有點問題,還有這是因為圖片像素有點低

代碼詳解

導入包

  • torch,numpy這些都是數(shù)據(jù)處理過程中需要的包
  • matplotlib為了畫圖
  • sklearn主要是為了它本身帶的數(shù)據(jù)
  • random主要是為了選擇標準數(shù)據(jù)更具有隨機性
  • os,shutil,imageio這三個庫是為了畫出gif動態(tài)圖
import torch import torch.nn as nn import numpy as np import matplotlib.pyplot as plt from sklearn import datasets import random import os import shutil import imageio

創(chuàng)建臨時文件夾

PNGFILE = './png/' if not os.path.exists(PNGFILE):os.mkdir(PNGFILE) else:shutil.rmtree(PNGFILE)os.mkdir(PNGFILE)

這里會創(chuàng)建一個臨時的文件夾png,會把中途生成的那些圖片都存在這,然后我就可以用這些png來生成gif文件

模型參數(shù)

  • BATCH_SIZE這個參數(shù)表示每次用多少的數(shù)據(jù)來進行考量。(數(shù)值多的話模型進化的會稍微快點)
  • LR_G跟LR_D表示兩個模型的學習率
  • N_IDEAS:啟發(fā)式因子(生成函數(shù)的初始層的節(jié)點數(shù))。因為我們要操作的節(jié)點數(shù)量會特別大(特別是圖像問題,但是如果輸入節(jié)點過于大的話,會需要大量的計算資源。所以用小一點的這個基本夠用就行了)
  • target_num :表示的是想要生成的數(shù)字。由于數(shù)據(jù)集中只有(0到9)所以,這里也只能取0到9。
  • image_max表示圖片像素點的最大值,這個一開始我用到了,但是后來我修改了代碼之后,就用不到了。
  • ART_COMPONENTS:像素點數(shù)量(其實本質(zhì)上跟前一個版本的參考節(jié)點數(shù)都是一樣的)
# Hyper Parameters BATCH_SIZE = 64 LR_G = 0.00001 # learning rate for generator LR_D = 0.00001 # learning rate for discriminator N_IDEAS = 6 # think of this as number of ideas for generating an art work (Generator) target_num = 0 # target Numberdigits = datasets.load_digits() target = digits.target data = digits.data[target == target_num] image_max = max(data.reshape((-1,))) ART_COMPONENTS = data.shape[-1] # it could be total point G can draw in the canvas

標準數(shù)據(jù)

這個函數(shù)本質(zhì)上,這個區(qū)間上選BATCH_SIZE個標準數(shù)據(jù)。
但是,random.sample只能輸入的是list所以需要先把data轉(zhuǎn)成list,但是轉(zhuǎn)出來的list又不能直接變成torch中的Tensor,這里需要再轉(zhuǎn)成ndarray,之后再轉(zhuǎn)成Tensor,但是要注意在后面加一個.float()函數(shù)的操作。

def artist_works(): # painting from the famous artist (real target)return torch.from_numpy(np.array(random.sample(list(data), BATCH_SIZE))).float()

構建模型

生成器模型,但是Linear轉(zhuǎn)成的數(shù)據(jù)是有可能有負數(shù)的數(shù)據(jù)的,但是作為圖片肯定是不可以有這樣的數(shù)據(jù)的。因為數(shù)據(jù)一定是需要為大于等于0的數(shù)據(jù)。

所以搭建的這個模型最后一定要加一個ReLU()這樣的類似的,來保證沒有0的情況。

G = nn.Sequential( # Generatornn.Linear(N_IDEAS, 128), # random ideas (could from normal distribution)nn.ReLU(),nn.Linear(128, ART_COMPONENTS), # making a painting from these random ideasnn.ReLU(), )D = nn.Sequential( # Discriminatornn.Linear(ART_COMPONENTS, 128), # receive art work either from the famous artist or a newbie like Gnn.ReLU(),nn.Linear(128, 1),nn.Sigmoid(), # tell the probability that the art work is made by artist )

構建最優(yōu)化的模型

opt_D = torch.optim.Adam(D.parameters(), lr=LR_D) opt_G = torch.optim.Adam(G.parameters(), lr=LR_G)

迭代優(yōu)化

這跟之前的是類似的。

for step in range(10000):artist_paintings = artist_works() # real painting from artistG_ideas = torch.randn(BATCH_SIZE, N_IDEAS) # random ideasG_paintings = G(G_ideas) # fake painting from G (random ideas)prob_artist0 = D(artist_paintings) # D try to increase this probprob_artist1 = D(G_paintings) # D try to reduce this probD_loss = - torch.mean(torch.log(prob_artist0) + torch.log(1. - prob_artist1))G_loss = torch.mean(torch.log(1. - prob_artist1))opt_D.zero_grad()D_loss.backward(retain_graph=True) # reusing computational graphopt_D.step()opt_G.zero_grad()G_loss.backward(retain_graph=True)opt_G.step()

畫圖并保存

if step % 100 == 0: # plottingplt.cla()tempdata = G_paintings[0].detach().numpy()tempdata = tempdata.reshape((8, 8))plt.imshow(tempdata, cmap=plt.cm.gray_r)# plt.draw()plt.savefig(PNGFILE + '%d.png' % times)filedatalist.append(PNGFILE + '%d.png' % times)times += 1plt.pause(0.01)

生成gif

generated_images = [] for png_path in filedatalist:generated_images.append(imageio.imread(png_path)) shutil.rmtree(PNGFILE) imageio.mimsave('gan.gif', generated_images, 'GIF', duration=0.1)

全部代碼

import torch import torch.nn as nn import numpy as np import matplotlib.pyplot as plt from sklearn import datasets import random import os import shutil import imageioPNGFILE = './png/' if not os.path.exists(PNGFILE):os.mkdir(PNGFILE) else:shutil.rmtree(PNGFILE)os.mkdir(PNGFILE)# Hyper Parameters BATCH_SIZE = 64 LR_G = 0.00001 # learning rate for generator LR_D = 0.00001 # learning rate for discriminator N_IDEAS = 6 # think of this as number of ideas for generating an art work (Generator) target_num = 0 # target Numberdigits = datasets.load_digits() target = digits.target data = digits.data[target == target_num] image_max = max(data.reshape((-1,))) ART_COMPONENTS = data.shape[-1] # it could be total point G can draw in the canvasdef artist_works(): # painting from the famous artist (real target)return torch.from_numpy(np.array(random.sample(list(data), BATCH_SIZE))).float()G = nn.Sequential( # Generatornn.Linear(N_IDEAS, 128), # random ideas (could from normal distribution)nn.ReLU(),nn.Linear(128, ART_COMPONENTS), # making a painting from these random ideasnn.ReLU(), )D = nn.Sequential( # Discriminatornn.Linear(ART_COMPONENTS, 128), # receive art work either from the famous artist or a newbie like Gnn.ReLU(),nn.Linear(128, 1),nn.Sigmoid(), # tell the probability that the art work is made by artist )opt_D = torch.optim.Adam(D.parameters(), lr=LR_D) opt_G = torch.optim.Adam(G.parameters(), lr=LR_G) times = 0filedatalist = []for step in range(10000):artist_paintings = artist_works() # real painting from artistG_ideas = torch.randn(BATCH_SIZE, N_IDEAS) # random ideasG_paintings = G(G_ideas) # fake painting from G (random ideas)prob_artist0 = D(artist_paintings) # D try to increase this probprob_artist1 = D(G_paintings) # D try to reduce this probD_loss = - torch.mean(torch.log(prob_artist0) + torch.log(1. - prob_artist1))G_loss = torch.mean(torch.log(1. - prob_artist1))opt_D.zero_grad()D_loss.backward(retain_graph=True) # reusing computational graphopt_D.step()opt_G.zero_grad()G_loss.backward(retain_graph=True)opt_G.step()if step % 100 == 0: # plottingplt.cla()tempdata = G_paintings[0].detach().numpy()tempdata = tempdata.reshape((8, 8))plt.imshow(tempdata, cmap=plt.cm.gray_r)# plt.draw()plt.savefig(PNGFILE + '%d.png' % times)filedatalist.append(PNGFILE + '%d.png' % times)times += 1plt.pause(0.01)generated_images = [] for png_path in filedatalist:generated_images.append(imageio.imread(png_path)) shutil.rmtree(PNGFILE) imageio.mimsave('gan.gif', generated_images, 'GIF', duration=0.1) 《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的【GANs入门】pytorch-GANs任务迁移-单个目标(数字的生成)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品一区二区久久毛片 | 欧美一级射 | 香蕉狠狠爱视频 | 好吊色综合 | 人妻视频一区二区 | 久久久www成人免费无遮挡大片 | 欧美三日本三级少妇三 | 欧美人与动牲交a欧美精品 欧美三级在线看 | 国产视频一区二区在线播放 | 日本公妇乱偷中文字幕 | 久久无码视频网站 | 精品亚洲永久免费精品 | 182tv福利视频 | 日韩另类 | 国产欧美在线看 | 亚洲第一a | 亚洲国产日韩av | 亚洲日日干| 成人动漫在线免费观看 | 日本毛片在线看 | 丝袜美腿中文字幕 | 国产日韩久久久 | 伊人网av在线 | 成人a毛片| 中国黄色免费网站 | 午夜精品小视频 | 亚洲射情| 99热在线观看免费精品 | 国产欧美日韩成人 | 国产不卡一二三 | 一级特黄肉体裸片 | 久久久久人妻一区精品色欧美 | 亚洲综合成人网 | 欧美性猛交乱大交 | 日韩精品一区在线播放 | 爱情岛亚洲论坛入口 | 一区二区精品在线观看 | 蜜桃臀av一区二区三区 | 欧美专区 日韩专区 | 精品一区二区三区不卡 | 精品婷婷 | 国产欧美激情视频 | 国产中文字幕免费 | 国产青青青 | 用力挺进新婚白嫩少妇 | 国产swag在线 | 亚洲第一免费播放区 | 中文字幕色网 | 91插插插插插 | 小视频在线观看 | 日本成人毛片 | 欧美激情视频网 | 欧美偷拍综合 | 羞羞草影院 | 欧洲最强rapper网站直播 | 日韩欧美xxx | 九九热精品视频在线播放 | 亚洲视频一区在线观看 | 91看片淫黄大片 | 国产精品系列在线观看 | bt男人天堂| 中文幕无线码中文字蜜桃 | 日韩在线你懂的 | 亚洲日本国产精品 | 日本黄网站在线观看 | 久久久久久9| 久久精品www| 日本成人在线播放 | 亚洲视频小说 | 久久午夜鲁丝 | 操视频网站| 清清草免费视频 | 中国精品毛片 | 在线观看免费福利 | 日本护士╳╳╳hd少妇 | 中文字幕一区二区人妻电影丶 | 国产又黄又猛的视频 | 国产av无码专区亚洲av麻豆 | 天天碰天天碰 | 欧美人妻一区二区 | 91禁在线动漫 | 污污免费观看 | 日韩av免费在线看 | 欧美一区精品 | 午夜精品电影 | 三级精品在线 | 女人扒开腿让男人桶爽 | 被两个男人吃奶三p爽文 | 亚洲欧美中文日韩在线观看 | jizz免费| 一区国产精品 | 黄片毛片在线免费观看 | 在线免费观看黄色av | 射黄视频| 国产不卡视频在线播放 | 偷拍视频一区 | 婷婷亚洲一区 | 亚洲美女视频在线观看 | 粉嫩av四季av绯色av |