第G7周:Semi-Supervised GAN 理论与实战

  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍖 原作者:K同学啊 | 接辅导、项目定制
  • 🚀 文章来源:K同学的学习圈子

参考论文:《Semi-Supervised Learning with Generative Adversarial Networks》

一、理论知识

1.1 SGAN提出用于解决哪些问题

SGAN被提出用于解决半监督学习问题。什么是半监督学习?举个例子,半监督学习就像是在学习时,有一部分作业是老师给了标准答案,但还有一大部分作业是没有答案的。你要尽力根据已知的答案来学习,然后猜测其他作业的答案。这就是半监督学习。
SGAN利用生成对抗网络(GAN)的框架,结合生成器和判别器的协同训练,通过生成器生成样本来扩充训练数据,从而提高模型在半监督学习任务上的性能。

1.2 SGAN模型

在标准的生成对抗网络(GAN)中,鉴别器网络 D D D 负责输出关于输入图像来自数据生成分布中的概率。传统方法中,这由一个单个 sigmoid 单元结束的前馈式网络实现,表示输入图像是真实数据的概率。然而,也可以通过使用一个 softmax 输出层来实现,每个类对应一个单元,包括真实数据和生成数据两类,即 [real, fake]。这种修改后的结构使得鉴别器 D D D 具有 N + 1 N+1 N+1 个输出单元,分别对应于 N N N 个真实数据类别和生成数据类别。在这种情况下, D D D 也可以被视为一个分类器,我们将这种网络称为 D / C D/C D/C
在这里插入图片描述

二、实战

2.1 前期准备

先导入所需库和模块,然后创建一个名为"images"的文件夹,用于保存训练过程中生成的图像,之后定义参数,包括训练的轮数(n_epochs)、批次大小(batch_size)、学习率(lr)等,最后使用torch.cuda.is_available()函数检查CUDA是否可用。

import argparse
import os
import numpy as np
import mathimport torchvision.transforms as transforms
from torchvision.utils import save_imagefrom torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variableimport torch.nn as nn
import torch.nn.functional as F
import torchos.makedirs("images", exist_ok=True)parser = argparse.ArgumentParser()
parser.add_argument("--n_epochs", type=int, default=50, 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=4, 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("--num_classes", type=int, default=10, help="number of classes for dataset")
parser.add_argument("--img_size", type=int, default=32, 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 between image sampling")
opt = parser.parse_args()
print(opt)cuda = True if torch.cuda.is_available() else False

Namespace(n_epochs=50, batch_size=64, lr=0.0002, b1=0.5, b2=0.999, n_cpu=4, latent_dim=100, num_classes=10, img_size=32, channels=1, sample_interval=400)

2.2 权重初始化与模型定义

def weights_init_normal(m):classname = m.__class__.__name__if classname.find("Conv") != -1:torch.nn.init.normal_(m.weight.data, 0.0, 0.02)elif classname.find("BatchNorm") != -1:torch.nn.init.normal_(m.weight.data, 1.0, 0.02)torch.nn.init.constant_(m.bias.data, 0.0)class Generator(nn.Module):def __init__(self):super(Generator, self).__init__()self.label_emb = nn.Embedding(opt.num_classes, opt.latent_dim)self.init_size = opt.img_size // 4  # Initial size before upsamplingself.l1 = nn.Sequential(nn.Linear(opt.latent_dim, 128 * self.init_size ** 2))self.conv_blocks = nn.Sequential(nn.BatchNorm2d(128),nn.Upsample(scale_factor=2),nn.Conv2d(128, 128, 3, stride=1, padding=1),nn.BatchNorm2d(128, 0.8),nn.LeakyReLU(0.2, inplace=True),nn.Upsample(scale_factor=2),nn.Conv2d(128, 64, 3, stride=1, padding=1),nn.BatchNorm2d(64, 0.8),nn.LeakyReLU(0.2, inplace=True),nn.Conv2d(64, opt.channels, 3, stride=1, padding=1),nn.Tanh(),)def forward(self, noise):out = self.l1(noise)out = out.view(out.shape[0], 128, self.init_size, self.init_size)img = self.conv_blocks(out)return imgclass Discriminator(nn.Module):def __init__(self):super(Discriminator, self).__init__()def discriminator_block(in_filters, out_filters, bn=True):"""Returns layers of each discriminator block"""block = [nn.Conv2d(in_filters, out_filters, 3, 2, 1), nn.LeakyReLU(0.2, inplace=True), nn.Dropout2d(0.25)]if bn:block.append(nn.BatchNorm2d(out_filters, 0.8))return blockself.conv_blocks = nn.Sequential(*discriminator_block(opt.channels, 16, bn=False),*discriminator_block(16, 32),*discriminator_block(32, 64),*discriminator_block(64, 128),)# The height and width of downsampled imageds_size = opt.img_size // 2 ** 4# Output layersself.adv_layer = nn.Sequential(nn.Linear(128 * ds_size ** 2, 1), nn.Sigmoid())self.aux_layer = nn.Sequential(nn.Linear(128 * ds_size ** 2, opt.num_classes + 1), nn.Softmax())def forward(self, img):out = self.conv_blocks(img)out = out.view(out.shape[0], -1)validity = self.adv_layer(out)label = self.aux_layer(out)return validity, label

2.3 模型配置

# Loss functions
adversarial_loss = torch.nn.BCELoss()
auxiliary_loss = torch.nn.CrossEntropyLoss()# Initialize generator and discriminator
generator = Generator()
discriminator = Discriminator()if cuda:generator.cuda()discriminator.cuda()adversarial_loss.cuda()auxiliary_loss.cuda()# Initialize weights
generator.apply(weights_init_normal)
discriminator.apply(weights_init_normal)# 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))FloatTensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if cuda else torch.LongTensor

2.4 训练

for epoch in range(opt.n_epochs):for i, (imgs, labels) in enumerate(dataloader):batch_size = imgs.shape[0]# Adversarial ground truthsvalid = Variable(FloatTensor(batch_size, 1).fill_(1.0), requires_grad=False)fake = Variable(FloatTensor(batch_size, 1).fill_(0.0), requires_grad=False)fake_aux_gt = Variable(LongTensor(batch_size).fill_(opt.num_classes), requires_grad=False)# Configure inputreal_imgs = Variable(imgs.type(FloatTensor))labels = Variable(labels.type(LongTensor))# -----------------#  Train Generator# -----------------optimizer_G.zero_grad()# Sample noise and labels as generator inputz = Variable(FloatTensor(np.random.normal(0, 1, (batch_size, opt.latent_dim))))# Generate a batch of imagesgen_imgs = generator(z)# Loss measures generator's ability to fool the discriminatorvalidity, _ = discriminator(gen_imgs)g_loss = adversarial_loss(validity, valid)g_loss.backward()optimizer_G.step()# ---------------------#  Train Discriminator# ---------------------optimizer_D.zero_grad()# Loss for real imagesreal_pred, real_aux = discriminator(real_imgs)d_real_loss = (adversarial_loss(real_pred, valid) + auxiliary_loss(real_aux, labels)) / 2# Loss for fake imagesfake_pred, fake_aux = discriminator(gen_imgs.detach())d_fake_loss = (adversarial_loss(fake_pred, fake) + auxiliary_loss(fake_aux, fake_aux_gt)) / 2# Total discriminator lossd_loss = (d_real_loss + d_fake_loss) / 2# Calculate discriminator accuracypred = np.concatenate([real_aux.data.cpu().numpy(), fake_aux.data.cpu().numpy()], axis=0)gt = np.concatenate([labels.data.cpu().numpy(), fake_aux_gt.data.cpu().numpy()], axis=0)d_acc = np.mean(np.argmax(pred, axis=1) == gt)d_loss.backward()optimizer_D.step()batches_done = epoch * len(dataloader) + iif batches_done % opt.sample_interval == 0:save_image(gen_imgs.data[:25], "images/%d.png" % batches_done, nrow=5, normalize=True)print("[Epoch %d/%d] [Batch %d/%d] [D loss: %f, acc: %d%%] [G loss: %f]"% (epoch, opt.n_epochs, i, len(dataloader), d_loss.item(), 100 * d_acc, g_loss.item()))

[Epoch 0/50] [Batch 937/938] [D loss: 1.352186, acc: 50%] [G loss: 0.663589]
[Epoch 1/50] [Batch 937/938] [D loss: 1.235517, acc: 50%] [G loss: 1.671782]
[Epoch 2/50] [Batch 937/938] [D loss: 1.045947, acc: 65%] [G loss: 1.315296]
[Epoch 3/50] [Batch 937/938] [D loss: 1.039367, acc: 70%] [G loss: 2.646401]
[Epoch 4/50] [Batch 937/938] [D loss: 0.899272, acc: 85%] [G loss: 2.424302]
[Epoch 5/50] [Batch 937/938] [D loss: 1.031800, acc: 75%] [G loss: 1.979734]
[Epoch 6/50] [Batch 937/938] [D loss: 0.865867, acc: 87%] [G loss: 4.274090]
[Epoch 7/50] [Batch 937/938] [D loss: 0.862276, acc: 92%] [G loss: 3.419953]
[Epoch 8/50] [Batch 937/938] [D loss: 1.005301, acc: 82%] [G loss: 1.867689]
[Epoch 9/50] [Batch 937/938] [D loss: 0.919378, acc: 90%] [G loss: 4.254766]
[Epoch 10/50] [Batch 937/938] [D loss: 0.886876, acc: 90%] [G loss: 4.498873]
[Epoch 11/50] [Batch 937/938] [D loss: 0.969284, acc: 82%] [G loss: 3.070788]
[Epoch 12/50] [Batch 937/938] [D loss: 0.893068, acc: 85%] [G loss: 6.498258]
[Epoch 13/50] [Batch 937/938] [D loss: 0.840133, acc: 90%] [G loss: 8.441454]
[Epoch 14/50] [Batch 937/938] [D loss: 1.024140, acc: 84%] [G loss: 12.355464]
[Epoch 15/50] [Batch 937/938] [D loss: 0.821921, acc: 95%] [G loss: 6.133313]
[Epoch 16/50] [Batch 937/938] [D loss: 0.818303, acc: 95%] [G loss: 4.394979]
[Epoch 17/50] [Batch 937/938] [D loss: 0.806723, acc: 95%] [G loss: 6.602290]
[Epoch 18/50] [Batch 937/938] [D loss: 0.884590, acc: 92%] [G loss: 3.963483]
[Epoch 19/50] [Batch 937/938] [D loss: 1.092136, acc: 71%] [G loss: 8.668056]
[Epoch 20/50] [Batch 937/938] [D loss: 0.936026, acc: 89%] [G loss: 2.470026]
[Epoch 21/50] [Batch 937/938] [D loss: 0.875962, acc: 89%] [G loss: 5.632479]
[Epoch 22/50] [Batch 937/938] [D loss: 0.811583, acc: 95%] [G loss: 10.195074]
[Epoch 23/50] [Batch 937/938] [D loss: 0.789067, acc: 100%] [G loss: 11.308340]
[Epoch 24/50] [Batch 937/938] [D loss: 0.795523, acc: 96%] [G loss: 8.784575]
[Epoch 25/50] [Batch 937/938] [D loss: 0.865301, acc: 92%] [G loss: 10.207865]
[Epoch 26/50] [Batch 937/938] [D loss: 0.786401, acc: 100%] [G loss: 3.394327]
[Epoch 27/50] [Batch 937/938] [D loss: 0.796065, acc: 95%] [G loss: 7.474880]
[Epoch 28/50] [Batch 937/938] [D loss: 0.805746, acc: 96%] [G loss: 5.651248]
[Epoch 29/50] [Batch 937/938] [D loss: 0.773570, acc: 100%] [G loss: 7.849866]
[Epoch 30/50] [Batch 937/938] [D loss: 0.804366, acc: 96%] [G loss: 7.017126]
[Epoch 31/50] [Batch 937/938] [D loss: 0.898856, acc: 87%] [G loss: 11.380006]
[Epoch 32/50] [Batch 937/938] [D loss: 0.784667, acc: 98%] [G loss: 11.001215]
[Epoch 33/50] [Batch 937/938] [D loss: 0.781652, acc: 100%] [G loss: 8.813923]
[Epoch 34/50] [Batch 937/938] [D loss: 0.780957, acc: 98%] [G loss: 7.537516]
[Epoch 35/50] [Batch 937/938] [D loss: 0.784157, acc: 98%] [G loss: 9.816891]
[Epoch 36/50] [Batch 937/938] [D loss: 0.892552, acc: 92%] [G loss: 10.325437]
[Epoch 37/50] [Batch 937/938] [D loss: 0.788715, acc: 96%] [G loss: 12.527905]
[Epoch 38/50] [Batch 937/938] [D loss: 0.780603, acc: 98%] [G loss: 6.954862]
[Epoch 39/50] [Batch 937/938] [D loss: 0.853637, acc: 93%] [G loss: 11.160628]
[Epoch 40/50] [Batch 937/938] [D loss: 0.825676, acc: 95%] [G loss: 10.061171]
[Epoch 41/50] [Batch 937/938] [D loss: 0.782195, acc: 98%] [G loss: 7.089082]
[Epoch 42/50] [Batch 937/938] [D loss: 0.780453, acc: 98%] [G loss: 7.865266]
[Epoch 43/50] [Batch 937/938] [D loss: 0.785953, acc: 98%] [G loss: 7.176102]
[Epoch 44/50] [Batch 937/938] [D loss: 0.783754, acc: 98%] [G loss: 13.567838]
[Epoch 45/50] [Batch 937/938] [D loss: 0.798364, acc: 95%] [G loss: 15.339465]
[Epoch 46/50] [Batch 937/938] [D loss: 0.809889, acc: 92%] [G loss: 7.125263]
[Epoch 47/50] [Batch 937/938] [D loss: 0.807932, acc: 98%] [G loss: 7.732235]
[Epoch 48/50] [Batch 937/938] [D loss: 0.781007, acc: 98%] [G loss: 13.505493]
[Epoch 49/50] [Batch 937/938] [D loss: 0.780933, acc: 98%] [G loss: 10.880657]

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
最开始到三十多轮程序意外终止,尝试将n_cpu数下调至4,成功运行。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/619934.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

企业级OVSSL证书的五大优势

在数字化时代,企业级OVSSL(Organization Validation Secure Sockets Layer)证书已成为保护网站安全、提升用户信任度的重要工具。越来越多企业在自身网络安全方面更倾向于OVSSL证书,以下就带你了解企业级OVSSL证书的五大优势&…

【LeetCode】2635. 转换数组中的每个元素

转换数组中的每个元素 编写一个函数,这个函数接收一个整数数组 arr 和一个映射函数 fn,通过该映射函数返回一个新的数组。 返回数组的创建语句应为 returnedArray[i] fn(arr[i], i)。 请你在不使用内置方法 Array.map 的前提下解决这个问题。 示例 1:…

第 6 章 URDF、Gazebo与Rviz综合应用(自学二刷笔记)

重要参考: 课程链接:https://www.bilibili.com/video/BV1Ci4y1L7ZZ 讲义链接:Introduction Autolabor-ROS机器人入门课程《ROS理论与实践》零基础教程 6.7.2 雷达信息仿真以及显示 通过 Gazebo 模拟激光雷达传感器,并在 Rviz 中显示激光数据。 实现…

#381. 四边形继承练习

太爽了 甚至还现学了叉积判断线段是否相交和求面积的方法 先给出我的代码&#xff1a; #include <iostream> #include <vector> #include <iomanip> #include <cmath>using namespace std;//下面需要补充多个类的声明及实现代码 const double EPS 1…

RocketMQ 02 功能大纲介绍

RocketMQ 02 主流的MQ有很多&#xff0c;比如ActiveMQ、RabbitMQ、RocketMQ、Kafka、ZeroMQ等。 之前阿里巴巴也是使用ActiveMQ&#xff0c;随着业务发展&#xff0c;ActiveMQ IO 模块出现瓶颈&#xff0c;后来阿里巴巴 通过一系列优化但是还是不能很好的解决&#xff0c;之后…

小程序变更主体需要多久?

小程序迁移变更主体有什么作用&#xff1f;小程序迁移变更主体的好处有很多哦&#xff01;比如可以获得更多权限功能、公司变更或注销时可以保证账号的正常使用、收购账号后可以改变归属权或使用权等等。小程序迁移变更主体的条件有哪些&#xff1f;1、新主体必须是企业主体&am…

判断IQ水平-第12届蓝桥杯选拔赛Python真题精选

[导读]&#xff1a;超平老师的Scratch蓝桥杯真题解读系列在推出之后&#xff0c;受到了广大老师和家长的好评&#xff0c;非常感谢各位的认可和厚爱。作为回馈&#xff0c;超平老师计划推出《Python蓝桥杯真题解析100讲》&#xff0c;这是解读系列的第50讲。 判断IQ水平&#…

如何检测和避免线程死锁?

在日常开发中涉及到多线程开发时候就很容易会产生死锁 what: 什么是线程死锁? 线程死锁是指两个或两个以上的线程在执行过程中&#xff0c;由于竞争资源或者由于彼此通信而造成的一种阻塞现象。当这些线程互相持有对方所需要的资源时&#xff0c;会互相等待对方释放资源&am…

排序算法—堆排序

文章目录 堆排序堆思路过程建堆排序 代码实现 堆排序 时间复杂度&#xff1a;O(N*logN) 稳定性&#xff1a;不稳定&#xff08;相同元素排序后的相对位置改变&#xff09; 堆 堆的逻辑结构是一棵完全二叉树&#xff1b;堆的物理结构是一个数组&#xff0c;通过下标表示父子结…

Shell脚本学习(一):Shell内置命令与Shell运算符

Shell内置命令 理解内置命令的含义。 内置命令介绍 Shell内置命令&#xff0c;就是由Bash Shell自身提供的命令&#xff0c;而不是文件系统中的可执行文件。 使用type 可以用来确定一个命令是否是内置命令&#xff1a; type 命令演示&#xff1a; 对于上述演示的两个命令来…

普乐蛙VR神州飞船设备VR太空舱体验馆VR博物馆

中国航天式浪漫知多少&#xff1f;千百年来古人对浩瀚宇宙有着无尽的浪漫想象&#xff0c;而在一代又一代中国航天事业奋斗者的努力中&#xff0c;远古神话不再是幻想&#xff0c;它终被照进现实——中国载人飞船“神舟”、中国载人空间站“天宫”、中国绕月人造卫星“嫦娥一号…

mybatis的一对多

业务&#xff1a;通常主表从表 查询&#xff0c;一对多关系&#xff0c;通常是先查主表&#xff0c;然后拿主表的 关联字段与从表关联。在代码中 通常用for 循环等方法给 从表的数据赋值&#xff0c;很麻烦&#xff0c;&#xff0c;&#xff0c;很麻烦。。。。 用mybatis的…