欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch GAN偽造手寫體mnist數(shù)據(jù)集方式

 更新時間:2020年01月10日 09:38:29   作者:ZJE_ANDY  
今天小編就為大家分享一篇pytorch GAN偽造手寫體mnist數(shù)據(jù)集方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

一,mnist數(shù)據(jù)集

形如上圖的數(shù)字手寫體就是mnist數(shù)據(jù)集。

二,GAN原理(生成對抗網(wǎng)絡(luò))

GAN網(wǎng)絡(luò)一共由兩部分組成:一個是偽造器(Generator,簡稱G),一個是判別器(Discrimniator,簡稱D)

一開始,G由服從某幾個分布(如高斯分布)的噪音組成,生成的圖片不斷送給D判斷是否正確,直到G生成的圖片連D都判斷以為是真的。D每一輪除了看過G生成的假圖片以外,還要見數(shù)據(jù)集中的真圖片,以前者和后者得到的損失函數(shù)值為依據(jù)更新D網(wǎng)絡(luò)中的權(quán)值。因此G和D都在不停地更新權(quán)值。以下圖為例:

在v1時的G只不過是 一堆噪聲,見過數(shù)據(jù)集(real images)的D肯定能判斷出G所生成的是假的。當(dāng)然G也能知道D判斷它是假的這個結(jié)果,因此G就會更新權(quán)值,到v2的時候,G就能生成更逼真的圖片來讓D判斷,當(dāng)然在v2時D也是會先看一次真圖片,再去判斷G所生成的圖片。以此類推,不斷循環(huán)就是GAN的思想。

三,訓(xùn)練代碼

import argparse
import os
import numpy as np
import math
 
import torchvision.transforms as transforms
from torchvision.utils import save_image
 
from torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variable
 
import torch.nn as nn
import torch.nn.functional as F
import torch
 
os.makedirs("images", exist_ok=True)
 
parser = argparse.ArgumentParser()
parser.add_argument("--n_epochs", type=int, default=200, help="number of epochs of training")
parser.add_argument("--batch_size", type=int, default=64, help="size of the batches")
parser.add_argument("--lr", type=float, default=0.0002, help="adam: learning rate")
parser.add_argument("--b1", type=float, default=0.5, help="adam: decay of first order momentum of gradient")
parser.add_argument("--b2", type=float, default=0.999, help="adam: decay of first order momentum of gradient")
parser.add_argument("--n_cpu", type=int, default=8, help="number of cpu threads to use during batch generation")
parser.add_argument("--latent_dim", type=int, default=100, help="dimensionality of the latent space")
parser.add_argument("--img_size", type=int, default=28, help="size of each image dimension")
parser.add_argument("--channels", type=int, default=1, help="number of image channels")
parser.add_argument("--sample_interval", type=int, default=400, help="interval betwen image samples")
opt = parser.parse_args()
print(opt)
 
img_shape = (opt.channels, opt.img_size, opt.img_size) # 確定圖片輸入的格式為(1,28,28),由于mnist數(shù)據(jù)集是灰度圖所以通道為1
cuda = True if torch.cuda.is_available() else False
 
 
class Generator(nn.Module):
 def __init__(self):
  super(Generator, self).__init__()
 
  def block(in_feat, out_feat, normalize=True):
   layers = [nn.Linear(in_feat, out_feat)]
   if normalize:
    layers.append(nn.BatchNorm1d(out_feat, 0.8))
   layers.append(nn.LeakyReLU(0.2, inplace=True))
   return layers
 
  self.model = nn.Sequential(
   *block(opt.latent_dim, 128, normalize=False),
   *block(128, 256),
   *block(256, 512),
   *block(512, 1024),
   nn.Linear(1024, int(np.prod(img_shape))),
   nn.Tanh()
  )
 
 def forward(self, z):
  img = self.model(z)
  img = img.view(img.size(0), *img_shape)
  return img
 
 
class Discriminator(nn.Module):
 def __init__(self):
  super(Discriminator, self).__init__()
 
  self.model = nn.Sequential(
   nn.Linear(int(np.prod(img_shape)), 512),
   nn.LeakyReLU(0.2, inplace=True),
   nn.Linear(512, 256),
   nn.LeakyReLU(0.2, inplace=True),
   nn.Linear(256, 1),
   nn.Sigmoid(),
  )
 
 def forward(self, img):
  img_flat = img.view(img.size(0), -1)
  validity = self.model(img_flat)
  return validity
 
 
# Loss function
adversarial_loss = torch.nn.BCELoss()
 
# Initialize generator and discriminator
generator = Generator()
discriminator = Discriminator()
 
if cuda:
 generator.cuda()
 discriminator.cuda()
 adversarial_loss.cuda()
 
# Configure data loader
os.makedirs("../../data/mnist", exist_ok=True)
dataloader = torch.utils.data.DataLoader(
 datasets.MNIST(
  "../../data/mnist",
  train=True,
  download=True,
  transform=transforms.Compose(
   [transforms.Resize(opt.img_size), transforms.ToTensor(), transforms.Normalize([0.5], [0.5])]
  ),
 ),
 batch_size=opt.batch_size,
 shuffle=True,
)
 
# Optimizers
optimizer_G = torch.optim.Adam(generator.parameters(), lr=opt.lr, betas=(opt.b1, opt.b2))
optimizer_D = torch.optim.Adam(discriminator.parameters(), lr=opt.lr, betas=(opt.b1, opt.b2))
 
Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
 
# ----------
# Training
# ----------
if __name__ == '__main__':
 for epoch in range(opt.n_epochs):
  for i, (imgs, _) in enumerate(dataloader):
   # print(imgs.shape)
   # Adversarial ground truths
   valid = Variable(Tensor(imgs.size(0), 1).fill_(1.0), requires_grad=False) # 全1
   fake = Variable(Tensor(imgs.size(0), 1).fill_(0.0), requires_grad=False) # 全0
   # Configure input
   real_imgs = Variable(imgs.type(Tensor))
 
   # -----------------
   # Train Generator
   # -----------------
 
   optimizer_G.zero_grad() # 清空G網(wǎng)絡(luò) 上一個batch的梯度
 
   # Sample noise as generator input
   z = Variable(Tensor(np.random.normal(0, 1, (imgs.shape[0], opt.latent_dim)))) # 生成的噪音,均值為0方差為1維度為(64,100)的噪音
   # Generate a batch of images
   gen_imgs = generator(z)
   # Loss measures generator's ability to fool the discriminator
   g_loss = adversarial_loss(discriminator(gen_imgs), valid)
 
   g_loss.backward() # g_loss用于更新G網(wǎng)絡(luò)的權(quán)值,g_loss于D網(wǎng)絡(luò)的判斷結(jié)果 有關(guān)
   optimizer_G.step()
 
   # ---------------------
   # Train Discriminator
   # ---------------------
 
   optimizer_D.zero_grad() # 清空D網(wǎng)絡(luò) 上一個batch的梯度
   # Measure discriminator's ability to classify real from generated samples
   real_loss = adversarial_loss(discriminator(real_imgs), valid)
   fake_loss = adversarial_loss(discriminator(gen_imgs.detach()), fake)
   d_loss = (real_loss + fake_loss) / 2
 
   d_loss.backward() # d_loss用于更新D網(wǎng)絡(luò)的權(quán)值
   optimizer_D.step()
 
   print(
    "[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f]"
    % (epoch, opt.n_epochs, i, len(dataloader), d_loss.item(), g_loss.item())
   )
 
   batches_done = epoch * len(dataloader) + i
   if batches_done % opt.sample_interval == 0:
    save_image(gen_imgs.data[:25], "images/%d.png" % batches_done, nrow=5, normalize=True) # 保存一個batchsize中的25張
   if (epoch+1) %2 ==0:
    print('save..')
    torch.save(generator,'g%d.pth' % epoch)
    torch.save(discriminator,'d%d.pth' % epoch)

運行結(jié)果:

一開始時,G生成的全是雜音:

然后逐漸呈現(xiàn)數(shù)字的雛形:

最后一次生成的結(jié)果:

四,測試代碼:

導(dǎo)入最后保存生成器的模型:

from gan import Generator,Discriminator
import torch
import matplotlib.pyplot as plt
from torch.autograd import Variable
import numpy as np
from torchvision.utils import save_image
 
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
Tensor = torch.cuda.FloatTensor
g = torch.load('g199.pth') #導(dǎo)入生成器Generator模型
#d = torch.load('d.pth')
g = g.to(device)
#d = d.to(device)
 
z = Variable(Tensor(np.random.normal(0, 1, (64, 100)))) #輸入的噪音
gen_imgs =g(z) #生產(chǎn)圖片
save_image(gen_imgs.data[:25], "images.png" , nrow=5, normalize=True)

生成結(jié)果:

以上這篇pytorch GAN偽造手寫體mnist數(shù)據(jù)集方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python將原圖裁剪為固定尺寸小圖

    python將原圖裁剪為固定尺寸小圖

    這篇文章主要為大家詳細(xì)介紹了python將原圖裁剪為固定尺寸小圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 解決python xx.py文件點擊完之后一閃而過的問題

    解決python xx.py文件點擊完之后一閃而過的問題

    今天小編就為大家分享一篇解決python xx.py文件點擊完之后一閃而過的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • 如何使用Python保存PPT中的形狀為圖像文件

    如何使用Python保存PPT中的形狀為圖像文件

    將PowerPoint演示文稿中的形狀(幻燈片中的內(nèi)容元素,包括文本框、圖形、圖片、圖表等)保存為圖片是方便內(nèi)容跨平臺分享和再利用的有效手段,本文將演示如何使用Python保存PowerPoint演示文稿中的形狀為圖像文件,需要的朋友可以參考下
    2024-10-10
  • Python print不能立即打印的解決方式

    Python print不能立即打印的解決方式

    今天小編就為大家分享一篇Python print不能立即打印的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python使用低通濾波器模糊圖像功能實現(xiàn)

    Python使用低通濾波器模糊圖像功能實現(xiàn)

    這篇文章主要介紹了Python使用低通濾波器模糊圖像,我們介紹了多種不同類型的濾波器核與卷積操作,使用 scipy.ndimage 模塊中的濾波器模糊圖像,利用 scipy.fftpack 模塊的 fft2() 函數(shù)實現(xiàn)高斯模糊,介紹了scipy.signal模塊的彩色圖像頻域卷積,需要的朋友可以參考下
    2023-03-03
  • 使用Python解決常見格式圖像讀取nii,dicom,mhd

    使用Python解決常見格式圖像讀取nii,dicom,mhd

    這篇文章主要介紹了使用Python解決常見格式圖像讀取nii,dicom,mhd,下文具體操作過程需要的小伙伴可以參考一下
    2022-04-04
  • 教你使用Python獲取QQ音樂某個歌手的歌單

    教你使用Python獲取QQ音樂某個歌手的歌單

    這篇文章主要介紹了Python獲取QQ音樂某個歌手的歌單,從qq音樂中獲取某個你喜歡的歌手的清單,涉及到的庫有requests、json,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • 菜鳥使用python實現(xiàn)正則檢測密碼合法性

    菜鳥使用python實現(xiàn)正則檢測密碼合法性

    本文給大家分享了2則使用Python實現(xiàn)正則表達式檢測密碼合法性的代碼,由于是新手,所以方法比較笨,不過還是分享給小伙伴,希望對大家能夠有所幫助。
    2016-01-01
  • 淺談django框架集成swagger以及自定義參數(shù)問題

    淺談django框架集成swagger以及自定義參數(shù)問題

    這篇文章主要介紹了淺談django框架集成swagger以及自定義參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 解決Pycharm運行時找不到文件的問題

    解決Pycharm運行時找不到文件的問題

    今天小編就為大家分享一篇解決Pycharm運行時找不到文件的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10

最新評論