BUCT-Vision / weekly-review

Weekly-review of BUCT Lab-614
https://github.com/DIYer22/weekly-review/issues
29 stars 4 forks source link

Review-2018.09.23-张云帆 #289

Open ghost opened 6 years ago

ghost commented 6 years ago

上周总结

1.李宏毅GAN的课程看了5讲,数学部分跳了[math warning]。 2.玩了玩风格迁移的GAN,感觉蛮好玩的... 参考1 参考2 参考3 3.然后想尝试自己写一下各种GAN,找了一些适合我这种新手看的... 参考4 参考5 4.自己也半抄半写的写了一下DCGAN,加了注释。 怎么把代码折叠啊

import torch as t
from torch import nn
from torch.autograd import Variable
from torch.optim import Adam
from torchvision import transforms
from torchvision.utils import make_grid
from torch.utils.data import DataLoader
from torchvision.datasets import CIFAR10
from pylab import plt  #pylab结合了pyplot和numpy

class Config:
    lr = 0.0002
    nz = 100 #噪声维度
    image_size = 64
    image_size2 = 64
    nc = 3 #图片是三通道的
    ngf = 64 #G的特征层数
    ndf = 64 #D的特征层数
    beta1 = 0.5
    batch_size = 32
    max_epoch = 10
    workers = 0
    gpu = True

opt = Config()

#载入数据
transform = transforms.Compose([
    transforms.Resize(opt.image_size),
    transforms.ToTensor(),
    transforms.Normalize([0.5]*3,[0.5]*3) #均值&标准差
])

dataset = CIFAR10(root='cifar10/',transform=transform,download=True)
dataloader = DataLoader(dataset,opt.batch_size,shuffle=True,num_workers=opt.workers)

#输入的是噪声图片的维度
netg = nn.Sequential(
    nn.ConvTranspose2d(opt.nz,opt.ngf*8,4,1,0,bias=False),
    nn.BatchNorm2d(opt.ngf*8),
    nn.ReLU(True),

    nn.ConvTranspose2d(opt.ngf*8,opt.ngf*4,4,2,1,bias=False),
    nn.BatchNorm2d(opt.ngf*4),
    nn.ReLU(True),

nn.ConvTranspose2d(opt.ngf*4,opt.ngf*2,4,2,1,bias=False),
nn.BatchNorm2d(opt.ngf*2),
nn.ReLU(True),

nn.ConvTranspose2d(opt.ngf*2,opt.ngf,4,2,1,bias=False),
nn.BatchNorm2d(opt.ngf),
nn.ReLU(True),

nn.ConvTranspose2d(opt.ngf,opt.nc,4,2,1,bias=False),
nn.Tanh()    #输出的是FAKE图片的维度
)

netd = nn.Sequential(
    nn.Conv2d(opt.nc,opt.ndf,4,2,1,bias=False),
    nn.LeakyReLU(0.2,inplace=True),

nn.Conv2d(opt.ndf, opt.ndf*2, 4, 2, 1, bias=False),
nn.BatchNorm2d(opt.ndf*2),
nn.LeakyReLU(0.2, inplace=True),

nn.Conv2d(opt.ndf*2, opt.ndf*4, 4, 2, 1, bias=False),
nn.BatchNorm2d(opt.ndf * 4),
nn.LeakyReLU(0.2, inplace=True),

nn.Conv2d(opt.ndf*4, opt.ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(opt.ndf * 8),
nn.LeakyReLU(0.2, inplace=True),

nn.Conv2d(opt.ndf*8,1,4,1,0,bias=False),
nn.Sigmoid()
)

#optimizer
optimizerD = Adam(netd.parameters(),lr = opt.lr,betas=(opt.beta1,0.999))
optimizerG = Adam(netg.parameters(),lr = opt.lr,betas=(opt.beta1,0.999))

#criterion
criterion = nn.BCELoss()

fix_noise = Variable(t.FloatTensor(opt.batch_size,opt.nz,1,1).normal_(0,1))#高斯分布N(0,1)
if opt.gpu:
    fix_noise = fix_noise.cuda()
    netd.cuda()
    netg.cuda()
    criterion.cuda()

print("开始训练")

for epoch in range(opt.max_epoch):
    for ii,data in enumerate(dataloader,start=0):
        real,_ = data
        input = Variable(real)
        label = Variable(t.ones(input.size(0)))#一开始训练DIS用real image 所以给的label都是1,所以这个label大小和batch_size大小一样
        noise = t.randn(input.size(0),opt.nz,1,1)#不是很理解后面两个1是干啥用的
        noise = Variable(noise)

    if opt.gpu:
        noise = noise.cuda()
        input = input.cuda()
        label = label.cuda()

    #____train disc____
    netd.zero_grad()
    #用real image train
    output = netd(input)
    loss_real = criterion(output.squeeze(),label)#output 与 1之间的loss
    loss_real.backward()
    # D_x = output.data.mean()#这是平均loss
    #用fake image train
    fake_pic = netg(noise).detach()#截断反向传播,只影响G不影响D
    output2 = netd(fake_pic)
    label.data.fill_(0) #把label的1改成0,因为是fake image
    loss_fake = criterion(output2.squeeze(),label)
    loss_fake.backward()
    # D_x2 = output2.data.mean()
    error_D = loss_real+loss_fake
    optimizerD.step()

    #_____train generator__
    netg.zero_grad()
    label.data.fill_(1) #要计算的是生存的图片与真实的loss,所以是1
    noise.data.normal_(0,1)#产生0-1的高斯噪声
    fake_pic = netg(noise)
    output = netd(fake_pic)
    loss_G = criterion(output.squeeze(),label)
    loss_G.backward()
    optimizerG.step()
    # D_G_z2 = output.data.mean()

if epoch%2 == 0:
    fake_u = netg(fix_noise)
    imgs = make_grid(fake_u.data*0.5+0.5).cpu() #chw
    plt.imshow(imgs.permute(1,2,0).numpy())
    plt.show()

5.LeetCode 写了7题 6.应付了两天本专业的课程设计 7.计网没咋看

RonDen commented 6 years ago

把文件传到自己的github上然后弄链接吧

ilydouble commented 6 years ago

对于初学者,推荐codewars,对于算法推荐leecode,然后gan的方法一直把控不住,我们也在做人脸的特征迁移和风格迁移。

ilydouble commented 6 years ago

你跟瞿安国讨论一下他做像素头像生成

ghost commented 6 years ago

之前也写过蛮多oj题,感觉leetcode上题目难度比较适中,所以有时候会写个一两题 和瞿安国同学已经交流过了