【GAN论文解读系列】NeurIPS 2016 InfoGAN 使用InfoGAN解耦出可解释的特征


相关资料

来自:https://github.com/hwalsuklee/tensorflow-generative-model-collections

InfoGAN的动机

InfoGAN模型图

在InfoGAN中输入Generator的噪音z分成了两个部分:一部分是随机噪音z’,另一部分是由若干个隐向量拼接而成latent code c。

c里面的每个维度符合先验的概率分布,比如categorical code $c_1\sim Cat(K=10,p=0.1)$,two continuous codes $c2,c3\sim Unif(-1,1)$

注:一般来说,服从Categorical Distribution的变量都是一个向量,并且是一个One-hot编码的形式,所以$c_1$是一个10维的one-hot向量

InfoGAN原论文的描述:

InfoGAN的优化目标的推导逻辑

这里是直接截取了我的notion笔记

模型训练效果

在对模型进行187200次参数更新后,我们现在展示一下模型的效果

Generator的输入:z = [c1, c2, c3, z']

注意:我们事先并不知道c1, c2,c3代表什么语义特征,是模型自己学出来的。

  • 每一行c1向量有规律的变换(从[1,0,..0]到[0,0,…1]),其余维度c2,c3,z’保持不变
  • 每一列c2有规律的变化(从-1到1),其余维度c1,c3,z’保持不变(我们解耦出来一个有语义的特征c2,表示数字的倾斜度

  • 每一行c1向量有规律的变换(从[1,0,..0]到[0,0,…1]),其余维度c2,c3,z’保持不变
  • 每一列c3有规律的变化(从-1到1),其余维度c1,c2,z’保持不变(我们解耦出来一个有语义的特征c3,表示数字的宽度

附:InfoGAN论文中的实验结果如下


InfoGAN的训练是无监督的

我们在训练的时候不需要真实图片上的数字标签,也就是说for i, (imgs, labels) in enumerate(dataloader)的labels我们并不会用到。

Discrete code

Latent code c中有个离散的编码,比如[1,0,…0],我们是用它来capture数字类型这个特征的。但是我们并没有指定[1,0,…,0]对应数字0,[0,0,1,….,0]对应数字2,实际上我们跑了InfoGAN后生成的数字的对应关系是模型自己学到的。

论文中也提到了这一点

InfoGAN代码(Pytorch)

数据集:MNIST

训练Generator时,我们是希望Fake images的标签尽量预测为1

import argparse
import os
import numpy as np
import math
import itertools

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
import sys

os.makedirs("images/static/", exist_ok=True)
os.makedirs("images/varying_c1/", exist_ok=True)
os.makedirs("images/varying_c2/", 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=62, help="dimensionality of the latent space")
parser.add_argument("--code_dim", type=int, default=2, help="latent code")
parser.add_argument("--n_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


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)


def to_categorical(y, num_columns):
    """Returns one-hot encoded Variable"""
    y_cat = np.zeros((y.shape[0], num_columns))
    y_cat[range(y.shape[0]), y] = 1.0

    return Variable(FloatTensor(y_cat))


class Generator(nn.Module):
    def __init__(self):
        super(Generator, self).__init__()
        input_dim = opt.latent_dim + opt.n_classes + opt.code_dim

        self.init_size = opt.img_size // 4  # Initial size before upsampling
        self.l1 = nn.Sequential(nn.Linear(input_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, labels, code):
        # [b, 62], [b,10], [b,2]
        gen_input = torch.cat((noise, labels, code), -1)
        out = self.l1(gen_input)
        # 重构成b个128*8*8的图
        out = out.view(out.shape[0], 128, self.init_size, self.init_size)
        # 然后进入卷积层,得到b个1*32*32的图片
        img = self.conv_blocks(out)
        return img


class 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 block

        self.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 image
        ds_size = opt.img_size // 2 ** 4

        # Output layers
        # Discriminator的最后一层
        self.adv_layer = nn.Sequential(nn.Linear(128 * ds_size ** 2, 1))
        # Classifier的最后一层
        self.aux_layer = nn.Sequential(nn.Linear(128 * ds_size ** 2, opt.n_classes), nn.Softmax())
        self.latent_layer = nn.Sequential(nn.Linear(128 * ds_size ** 2, opt.code_dim))

    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)
        latent_code = self.latent_layer(out)
        # c = [label, latent_code],即 [64*10,64*2]
        return validity, label, latent_code


# Loss functions
adversarial_loss = torch.nn.MSELoss()
categorical_loss = torch.nn.CrossEntropyLoss()
continuous_loss = torch.nn.MSELoss()

# Loss weights
lambda_cat = 1
lambda_con = 0.1

# Initialize generator and discriminator
generator = Generator()
discriminator = Discriminator()

if cuda:
    generator.cuda()
    discriminator.cuda()
    adversarial_loss.cuda()
    categorical_loss.cuda()
    continuous_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))
optimizer_info = torch.optim.Adam(
    itertools.chain(generator.parameters(), 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

# Static generator inputs for sampling
# 这里两个是画图用的
static_z = Variable(FloatTensor(np.zeros((opt.n_classes ** 2, opt.latent_dim))))
static_label = to_categorical(
    np.array([num for _ in range(opt.n_classes) for num in range(opt.n_classes)]), num_columns=opt.n_classes
)
static_code = Variable(FloatTensor(np.zeros((opt.n_classes ** 2, opt.code_dim))))


def sample_image(n_row, batches_done):
    """Saves a grid of generated digits ranging from 0 to n_classes"""
    # Static sample
    z = Variable(FloatTensor(np.random.normal(0, 1, (n_row ** 2, opt.latent_dim))))
    static_sample = generator(z, static_label, static_code)
    # 保存成10*10的png
    save_image(static_sample.data, "images/static/%d.png" % batches_done, nrow=n_row, normalize=True)

    # Get varied c1 and c2
    zeros = np.zeros((n_row ** 2, 1))
    c_varied = np.repeat(np.linspace(-1, 1, n_row)[:, np.newaxis], n_row, 0)
    # 让c1从-1到1变化
    c1 = Variable(FloatTensor(np.concatenate((c_varied, zeros), -1)))
    c2 = Variable(FloatTensor(np.concatenate((zeros, c_varied), -1)))
    # static_z全是0,static_label: [[1,0,..0],[0,1,..,0],...]
    sample1 = generator(static_z, static_label, c1)  # c1是两维的,但只有第一维在变化,在子图中从上往下会变化
    sample2 = generator(static_z, static_label, c2)
    save_image(sample1.data, "images/varying_c1/%d.png" % batches_done, nrow=n_row, normalize=True)
    save_image(sample2.data, "images/varying_c2/%d.png" % batches_done, nrow=n_row, normalize=True)


# ----------
#  Training
# ----------

for epoch in range(opt.n_epochs):
    print("")
    for i, (imgs, labels) in enumerate(dataloader):
        # imgs: 64*1*32*32
        batch_size = imgs.shape[0]

        # Adversarial ground truths
        valid = Variable(FloatTensor(batch_size, 1).fill_(1.0), requires_grad=False)
        fake = Variable(FloatTensor(batch_size, 1).fill_(0.0), requires_grad=False)

        # Configure input
        # real_imgs: 64*1*32*32
        real_imgs = Variable(imgs.type(FloatTensor))
        # labels: 64*10 (one-hot)
        labels = to_categorical(labels.numpy(), num_columns=opt.n_classes)

        # -----------------
        #  Train Generator (这一步单纯希望生成的图片x能骗过Discriminator)
        # -----------------

        optimizer_G.zero_grad()

        # Sample noise and labels as generator input
        # 从正态分布中得到64个隐向量z
        z = Variable(FloatTensor(np.random.normal(0, 1, (batch_size, opt.latent_dim))))
        # 从离散均匀分布中产生64个one-hot向量
        label_input = to_categorical(np.random.randint(0, opt.n_classes, batch_size), num_columns=opt.n_classes)
        # 从连续均匀分布uniform中采样64个[c1,c2]向量
        code_input = Variable(FloatTensor(np.random.uniform(-1, 1, (batch_size, opt.code_dim))))

        # Generate a batch of images
        gen_imgs = generator(z, label_input, code_input)

        # Loss measures generator's ability to fool the discriminator
        validity, _, _ = discriminator(gen_imgs)
        g_loss = adversarial_loss(validity, valid)  # 希望让假图片的预测概率接近1的方式来骗过discriminator

        g_loss.backward()
        optimizer_G.step()

        # ---------------------
        #  Train Discriminator
        # ---------------------

        optimizer_D.zero_grad()

        # Loss for real images
        real_pred, _, _ = discriminator(real_imgs)
        d_real_loss = adversarial_loss(real_pred, valid)  # 让真图片预测概率接近1

        # Loss for fake images
        fake_pred, _, _ = discriminator(gen_imgs.detach())  # 因为这里是把gen_imgs当做输入数据,来训练D的参数,所以要detach
        d_fake_loss = adversarial_loss(fake_pred, fake)  # 让假图片预测概率接近0

        # Total discriminator loss
        d_loss = (d_real_loss + d_fake_loss) / 2

        d_loss.backward()
        optimizer_D.step()

        # ------------------
        # Information Loss
        # ------------------
        # 这一步互信息会重新生成新的z和c,然后训练[z,c]->Generator->x->Classifier->c'这条路线
        # c = [0, 1, 0, 0, ...,0   , c1, c2]

        optimizer_info.zero_grad()

        # Sample labels
        sampled_labels = np.random.randint(0, opt.n_classes, batch_size)

        # Ground truth labels
        gt_labels = Variable(LongTensor(sampled_labels), requires_grad=False)

        # Sample noise, labels and code as generator input
        z = Variable(FloatTensor(np.random.normal(0, 1, (batch_size, opt.latent_dim))))
        label_input = to_categorical(sampled_labels, num_columns=opt.n_classes)
        code_input = Variable(FloatTensor(np.random.uniform(-1, 1, (batch_size, opt.code_dim))))

        gen_imgs = generator(z, label_input, code_input)
        _, pred_label, pred_code = discriminator(gen_imgs)
        # 直接算loss,看Classifier能不能还原c
        info_loss = lambda_cat * categorical_loss(pred_label, gt_labels) + lambda_con * continuous_loss(
            pred_code, code_input
        )

        info_loss.backward()
        optimizer_info.step()

        # --------------
        # Log Progress
        # --------------

        sys.stdout.write(
            "\r[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f] [info loss: %f]"
            % (epoch, opt.n_epochs, i, len(dataloader), d_loss.item(), g_loss.item(), info_loss.item()))
        sys.stdout.flush()

        # print(
        #     "[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f] [info loss: %f]"
        #     % (epoch, opt.n_epochs, i, len(dataloader), d_loss.item(), g_loss.item(), info_loss.item())
        # )

        batches_done = epoch * len(dataloader) + i
        if batches_done % opt.sample_interval == 0:
            sample_image(n_row=10, batches_done=batches_done)

附:Generative Models理想的特征解耦效果


Author: SHWEI
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source SHWEI !
评论
 Previous
【知识蒸馏论文解读】Dataset Distillation 创新性十足的数据集蒸馏 【知识蒸馏论文解读】Dataset Distillation 创新性十足的数据集蒸馏
写在前面最近对数据集蒸馏比较感兴趣,抽时间看了下这篇经典的数据蒸馏论文《Dataset Distillation》,它是属于knowledge distillation领域的工作。 论文地址:https://arxiv.org/pdf/
2021-12-17
Next 
【NIPS 2016图神经网络论文解读】Variational Graph Auto-Encoders (VGAE) 基于VAE的图变分自编码器 【NIPS 2016图神经网络论文解读】Variational Graph Auto-Encoders (VGAE) 基于VAE的图变分自编码器
写在前面 论文题目:Variational Graph Auto-Encoders 论文作者:Thomas N. Kipf, Max Welling 论文组织:University of Amsterdam 论文会议:NIPS Works
2021-12-10
  TOC