Showing preview only (766K chars total). Download the full file or copy to clipboard to get everything.
Repository: YangNaruto/FQ-GAN
Branch: master
Commit: bc43d45700b2
Files: 87
Total size: 732.2 KB
Directory structure:
gitextract_joocyhq2/
├── FQ-BigGAN/
│ ├── BigGAN.py
│ ├── BigGANdeep.py
│ ├── LICENSE
│ ├── TFHub/
│ │ ├── README.md
│ │ ├── biggan_v1.py
│ │ └── converter.py
│ ├── animal_hash.py
│ ├── calculate_inception_moments.py
│ ├── datasets.py
│ ├── inception_tf13.py
│ ├── inception_utils.py
│ ├── layers.py
│ ├── losses.py
│ ├── make_hdf5.py
│ ├── sample.py
│ ├── scripts/
│ │ ├── launch_C10.sh
│ │ ├── launch_C100.sh
│ │ ├── launch_I128_bs256x4.sh
│ │ ├── launch_I64_bs128x4.sh
│ │ └── utils/
│ │ ├── duplicate.sh
│ │ ├── prepare_data.sh
│ │ └── trans.py
│ ├── sync_batchnorm/
│ │ ├── __init__.py
│ │ ├── batchnorm.py
│ │ ├── batchnorm_reimpl.py
│ │ ├── comm.py
│ │ ├── replicate.py
│ │ └── unittest.py
│ ├── train.py
│ ├── train_fns.py
│ ├── utility/
│ │ ├── extract_imagenet.py
│ │ └── untar.py
│ ├── utils.py
│ └── vq_layer.py
├── FQ-StyleGAN/
│ ├── LICENSE.txt
│ ├── dataset_tool.py
│ ├── dnnlib/
│ │ ├── __init__.py
│ │ ├── submission/
│ │ │ ├── __init__.py
│ │ │ ├── internal/
│ │ │ │ ├── __init__.py
│ │ │ │ └── local.py
│ │ │ ├── run_context.py
│ │ │ └── submit.py
│ │ ├── tflib/
│ │ │ ├── __init__.py
│ │ │ ├── autosummary.py
│ │ │ ├── custom_ops.py
│ │ │ ├── network.py
│ │ │ ├── ops/
│ │ │ │ ├── __init__.py
│ │ │ │ ├── fused_bias_act.cu
│ │ │ │ ├── fused_bias_act.py
│ │ │ │ ├── upfirdn_2d.cu
│ │ │ │ └── upfirdn_2d.py
│ │ │ ├── optimizer.py
│ │ │ └── tfutil.py
│ │ └── util.py
│ ├── metrics/
│ │ ├── __init__.py
│ │ ├── frechet_inception_distance.py
│ │ ├── inception_score.py
│ │ ├── linear_separability.py
│ │ ├── metric_base.py
│ │ ├── metric_defaults.py
│ │ ├── perceptual_path_length.py
│ │ └── precision_recall.py
│ ├── pretrained_networks.py
│ ├── projector.py
│ ├── run_generator.py
│ ├── run_metrics.py
│ ├── run_projector.py
│ ├── run_training.py
│ ├── test_nvcc
│ ├── test_nvcc.cu
│ └── training/
│ ├── __init__.py
│ ├── dataset.py
│ ├── loss.py
│ ├── misc.py
│ ├── networks_stylegan.py
│ ├── networks_stylegan2.py
│ └── training_loop.py
├── FQ-U-GAT-IT/
│ ├── LICENSE
│ ├── UGATIT.py
│ ├── dataset/
│ │ └── download_dataset_1.sh
│ ├── download_dataset_2.sh
│ ├── logger.py
│ ├── main.py
│ ├── ops.py
│ ├── utils.py
│ └── vq_layer.py
└── README.md
================================================
FILE CONTENTS
================================================
================================================
FILE: FQ-BigGAN/BigGAN.py
================================================
import numpy as np
import math
import functools
import torch
import torch.nn as nn
from torch.nn import init
import torch.optim as optim
import torch.nn.functional as F
from torch.nn import Parameter as P
from vq_layer import Quantize
import layers
from sync_batchnorm import SynchronizedBatchNorm2d as SyncBatchNorm2d
# Architectures for G
# Attention is passed in in the format '32_64' to mean applying an attention
# block at both resolution 32x32 and 64x64. Just '64' will apply at 64x64.
def G_arch(ch=64, attention='64', ksize='333333', dilation='111111'):
arch = {}
arch[512] = {'in_channels' : [ch * item for item in [16, 16, 8, 8, 4, 2, 1]],
'out_channels' : [ch * item for item in [16, 8, 8, 4, 2, 1, 1]],
'upsample' : [True] * 7,
'resolution' : [8, 16, 32, 64, 128, 256, 512],
'attention' : {2**i: (2**i in [int(item) for item in attention.split('_')])
for i in range(3,10)}}
arch[256] = {'in_channels' : [ch * item for item in [16, 16, 8, 8, 4, 2]],
'out_channels' : [ch * item for item in [16, 8, 8, 4, 2, 1]],
'upsample' : [True] * 6,
'resolution' : [8, 16, 32, 64, 128, 256],
'attention' : {2**i: (2**i in [int(item) for item in attention.split('_')])
for i in range(3,9)}}
arch[128] = {'in_channels' : [ch * item for item in [16, 16, 8, 4, 2]],
'out_channels' : [ch * item for item in [16, 8, 4, 2, 1]],
'upsample' : [True] * 5,
'resolution' : [8, 16, 32, 64, 128],
'attention' : {2**i: (2**i in [int(item) for item in attention.split('_')])
for i in range(3,8)}}
arch[64] = {'in_channels' : [ch * item for item in [16, 16, 8, 4]],
'out_channels' : [ch * item for item in [16, 8, 4, 2]],
'upsample' : [True] * 4,
'resolution' : [8, 16, 32, 64],
'attention' : {2**i: (2**i in [int(item) for item in attention.split('_')])
for i in range(3,7)}}
arch[32] = {'in_channels' : [ch * item for item in [4, 4, 4]],
'out_channels' : [ch * item for item in [4, 4, 4]],
'upsample' : [True] * 3,
'resolution' : [8, 16, 32],
'attention' : {2**i: (2**i in [int(item) for item in attention.split('_')])
for i in range(3,6)}}
return arch
class Generator(nn.Module):
def __init__(self, G_ch=64, dim_z=128, bottom_width=4, resolution=128,
G_kernel_size=3, G_attn='64', n_classes=1000,
num_G_SVs=1, num_G_SV_itrs=1,
G_shared=True, shared_dim=0, hier=False,
cross_replica=False, mybn=False,
G_activation=nn.ReLU(inplace=False),
G_lr=5e-5, G_B1=0.0, G_B2=0.999, adam_eps=1e-8,
BN_eps=1e-5, SN_eps=1e-12, G_mixed_precision=False, G_fp16=False,
G_init='ortho', skip_init=False, no_optim=False,
G_param='SN', norm_style='bn',
**kwargs):
super(Generator, self).__init__()
# Channel width mulitplier
self.ch = G_ch
# Dimensionality of the latent space
self.dim_z = dim_z
# The initial spatial dimensions
self.bottom_width = bottom_width
# Resolution of the output
self.resolution = resolution
# Kernel size?
self.kernel_size = G_kernel_size
# Attention?
self.attention = G_attn
# number of classes, for use in categorical conditional generation
self.n_classes = n_classes
# Use shared embeddings?
self.G_shared = G_shared
# Dimensionality of the shared embedding? Unused if not using G_shared
self.shared_dim = shared_dim if shared_dim > 0 else dim_z
# Hierarchical latent space?
self.hier = hier
# Cross replica batchnorm?
self.cross_replica = cross_replica
# Use my batchnorm?
self.mybn = mybn
# nonlinearity for residual blocks
self.activation = G_activation
# Initialization style
self.init = G_init
# Parameterization style
self.G_param = G_param
# Normalization style
self.norm_style = norm_style
# Epsilon for BatchNorm?
self.BN_eps = BN_eps
# Epsilon for Spectral Norm?
self.SN_eps = SN_eps
# fp16?
self.fp16 = G_fp16
# Architecture dict
self.arch = G_arch(self.ch, self.attention)[resolution]
# If using hierarchical latents, adjust z
if self.hier:
# Number of places z slots into
self.num_slots = len(self.arch['in_channels']) + 1
self.z_chunk_size = (self.dim_z // self.num_slots)
# Recalculate latent dimensionality for even splitting into chunks
self.dim_z = self.z_chunk_size * self.num_slots
else:
self.num_slots = 1
self.z_chunk_size = 0
# Which convs, batchnorms, and linear layers to use
if self.G_param == 'SN':
self.which_conv = functools.partial(layers.SNConv2d,
kernel_size=3, padding=1,
num_svs=num_G_SVs, num_itrs=num_G_SV_itrs,
eps=self.SN_eps)
self.which_linear = functools.partial(layers.SNLinear,
num_svs=num_G_SVs, num_itrs=num_G_SV_itrs,
eps=self.SN_eps)
else:
self.which_conv = functools.partial(nn.Conv2d, kernel_size=3, padding=1)
self.which_linear = nn.Linear
# We use a non-spectral-normed embedding here regardless;
# For some reason applying SN to G's embedding seems to randomly cripple G
self.which_embedding = nn.Embedding
bn_linear = (functools.partial(self.which_linear, bias=False) if self.G_shared
else self.which_embedding)
##TODO: Modify BN
self.which_bn = functools.partial(layers.ccbn,
which_linear=bn_linear,
cross_replica=self.cross_replica,
mybn=self.mybn,
input_size=(self.shared_dim + self.z_chunk_size if self.G_shared
else self.n_classes),
norm_style=self.norm_style,
eps=self.BN_eps)
# self.which_bn = functools.partial(layers.bn,
# cross_replica=self.cross_replica,
# mybn=self.mybn,
# eps=self.BN_eps)
# Prepare model
# If not using shared embeddings, self.shared is just a passthrough
self.shared = (self.which_embedding(n_classes, self.shared_dim) if G_shared
else layers.identity())
# First linear layer
self.linear = self.which_linear(self.dim_z // self.num_slots,
self.arch['in_channels'][0] * (self.bottom_width **2))
# self.blocks is a doubly-nested list of modules, the outer loop intended
# to be over blocks at a given resolution (resblocks and/or self-attention)
# while the inner loop is over a given block
self.blocks = []
for index in range(len(self.arch['out_channels'])):
self.blocks += [[layers.GBlock(in_channels=self.arch['in_channels'][index],
out_channels=self.arch['out_channels'][index],
which_conv=self.which_conv,
which_bn=self.which_bn,
activation=self.activation,
upsample=(functools.partial(F.interpolate, scale_factor=2)
if self.arch['upsample'][index] else None))]]
# If attention on this block, attach it to the end
if self.arch['attention'][self.arch['resolution'][index]]:
print('Adding attention layer in G at resolution %d' % self.arch['resolution'][index])
self.blocks[-1] += [layers.Attention(self.arch['out_channels'][index], self.which_conv)]
# Turn self.blocks into a ModuleList so that it's all properly registered.
self.blocks = nn.ModuleList([nn.ModuleList(block) for block in self.blocks])
# output layer: batchnorm-relu-conv.
# Consider using a non-spectral conv here
self.output_layer = nn.Sequential(layers.bn(self.arch['out_channels'][-1],
cross_replica=self.cross_replica,
mybn=self.mybn),
self.activation,
self.which_conv(self.arch['out_channels'][-1], 3))
# Initialize weights. Optionally skip init for testing.
if not skip_init:
self.init_weights()
# Set up optimizer
# If this is an EMA copy, no need for an optim, so just return now
if no_optim:
return
self.lr, self.B1, self.B2, self.adam_eps = G_lr, G_B1, G_B2, adam_eps
if G_mixed_precision:
print('Using fp16 adam in G...')
import utils
self.optim = utils.Adam16(params=self.parameters(), lr=self.lr,
betas=(self.B1, self.B2), weight_decay=0,
eps=self.adam_eps)
else:
self.optim = optim.Adam(params=self.parameters(), lr=self.lr,
betas=(self.B1, self.B2), weight_decay=0,
eps=self.adam_eps)
# LR scheduling, left here for forward compatibility
# self.lr_sched = {'itr' : 0}# if self.progressive else {}
# self.j = 0
# Initialize
def init_weights(self):
self.param_count = 0
for module in self.modules():
if (isinstance(module, nn.Conv2d)
or isinstance(module, nn.Linear)
or isinstance(module, nn.Embedding)):
if self.init == 'ortho':
init.orthogonal_(module.weight)
elif self.init == 'N02':
init.normal_(module.weight, 0, 0.02)
elif self.init in ['glorot', 'xavier']:
init.xavier_uniform_(module.weight)
else:
print('Init style not recognized...')
self.param_count += sum([p.data.nelement() for p in module.parameters()])
print('Param count for G''s initialized parameters: %d' % self.param_count)
# Note on this forward function: we pass in a y vector which has
# already been passed through G.shared to enable easy class-wise
# interpolation later. If we passed in the one-hot and then ran it through
# G.shared in this forward function, it would be harder to handle.
def forward(self, z, y):
# If hierarchical, concatenate zs and ys
if self.hier:
zs = torch.split(z, self.z_chunk_size, 1)
z = zs[0]
ys = [torch.cat([y, item], 1) for item in zs[1:]]
else:
ys = [y] * len(self.blocks)
# First linear layer
h = self.linear(z)
# Reshape
h = h.view(h.size(0), -1, self.bottom_width, self.bottom_width)
# Loop over blocks
for index, blocklist in enumerate(self.blocks):
# Second inner loop in case block has multiple layers
for block in blocklist:
h = block(h, ys[index])
# Apply batchnorm-relu-conv-tanh at output
return torch.tanh(self.output_layer(h))
# Discriminator architecture, same paradigm as G's above
def D_arch(ch=64, attention='64',ksize='333333', dilation='111111'):
arch = {}
arch[256] = {'in_channels' : [3] + [ch*item for item in [1, 2, 4, 8, 8, 16]],
'out_channels' : [item * ch for item in [1, 2, 4, 8, 8, 16, 16]],
'downsample' : [True] * 6 + [False],
'resolution' : [128, 64, 32, 16, 8, 4, 4 ],
'attention' : {2**i: 2**i in [int(item) for item in attention.split('_')]
for i in range(2,8)}}
arch[128] = {'in_channels' : [3] + [ch*item for item in [1, 2, 4, 8, 16]],
'out_channels' : [item * ch for item in [1, 2, 4, 8, 16, 16]],
'downsample' : [True] * 5 + [False],
'resolution' : [64, 32, 16, 8, 4, 4],
'attention' : {2**i: 2**i in [int(item) for item in attention.split('_')]
for i in range(2,8)}}
arch[64] = {'in_channels' : [3] + [ch*item for item in [1, 2, 4, 8]],
'out_channels' : [item * ch for item in [1, 2, 4, 8, 16]],
'downsample' : [True] * 4 + [False],
'resolution' : [32, 16, 8, 4, 4],
'attention' : {2**i: 2**i in [int(item) for item in attention.split('_')]
for i in range(2,7)}}
arch[32] = {'in_channels' : [3] + [item * ch for item in [4, 4, 4]],
'out_channels' : [item * ch for item in [4, 4, 4, 4]],
'downsample' : [True, True, False, False],
'resolution' : [16, 16, 16, 16],
'attention' : {2**i: 2**i in [int(item) for item in attention.split('_')]
for i in range(2,6)}}
return arch
class Discriminator(nn.Module):
def __init__(self, D_ch=64, D_wide=True, resolution=128,
D_kernel_size=3, D_attn='64', n_classes=1000,
num_D_SVs=1, num_D_SV_itrs=1, D_activation=nn.ReLU(inplace=False),
D_lr=2e-4, D_B1=0.0, D_B2=0.999, adam_eps=1e-8,
SN_eps=1e-12, output_dim=1, D_mixed_precision=False, D_fp16=False,
D_init='ortho', skip_init=False, D_param='SN',
dict_decay=0.8, commitment=0.5, discrete_layer='2', dict_size=10,
**kwargs):
super(Discriminator, self).__init__()
# Width multiplier
self.ch = D_ch
# Use Wide D as in BigGAN and SA-GAN or skinny D as in SN-GAN?
self.D_wide = D_wide
# Resolution
self.resolution = resolution
# Kernel size
self.kernel_size = D_kernel_size
# Attention?
self.attention = D_attn
# Number of classes
self.n_classes = n_classes
# Activation
self.activation = D_activation
# Initialization style
self.init = D_init
# Parameterization style
self.D_param = D_param
# Epsilon for Spectral Norm?
self.SN_eps = SN_eps
# Fp16?
self.fp16 = D_fp16
# Architecture
self.arch = D_arch(self.ch, self.attention)[resolution]
# Which convs, batchnorms, and linear layers to use
# No option to turn off SN in D right now
if self.D_param == 'SN':
self.which_conv = functools.partial(layers.SNConv2d,
kernel_size=3, padding=1,
num_svs=num_D_SVs, num_itrs=num_D_SV_itrs,
eps=self.SN_eps)
self.which_linear = functools.partial(layers.SNLinear,
num_svs=num_D_SVs, num_itrs=num_D_SV_itrs,
eps=self.SN_eps)
self.which_embedding = functools.partial(layers.SNEmbedding,
num_svs=num_D_SVs, num_itrs=num_D_SV_itrs,
eps=self.SN_eps)
# Prepare model
# self.blocks is a doubly-nested list of modules, the outer loop intended
# to be over blocks at a given resolution (resblocks and/or self-attention)
self.blocks = []
self.quant_layer = [int(x) for x in discrete_layer]
for index in range(len(self.arch['out_channels'])):
self.blocks += [[layers.DBlock(in_channels=self.arch['in_channels'][index],
out_channels=self.arch['out_channels'][index],
which_conv=self.which_conv,
wide=self.D_wide,
activation=self.activation,
preactivation=(index > 0),
downsample=(nn.AvgPool2d(2) if self.arch['downsample'][index] else None))]]
if index in self.quant_layer:
self.blocks[-1] += [Quantize(self.arch['out_channels'][index], 2 ** dict_size,
commitment=commitment, decay=dict_decay, )]
# If attention on this block, attach it to the end
if self.arch['attention'][self.arch['resolution'][index]]:
print('Adding attention layer in D at resolution %d' % self.arch['resolution'][index])
self.blocks[-1] += [layers.Attention(self.arch['out_channels'][index],
self.which_conv)]
# Turn self.blocks into a ModuleList so that it's all properly registered.
self.blocks = nn.ModuleList([nn.ModuleList(block) for block in self.blocks])
# Linear output layer. The output dimension is typically 1, but may be
# larger if we're e.g. turning this into a VAE with an inference output
self.linear = self.which_linear(self.arch['out_channels'][-1], output_dim)
# Embedding for projection discrimination
self.embed = self.which_embedding(self.n_classes, self.arch['out_channels'][-1])
# Initialize weights
if not skip_init:
self.init_weights()
# Set up optimizer
self.lr, self.B1, self.B2, self.adam_eps = D_lr, D_B1, D_B2, adam_eps
if D_mixed_precision:
print('Using fp16 adam in D...')
import utils
self.optim = utils.Adam16(params=self.parameters(), lr=self.lr,
betas=(self.B1, self.B2), weight_decay=0, eps=self.adam_eps)
else:
self.optim = optim.Adam(params=self.parameters(), lr=self.lr,
betas=(self.B1, self.B2), weight_decay=0, eps=self.adam_eps)
# LR scheduling, left here for forward compatibility
# self.lr_sched = {'itr' : 0}# if self.progressive else {}
# self.j = 0
# Initialize
def init_weights(self):
self.param_count = 0
for module in self.modules():
if (isinstance(module, nn.Conv2d)
or isinstance(module, nn.Linear)
or isinstance(module, nn.Embedding)):
if self.init == 'ortho':
init.orthogonal_(module.weight)
elif self.init == 'N02':
init.normal_(module.weight, 0, 0.02)
elif self.init in ['glorot', 'xavier']:
init.xavier_uniform_(module.weight)
else:
print('Init style not recognized...')
self.param_count += sum([p.data.nelement() for p in module.parameters()])
print('Param count for D''s initialized parameters: %d' % self.param_count)
def forward(self, x, y=None):
# Stick x into h for cleaner for loops without flow control
h = x
quant_loss = 0
# Loop over blocks
for index, blocklist in enumerate(self.blocks):
if index in self.quant_layer:
h = blocklist[0](h)
# print(h.shape)
h_, diff, ppl = blocklist[1](h)
if len(blocklist) == 3:
h = blocklist[2](h)
quant_loss += diff
else:
for block in blocklist:
h = block(h)
# Apply global sum pooling as in SN-GAN
h = torch.sum(self.activation(h), [2, 3])
# Get initial class-unconditional output
out = self.linear(h)
## TODO: Uncomment for Class conditional
# Get projection of final featureset onto class vectors and add to evidence
out = out + torch.sum(self.embed(y) * h, 1, keepdim=True)
return out, quant_loss, ppl
# Parallelized G_D to minimize cross-gpu communication
# Without this, Generator outputs would get all-gathered and then rebroadcast.
class G_D(nn.Module):
def __init__(self, G, D):
super(G_D, self).__init__()
self.G = G
self.D = D
def forward(self, z, gy, x=None, dy=None, train_G=False, return_G_z=False,
split_D=False):
# If training G, enable grad tape
with torch.set_grad_enabled(train_G):
# Get Generator output given noise
G_z = self.G(z, self.G.shared(gy))
# Cast as necessary
if self.G.fp16 and not self.D.fp16:
G_z = G_z.float()
if self.D.fp16 and not self.G.fp16:
G_z = G_z.half()
# Split_D means to run D once with real data and once with fake,
# rather than concatenating along the batch dimension.
if split_D:
D_fake, quant_loss_fake, ppl = self.D(G_z, gy)
if x is not None:
D_real, quant_loss_real, ppl = self.D(x, dy)
return D_fake, D_real, quant_loss_fake, quant_loss_real
else:
if return_G_z:
return D_fake, G_z
else:
return D_fake, quant_loss_fake
# If real data is provided, concatenate it with the Generator's output
# along the batch dimension for improved efficiency.
else:
D_input = torch.cat([G_z, x], 0) if x is not None else G_z
D_class = torch.cat([gy, dy], 0) if dy is not None else gy
# Get Discriminator output
D_out, quant_loss, ppl = self.D(D_input, D_class)
# print(torch.split(D_out, [G_z.shape[0], x.shape[0]]))
if x is not None:
D_real, D_fake = torch.split(D_out, [G_z.shape[0], x.shape[0]])
quant_loss_real, quant_loss_fake = torch.split(quant_loss, (G_z.shape[0],
x.shape[0]), dim=0)
return D_real, D_fake, quant_loss_real, quant_loss_fake, ppl.view(-1, 1) # D_fake,
# D_real
else:
if return_G_z:
return D_out, G_z
else:
return D_out, quant_loss
================================================
FILE: FQ-BigGAN/BigGANdeep.py
================================================
import numpy as np
import math
import functools
import torch
import torch.nn as nn
from torch.nn import init
import torch.optim as optim
import torch.nn.functional as F
from torch.nn import Parameter as P
import layers
from sync_batchnorm import SynchronizedBatchNorm2d as SyncBatchNorm2d
# BigGAN-deep: uses a different resblock and pattern
# Architectures for G
# Attention is passed in in the format '32_64' to mean applying an attention
# block at both resolution 32x32 and 64x64. Just '64' will apply at 64x64.
# Channel ratio is the ratio of
class GBlock(nn.Module):
def __init__(self, in_channels, out_channels,
which_conv=nn.Conv2d, which_bn=layers.bn, activation=None,
upsample=None, channel_ratio=4):
super(GBlock, self).__init__()
self.in_channels, self.out_channels = in_channels, out_channels
self.hidden_channels = self.in_channels // channel_ratio
self.which_conv, self.which_bn = which_conv, which_bn
self.activation = activation
# Conv layers
self.conv1 = self.which_conv(self.in_channels, self.hidden_channels,
kernel_size=1, padding=0)
self.conv2 = self.which_conv(self.hidden_channels, self.hidden_channels)
self.conv3 = self.which_conv(self.hidden_channels, self.hidden_channels)
self.conv4 = self.which_conv(self.hidden_channels, self.out_channels,
kernel_size=1, padding=0)
# Batchnorm layers
self.bn1 = self.which_bn(self.in_channels)
self.bn2 = self.which_bn(self.hidden_channels)
self.bn3 = self.which_bn(self.hidden_channels)
self.bn4 = self.which_bn(self.hidden_channels)
# upsample layers
self.upsample = upsample
def forward(self, x, y):
# Project down to channel ratio
h = self.conv1(self.activation(self.bn1(x, y)))
# Apply next BN-ReLU
h = self.activation(self.bn2(h, y))
# Drop channels in x if necessary
if self.in_channels != self.out_channels:
x = x[:, :self.out_channels]
# Upsample both h and x at this point
if self.upsample:
h = self.upsample(h)
x = self.upsample(x)
# 3x3 convs
h = self.conv2(h)
h = self.conv3(self.activation(self.bn3(h, y)))
# Final 1x1 conv
h = self.conv4(self.activation(self.bn4(h, y)))
return h + x
def G_arch(ch=64, attention='64', ksize='333333', dilation='111111'):
arch = {}
arch[256] = {'in_channels' : [ch * item for item in [16, 16, 8, 8, 4, 2]],
'out_channels' : [ch * item for item in [16, 8, 8, 4, 2, 1]],
'upsample' : [True] * 6,
'resolution' : [8, 16, 32, 64, 128, 256],
'attention' : {2**i: (2**i in [int(item) for item in attention.split('_')])
for i in range(3,9)}}
arch[128] = {'in_channels' : [ch * item for item in [16, 16, 8, 4, 2]],
'out_channels' : [ch * item for item in [16, 8, 4, 2, 1]],
'upsample' : [True] * 5,
'resolution' : [8, 16, 32, 64, 128],
'attention' : {2**i: (2**i in [int(item) for item in attention.split('_')])
for i in range(3,8)}}
arch[64] = {'in_channels' : [ch * item for item in [16, 16, 8, 4]],
'out_channels' : [ch * item for item in [16, 8, 4, 2]],
'upsample' : [True] * 4,
'resolution' : [8, 16, 32, 64],
'attention' : {2**i: (2**i in [int(item) for item in attention.split('_')])
for i in range(3,7)}}
arch[32] = {'in_channels' : [ch * item for item in [4, 4, 4]],
'out_channels' : [ch * item for item in [4, 4, 4]],
'upsample' : [True] * 3,
'resolution' : [8, 16, 32],
'attention' : {2**i: (2**i in [int(item) for item in attention.split('_')])
for i in range(3,6)}}
return arch
class Generator(nn.Module):
def __init__(self, G_ch=64, G_depth=2, dim_z=128, bottom_width=4, resolution=128,
G_kernel_size=3, G_attn='64', n_classes=1000,
num_G_SVs=1, num_G_SV_itrs=1,
G_shared=True, shared_dim=0, hier=False,
cross_replica=False, mybn=False,
G_activation=nn.ReLU(inplace=False),
G_lr=5e-5, G_B1=0.0, G_B2=0.999, adam_eps=1e-8,
BN_eps=1e-5, SN_eps=1e-12, G_mixed_precision=False, G_fp16=False,
G_init='ortho', skip_init=False, no_optim=False,
G_param='SN', norm_style='bn',
**kwargs):
super(Generator, self).__init__()
# Channel width mulitplier
self.ch = G_ch
# Number of resblocks per stage
self.G_depth = G_depth
# Dimensionality of the latent space
self.dim_z = dim_z
# The initial spatial dimensions
self.bottom_width = bottom_width
# Resolution of the output
self.resolution = resolution
# Kernel size?
self.kernel_size = G_kernel_size
# Attention?
self.attention = G_attn
# number of classes, for use in categorical conditional generation
self.n_classes = n_classes
# Use shared embeddings?
self.G_shared = G_shared
# Dimensionality of the shared embedding? Unused if not using G_shared
self.shared_dim = shared_dim if shared_dim > 0 else dim_z
# Hierarchical latent space?
self.hier = hier
# Cross replica batchnorm?
self.cross_replica = cross_replica
# Use my batchnorm?
self.mybn = mybn
# nonlinearity for residual blocks
self.activation = G_activation
# Initialization style
self.init = G_init
# Parameterization style
self.G_param = G_param
# Normalization style
self.norm_style = norm_style
# Epsilon for BatchNorm?
self.BN_eps = BN_eps
# Epsilon for Spectral Norm?
self.SN_eps = SN_eps
# fp16?
self.fp16 = G_fp16
# Architecture dict
self.arch = G_arch(self.ch, self.attention)[resolution]
# Which convs, batchnorms, and linear layers to use
if self.G_param == 'SN':
self.which_conv = functools.partial(layers.SNConv2d,
kernel_size=3, padding=1,
num_svs=num_G_SVs, num_itrs=num_G_SV_itrs,
eps=self.SN_eps)
self.which_linear = functools.partial(layers.SNLinear,
num_svs=num_G_SVs, num_itrs=num_G_SV_itrs,
eps=self.SN_eps)
else:
self.which_conv = functools.partial(nn.Conv2d, kernel_size=3, padding=1)
self.which_linear = nn.Linear
# We use a non-spectral-normed embedding here regardless;
# For some reason applying SN to G's embedding seems to randomly cripple G
self.which_embedding = nn.Embedding
bn_linear = (functools.partial(self.which_linear, bias=False) if self.G_shared
else self.which_embedding)
self.which_bn = functools.partial(layers.ccbn,
which_linear=bn_linear,
cross_replica=self.cross_replica,
mybn=self.mybn,
input_size=(self.shared_dim + self.dim_z if self.G_shared
else self.n_classes),
norm_style=self.norm_style,
eps=self.BN_eps)
# Prepare model
# If not using shared embeddings, self.shared is just a passthrough
self.shared = (self.which_embedding(n_classes, self.shared_dim) if G_shared
else layers.identity())
# First linear layer
self.linear = self.which_linear(self.dim_z + self.shared_dim, self.arch['in_channels'][0] * (self.bottom_width **2))
# self.blocks is a doubly-nested list of modules, the outer loop intended
# to be over blocks at a given resolution (resblocks and/or self-attention)
# while the inner loop is over a given block
self.blocks = []
for index in range(len(self.arch['out_channels'])):
self.blocks += [[GBlock(in_channels=self.arch['in_channels'][index],
out_channels=self.arch['in_channels'][index] if g_index==0 else self.arch['out_channels'][index],
which_conv=self.which_conv,
which_bn=self.which_bn,
activation=self.activation,
upsample=(functools.partial(F.interpolate, scale_factor=2)
if self.arch['upsample'][index] and g_index == (self.G_depth-1) else None))]
for g_index in range(self.G_depth)]
# If attention on this block, attach it to the end
if self.arch['attention'][self.arch['resolution'][index]]:
print('Adding attention layer in G at resolution %d' % self.arch['resolution'][index])
self.blocks[-1] += [layers.Attention(self.arch['out_channels'][index], self.which_conv)]
# Turn self.blocks into a ModuleList so that it's all properly registered.
self.blocks = nn.ModuleList([nn.ModuleList(block) for block in self.blocks])
# output layer: batchnorm-relu-conv.
# Consider using a non-spectral conv here
self.output_layer = nn.Sequential(layers.bn(self.arch['out_channels'][-1],
cross_replica=self.cross_replica,
mybn=self.mybn),
self.activation,
self.which_conv(self.arch['out_channels'][-1], 3))
# Initialize weights. Optionally skip init for testing.
if not skip_init:
self.init_weights()
# Set up optimizer
# If this is an EMA copy, no need for an optim, so just return now
if no_optim:
return
self.lr, self.B1, self.B2, self.adam_eps = G_lr, G_B1, G_B2, adam_eps
if G_mixed_precision:
print('Using fp16 adam in G...')
import utils
self.optim = utils.Adam16(params=self.parameters(), lr=self.lr,
betas=(self.B1, self.B2), weight_decay=0,
eps=self.adam_eps)
else:
self.optim = optim.Adam(params=self.parameters(), lr=self.lr,
betas=(self.B1, self.B2), weight_decay=0,
eps=self.adam_eps)
# LR scheduling, left here for forward compatibility
# self.lr_sched = {'itr' : 0}# if self.progressive else {}
# self.j = 0
# Initialize
def init_weights(self):
self.param_count = 0
for module in self.modules():
if (isinstance(module, nn.Conv2d)
or isinstance(module, nn.Linear)
or isinstance(module, nn.Embedding)):
if self.init == 'ortho':
init.orthogonal_(module.weight)
elif self.init == 'N02':
init.normal_(module.weight, 0, 0.02)
elif self.init in ['glorot', 'xavier']:
init.xavier_uniform_(module.weight)
else:
print('Init style not recognized...')
self.param_count += sum([p.data.nelement() for p in module.parameters()])
print('Param count for G''s initialized parameters: %d' % self.param_count)
# Note on this forward function: we pass in a y vector which has
# already been passed through G.shared to enable easy class-wise
# interpolation later. If we passed in the one-hot and then ran it through
# G.shared in this forward function, it would be harder to handle.
# NOTE: The z vs y dichotomy here is for compatibility with not-y
def forward(self, z, y):
# If hierarchical, concatenate zs and ys
if self.hier:
z = torch.cat([y, z], 1)
y = z
# First linear layer
h = self.linear(z)
# Reshape
h = h.view(h.size(0), -1, self.bottom_width, self.bottom_width)
# Loop over blocks
for index, blocklist in enumerate(self.blocks):
# Second inner loop in case block has multiple layers
for block in blocklist:
h = block(h, y)
# Apply batchnorm-relu-conv-tanh at output
return torch.tanh(self.output_layer(h))
class DBlock(nn.Module):
def __init__(self, in_channels, out_channels, which_conv=layers.SNConv2d, wide=True,
preactivation=True, activation=None, downsample=None,
channel_ratio=4):
super(DBlock, self).__init__()
self.in_channels, self.out_channels = in_channels, out_channels
# If using wide D (as in SA-GAN and BigGAN), change the channel pattern
self.hidden_channels = self.out_channels // channel_ratio
self.which_conv = which_conv
self.preactivation = preactivation
self.activation = activation
self.downsample = downsample
# Conv layers
self.conv1 = self.which_conv(self.in_channels, self.hidden_channels,
kernel_size=1, padding=0)
self.conv2 = self.which_conv(self.hidden_channels, self.hidden_channels)
self.conv3 = self.which_conv(self.hidden_channels, self.hidden_channels)
self.conv4 = self.which_conv(self.hidden_channels, self.out_channels,
kernel_size=1, padding=0)
self.learnable_sc = True if (in_channels != out_channels) else False
if self.learnable_sc:
self.conv_sc = self.which_conv(in_channels, out_channels - in_channels,
kernel_size=1, padding=0)
def shortcut(self, x):
if self.downsample:
x = self.downsample(x)
if self.learnable_sc:
x = torch.cat([x, self.conv_sc(x)], 1)
return x
def forward(self, x):
# 1x1 bottleneck conv
h = self.conv1(F.relu(x))
# 3x3 convs
h = self.conv2(self.activation(h))
h = self.conv3(self.activation(h))
# relu before downsample
h = self.activation(h)
# downsample
if self.downsample:
h = self.downsample(h)
# final 1x1 conv
h = self.conv4(h)
return h + self.shortcut(x)
# Discriminator architecture, same paradigm as G's above
def D_arch(ch=64, attention='64',ksize='333333', dilation='111111'):
arch = {}
arch[256] = {'in_channels' : [item * ch for item in [1, 2, 4, 8, 8, 16]],
'out_channels' : [item * ch for item in [2, 4, 8, 8, 16, 16]],
'downsample' : [True] * 6 + [False],
'resolution' : [128, 64, 32, 16, 8, 4, 4 ],
'attention' : {2**i: 2**i in [int(item) for item in attention.split('_')]
for i in range(2,8)}}
arch[128] = {'in_channels' : [item * ch for item in [1, 2, 4, 8, 16]],
'out_channels' : [item * ch for item in [2, 4, 8, 16, 16]],
'downsample' : [True] * 5 + [False],
'resolution' : [64, 32, 16, 8, 4, 4],
'attention' : {2**i: 2**i in [int(item) for item in attention.split('_')]
for i in range(2,8)}}
arch[64] = {'in_channels' : [item * ch for item in [1, 2, 4, 8]],
'out_channels' : [item * ch for item in [2, 4, 8, 16]],
'downsample' : [True] * 4 + [False],
'resolution' : [32, 16, 8, 4, 4],
'attention' : {2**i: 2**i in [int(item) for item in attention.split('_')]
for i in range(2,7)}}
arch[32] = {'in_channels' : [item * ch for item in [4, 4, 4]],
'out_channels' : [item * ch for item in [4, 4, 4]],
'downsample' : [True, True, False, False],
'resolution' : [16, 16, 16, 16],
'attention' : {2**i: 2**i in [int(item) for item in attention.split('_')]
for i in range(2,6)}}
return arch
class Discriminator(nn.Module):
def __init__(self, D_ch=64, D_wide=True, D_depth=2, resolution=128,
D_kernel_size=3, D_attn='64', n_classes=1000,
num_D_SVs=1, num_D_SV_itrs=1, D_activation=nn.ReLU(inplace=False),
D_lr=2e-4, D_B1=0.0, D_B2=0.999, adam_eps=1e-8,
SN_eps=1e-12, output_dim=1, D_mixed_precision=False, D_fp16=False,
D_init='ortho', skip_init=False, D_param='SN', **kwargs):
super(Discriminator, self).__init__()
# Width multiplier
self.ch = D_ch
# Use Wide D as in BigGAN and SA-GAN or skinny D as in SN-GAN?
self.D_wide = D_wide
# How many resblocks per stage?
self.D_depth = D_depth
# Resolution
self.resolution = resolution
# Kernel size
self.kernel_size = D_kernel_size
# Attention?
self.attention = D_attn
# Number of classes
self.n_classes = n_classes
# Activation
self.activation = D_activation
# Initialization style
self.init = D_init
# Parameterization style
self.D_param = D_param
# Epsilon for Spectral Norm?
self.SN_eps = SN_eps
# Fp16?
self.fp16 = D_fp16
# Architecture
self.arch = D_arch(self.ch, self.attention)[resolution]
# Which convs, batchnorms, and linear layers to use
# No option to turn off SN in D right now
if self.D_param == 'SN':
self.which_conv = functools.partial(layers.SNConv2d,
kernel_size=3, padding=1,
num_svs=num_D_SVs, num_itrs=num_D_SV_itrs,
eps=self.SN_eps)
self.which_linear = functools.partial(layers.SNLinear,
num_svs=num_D_SVs, num_itrs=num_D_SV_itrs,
eps=self.SN_eps)
self.which_embedding = functools.partial(layers.SNEmbedding,
num_svs=num_D_SVs, num_itrs=num_D_SV_itrs,
eps=self.SN_eps)
# Prepare model
# Stem convolution
self.input_conv = self.which_conv(3, self.arch['in_channels'][0])
# self.blocks is a doubly-nested list of modules, the outer loop intended
# to be over blocks at a given resolution (resblocks and/or self-attention)
self.blocks = []
for index in range(len(self.arch['out_channels'])):
self.blocks += [[DBlock(in_channels=self.arch['in_channels'][index] if d_index==0 else self.arch['out_channels'][index],
out_channels=self.arch['out_channels'][index],
which_conv=self.which_conv,
wide=self.D_wide,
activation=self.activation,
preactivation=True,
downsample=(nn.AvgPool2d(2) if self.arch['downsample'][index] and d_index==0 else None))
for d_index in range(self.D_depth)]]
# If attention on this block, attach it to the end
if self.arch['attention'][self.arch['resolution'][index]]:
print('Adding attention layer in D at resolution %d' % self.arch['resolution'][index])
self.blocks[-1] += [layers.Attention(self.arch['out_channels'][index],
self.which_conv)]
# Turn self.blocks into a ModuleList so that it's all properly registered.
self.blocks = nn.ModuleList([nn.ModuleList(block) for block in self.blocks])
# Linear output layer. The output dimension is typically 1, but may be
# larger if we're e.g. turning this into a VAE with an inference output
self.linear = self.which_linear(self.arch['out_channels'][-1], output_dim)
# Embedding for projection discrimination
self.embed = self.which_embedding(self.n_classes, self.arch['out_channels'][-1])
# Initialize weights
if not skip_init:
self.init_weights()
# Set up optimizer
self.lr, self.B1, self.B2, self.adam_eps = D_lr, D_B1, D_B2, adam_eps
if D_mixed_precision:
print('Using fp16 adam in D...')
import utils
self.optim = utils.Adam16(params=self.parameters(), lr=self.lr,
betas=(self.B1, self.B2), weight_decay=0, eps=self.adam_eps)
else:
self.optim = optim.Adam(params=self.parameters(), lr=self.lr,
betas=(self.B1, self.B2), weight_decay=0, eps=self.adam_eps)
# LR scheduling, left here for forward compatibility
# self.lr_sched = {'itr' : 0}# if self.progressive else {}
# self.j = 0
# Initialize
def init_weights(self):
self.param_count = 0
for module in self.modules():
if (isinstance(module, nn.Conv2d)
or isinstance(module, nn.Linear)
or isinstance(module, nn.Embedding)):
if self.init == 'ortho':
init.orthogonal_(module.weight)
elif self.init == 'N02':
init.normal_(module.weight, 0, 0.02)
elif self.init in ['glorot', 'xavier']:
init.xavier_uniform_(module.weight)
else:
print('Init style not recognized...')
self.param_count += sum([p.data.nelement() for p in module.parameters()])
print('Param count for D''s initialized parameters: %d' % self.param_count)
def forward(self, x, y=None):
# Run input conv
h = self.input_conv(x)
# Loop over blocks
for index, blocklist in enumerate(self.blocks):
for block in blocklist:
h = block(h)
# Apply global sum pooling as in SN-GAN
h = torch.sum(self.activation(h), [2, 3])
# Get initial class-unconditional output
out = self.linear(h)
# Get projection of final featureset onto class vectors and add to evidence
out = out + torch.sum(self.embed(y) * h, 1, keepdim=True)
return out
# Parallelized G_D to minimize cross-gpu communication
# Without this, Generator outputs would get all-gathered and then rebroadcast.
class G_D(nn.Module):
def __init__(self, G, D):
super(G_D, self).__init__()
self.G = G
self.D = D
def forward(self, z, gy, x=None, dy=None, train_G=False, return_G_z=False,
split_D=False):
# If training G, enable grad tape
with torch.set_grad_enabled(train_G):
# Get Generator output given noise
G_z = self.G(z, self.G.shared(gy))
# Cast as necessary
if self.G.fp16 and not self.D.fp16:
G_z = G_z.float()
if self.D.fp16 and not self.G.fp16:
G_z = G_z.half()
# Split_D means to run D once with real data and once with fake,
# rather than concatenating along the batch dimension.
if split_D:
D_fake = self.D(G_z, gy)
if x is not None:
D_real = self.D(x, dy)
return D_fake, D_real
else:
if return_G_z:
return D_fake, G_z
else:
return D_fake
# If real data is provided, concatenate it with the Generator's output
# along the batch dimension for improved efficiency.
else:
D_input = torch.cat([G_z, x], 0) if x is not None else G_z
D_class = torch.cat([gy, dy], 0) if dy is not None else gy
# Get Discriminator output
D_out = self.D(D_input, D_class)
if x is not None:
return torch.split(D_out, [G_z.shape[0], x.shape[0]]) # D_fake, D_real
else:
if return_G_z:
return D_out, G_z
else:
return D_out
================================================
FILE: FQ-BigGAN/LICENSE
================================================
MIT License
Copyright (c) 2019 Andy Brock
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: FQ-BigGAN/TFHub/README.md
================================================
# BigGAN-PyTorch TFHub converter
This dir contains scripts for taking the [pre-trained generator weights from TFHub](https://tfhub.dev/s?q=biggan) and porting them to BigGAN-Pytorch.
In addition to the base libraries for BigGAN-PyTorch, to run this code you will need:
TensorFlow
TFHub
parse
Note that this code is only presently set up to run the ported models without truncation--you'll need to accumulate standing stats at each truncation level yourself if you wish to employ it.
To port the 128x128 model from tfhub, produce a pretrained weights .pth file, and generate samples using all your GPUs, run
`python converter.py -r 128 --generate_samples --parallel`
================================================
FILE: FQ-BigGAN/TFHub/biggan_v1.py
================================================
# BigGAN V1:
# This is now deprecated code used for porting the TFHub modules to pytorch,
# included here for reference only.
import numpy as np
import torch
from scipy.stats import truncnorm
from torch import nn
from torch.nn import Parameter
from torch.nn import functional as F
def l2normalize(v, eps=1e-4):
return v / (v.norm() + eps)
def truncated_z_sample(batch_size, z_dim, truncation=0.5, seed=None):
state = None if seed is None else np.random.RandomState(seed)
values = truncnorm.rvs(-2, 2, size=(batch_size, z_dim), random_state=state)
return truncation * values
def denorm(x):
out = (x + 1) / 2
return out.clamp_(0, 1)
class SpectralNorm(nn.Module):
def __init__(self, module, name='weight', power_iterations=1):
super(SpectralNorm, self).__init__()
self.module = module
self.name = name
self.power_iterations = power_iterations
if not self._made_params():
self._make_params()
def _update_u_v(self):
u = getattr(self.module, self.name + "_u")
v = getattr(self.module, self.name + "_v")
w = getattr(self.module, self.name + "_bar")
height = w.data.shape[0]
_w = w.view(height, -1)
for _ in range(self.power_iterations):
v = l2normalize(torch.matmul(_w.t(), u))
u = l2normalize(torch.matmul(_w, v))
sigma = u.dot((_w).mv(v))
setattr(self.module, self.name, w / sigma.expand_as(w))
def _made_params(self):
try:
getattr(self.module, self.name + "_u")
getattr(self.module, self.name + "_v")
getattr(self.module, self.name + "_bar")
return True
except AttributeError:
return False
def _make_params(self):
w = getattr(self.module, self.name)
height = w.data.shape[0]
width = w.view(height, -1).data.shape[1]
u = Parameter(w.data.new(height).normal_(0, 1), requires_grad=False)
v = Parameter(w.data.new(height).normal_(0, 1), requires_grad=False)
u.data = l2normalize(u.data)
v.data = l2normalize(v.data)
w_bar = Parameter(w.data)
del self.module._parameters[self.name]
self.module.register_parameter(self.name + "_u", u)
self.module.register_parameter(self.name + "_v", v)
self.module.register_parameter(self.name + "_bar", w_bar)
def forward(self, *args):
self._update_u_v()
return self.module.forward(*args)
class SelfAttention(nn.Module):
""" Self Attention Layer"""
def __init__(self, in_dim, activation=F.relu):
super().__init__()
self.chanel_in = in_dim
self.activation = activation
self.theta = SpectralNorm(nn.Conv2d(in_channels=in_dim, out_channels=in_dim // 8, kernel_size=1, bias=False))
self.phi = SpectralNorm(nn.Conv2d(in_channels=in_dim, out_channels=in_dim // 8, kernel_size=1, bias=False))
self.pool = nn.MaxPool2d(2, 2)
self.g = SpectralNorm(nn.Conv2d(in_channels=in_dim, out_channels=in_dim // 2, kernel_size=1, bias=False))
self.o_conv = SpectralNorm(nn.Conv2d(in_channels=in_dim // 2, out_channels=in_dim, kernel_size=1, bias=False))
self.gamma = nn.Parameter(torch.zeros(1))
self.softmax = nn.Softmax(dim=-1)
def forward(self, x):
m_batchsize, C, width, height = x.size()
N = height * width
theta = self.theta(x)
phi = self.phi(x)
phi = self.pool(phi)
phi = phi.view(m_batchsize, -1, N // 4)
theta = theta.view(m_batchsize, -1, N)
theta = theta.permute(0, 2, 1)
attention = self.softmax(torch.bmm(theta, phi))
g = self.pool(self.g(x)).view(m_batchsize, -1, N // 4)
attn_g = torch.bmm(g, attention.permute(0, 2, 1)).view(m_batchsize, -1, width, height)
out = self.o_conv(attn_g)
return self.gamma * out + x
class ConditionalBatchNorm2d(nn.Module):
def __init__(self, num_features, num_classes, eps=1e-4, momentum=0.1):
super().__init__()
self.num_features = num_features
self.bn = nn.BatchNorm2d(num_features, affine=False, eps=eps, momentum=momentum)
self.gamma_embed = SpectralNorm(nn.Linear(num_classes, num_features, bias=False))
self.beta_embed = SpectralNorm(nn.Linear(num_classes, num_features, bias=False))
def forward(self, x, y):
out = self.bn(x)
gamma = self.gamma_embed(y) + 1
beta = self.beta_embed(y)
out = gamma.view(-1, self.num_features, 1, 1) * out + beta.view(-1, self.num_features, 1, 1)
return out
class GBlock(nn.Module):
def __init__(
self,
in_channel,
out_channel,
kernel_size=[3, 3],
padding=1,
stride=1,
n_class=None,
bn=True,
activation=F.relu,
upsample=True,
downsample=False,
z_dim=148,
):
super().__init__()
self.conv0 = SpectralNorm(
nn.Conv2d(in_channel, out_channel, kernel_size, stride, padding, bias=True if bn else True)
)
self.conv1 = SpectralNorm(
nn.Conv2d(out_channel, out_channel, kernel_size, stride, padding, bias=True if bn else True)
)
self.skip_proj = False
if in_channel != out_channel or upsample or downsample:
self.conv_sc = SpectralNorm(nn.Conv2d(in_channel, out_channel, 1, 1, 0))
self.skip_proj = True
self.upsample = upsample
self.downsample = downsample
self.activation = activation
self.bn = bn
if bn:
self.HyperBN = ConditionalBatchNorm2d(in_channel, z_dim)
self.HyperBN_1 = ConditionalBatchNorm2d(out_channel, z_dim)
def forward(self, input, condition=None):
out = input
if self.bn:
out = self.HyperBN(out, condition)
out = self.activation(out)
if self.upsample:
out = F.interpolate(out, scale_factor=2)
out = self.conv0(out)
if self.bn:
out = self.HyperBN_1(out, condition)
out = self.activation(out)
out = self.conv1(out)
if self.downsample:
out = F.avg_pool2d(out, 2)
if self.skip_proj:
skip = input
if self.upsample:
skip = F.interpolate(skip, scale_factor=2)
skip = self.conv_sc(skip)
if self.downsample:
skip = F.avg_pool2d(skip, 2)
else:
skip = input
return out + skip
class Generator128(nn.Module):
def __init__(self, code_dim=120, n_class=1000, chn=96, debug=False):
super().__init__()
self.linear = nn.Linear(n_class, 128, bias=False)
if debug:
chn = 8
self.first_view = 16 * chn
self.G_linear = SpectralNorm(nn.Linear(20, 4 * 4 * 16 * chn))
z_dim = code_dim + 28
self.GBlock = nn.ModuleList([
GBlock(16 * chn, 16 * chn, n_class=n_class, z_dim=z_dim),
GBlock(16 * chn, 8 * chn, n_class=n_class, z_dim=z_dim),
GBlock(8 * chn, 4 * chn, n_class=n_class, z_dim=z_dim),
GBlock(4 * chn, 2 * chn, n_class=n_class, z_dim=z_dim),
GBlock(2 * chn, 1 * chn, n_class=n_class, z_dim=z_dim),
])
self.sa_id = 4
self.num_split = len(self.GBlock) + 1
self.attention = SelfAttention(2 * chn)
self.ScaledCrossReplicaBN = nn.BatchNorm2d(1 * chn, eps=1e-4)
self.colorize = SpectralNorm(nn.Conv2d(1 * chn, 3, [3, 3], padding=1))
def forward(self, input, class_id):
codes = torch.chunk(input, self.num_split, 1)
class_emb = self.linear(class_id) # 128
out = self.G_linear(codes[0])
out = out.view(-1, 4, 4, self.first_view).permute(0, 3, 1, 2)
for i, (code, GBlock) in enumerate(zip(codes[1:], self.GBlock)):
if i == self.sa_id:
out = self.attention(out)
condition = torch.cat([code, class_emb], 1)
out = GBlock(out, condition)
out = self.ScaledCrossReplicaBN(out)
out = F.relu(out)
out = self.colorize(out)
return torch.tanh(out)
class Generator256(nn.Module):
def __init__(self, code_dim=140, n_class=1000, chn=96, debug=False):
super().__init__()
self.linear = nn.Linear(n_class, 128, bias=False)
if debug:
chn = 8
self.first_view = 16 * chn
self.G_linear = SpectralNorm(nn.Linear(20, 4 * 4 * 16 * chn))
self.GBlock = nn.ModuleList([
GBlock(16 * chn, 16 * chn, n_class=n_class),
GBlock(16 * chn, 8 * chn, n_class=n_class),
GBlock(8 * chn, 8 * chn, n_class=n_class),
GBlock(8 * chn, 4 * chn, n_class=n_class),
GBlock(4 * chn, 2 * chn, n_class=n_class),
GBlock(2 * chn, 1 * chn, n_class=n_class),
])
self.sa_id = 5
self.num_split = len(self.GBlock) + 1
self.attention = SelfAttention(2 * chn)
self.ScaledCrossReplicaBN = nn.BatchNorm2d(1 * chn, eps=1e-4)
self.colorize = SpectralNorm(nn.Conv2d(1 * chn, 3, [3, 3], padding=1))
def forward(self, input, class_id):
codes = torch.chunk(input, self.num_split, 1)
class_emb = self.linear(class_id) # 128
out = self.G_linear(codes[0])
out = out.view(-1, 4, 4, self.first_view).permute(0, 3, 1, 2)
for i, (code, GBlock) in enumerate(zip(codes[1:], self.GBlock)):
if i == self.sa_id:
out = self.attention(out)
condition = torch.cat([code, class_emb], 1)
out = GBlock(out, condition)
out = self.ScaledCrossReplicaBN(out)
out = F.relu(out)
out = self.colorize(out)
return torch.tanh(out)
class Generator512(nn.Module):
def __init__(self, code_dim=128, n_class=1000, chn=96, debug=False):
super().__init__()
self.linear = nn.Linear(n_class, 128, bias=False)
if debug:
chn = 8
self.first_view = 16 * chn
self.G_linear = SpectralNorm(nn.Linear(16, 4 * 4 * 16 * chn))
z_dim = code_dim + 16
self.GBlock = nn.ModuleList([
GBlock(16 * chn, 16 * chn, n_class=n_class, z_dim=z_dim),
GBlock(16 * chn, 8 * chn, n_class=n_class, z_dim=z_dim),
GBlock(8 * chn, 8 * chn, n_class=n_class, z_dim=z_dim),
GBlock(8 * chn, 4 * chn, n_class=n_class, z_dim=z_dim),
GBlock(4 * chn, 2 * chn, n_class=n_class, z_dim=z_dim),
GBlock(2 * chn, 1 * chn, n_class=n_class, z_dim=z_dim),
GBlock(1 * chn, 1 * chn, n_class=n_class, z_dim=z_dim),
])
self.sa_id = 4
self.num_split = len(self.GBlock) + 1
self.attention = SelfAttention(4 * chn)
self.ScaledCrossReplicaBN = nn.BatchNorm2d(1 * chn)
self.colorize = SpectralNorm(nn.Conv2d(1 * chn, 3, [3, 3], padding=1))
def forward(self, input, class_id):
codes = torch.chunk(input, self.num_split, 1)
class_emb = self.linear(class_id) # 128
out = self.G_linear(codes[0])
out = out.view(-1, 4, 4, self.first_view).permute(0, 3, 1, 2)
for i, (code, GBlock) in enumerate(zip(codes[1:], self.GBlock)):
if i == self.sa_id:
out = self.attention(out)
condition = torch.cat([code, class_emb], 1)
out = GBlock(out, condition)
out = self.ScaledCrossReplicaBN(out)
out = F.relu(out)
out = self.colorize(out)
return torch.tanh(out)
class Discriminator(nn.Module):
def __init__(self, n_class=1000, chn=96, debug=False):
super().__init__()
def conv(in_channel, out_channel, downsample=True):
return GBlock(in_channel, out_channel, bn=False, upsample=False, downsample=downsample)
if debug:
chn = 8
self.debug = debug
self.pre_conv = nn.Sequential(
SpectralNorm(nn.Conv2d(3, 1 * chn, 3, padding=1)),
nn.ReLU(),
SpectralNorm(nn.Conv2d(1 * chn, 1 * chn, 3, padding=1)),
nn.AvgPool2d(2),
)
self.pre_skip = SpectralNorm(nn.Conv2d(3, 1 * chn, 1))
self.conv = nn.Sequential(
conv(1 * chn, 1 * chn, downsample=True),
conv(1 * chn, 2 * chn, downsample=True),
SelfAttention(2 * chn),
conv(2 * chn, 2 * chn, downsample=True),
conv(2 * chn, 4 * chn, downsample=True),
conv(4 * chn, 8 * chn, downsample=True),
conv(8 * chn, 8 * chn, downsample=True),
conv(8 * chn, 16 * chn, downsample=True),
conv(16 * chn, 16 * chn, downsample=False),
)
self.linear = SpectralNorm(nn.Linear(16 * chn, 1))
self.embed = nn.Embedding(n_class, 16 * chn)
self.embed.weight.data.uniform_(-0.1, 0.1)
self.embed = SpectralNorm(self.embed)
def forward(self, input, class_id):
out = self.pre_conv(input)
out += self.pre_skip(F.avg_pool2d(input, 2))
out = self.conv(out)
out = F.relu(out)
out = out.view(out.size(0), out.size(1), -1)
out = out.sum(2)
out_linear = self.linear(out).squeeze(1)
embed = self.embed(class_id)
prod = (out * embed).sum(1)
return out_linear + prod
================================================
FILE: FQ-BigGAN/TFHub/converter.py
================================================
"""Utilities for converting TFHub BigGAN generator weights to PyTorch.
Recommended usage:
To convert all BigGAN variants and generate test samples, use:
```bash
CUDA_VISIBLE_DEVICES=0 python converter.py --generate_samples
```
See `parse_args` for additional options.
"""
import argparse
import os
import sys
import h5py
import torch
import torch.nn as nn
from torchvision.utils import save_image
import tensorflow as tf
import tensorflow_hub as hub
import parse
# import reference biggan from this folder
import biggan_v1 as biggan_for_conversion
# Import model from main folder
sys.path.append('..')
import BigGAN
DEVICE = 'cuda'
HDF5_TMPL = 'biggan-{}.h5'
PTH_TMPL = 'biggan-{}.pth'
MODULE_PATH_TMPL = 'https://tfhub.dev/deepmind/biggan-{}/2'
Z_DIMS = {
128: 120,
256: 140,
512: 128}
RESOLUTIONS = list(Z_DIMS)
def dump_tfhub_to_hdf5(module_path, hdf5_path, redownload=False):
"""Loads TFHub weights and saves them to intermediate HDF5 file.
Args:
module_path ([Path-like]): Path to TFHub module.
hdf5_path ([Path-like]): Path to output HDF5 file.
Returns:
[h5py.File]: Loaded hdf5 file containing module weights.
"""
if os.path.exists(hdf5_path) and (not redownload):
print('Loading BigGAN hdf5 file from:', hdf5_path)
return h5py.File(hdf5_path, 'r')
print('Loading BigGAN module from:', module_path)
tf.reset_default_graph()
hub.Module(module_path)
print('Loaded BigGAN module from:', module_path)
initializer = tf.global_variables_initializer()
sess = tf.Session()
sess.run(initializer)
print('Saving BigGAN weights to :', hdf5_path)
h5f = h5py.File(hdf5_path, 'w')
for var in tf.global_variables():
val = sess.run(var)
h5f.create_dataset(var.name, data=val)
print(f'Saving {var.name} with shape {val.shape}')
h5f.close()
return h5py.File(hdf5_path, 'r')
class TFHub2Pytorch(object):
TF_ROOT = 'module'
NUM_GBLOCK = {
128: 5,
256: 6,
512: 7
}
w = 'w'
b = 'b'
u = 'u0'
v = 'u1'
gamma = 'gamma'
beta = 'beta'
def __init__(self, state_dict, tf_weights, resolution=256, load_ema=True, verbose=False):
self.state_dict = state_dict
self.tf_weights = tf_weights
self.resolution = resolution
self.verbose = verbose
if load_ema:
for name in ['w', 'b', 'gamma', 'beta']:
setattr(self, name, getattr(self, name) + '/ema_b999900')
def load(self):
self.load_generator()
return self.state_dict
def load_generator(self):
GENERATOR_ROOT = os.path.join(self.TF_ROOT, 'Generator')
for i in range(self.NUM_GBLOCK[self.resolution]):
name_tf = os.path.join(GENERATOR_ROOT, 'GBlock')
name_tf += f'_{i}' if i != 0 else ''
self.load_GBlock(f'GBlock.{i}.', name_tf)
self.load_attention('attention.', os.path.join(GENERATOR_ROOT, 'attention'))
self.load_linear('linear', os.path.join(self.TF_ROOT, 'linear'), bias=False)
self.load_snlinear('G_linear', os.path.join(GENERATOR_ROOT, 'G_Z', 'G_linear'))
self.load_colorize('colorize', os.path.join(GENERATOR_ROOT, 'conv_2d'))
self.load_ScaledCrossReplicaBNs('ScaledCrossReplicaBN',
os.path.join(GENERATOR_ROOT, 'ScaledCrossReplicaBN'))
def load_linear(self, name_pth, name_tf, bias=True):
self.state_dict[name_pth + '.weight'] = self.load_tf_tensor(name_tf, self.w).permute(1, 0)
if bias:
self.state_dict[name_pth + '.bias'] = self.load_tf_tensor(name_tf, self.b)
def load_snlinear(self, name_pth, name_tf, bias=True):
self.state_dict[name_pth + '.module.weight_u'] = self.load_tf_tensor(name_tf, self.u).squeeze()
self.state_dict[name_pth + '.module.weight_v'] = self.load_tf_tensor(name_tf, self.v).squeeze()
self.state_dict[name_pth + '.module.weight_bar'] = self.load_tf_tensor(name_tf, self.w).permute(1, 0)
if bias:
self.state_dict[name_pth + '.module.bias'] = self.load_tf_tensor(name_tf, self.b)
def load_colorize(self, name_pth, name_tf):
self.load_snconv(name_pth, name_tf)
def load_GBlock(self, name_pth, name_tf):
self.load_convs(name_pth, name_tf)
self.load_HyperBNs(name_pth, name_tf)
def load_convs(self, name_pth, name_tf):
self.load_snconv(name_pth + 'conv0', os.path.join(name_tf, 'conv0'))
self.load_snconv(name_pth + 'conv1', os.path.join(name_tf, 'conv1'))
self.load_snconv(name_pth + 'conv_sc', os.path.join(name_tf, 'conv_sc'))
def load_snconv(self, name_pth, name_tf, bias=True):
if self.verbose:
print(f'loading: {name_pth} from {name_tf}')
self.state_dict[name_pth + '.module.weight_u'] = self.load_tf_tensor(name_tf, self.u).squeeze()
self.state_dict[name_pth + '.module.weight_v'] = self.load_tf_tensor(name_tf, self.v).squeeze()
self.state_dict[name_pth + '.module.weight_bar'] = self.load_tf_tensor(name_tf, self.w).permute(3, 2, 0, 1)
if bias:
self.state_dict[name_pth + '.module.bias'] = self.load_tf_tensor(name_tf, self.b).squeeze()
def load_conv(self, name_pth, name_tf, bias=True):
self.state_dict[name_pth + '.weight_u'] = self.load_tf_tensor(name_tf, self.u).squeeze()
self.state_dict[name_pth + '.weight_v'] = self.load_tf_tensor(name_tf, self.v).squeeze()
self.state_dict[name_pth + '.weight_bar'] = self.load_tf_tensor(name_tf, self.w).permute(3, 2, 0, 1)
if bias:
self.state_dict[name_pth + '.bias'] = self.load_tf_tensor(name_tf, self.b)
def load_HyperBNs(self, name_pth, name_tf):
self.load_HyperBN(name_pth + 'HyperBN', os.path.join(name_tf, 'HyperBN'))
self.load_HyperBN(name_pth + 'HyperBN_1', os.path.join(name_tf, 'HyperBN_1'))
def load_ScaledCrossReplicaBNs(self, name_pth, name_tf):
self.state_dict[name_pth + '.bias'] = self.load_tf_tensor(name_tf, self.beta).squeeze()
self.state_dict[name_pth + '.weight'] = self.load_tf_tensor(name_tf, self.gamma).squeeze()
self.state_dict[name_pth + '.running_mean'] = self.load_tf_tensor(name_tf + 'bn', 'accumulated_mean')
self.state_dict[name_pth + '.running_var'] = self.load_tf_tensor(name_tf + 'bn', 'accumulated_var')
self.state_dict[name_pth + '.num_batches_tracked'] = torch.tensor(
self.tf_weights[os.path.join(name_tf + 'bn', 'accumulation_counter:0')][()], dtype=torch.float32)
def load_HyperBN(self, name_pth, name_tf):
if self.verbose:
print(f'loading: {name_pth} from {name_tf}')
beta = name_pth + '.beta_embed.module'
gamma = name_pth + '.gamma_embed.module'
self.state_dict[beta + '.weight_u'] = self.load_tf_tensor(os.path.join(name_tf, 'beta'), self.u).squeeze()
self.state_dict[gamma + '.weight_u'] = self.load_tf_tensor(os.path.join(name_tf, 'gamma'), self.u).squeeze()
self.state_dict[beta + '.weight_v'] = self.load_tf_tensor(os.path.join(name_tf, 'beta'), self.v).squeeze()
self.state_dict[gamma + '.weight_v'] = self.load_tf_tensor(os.path.join(name_tf, 'gamma'), self.v).squeeze()
self.state_dict[beta + '.weight_bar'] = self.load_tf_tensor(os.path.join(name_tf, 'beta'), self.w).permute(1, 0)
self.state_dict[gamma +
'.weight_bar'] = self.load_tf_tensor(os.path.join(name_tf, 'gamma'), self.w).permute(1, 0)
cr_bn_name = name_tf.replace('HyperBN', 'CrossReplicaBN')
self.state_dict[name_pth + '.bn.running_mean'] = self.load_tf_tensor(cr_bn_name, 'accumulated_mean')
self.state_dict[name_pth + '.bn.running_var'] = self.load_tf_tensor(cr_bn_name, 'accumulated_var')
self.state_dict[name_pth + '.bn.num_batches_tracked'] = torch.tensor(
self.tf_weights[os.path.join(cr_bn_name, 'accumulation_counter:0')][()], dtype=torch.float32)
def load_attention(self, name_pth, name_tf):
self.load_snconv(name_pth + 'theta', os.path.join(name_tf, 'theta'), bias=False)
self.load_snconv(name_pth + 'phi', os.path.join(name_tf, 'phi'), bias=False)
self.load_snconv(name_pth + 'g', os.path.join(name_tf, 'g'), bias=False)
self.load_snconv(name_pth + 'o_conv', os.path.join(name_tf, 'o_conv'), bias=False)
self.state_dict[name_pth + 'gamma'] = self.load_tf_tensor(name_tf, self.gamma)
def load_tf_tensor(self, prefix, var, device='0'):
name = os.path.join(prefix, var) + f':{device}'
return torch.from_numpy(self.tf_weights[name][:])
# Convert from v1: This function maps
def convert_from_v1(hub_dict, resolution=128):
weightname_dict = {'weight_u': 'u0', 'weight_bar': 'weight', 'bias': 'bias'}
convnum_dict = {'conv0': 'conv1', 'conv1': 'conv2', 'conv_sc': 'conv_sc'}
attention_blocknum = {128: 3, 256: 4, 512: 3}[resolution]
hub2me = {'linear.weight': 'shared.weight', # This is actually the shared weight
# Linear stuff
'G_linear.module.weight_bar': 'linear.weight',
'G_linear.module.bias': 'linear.bias',
'G_linear.module.weight_u': 'linear.u0',
# output layer stuff
'ScaledCrossReplicaBN.weight': 'output_layer.0.gain',
'ScaledCrossReplicaBN.bias': 'output_layer.0.bias',
'ScaledCrossReplicaBN.running_mean': 'output_layer.0.stored_mean',
'ScaledCrossReplicaBN.running_var': 'output_layer.0.stored_var',
'colorize.module.weight_bar': 'output_layer.2.weight',
'colorize.module.bias': 'output_layer.2.bias',
'colorize.module.weight_u': 'output_layer.2.u0',
# Attention stuff
'attention.gamma': 'blocks.%d.1.gamma' % attention_blocknum,
'attention.theta.module.weight_u': 'blocks.%d.1.theta.u0' % attention_blocknum,
'attention.theta.module.weight_bar': 'blocks.%d.1.theta.weight' % attention_blocknum,
'attention.phi.module.weight_u': 'blocks.%d.1.phi.u0' % attention_blocknum,
'attention.phi.module.weight_bar': 'blocks.%d.1.phi.weight' % attention_blocknum,
'attention.g.module.weight_u': 'blocks.%d.1.g.u0' % attention_blocknum,
'attention.g.module.weight_bar': 'blocks.%d.1.g.weight' % attention_blocknum,
'attention.o_conv.module.weight_u': 'blocks.%d.1.o.u0' % attention_blocknum,
'attention.o_conv.module.weight_bar':'blocks.%d.1.o.weight' % attention_blocknum,
}
# Loop over the hub dict and build the hub2me map
for name in hub_dict.keys():
if 'GBlock' in name:
if 'HyperBN' not in name: # it's a conv
out = parse.parse('GBlock.{:d}.{}.module.{}',name)
blocknum, convnum, weightname = out
if weightname not in weightname_dict:
continue # else hyperBN in
out_name = 'blocks.%d.0.%s.%s' % (blocknum, convnum_dict[convnum], weightname_dict[weightname]) # Increment conv number by 1
else: # hyperbn not conv
BNnum = 2 if 'HyperBN_1' in name else 1
if 'embed' in name:
out = parse.parse('GBlock.{:d}.{}.module.{}',name)
blocknum, gamma_or_beta, weightname = out
if weightname not in weightname_dict: # Ignore weight_v
continue
out_name = 'blocks.%d.0.bn%d.%s.%s' % (blocknum, BNnum, 'gain' if 'gamma' in gamma_or_beta else 'bias', weightname_dict[weightname])
else:
out = parse.parse('GBlock.{:d}.{}.bn.{}',name)
blocknum, dummy, mean_or_var = out
if 'num_batches_tracked' in mean_or_var:
continue
out_name = 'blocks.%d.0.bn%d.%s' % (blocknum, BNnum, 'stored_mean' if 'mean' in mean_or_var else 'stored_var')
hub2me[name] = out_name
# Invert the hub2me map
me2hub = {hub2me[item]: item for item in hub2me}
new_dict = {}
dimz_dict = {128: 20, 256: 20, 512:16}
for item in me2hub:
# Swap input dim ordering on batchnorm bois to account for my arbitrary change of ordering when concatenating Ys and Zs
if ('bn' in item and 'weight' in item) and ('gain' in item or 'bias' in item) and ('output_layer' not in item):
new_dict[item] = torch.cat([hub_dict[me2hub[item]][:, -128:], hub_dict[me2hub[item]][:, :dimz_dict[resolution]]], 1)
# Reshape the first linear weight, bias, and u0
elif item == 'linear.weight':
new_dict[item] = hub_dict[me2hub[item]].contiguous().view(4, 4, 96 * 16, -1).permute(2,0,1,3).contiguous().view(-1,dimz_dict[resolution])
elif item == 'linear.bias':
new_dict[item] = hub_dict[me2hub[item]].view(4, 4, 96 * 16).permute(2,0,1).contiguous().view(-1)
elif item == 'linear.u0':
new_dict[item] = hub_dict[me2hub[item]].view(4, 4, 96 * 16).permute(2,0,1).contiguous().view(1, -1)
elif me2hub[item] == 'linear.weight': # THIS IS THE SHARED WEIGHT NOT THE FIRST LINEAR LAYER
# Transpose shared weight so that it's an embedding
new_dict[item] = hub_dict[me2hub[item]].t()
elif 'weight_u' in me2hub[item]: # Unsqueeze u0s
new_dict[item] = hub_dict[me2hub[item]].unsqueeze(0)
else:
new_dict[item] = hub_dict[me2hub[item]]
return new_dict
def get_config(resolution):
attn_dict = {128: '64', 256: '128', 512: '64'}
dim_z_dict = {128: 120, 256: 140, 512: 128}
config = {'G_param': 'SN', 'D_param': 'SN',
'G_ch': 96, 'D_ch': 96,
'D_wide': True, 'G_shared': True,
'shared_dim': 128, 'dim_z': dim_z_dict[resolution],
'hier': True, 'cross_replica': False,
'mybn': False, 'G_activation': nn.ReLU(inplace=True),
'G_attn': attn_dict[resolution],
'norm_style': 'bn',
'G_init': 'ortho', 'skip_init': True, 'no_optim': True,
'G_fp16': False, 'G_mixed_precision': False,
'accumulate_stats': False, 'num_standing_accumulations': 16,
'G_eval_mode': True,
'BN_eps': 1e-04, 'SN_eps': 1e-04,
'num_G_SVs': 1, 'num_G_SV_itrs': 1, 'resolution': resolution,
'n_classes': 1000}
return config
def convert_biggan(resolution, weight_dir, redownload=False, no_ema=False, verbose=False):
module_path = MODULE_PATH_TMPL.format(resolution)
hdf5_path = os.path.join(weight_dir, HDF5_TMPL.format(resolution))
pth_path = os.path.join(weight_dir, PTH_TMPL.format(resolution))
tf_weights = dump_tfhub_to_hdf5(module_path, hdf5_path, redownload=redownload)
G_temp = getattr(biggan_for_conversion, f'Generator{resolution}')()
state_dict_temp = G_temp.state_dict()
converter = TFHub2Pytorch(state_dict_temp, tf_weights, resolution=resolution,
load_ema=(not no_ema), verbose=verbose)
state_dict_v1 = converter.load()
state_dict = convert_from_v1(state_dict_v1, resolution)
# Get the config, build the model
config = get_config(resolution)
G = BigGAN.Generator(**config)
G.load_state_dict(state_dict, strict=False) # Ignore missing sv0 entries
torch.save(state_dict, pth_path)
# output_location ='pretrained_weights/TFHub-PyTorch-128.pth'
return G
def generate_sample(G, z_dim, batch_size, filename, parallel=False):
G.eval()
G.to(DEVICE)
with torch.no_grad():
z = torch.randn(batch_size, G.dim_z).to(DEVICE)
y = torch.randint(low=0, high=1000, size=(batch_size,),
device=DEVICE, dtype=torch.int64, requires_grad=False)
if parallel:
images = nn.parallel.data_parallel(G, (z, G.shared(y)))
else:
images = G(z, G.shared(y))
save_image(images, filename, scale_each=True, normalize=True)
def parse_args():
usage = 'Parser for conversion script.'
parser = argparse.ArgumentParser(description=usage)
parser.add_argument(
'--resolution', '-r', type=int, default=None, choices=[128, 256, 512],
help='Resolution of TFHub module to convert. Converts all resolutions if None.')
parser.add_argument(
'--redownload', action='store_true', default=False,
help='Redownload weights and overwrite current hdf5 file, if present.')
parser.add_argument(
'--weights_dir', type=str, default='pretrained_weights')
parser.add_argument(
'--samples_dir', type=str, default='pretrained_samples')
parser.add_argument(
'--no_ema', action='store_true', default=False,
help='Do not load ema weights.')
parser.add_argument(
'--verbose', action='store_true', default=False,
help='Additionally logging.')
parser.add_argument(
'--generate_samples', action='store_true', default=False,
help='Generate test sample with pretrained model.')
parser.add_argument(
'--batch_size', type=int, default=64,
help='Batch size used for test sample.')
parser.add_argument(
'--parallel', action='store_true', default=False,
help='Parallelize G?')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
os.makedirs(args.weights_dir, exist_ok=True)
os.makedirs(args.samples_dir, exist_ok=True)
if args.resolution is not None:
G = convert_biggan(args.resolution, args.weights_dir,
redownload=args.redownload,
no_ema=args.no_ema, verbose=args.verbose)
if args.generate_samples:
filename = os.path.join(args.samples_dir, f'biggan{args.resolution}_samples.jpg')
print('Generating samples...')
generate_sample(G, Z_DIMS[args.resolution], args.batch_size, filename, args.parallel)
else:
for res in RESOLUTIONS:
G = convert_biggan(res, args.weights_dir,
redownload=args.redownload,
no_ema=args.no_ema, verbose=args.verbose)
if args.generate_samples:
filename = os.path.join(args.samples_dir, f'biggan{res}_samples.jpg')
print('Generating samples...')
generate_sample(G, Z_DIMS[res], args.batch_size, filename, args.parallel)
================================================
FILE: FQ-BigGAN/animal_hash.py
================================================
c = ['Aardvark', 'Abyssinian', 'Affenpinscher', 'Akbash', 'Akita', 'Albatross',
'Alligator', 'Alpaca', 'Angelfish', 'Ant', 'Anteater', 'Antelope', 'Ape',
'Armadillo', 'Ass', 'Avocet', 'Axolotl', 'Baboon', 'Badger', 'Balinese',
'Bandicoot', 'Barb', 'Barnacle', 'Barracuda', 'Bat', 'Beagle', 'Bear',
'Beaver', 'Bee', 'Beetle', 'Binturong', 'Bird', 'Birman', 'Bison',
'Bloodhound', 'Boar', 'Bobcat', 'Bombay', 'Bongo', 'Bonobo', 'Booby',
'Budgerigar', 'Buffalo', 'Bulldog', 'Bullfrog', 'Burmese', 'Butterfly',
'Caiman', 'Camel', 'Capybara', 'Caracal', 'Caribou', 'Cassowary', 'Cat',
'Caterpillar', 'Catfish', 'Cattle', 'Centipede', 'Chameleon', 'Chamois',
'Cheetah', 'Chicken', 'Chihuahua', 'Chimpanzee', 'Chinchilla', 'Chinook',
'Chipmunk', 'Chough', 'Cichlid', 'Clam', 'Coati', 'Cobra', 'Cockroach',
'Cod', 'Collie', 'Coral', 'Cormorant', 'Cougar', 'Cow', 'Coyote',
'Crab', 'Crane', 'Crocodile', 'Crow', 'Curlew', 'Cuscus', 'Cuttlefish',
'Dachshund', 'Dalmatian', 'Deer', 'Dhole', 'Dingo', 'Dinosaur', 'Discus',
'Dodo', 'Dog', 'Dogball', 'Dogfish', 'Dolphin', 'Donkey', 'Dormouse',
'Dove', 'Dragonfly', 'Drever', 'Duck', 'Dugong', 'Dunker', 'Dunlin',
'Eagle', 'Earwig', 'Echidna', 'Eel', 'Eland', 'Elephant', 'ElephantSeal',
'Elk', 'Emu', 'Falcon', 'Ferret', 'Finch', 'Fish', 'Flamingo', 'Flounder',
'Fly', 'Fossa', 'Fox', 'Frigatebird', 'Frog', 'Galago', 'Gar', 'Gaur',
'Gazelle', 'Gecko', 'Gerbil', 'Gharial', 'GiantPanda', 'Gibbon', 'Giraffe',
'Gnat', 'Gnu', 'Goat', 'Goldfinch', 'Goldfish', 'Goose', 'Gopher',
'Gorilla', 'Goshawk', 'Grasshopper', 'Greyhound', 'Grouse', 'Guanaco',
'GuineaFowl', 'GuineaPig', 'Gull', 'Guppy', 'Hamster', 'Hare', 'Harrier',
'Havanese', 'Hawk', 'Hedgehog', 'Heron', 'Herring', 'Himalayan',
'Hippopotamus', 'Hornet', 'Horse', 'Human', 'Hummingbird', 'Hyena',
'Ibis', 'Iguana', 'Impala', 'Indri', 'Insect', 'Jackal', 'Jaguar',
'Javanese', 'Jay', 'Jellyfish', 'Kakapo', 'Kangaroo', 'Kingfisher',
'Kiwi', 'Koala', 'KomodoDragon', 'Kouprey', 'Kudu', 'Labradoodle',
'Ladybird', 'Lapwing', 'Lark', 'Lemming', 'Lemur', 'Leopard', 'Liger',
'Lion', 'Lionfish', 'Lizard', 'Llama', 'Lobster', 'Locust', 'Loris',
'Louse', 'Lynx', 'Lyrebird', 'Macaw', 'Magpie', 'Mallard', 'Maltese',
'Manatee', 'Mandrill', 'Markhor', 'Marten', 'Mastiff', 'Mayfly', 'Meerkat',
'Millipede', 'Mink', 'Mole', 'Molly', 'Mongoose', 'Mongrel', 'Monkey',
'Moorhen', 'Moose', 'Mosquito', 'Moth', 'Mouse', 'Mule', 'Narwhal',
'Neanderthal', 'Newfoundland', 'Newt', 'Nightingale', 'Numbat', 'Ocelot',
'Octopus', 'Okapi', 'Olm', 'Opossum', 'Orang-utan', 'Oryx', 'Ostrich',
'Otter', 'Owl', 'Ox', 'Oyster', 'Pademelon', 'Panther', 'Parrot',
'Partridge', 'Peacock', 'Peafowl', 'Pekingese', 'Pelican', 'Penguin',
'Persian', 'Pheasant', 'Pig', 'Pigeon', 'Pika', 'Pike', 'Piranha',
'Platypus', 'Pointer', 'Pony', 'Poodle', 'Porcupine', 'Porpoise',
'Possum', 'PrairieDog', 'Prawn', 'Puffin', 'Pug', 'Puma', 'Quail',
'Quelea', 'Quetzal', 'Quokka', 'Quoll', 'Rabbit', 'Raccoon', 'Ragdoll',
'Rail', 'Ram', 'Rat', 'Rattlesnake', 'Raven', 'RedDeer', 'RedPanda',
'Reindeer', 'Rhinoceros', 'Robin', 'Rook', 'Rottweiler', 'Ruff',
'Salamander', 'Salmon', 'SandDollar', 'Sandpiper', 'Saola',
'Sardine', 'Scorpion', 'SeaLion', 'SeaUrchin', 'Seahorse',
'Seal', 'Serval', 'Shark', 'Sheep', 'Shrew', 'Shrimp', 'Siamese',
'Siberian', 'Skunk', 'Sloth', 'Snail', 'Snake', 'Snowshoe', 'Somali',
'Sparrow', 'Spider', 'Sponge', 'Squid', 'Squirrel', 'Starfish', 'Starling',
'Stingray', 'Stinkbug', 'Stoat', 'Stork', 'Swallow', 'Swan', 'Tang',
'Tapir', 'Tarsier', 'Termite', 'Tetra', 'Tiffany', 'Tiger', 'Toad',
'Tortoise', 'Toucan', 'Tropicbird', 'Trout', 'Tuatara', 'Turkey',
'Turtle', 'Uakari', 'Uguisu', 'Umbrellabird', 'Viper', 'Vulture',
'Wallaby', 'Walrus', 'Warthog', 'Wasp', 'WaterBuffalo', 'Weasel',
'Whale', 'Whippet', 'Wildebeest', 'Wolf', 'Wolverine', 'Wombat',
'Woodcock', 'Woodlouse', 'Woodpecker', 'Worm', 'Wrasse', 'Wren',
'Yak', 'Zebra', 'Zebu', 'Zonkey']
a = ['able', 'above', 'absent', 'absolute', 'abstract', 'abundant', 'academic',
'acceptable', 'accepted', 'accessible', 'accurate', 'accused', 'active',
'actual', 'acute', 'added', 'additional', 'adequate', 'adjacent',
'administrative', 'adorable', 'advanced', 'adverse', 'advisory',
'aesthetic', 'afraid', 'african', 'aggregate', 'aggressive', 'agreeable',
'agreed', 'agricultural', 'alert', 'alive', 'alleged', 'allied', 'alone',
'alright', 'alternative', 'amateur', 'amazing', 'ambitious', 'american',
'amused', 'ancient', 'angry', 'annoyed', 'annual', 'anonymous', 'anxious',
'appalling', 'apparent', 'applicable', 'appropriate', 'arab', 'arbitrary',
'architectural', 'armed', 'arrogant', 'artificial', 'artistic', 'ashamed',
'asian', 'asleep', 'assistant', 'associated', 'atomic', 'attractive',
'australian', 'automatic', 'autonomous', 'available', 'average',
'awake', 'aware', 'awful', 'awkward', 'back', 'bad', 'balanced', 'bare',
'basic', 'beautiful', 'beneficial', 'better', 'bewildered', 'big',
'binding', 'biological', 'bitter', 'bizarre', 'black', 'blank', 'blind',
'blonde', 'bloody', 'blue', 'blushing', 'boiling', 'bold', 'bored',
'boring', 'bottom', 'brainy', 'brave', 'breakable', 'breezy', 'brief',
'bright', 'brilliant', 'british', 'broad', 'broken', 'brown', 'bumpy',
'burning', 'busy', 'calm', 'canadian', 'capable', 'capitalist', 'careful',
'casual', 'catholic', 'causal', 'cautious', 'central', 'certain',
'changing', 'characteristic', 'charming', 'cheap', 'cheerful', 'chemical',
'chief', 'chilly', 'chinese', 'chosen', 'christian', 'chronic', 'chubby',
'circular', 'civic', 'civil', 'civilian', 'classic', 'classical', 'clean',
'clear', 'clever', 'clinical', 'close', 'closed', 'cloudy', 'clumsy',
'coastal', 'cognitive', 'coherent', 'cold', 'collective', 'colonial',
'colorful', 'colossal', 'coloured', 'colourful', 'combative', 'combined',
'comfortable', 'coming', 'commercial', 'common', 'communist', 'compact',
'comparable', 'comparative', 'compatible', 'competent', 'competitive',
'complete', 'complex', 'complicated', 'comprehensive', 'compulsory',
'conceptual', 'concerned', 'concrete', 'condemned', 'confident',
'confidential', 'confused', 'conscious', 'conservation', 'conservative',
'considerable', 'consistent', 'constant', 'constitutional',
'contemporary', 'content', 'continental', 'continued', 'continuing',
'continuous', 'controlled', 'controversial', 'convenient', 'conventional',
'convinced', 'convincing', 'cooing', 'cool', 'cooperative', 'corporate',
'correct', 'corresponding', 'costly', 'courageous', 'crazy', 'creative',
'creepy', 'criminal', 'critical', 'crooked', 'crowded', 'crucial',
'crude', 'cruel', 'cuddly', 'cultural', 'curious', 'curly', 'current',
'curved', 'cute', 'daily', 'damaged', 'damp', 'dangerous', 'dark', 'dead',
'deaf', 'deafening', 'dear', 'decent', 'decisive', 'deep', 'defeated',
'defensive', 'defiant', 'definite', 'deliberate', 'delicate', 'delicious',
'delighted', 'delightful', 'democratic', 'dependent', 'depressed',
'desirable', 'desperate', 'detailed', 'determined', 'developed',
'developing', 'devoted', 'different', 'difficult', 'digital', 'diplomatic',
'direct', 'dirty', 'disabled', 'disappointed', 'disastrous',
'disciplinary', 'disgusted', 'distant', 'distinct', 'distinctive',
'distinguished', 'disturbed', 'disturbing', 'diverse', 'divine', 'dizzy',
'domestic', 'dominant', 'double', 'doubtful', 'drab', 'dramatic',
'dreadful', 'driving', 'drunk', 'dry', 'dual', 'due', 'dull', 'dusty',
'dutch', 'dying', 'dynamic', 'eager', 'early', 'eastern', 'easy',
'economic', 'educational', 'eerie', 'effective', 'efficient',
'elaborate', 'elated', 'elderly', 'eldest', 'electoral', 'electric',
'electrical', 'electronic', 'elegant', 'eligible', 'embarrassed',
'embarrassing', 'emotional', 'empirical', 'empty', 'enchanting',
'encouraging', 'endless', 'energetic', 'english', 'enormous',
'enthusiastic', 'entire', 'entitled', 'envious', 'environmental', 'equal',
'equivalent', 'essential', 'established', 'estimated', 'ethical',
'ethnic', 'european', 'eventual', 'everyday', 'evident', 'evil',
'evolutionary', 'exact', 'excellent', 'exceptional', 'excess',
'excessive', 'excited', 'exciting', 'exclusive', 'existing', 'exotic',
'expected', 'expensive', 'experienced', 'experimental', 'explicit',
'extended', 'extensive', 'external', 'extra', 'extraordinary', 'extreme',
'exuberant', 'faint', 'fair', 'faithful', 'familiar', 'famous', 'fancy',
'fantastic', 'far', 'fascinating', 'fashionable', 'fast', 'fat', 'fatal',
'favourable', 'favourite', 'federal', 'fellow', 'female', 'feminist',
'few', 'fierce', 'filthy', 'final', 'financial', 'fine', 'firm', 'fiscal',
'fit', 'fixed', 'flaky', 'flat', 'flexible', 'fluffy', 'fluttering',
'flying', 'following', 'fond', 'foolish', 'foreign', 'formal',
'formidable', 'forthcoming', 'fortunate', 'forward', 'fragile',
'frail', 'frantic', 'free', 'french', 'frequent', 'fresh', 'friendly',
'frightened', 'front', 'frozen', 'fucking', 'full', 'full-time', 'fun',
'functional', 'fundamental', 'funny', 'furious', 'future', 'fuzzy',
'gastric', 'gay', 'general', 'generous', 'genetic', 'gentle', 'genuine',
'geographical', 'german', 'giant', 'gigantic', 'given', 'glad',
'glamorous', 'gleaming', 'global', 'glorious', 'golden', 'good',
'gorgeous', 'gothic', 'governing', 'graceful', 'gradual', 'grand',
'grateful', 'greasy', 'great', 'greek', 'green', 'grey', 'grieving',
'grim', 'gross', 'grotesque', 'growing', 'grubby', 'grumpy', 'guilty',
'handicapped', 'handsome', 'happy', 'hard', 'harsh', 'head', 'healthy',
'heavy', 'helpful', 'helpless', 'hidden', 'high', 'high-pitched',
'hilarious', 'hissing', 'historic', 'historical', 'hollow', 'holy',
'homeless', 'homely', 'hon', 'honest', 'horizontal', 'horrible',
'hostile', 'hot', 'huge', 'human', 'hungry', 'hurt', 'hushed', 'husky',
'icy', 'ideal', 'identical', 'ideological', 'ill', 'illegal',
'imaginative', 'immediate', 'immense', 'imperial', 'implicit',
'important', 'impossible', 'impressed', 'impressive', 'improved',
'inadequate', 'inappropriate', 'inc', 'inclined', 'increased',
'increasing', 'incredible', 'independent', 'indian', 'indirect',
'individual', 'industrial', 'inevitable', 'influential', 'informal',
'inherent', 'initial', 'injured', 'inland', 'inner', 'innocent',
'innovative', 'inquisitive', 'instant', 'institutional', 'insufficient',
'intact', 'integral', 'integrated', 'intellectual', 'intelligent',
'intense', 'intensive', 'interested', 'interesting', 'interim',
'interior', 'intermediate', 'internal', 'international', 'intimate',
'invisible', 'involved', 'iraqi', 'irish', 'irrelevant', 'islamic',
'isolated', 'israeli', 'italian', 'itchy', 'japanese', 'jealous',
'jewish', 'jittery', 'joint', 'jolly', 'joyous', 'judicial', 'juicy',
'junior', 'just', 'keen', 'key', 'kind', 'known', 'korean', 'labour',
'large', 'large-scale', 'late', 'latin', 'lazy', 'leading', 'left',
'legal', 'legislative', 'legitimate', 'lengthy', 'lesser', 'level',
'lexical', 'liable', 'liberal', 'light', 'like', 'likely', 'limited',
'linear', 'linguistic', 'liquid', 'literary', 'little', 'live', 'lively',
'living', 'local', 'logical', 'lonely', 'long', 'long-term', 'loose',
'lost', 'loud', 'lovely', 'low', 'loyal', 'ltd', 'lucky', 'mad',
'magenta', 'magic', 'magnetic', 'magnificent', 'main', 'major', 'male',
'mammoth', 'managerial', 'managing', 'manual', 'many', 'marginal',
'marine', 'marked', 'married', 'marvellous', 'marxist', 'mass', 'massive',
'mathematical', 'mature', 'maximum', 'mean', 'meaningful', 'mechanical',
'medical', 'medieval', 'melodic', 'melted', 'mental', 'mere',
'metropolitan', 'mid', 'middle', 'middle-class', 'mighty', 'mild',
'military', 'miniature', 'minimal', 'minimum', 'ministerial', 'minor',
'miserable', 'misleading', 'missing', 'misty', 'mixed', 'moaning',
'mobile', 'moderate', 'modern', 'modest', 'molecular', 'monetary',
'monthly', 'moral', 'motionless', 'muddy', 'multiple', 'mushy',
'musical', 'mute', 'mutual', 'mysterious', 'naked', 'narrow', 'nasty',
'national', 'native', 'natural', 'naughty', 'naval', 'near', 'nearby',
'neat', 'necessary', 'negative', 'neighbouring', 'nervous', 'net',
'neutral', 'new', 'nice', 'nineteenth-century', 'noble', 'noisy',
'normal', 'northern', 'nosy', 'notable', 'novel', 'nuclear', 'numerous',
'nursing', 'nutritious', 'nutty', 'obedient', 'objective', 'obliged',
'obnoxious', 'obvious', 'occasional', 'occupational', 'odd', 'official',
'ok', 'okay', 'old', 'old-fashioned', 'olympic', 'only', 'open',
'operational', 'opposite', 'optimistic', 'oral', 'orange', 'ordinary',
'organic', 'organisational', 'original', 'orthodox', 'other', 'outdoor',
'outer', 'outrageous', 'outside', 'outstanding', 'overall', 'overseas',
'overwhelming', 'painful', 'pale', 'palestinian', 'panicky', 'parallel',
'parental', 'parliamentary', 'part-time', 'partial', 'particular',
'passing', 'passive', 'past', 'patient', 'payable', 'peaceful',
'peculiar', 'perfect', 'permanent', 'persistent', 'personal', 'petite',
'philosophical', 'physical', 'pink', 'plain', 'planned', 'plastic',
'pleasant', 'pleased', 'poised', 'polish', 'polite', 'political', 'poor',
'popular', 'positive', 'possible', 'post-war', 'potential', 'powerful',
'practical', 'precious', 'precise', 'preferred', 'pregnant',
'preliminary', 'premier', 'prepared', 'present', 'presidential',
'pretty', 'previous', 'prickly', 'primary', 'prime', 'primitive',
'principal', 'printed', 'prior', 'private', 'probable', 'productive',
'professional', 'profitable', 'profound', 'progressive', 'prominent',
'promising', 'proper', 'proposed', 'prospective', 'protective',
'protestant', 'proud', 'provincial', 'psychiatric', 'psychological',
'public', 'puny', 'pure', 'purple', 'purring', 'puzzled', 'quaint',
'qualified', 'quick', 'quickest', 'quiet', 'racial', 'radical', 'rainy',
'random', 'rapid', 'rare', 'raspy', 'rational', 'ratty', 'raw', 'ready',
'real', 'realistic', 'rear', 'reasonable', 'recent', 'red', 'reduced',
'redundant', 'regional', 'registered', 'regular', 'regulatory', 'related',
'relative', 'relaxed', 'relevant', 'reliable', 'relieved', 'religious',
'reluctant', 'remaining', 'remarkable', 'remote', 'renewed',
'representative', 'repulsive', 'required', 'resident', 'residential',
'resonant', 'respectable', 'respective', 'responsible', 'resulting',
'retail', 'retired', 'revolutionary', 'rich', 'ridiculous', 'right',
'rigid', 'ripe', 'rising', 'rival', 'roasted', 'robust', 'rolling',
'roman', 'romantic', 'rotten', 'rough', 'round', 'royal', 'rubber',
'rude', 'ruling', 'running', 'rural', 'russian', 'sacred', 'sad', 'safe',
'salty', 'satisfactory', 'satisfied', 'scared', 'scary', 'scattered',
'scientific', 'scornful', 'scottish', 'scrawny', 'screeching',
'secondary', 'secret', 'secure', 'select', 'selected', 'selective',
'selfish', 'semantic', 'senior', 'sensible', 'sensitive', 'separate',
'serious', 'severe', 'sexual', 'shaggy', 'shaky', 'shallow', 'shared',
'sharp', 'sheer', 'shiny', 'shivering', 'shocked', 'short', 'short-term',
'shrill', 'shy', 'sick', 'significant', 'silent', 'silky', 'silly',
'similar', 'simple', 'single', 'skilled', 'skinny', 'sleepy', 'slight',
'slim', 'slimy', 'slippery', 'slow', 'small', 'smart', 'smiling',
'smoggy', 'smooth', 'so-called', 'social', 'socialist', 'soft', 'solar',
'sole', 'solid', 'sophisticated', 'sore', 'sorry', 'sound', 'sour',
'southern', 'soviet', 'spanish', 'spare', 'sparkling', 'spatial',
'special', 'specific', 'specified', 'spectacular', 'spicy', 'spiritual',
'splendid', 'spontaneous', 'sporting', 'spotless', 'spotty', 'square',
'squealing', 'stable', 'stale', 'standard', 'static', 'statistical',
'statutory', 'steady', 'steep', 'sticky', 'stiff', 'still', 'stingy',
'stormy', 'straight', 'straightforward', 'strange', 'strategic',
'strict', 'striking', 'striped', 'strong', 'structural', 'stuck',
'stupid', 'subjective', 'subsequent', 'substantial', 'subtle',
'successful', 'successive', 'sudden', 'sufficient', 'suitable',
'sunny', 'super', 'superb', 'superior', 'supporting', 'supposed',
'supreme', 'sure', 'surprised', 'surprising', 'surrounding',
'surviving', 'suspicious', 'sweet', 'swift', 'swiss', 'symbolic',
'sympathetic', 'systematic', 'tall', 'tame', 'tan', 'tart',
'tasteless', 'tasty', 'technical', 'technological', 'teenage',
'temporary', 'tender', 'tense', 'terrible', 'territorial', 'testy',
'then', 'theoretical', 'thick', 'thin', 'thirsty', 'thorough',
'thoughtful', 'thoughtless', 'thundering', 'tight', 'tiny', 'tired',
'top', 'tory', 'total', 'tough', 'toxic', 'traditional', 'tragic',
'tremendous', 'tricky', 'tropical', 'troubled', 'turkish', 'typical',
'ugliest', 'ugly', 'ultimate', 'unable', 'unacceptable', 'unaware',
'uncertain', 'unchanged', 'uncomfortable', 'unconscious', 'underground',
'underlying', 'unemployed', 'uneven', 'unexpected', 'unfair',
'unfortunate', 'unhappy', 'uniform', 'uninterested', 'unique', 'united',
'universal', 'unknown', 'unlikely', 'unnecessary', 'unpleasant',
'unsightly', 'unusual', 'unwilling', 'upper', 'upset', 'uptight',
'urban', 'urgent', 'used', 'useful', 'useless', 'usual', 'vague',
'valid', 'valuable', 'variable', 'varied', 'various', 'varying', 'vast',
'verbal', 'vertical', 'very', 'victorian', 'victorious', 'video-taped',
'violent', 'visible', 'visiting', 'visual', 'vital', 'vivacious',
'vivid', 'vocational', 'voiceless', 'voluntary', 'vulnerable',
'wandering', 'warm', 'wasteful', 'watery', 'weak', 'wealthy', 'weary',
'wee', 'weekly', 'weird', 'welcome', 'well', 'well-known', 'welsh',
'western', 'wet', 'whispering', 'white', 'whole', 'wicked', 'wide',
'wide-eyed', 'widespread', 'wild', 'willing', 'wise', 'witty',
'wonderful', 'wooden', 'working', 'working-class', 'worldwide',
'worried', 'worrying', 'worthwhile', 'worthy', 'written', 'wrong',
'yellow', 'young', 'yummy', 'zany', 'zealous']
b = ['abiding', 'accelerating', 'accepting', 'accomplishing', 'achieving',
'acquiring', 'acteding', 'activating', 'adapting', 'adding', 'addressing',
'administering', 'admiring', 'admiting', 'adopting', 'advising', 'affording',
'agreeing', 'alerting', 'alighting', 'allowing', 'altereding', 'amusing',
'analyzing', 'announcing', 'annoying', 'answering', 'anticipating',
'apologizing', 'appearing', 'applauding', 'applieding', 'appointing',
'appraising', 'appreciating', 'approving', 'arbitrating', 'arguing',
'arising', 'arranging', 'arresting', 'arriving', 'ascertaining', 'asking',
'assembling', 'assessing', 'assisting', 'assuring', 'attaching', 'attacking',
'attaining', 'attempting', 'attending', 'attracting', 'auditeding', 'avoiding',
'awaking', 'backing', 'baking', 'balancing', 'baning', 'banging', 'baring',
'bating', 'bathing', 'battling', 'bing', 'beaming', 'bearing', 'beating',
'becoming', 'beging', 'begining', 'behaving', 'beholding', 'belonging',
'bending', 'beseting', 'beting', 'biding', 'binding', 'biting', 'bleaching',
'bleeding', 'blessing', 'blinding', 'blinking', 'bloting', 'blowing',
'blushing', 'boasting', 'boiling', 'bolting', 'bombing', 'booking',
'boring', 'borrowing', 'bouncing', 'bowing', 'boxing', 'braking',
'branching', 'breaking', 'breathing', 'breeding', 'briefing', 'bringing',
'broadcasting', 'bruising', 'brushing', 'bubbling', 'budgeting', 'building',
'bumping', 'burning', 'bursting', 'burying', 'busting', 'buying', 'buzing',
'calculating', 'calling', 'camping', 'caring', 'carrying', 'carving',
'casting', 'cataloging', 'catching', 'causing', 'challenging', 'changing',
'charging', 'charting', 'chasing', 'cheating', 'checking', 'cheering',
'chewing', 'choking', 'choosing', 'choping', 'claiming', 'claping',
'clarifying', 'classifying', 'cleaning', 'clearing', 'clinging', 'cliping',
'closing', 'clothing', 'coaching', 'coiling', 'collecting', 'coloring',
'combing', 'coming', 'commanding', 'communicating', 'comparing', 'competing',
'compiling', 'complaining', 'completing', 'composing', 'computing',
'conceiving', 'concentrating', 'conceptualizing', 'concerning', 'concluding',
'conducting', 'confessing', 'confronting', 'confusing', 'connecting',
'conserving', 'considering', 'consisting', 'consolidating', 'constructing',
'consulting', 'containing', 'continuing', 'contracting', 'controling',
'converting', 'coordinating', 'copying', 'correcting', 'correlating',
'costing', 'coughing', 'counseling', 'counting', 'covering', 'cracking',
'crashing', 'crawling', 'creating', 'creeping', 'critiquing', 'crossing',
'crushing', 'crying', 'curing', 'curling', 'curving', 'cuting', 'cycling',
'daming', 'damaging', 'dancing', 'daring', 'dealing', 'decaying', 'deceiving',
'deciding', 'decorating', 'defining', 'delaying', 'delegating', 'delighting',
'delivering', 'demonstrating', 'depending', 'describing', 'deserting',
'deserving', 'designing', 'destroying', 'detailing', 'detecting',
'determining', 'developing', 'devising', 'diagnosing', 'diging',
'directing', 'disagreing', 'disappearing', 'disapproving', 'disarming',
'discovering', 'disliking', 'dispensing', 'displaying', 'disproving',
'dissecting', 'distributing', 'diving', 'diverting', 'dividing', 'doing',
'doubling', 'doubting', 'drafting', 'draging', 'draining', 'dramatizing',
'drawing', 'dreaming', 'dressing', 'drinking', 'driping', 'driving',
'dropping', 'drowning', 'druming', 'drying', 'dusting', 'dwelling',
'earning', 'eating', 'editeding', 'educating', 'eliminating',
'embarrassing', 'employing', 'emptying', 'enacteding', 'encouraging',
'ending', 'enduring', 'enforcing', 'engineering', 'enhancing',
'enjoying', 'enlisting', 'ensuring', 'entering', 'entertaining',
'escaping', 'establishing', 'estimating', 'evaluating', 'examining',
'exceeding', 'exciting', 'excusing', 'executing', 'exercising', 'exhibiting',
'existing', 'expanding', 'expecting', 'expediting', 'experimenting',
'explaining', 'exploding', 'expressing', 'extending', 'extracting',
'facing', 'facilitating', 'fading', 'failing', 'fancying', 'fastening',
'faxing', 'fearing', 'feeding', 'feeling', 'fencing', 'fetching', 'fighting',
'filing', 'filling', 'filming', 'finalizing', 'financing', 'finding',
'firing', 'fiting', 'fixing', 'flaping', 'flashing', 'fleing', 'flinging',
'floating', 'flooding', 'flowing', 'flowering', 'flying', 'folding',
'following', 'fooling', 'forbiding', 'forcing', 'forecasting', 'foregoing',
'foreseing', 'foretelling', 'forgeting', 'forgiving', 'forming',
'formulating', 'forsaking', 'framing', 'freezing', 'frightening', 'frying',
'gathering', 'gazing', 'generating', 'geting', 'giving', 'glowing', 'gluing',
'going', 'governing', 'grabing', 'graduating', 'grating', 'greasing', 'greeting',
'grinning', 'grinding', 'griping', 'groaning', 'growing', 'guaranteeing',
'guarding', 'guessing', 'guiding', 'hammering', 'handing', 'handling',
'handwriting', 'hanging', 'happening', 'harassing', 'harming', 'hating',
'haunting', 'heading', 'healing', 'heaping', 'hearing', 'heating', 'helping',
'hiding', 'hitting', 'holding', 'hooking', 'hoping', 'hopping', 'hovering',
'hugging', 'hmuming', 'hunting', 'hurrying', 'hurting', 'hypothesizing',
'identifying', 'ignoring', 'illustrating', 'imagining', 'implementing',
'impressing', 'improving', 'improvising', 'including', 'increasing',
'inducing', 'influencing', 'informing', 'initiating', 'injecting',
'injuring', 'inlaying', 'innovating', 'inputing', 'inspecting',
'inspiring', 'installing', 'instituting', 'instructing', 'insuring',
'integrating', 'intending', 'intensifying', 'interesting',
'interfering', 'interlaying', 'interpreting', 'interrupting',
'interviewing', 'introducing', 'inventing', 'inventorying',
'investigating', 'inviting', 'irritating', 'itching', 'jailing',
'jamming', 'jogging', 'joining', 'joking', 'judging', 'juggling', 'jumping',
'justifying', 'keeping', 'kepting', 'kicking', 'killing', 'kissing', 'kneeling',
'kniting', 'knocking', 'knotting', 'knowing', 'labeling', 'landing', 'lasting',
'laughing', 'launching', 'laying', 'leading', 'leaning', 'leaping', 'learning',
'leaving', 'lecturing', 'leding', 'lending', 'leting', 'leveling',
'licensing', 'licking', 'lying', 'lifteding', 'lighting', 'lightening',
'liking', 'listing', 'listening', 'living', 'loading', 'locating',
'locking', 'loging', 'longing', 'looking', 'losing', 'loving',
'maintaining', 'making', 'maning', 'managing', 'manipulating',
'manufacturing', 'mapping', 'marching', 'marking', 'marketing',
'marrying', 'matching', 'mating', 'mattering', 'meaning', 'measuring',
'meddling', 'mediating', 'meeting', 'melting', 'melting', 'memorizing',
'mending', 'mentoring', 'milking', 'mining', 'misleading', 'missing',
'misspelling', 'mistaking', 'misunderstanding', 'mixing', 'moaning',
'modeling', 'modifying', 'monitoring', 'mooring', 'motivating',
'mourning', 'moving', 'mowing', 'muddling', 'muging', 'multiplying',
'murdering', 'nailing', 'naming', 'navigating', 'needing', 'negotiating',
'nesting', 'noding', 'nominating', 'normalizing', 'noting', 'noticing',
'numbering', 'obeying', 'objecting', 'observing', 'obtaining', 'occuring',
'offending', 'offering', 'officiating', 'opening', 'operating', 'ordering',
'organizing', 'orienteding', 'originating', 'overcoming', 'overdoing',
'overdrawing', 'overflowing', 'overhearing', 'overtaking', 'overthrowing',
'owing', 'owning', 'packing', 'paddling', 'painting', 'parking', 'parting',
'participating', 'passing', 'pasting', 'pating', 'pausing', 'paying',
'pecking', 'pedaling', 'peeling', 'peeping', 'perceiving', 'perfecting',
'performing', 'permiting', 'persuading', 'phoning', 'photographing',
'picking', 'piloting', 'pinching', 'pining', 'pinpointing', 'pioneering',
'placing', 'planing', 'planting', 'playing', 'pleading', 'pleasing',
'plugging', 'pointing', 'poking', 'polishing', 'poping', 'possessing',
'posting', 'pouring', 'practicing', 'praiseding', 'praying', 'preaching',
'preceding', 'predicting', 'prefering', 'preparing', 'prescribing',
'presenting', 'preserving', 'preseting', 'presiding', 'pressing',
'pretending', 'preventing', 'pricking', 'printing', 'processing',
'procuring', 'producing', 'professing', 'programing', 'progressing',
'projecting', 'promising', 'promoting', 'proofreading', 'proposing',
'protecting', 'proving', 'providing', 'publicizing', 'pulling', 'pumping',
'punching', 'puncturing', 'punishing', 'purchasing', 'pushing', 'puting',
'qualifying', 'questioning', 'queuing', 'quiting', 'racing', 'radiating',
'raining', 'raising', 'ranking', 'rating', 'reaching', 'reading',
'realigning', 'realizing', 'reasoning', 'receiving', 'recognizing',
'recommending', 'reconciling', 'recording', 'recruiting', 'reducing',
'referring', 'reflecting', 'refusing', 'regreting', 'regulating',
'rehabilitating', 'reigning', 'reinforcing', 'rejecting', 'rejoicing',
'relating', 'relaxing', 'releasing', 'relying', 'remaining', 'remembering',
'reminding', 'removing', 'rendering', 'reorganizing', 'repairing',
'repeating', 'replacing', 'replying', 'reporting', 'representing',
'reproducing', 'requesting', 'rescuing', 'researching', 'resolving',
'responding', 'restoreding', 'restructuring', 'retiring', 'retrieving',
'returning', 'reviewing', 'revising', 'rhyming', 'riding', 'riding',
'ringing', 'rinsing', 'rising', 'risking', 'robing', 'rocking', 'rolling',
'roting', 'rubing', 'ruining', 'ruling', 'runing', 'rushing', 'sacking',
'sailing', 'satisfying', 'saving', 'sawing', 'saying', 'scaring',
'scattering', 'scheduling', 'scolding', 'scorching', 'scraping',
'scratching', 'screaming', 'screwing', 'scribbling', 'scrubing',
'sealing', 'searching', 'securing', 'seing', 'seeking', 'selecting',
'selling', 'sending', 'sensing', 'separating', 'serving', 'servicing',
'seting', 'settling', 'sewing', 'shading', 'shaking', 'shaping',
'sharing', 'shaving', 'shearing', 'sheding', 'sheltering', 'shining',
'shivering', 'shocking', 'shoing', 'shooting', 'shoping', 'showing',
'shrinking', 'shruging', 'shuting', 'sighing', 'signing', 'signaling',
'simplifying', 'sining', 'singing', 'sinking', 'siping', 'siting',
'sketching', 'skiing', 'skiping', 'slaping', 'slaying', 'sleeping',
'sliding', 'slinging', 'slinking', 'sliping', 'sliting', 'slowing',
'smashing', 'smelling', 'smiling', 'smiting', 'smoking', 'snatching',
'sneaking', 'sneezing', 'sniffing', 'snoring', 'snowing', 'soaking',
'solving', 'soothing', 'soothsaying', 'sorting', 'sounding', 'sowing',
'sparing', 'sparking', 'sparkling', 'speaking', 'specifying', 'speeding',
'spelling', 'spending', 'spilling', 'spining', 'spiting', 'spliting',
'spoiling', 'spoting', 'spraying', 'spreading', 'springing', 'sprouting',
'squashing', 'squeaking', 'squealing', 'squeezing', 'staining', 'stamping',
'standing', 'staring', 'starting', 'staying', 'stealing', 'steering',
'stepping', 'sticking', 'stimulating', 'stinging', 'stinking', 'stirring',
'stitching', 'stoping', 'storing', 'straping', 'streamlining',
'strengthening', 'stretching', 'striding', 'striking', 'stringing',
'stripping', 'striving', 'stroking', 'structuring', 'studying',
'stuffing', 'subleting', 'subtracting', 'succeeding', 'sucking',
'suffering', 'suggesting', 'suiting', 'summarizing', 'supervising',
'supplying', 'supporting', 'supposing', 'surprising', 'surrounding',
'suspecting', 'suspending', 'swearing', 'sweating', 'sweeping', 'swelling',
'swimming', 'swinging', 'switching', 'symbolizing', 'synthesizing',
'systemizing', 'tabulating', 'taking', 'talking', 'taming', 'taping',
'targeting', 'tasting', 'teaching', 'tearing', 'teasing', 'telephoning',
'telling', 'tempting', 'terrifying', 'testing', 'thanking', 'thawing',
'thinking', 'thriving', 'throwing', 'thrusting', 'ticking', 'tickling',
'tying', 'timing', 'tiping', 'tiring', 'touching', 'touring', 'towing',
'tracing', 'trading', 'training', 'transcribing', 'transfering',
'transforming', 'translating', 'transporting', 'traping', 'traveling',
'treading', 'treating', 'trembling', 'tricking', 'triping', 'troting',
'troubling', 'troubleshooting', 'trusting', 'trying', 'tuging', 'tumbling',
'turning', 'tutoring', 'twisting', 'typing', 'undergoing', 'understanding',
'undertaking', 'undressing', 'unfastening', 'unifying', 'uniting',
'unlocking', 'unpacking', 'untidying', 'updating', 'upgrading',
'upholding', 'upseting', 'using', 'utilizing', 'vanishing', 'verbalizing',
'verifying', 'vexing', 'visiting', 'wailing', 'waiting', 'waking',
'walking', 'wandering', 'wanting', 'warming', 'warning', 'washing',
'wasting', 'watching', 'watering', 'waving', 'wearing', 'weaving',
'wedding', 'weeping', 'weighing', 'welcoming', 'wending', 'weting',
'whining', 'whiping', 'whirling', 'whispering', 'whistling', 'wining',
'winding', 'winking', 'wiping', 'wishing', 'withdrawing', 'withholding',
'withstanding', 'wobbling', 'wondering', 'working', 'worrying', 'wrapping',
'wrecking', 'wrestling', 'wriggling', 'wringing', 'writing', 'x-raying',
'yawning', 'yelling', 'zipping', 'zooming']
================================================
FILE: FQ-BigGAN/calculate_inception_moments.py
================================================
''' Calculate Inception Moments
This script iterates over the dataset and calculates the moments of the
activations of the Inception net (needed for FID), and also returns
the Inception Score of the training data.
Note that if you don't shuffle the data, the IS of true data will be under-
estimated as it is label-ordered. By default, the data is not shuffled
so as to reduce non-determinism. '''
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import utils
import inception_utils
from tqdm import tqdm, trange
from argparse import ArgumentParser
def prepare_parser():
usage = 'Calculate and store inception metrics.'
parser = ArgumentParser(description=usage)
parser.add_argument(
'--dataset', type=str, default='I128_hdf5',
help='Which Dataset to train on, out of I128, I256, C10, C100...'
'Append _hdf5 to use the hdf5 version of the dataset. (default: %(default)s)')
parser.add_argument(
'--data_root', type=str, default='data',
help='Default location where data is stored (default: %(default)s)')
parser.add_argument(
'--batch_size', type=int, default=64,
help='Default overall batchsize (default: %(default)s)')
parser.add_argument(
'--parallel', action='store_true', default=False,
help='Train with multiple GPUs (default: %(default)s)')
parser.add_argument(
'--augment', action='store_true', default=False,
help='Augment with random crops and flips (default: %(default)s)')
parser.add_argument(
'--num_workers', type=int, default=8,
help='Number of dataloader workers (default: %(default)s)')
parser.add_argument(
'--shuffle', action='store_true', default=False,
help='Shuffle the data? (default: %(default)s)')
parser.add_argument(
'--seed', type=int, default=0,
help='Random seed to use.')
return parser
def run(config):
# Get loader
config['drop_last'] = False
loaders = utils.get_data_loaders(**config)
# Load inception net
net = inception_utils.load_inception_net(parallel=config['parallel'])
pool, logits, labels = [], [], []
device = 'cuda'
for i, (x, y) in enumerate(tqdm(loaders[0])):
x = x.to(device)
with torch.no_grad():
pool_val, logits_val = net(x)
pool += [np.asarray(pool_val.cpu())]
logits += [np.asarray(F.softmax(logits_val, 1).cpu())]
labels += [np.asarray(y.cpu())]
pool, logits, labels = [np.concatenate(item, 0) for item in [pool, logits, labels]]
# uncomment to save pool, logits, and labels to disk
# print('Saving pool, logits, and labels to disk...')
# np.savez(config['dataset']+'_inception_activations.npz',
# {'pool': pool, 'logits': logits, 'labels': labels})
# Calculate inception metrics and report them
print('Calculating inception metrics...')
IS_mean, IS_std = inception_utils.calculate_inception_score(logits)
print('Training data from dataset %s has IS of %5.5f +/- %5.5f' % (config['dataset'], IS_mean, IS_std))
# Prepare mu and sigma, save to disk. Remove "hdf5" by default
# (the FID code also knows to strip "hdf5")
print('Calculating means and covariances...')
mu, sigma = np.mean(pool, axis=0), np.cov(pool, rowvar=False)
print('Saving calculated means and covariances to disk...')
np.savez(config['dataset'].strip('_hdf5')+'_inception_moments.npz', **{'mu' : mu, 'sigma' : sigma})
def main():
# parse command line
parser = prepare_parser()
config = vars(parser.parse_args())
print(config)
run(config)
if __name__ == '__main__':
main()
================================================
FILE: FQ-BigGAN/datasets.py
================================================
''' Datasets
This file contains definitions for our CIFAR, ImageFolder, and HDF5 datasets
'''
import os
import os.path
import sys
from PIL import Image
import numpy as np
from tqdm import tqdm, trange
import torchvision.datasets as dset
import torchvision.transforms as transforms
from torchvision.datasets.utils import download_url, check_integrity
import torch.utils.data as data
from torch.utils.data import DataLoader
IMG_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm']
def is_image_file(filename):
"""Checks if a file is an image.
Args:
filename (string): path to a file
Returns:
bool: True if the filename ends with a known image extension
"""
filename_lower = filename.lower()
return any(filename_lower.endswith(ext) for ext in IMG_EXTENSIONS)
def find_classes(dir):
classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]
classes.sort()
class_to_idx = {classes[i]: i for i in range(len(classes))}
return classes, class_to_idx
def make_dataset(dir, class_to_idx):
images = []
dir = os.path.expanduser(dir)
for target in tqdm(sorted(os.listdir(dir))):
d = os.path.join(dir, target)
if not os.path.isdir(d):
continue
for root, _, fnames in sorted(os.walk(d)):
for fname in sorted(fnames):
if is_image_file(fname):
path = os.path.join(root, fname)
item = (path, class_to_idx[target])
images.append(item)
return images
def pil_loader(path):
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
def accimage_loader(path):
import accimage
try:
return accimage.Image(path)
except IOError:
# Potentially a decoding problem, fall back to PIL.Image
return pil_loader(path)
def default_loader(path):
from torchvision import get_image_backend
if get_image_backend() == 'accimage':
return accimage_loader(path)
else:
return pil_loader(path)
class ImageFolder(data.Dataset):
"""A generic data loader where the images are arranged in this way: ::
root/dogball/xxx.png
root/dogball/xxy.png
root/dogball/xxz.png
root/cat/123.png
root/cat/nsdf3.png
root/cat/asd932_.png
Args:
root (string): Root directory path.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
target_transform (callable, optional): A function/transform that takes in the
target and transforms it.
loader (callable, optional): A function to load an image given its path.
Attributes:
classes (list): List of the class names.
class_to_idx (dict): Dict with items (class_name, class_index).
imgs (list): List of (image path, class_index) tuples
"""
def __init__(self, root, transform=None, target_transform=None,
loader=default_loader, load_in_mem=False,
index_filename='imagenet_imgs.npz', **kwargs):
classes, class_to_idx = find_classes(root)
# Load pre-computed image directory walk
if os.path.exists(index_filename):
print('Loading pre-saved Index file %s...' % index_filename)
imgs = np.load(index_filename)['imgs']
# If first time, walk the folder directory and save the
# results to a pre-computed file.
else:
print('Generating Index file %s...' % index_filename)
imgs = make_dataset(root, class_to_idx)
np.savez_compressed(index_filename, **{'imgs' : imgs})
if len(imgs) == 0:
raise(RuntimeError("Found 0 images in subfolders of: " + root + "\n"
"Supported image extensions are: " + ",".join(IMG_EXTENSIONS)))
self.root = root
self.imgs = imgs
self.classes = classes
self.class_to_idx = class_to_idx
self.transform = transform
self.target_transform = target_transform
self.loader = loader
self.load_in_mem = load_in_mem
if self.load_in_mem:
print('Loading all images into memory...')
self.data, self.labels = [], []
for index in tqdm(range(len(self.imgs))):
path, target = imgs[index][0], imgs[index][1]
self.data.append(self.transform(self.loader(path)))
self.labels.append(target)
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is class_index of the target class.
"""
if self.load_in_mem:
img = self.data[index]
target = self.labels[index]
else:
path, target = self.imgs[index]
img = self.loader(str(path))
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
# print(img.size(), target)
return img, int(target)
def __len__(self):
return len(self.imgs)
def __repr__(self):
fmt_str = 'Dataset ' + self.__class__.__name__ + '\n'
fmt_str += ' Number of datapoints: {}\n'.format(self.__len__())
fmt_str += ' Root Location: {}\n'.format(self.root)
tmp = ' Transforms (if any): '
fmt_str += '{0}{1}\n'.format(tmp, self.transform.__repr__().replace('\n', '\n' + ' ' * len(tmp)))
tmp = ' Target Transforms (if any): '
fmt_str += '{0}{1}'.format(tmp, self.target_transform.__repr__().replace('\n', '\n' + ' ' * len(tmp)))
return fmt_str
''' ILSVRC_HDF5: A dataset to support I/O from an HDF5 to avoid
having to load individual images all the time. '''
import h5py as h5
import torch
class ILSVRC_HDF5(data.Dataset):
def __init__(self, root, transform=None, target_transform=None,
load_in_mem=False, train=True,download=False, validate_seed=0,
val_split=0, **kwargs): # last four are dummies
self.root = root
self.num_imgs = len(h5.File(root, 'r')['labels'])
# self.transform = transform
self.target_transform = target_transform
# Set the transform here
self.transform = transform
# load the entire dataset into memory?
self.load_in_mem = load_in_mem
# If loading into memory, do so now
if self.load_in_mem:
print('Loading %s into memory...' % root)
with h5.File(root,'r') as f:
self.data = f['imgs'][:]
self.labels = f['labels'][:]
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is class_index of the target class.
"""
# If loaded the entire dataset in RAM, get image from memory
if self.load_in_mem:
img = self.data[index]
target = self.labels[index]
# Else load it from disk
else:
with h5.File(self.root,'r') as f:
img = f['imgs'][index]
target = f['labels'][index]
# if self.transform is not None:
# img = self.transform(img)
# Apply my own transform
img = ((torch.from_numpy(img).float() / 255) - 0.5) * 2
if self.target_transform is not None:
target = self.target_transform(target)
return img, int(target)
def __len__(self):
return self.num_imgs
# return len(self.f['imgs'])
import pickle
class CIFAR10(dset.CIFAR10):
def __init__(self, root, train=True,
transform=None, target_transform=None,
download=True, validate_seed=0,
val_split=0, load_in_mem=True, **kwargs):
self.root = os.path.expanduser(root)
self.transform = transform
self.target_transform = target_transform
self.train = train # training set or test set
self.val_split = val_split
if download:
self.download()
if not self._check_integrity():
raise RuntimeError('Dataset not found or corrupted.' +
' You can use download=True to download it')
# now load the picked numpy arrays
self.data = []
self.labels= []
for fentry in self.train_list:
f = fentry[0]
file = os.path.join(self.root, self.base_folder, f)
fo = open(file, 'rb')
if sys.version_info[0] == 2:
entry = pickle.load(fo)
else:
entry = pickle.load(fo, encoding='latin1')
self.data.append(entry['data'])
if 'labels' in entry:
self.labels += entry['labels']
else:
self.labels += entry['fine_labels']
fo.close()
self.data = np.concatenate(self.data)
# Randomly select indices for validation
if self.val_split > 0:
label_indices = [[] for _ in range(max(self.labels)+1)]
for i,l in enumerate(self.labels):
label_indices[l] += [i]
label_indices = np.asarray(label_indices)
# randomly grab 500 elements of each class
np.random.seed(validate_seed)
self.val_indices = []
for l_i in label_indices:
self.val_indices += list(l_i[np.random.choice(len(l_i), int(len(self.data) * val_split) // (max(self.labels) + 1) ,replace=False)])
if self.train=='validate':
self.data = self.data[self.val_indices]
self.labels = list(np.asarray(self.labels)[self.val_indices])
self.data = self.data.reshape((int(50e3 * self.val_split), 3, 32, 32))
self.data = self.data.transpose((0, 2, 3, 1)) # convert to HWC
elif self.train:
print(np.shape(self.data))
if self.val_split > 0:
self.data = np.delete(self.data,self.val_indices,axis=0)
self.labels = list(np.delete(np.asarray(self.labels),self.val_indices,axis=0))
self.data = self.data.reshape((int(50e3 * (1.-self.val_split)), 3, 32, 32))
self.data = self.data.transpose((0, 2, 3, 1)) # convert to HWC
else:
f = self.test_list[0][0]
file = os.path.join(self.root, self.base_folder, f)
fo = open(file, 'rb')
if sys.version_info[0] == 2:
entry = pickle.load(fo)
else:
entry = pickle.load(fo, encoding='latin1')
self.data = entry['data']
if 'labels' in entry:
self.labels = entry['labels']
else:
self.labels = entry['fine_labels']
fo.close()
self.data = self.data.reshape((10000, 3, 32, 32))
self.data = self.data.transpose((0, 2, 3, 1)) # convert to HWC
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
img, target = self.data[index], self.labels[index]
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.data)
class CIFAR100(CIFAR10):
base_folder = 'cifar-100-python'
url = "http://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz"
filename = "cifar-100-python.tar.gz"
tgz_md5 = 'eb9058c3a382ffc7106e4002c42a8d85'
train_list = [
['train', '16019d7e3df5f24257cddd939b257f8d'],
]
test_list = [
['test', 'f0ef6b0ae62326f3e7ffdfab6717acfc'],
]
================================================
FILE: FQ-BigGAN/inception_tf13.py
================================================
''' Tensorflow inception score code
Derived from https://github.com/openai/improved-gan
Code derived from tensorflow/tensorflow/models/image/imagenet/classify_image.py
THIS CODE REQUIRES TENSORFLOW 1.3 or EARLIER to run in PARALLEL BATCH MODE
To use this code, run sample.py on your model with --sample_npz, and then
pass the experiment name in the --experiment_name.
This code also saves pool3 stats to an npz file for FID calculation
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os.path
import sys
import tarfile
import math
from tqdm import tqdm, trange
from argparse import ArgumentParser
import numpy as np
from six.moves import urllib
import tensorflow as tf
MODEL_DIR = ''
DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
softmax = None
def prepare_parser():
usage = 'Parser for TF1.3- Inception Score scripts.'
parser = ArgumentParser(description=usage)
parser.add_argument(
'--experiment_name', type=str, default='',
help='Which experiment''s samples.npz file to pull and evaluate')
parser.add_argument(
'--experiment_root', type=str, default='samples',
help='Default location where samples are stored (default: %(default)s)')
parser.add_argument(
'--batch_size', type=int, default=500,
help='Default overall batchsize (default: %(default)s)')
return parser
def run(config):
# Inception with TF1.3 or earlier.
# Call this function with list of images. Each of elements should be a
# numpy array with values ranging from 0 to 255.
def get_inception_score(images, splits=10):
assert(type(images) == list)
assert(type(images[0]) == np.ndarray)
assert(len(images[0].shape) == 3)
assert(np.max(images[0]) > 10)
assert(np.min(images[0]) >= 0.0)
inps = []
for img in images:
img = img.astype(np.float32)
inps.append(np.expand_dims(img, 0))
bs = config['batch_size']
with tf.Session() as sess:
preds, pools = [], []
n_batches = int(math.ceil(float(len(inps)) / float(bs)))
for i in trange(n_batches):
inp = inps[(i * bs):min((i + 1) * bs, len(inps))]
inp = np.concatenate(inp, 0)
pred, pool = sess.run([softmax, pool3], {'ExpandDims:0': inp})
preds.append(pred)
pools.append(pool)
preds = np.concatenate(preds, 0)
scores = []
for i in range(splits):
part = preds[(i * preds.shape[0] // splits):((i + 1) * preds.shape[0] // splits), :]
kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0)))
kl = np.mean(np.sum(kl, 1))
scores.append(np.exp(kl))
return np.mean(scores), np.std(scores), np.squeeze(np.concatenate(pools, 0))
# Init inception
def _init_inception():
global softmax, pool3
if not os.path.exists(MODEL_DIR):
os.makedirs(MODEL_DIR)
filename = DATA_URL.split('/')[-1]
filepath = os.path.join(MODEL_DIR, filename)
if not os.path.exists(filepath):
def _progress(count, block_size, total_size):
sys.stdout.write('\r>> Downloading %s %.1f%%' % (
filename, float(count * block_size) / float(total_size) * 100.0))
sys.stdout.flush()
filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
print()
statinfo = os.stat(filepath)
print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
tarfile.open(filepath, 'r:gz').extractall(MODEL_DIR)
with tf.gfile.FastGFile(os.path.join(
MODEL_DIR, 'classify_image_graph_def.pb'), 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
# Works with an arbitrary minibatch size.
with tf.Session() as sess:
pool3 = sess.graph.get_tensor_by_name('pool_3:0')
ops = pool3.graph.get_operations()
for op_idx, op in enumerate(ops):
for o in op.outputs:
shape = o.get_shape()
shape = [s.value for s in shape]
new_shape = []
for j, s in enumerate(shape):
if s == 1 and j == 0:
new_shape.append(None)
else:
new_shape.append(s)
o._shape = tf.TensorShape(new_shape)
w = sess.graph.get_operation_by_name("softmax/logits/MatMul").inputs[1]
logits = tf.matmul(tf.squeeze(pool3), w)
softmax = tf.nn.softmax(logits)
# if softmax is None: # No need to functionalize like this.
_init_inception()
fname = '%s/%s/samples.npz' % (config['experiment_root'], config['experiment_name'])
print('loading %s ...'%fname)
ims = np.load(fname)['x']
import time
t0 = time.time()
inc_mean, inc_std, pool_activations = get_inception_score(list(ims.swapaxes(1,2).swapaxes(2,3)), splits=10)
t1 = time.time()
print('Saving pool to numpy file for FID calculations...')
np.savez('%s/%s/TF_pool.npz' % (config['experiment_root'], config['experiment_name']), **{'pool_mean': np.mean(pool_activations,axis=0), 'pool_var': np.cov(pool_activations, rowvar=False)})
print('Inception took %3f seconds, score of %3f +/- %3f.'%(t1-t0, inc_mean, inc_std))
def main():
# parse command line and run
parser = prepare_parser()
config = vars(parser.parse_args())
print(config)
run(config)
if __name__ == '__main__':
main()
================================================
FILE: FQ-BigGAN/inception_utils.py
================================================
''' Inception utilities
This file contains methods for calculating IS and FID, using either
the original numpy code or an accelerated fully-pytorch version that
uses a fast newton-schulz approximation for the matrix sqrt. There are also
methods for acquiring a desired number of samples from the Generator,
and parallelizing the inbuilt PyTorch inception network.
NOTE that Inception Scores and FIDs calculated using these methods will
*not* be directly comparable to values calculated using the original TF
IS/FID code. You *must* use the TF model if you wish to report and compare
numbers. This code tends to produce IS values that are 5-10% lower than
those obtained through TF.
'''
import numpy as np
from scipy import linalg # For numpy FID
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Parameter as P
from torchvision.models.inception import inception_v3
# Module that wraps the inception network to enable use with dataparallel and
# returning pool features and logits.
class WrapInception(nn.Module):
def __init__(self, net):
super(WrapInception,self).__init__()
self.net = net
self.mean = P(torch.tensor([0.485, 0.456, 0.406]).view(1, -1, 1, 1),
requires_grad=False)
self.std = P(torch.tensor([0.229, 0.224, 0.225]).view(1, -1, 1, 1),
requires_grad=False)
def forward(self, x):
# Normalize x
x = (x + 1.) / 2.0
x = (x - self.mean) / self.std
# Upsample if necessary
if x.shape[2] != 299 or x.shape[3] != 299:
x = F.interpolate(x, size=(299, 299), mode='bilinear', align_corners=True)
# 299 x 299 x 3
x = self.net.Conv2d_1a_3x3(x)
# 149 x 149 x 32
x = self.net.Conv2d_2a_3x3(x)
# 147 x 147 x 32
x = self.net.Conv2d_2b_3x3(x)
# 147 x 147 x 64
x = F.max_pool2d(x, kernel_size=3, stride=2)
# 73 x 73 x 64
x = self.net.Conv2d_3b_1x1(x)
# 73 x 73 x 80
x = self.net.Conv2d_4a_3x3(x)
# 71 x 71 x 192
x = F.max_pool2d(x, kernel_size=3, stride=2)
# 35 x 35 x 192
x = self.net.Mixed_5b(x)
# 35 x 35 x 256
x = self.net.Mixed_5c(x)
# 35 x 35 x 288
x = self.net.Mixed_5d(x)
# 35 x 35 x 288
x = self.net.Mixed_6a(x)
# 17 x 17 x 768
x = self.net.Mixed_6b(x)
# 17 x 17 x 768
x = self.net.Mixed_6c(x)
# 17 x 17 x 768
x = self.net.Mixed_6d(x)
# 17 x 17 x 768
x = self.net.Mixed_6e(x)
# 17 x 17 x 768
# 17 x 17 x 768
x = self.net.Mixed_7a(x)
# 8 x 8 x 1280
x = self.net.Mixed_7b(x)
# 8 x 8 x 2048
x = self.net.Mixed_7c(x)
# 8 x 8 x 2048
pool = torch.mean(x.view(x.size(0), x.size(1), -1), 2)
# 1 x 1 x 2048
logits = self.net.fc(F.dropout(pool, training=False).view(pool.size(0), -1))
# 1000 (num_classes)
return pool, logits
# A pytorch implementation of cov, from Modar M. Alfadly
# https://discuss.pytorch.org/t/covariance-and-gradient-support/16217/2
def torch_cov(m, rowvar=False):
'''Estimate a covariance matrix given data.
Covariance indicates the level to which two variables vary together.
If we examine N-dimensional samples, `X = [x_1, x_2, ... x_N]^T`,
then the covariance matrix element `C_{ij}` is the covariance of
`x_i` and `x_j`. The element `C_{ii}` is the variance of `x_i`.
Args:
m: A 1-D or 2-D array containing multiple variables and observations.
Each row of `m` represents a variable, and each column a single
observation of all those variables.
rowvar: If `rowvar` is True, then each row represents a
variable, with observations in the columns. Otherwise, the
relationship is transposed: each column represents a variable,
while the rows contain observations.
Returns:
The covariance matrix of the variables.
'''
if m.dim() > 2:
raise ValueError('m has more than 2 dimensions')
if m.dim() < 2:
m = m.view(1, -1)
if not rowvar and m.size(0) != 1:
m = m.t()
# m = m.type(torch.double) # uncomment this line if desired
fact = 1.0 / (m.size(1) - 1)
m -= torch.mean(m, dim=1, keepdim=True)
mt = m.t() # if complex: mt = m.t().conj()
return fact * m.matmul(mt).squeeze()
# Pytorch implementation of matrix sqrt, from Tsung-Yu Lin, and Subhransu Maji
# https://github.com/msubhransu/matrix-sqrt
def sqrt_newton_schulz(A, numIters, dtype=None):
with torch.no_grad():
if dtype is None:
dtype = A.type()
batchSize = A.shape[0]
dim = A.shape[1]
normA = A.mul(A).sum(dim=1).sum(dim=1).sqrt()
Y = A.div(normA.view(batchSize, 1, 1).expand_as(A));
I = torch.eye(dim,dim).view(1, dim, dim).repeat(batchSize,1,1).type(dtype)
Z = torch.eye(dim,dim).view(1, dim, dim).repeat(batchSize,1,1).type(dtype)
for i in range(numIters):
T = 0.5*(3.0*I - Z.bmm(Y))
Y = Y.bmm(T)
Z = T.bmm(Z)
sA = Y*torch.sqrt(normA).view(batchSize, 1, 1).expand_as(A)
return sA
# FID calculator from TTUR--consider replacing this with GPU-accelerated cov
# calculations using torch?
def numpy_calculate_frechet_distance(mu1, sigma1, mu2, sigma2, eps=1e-6):
"""Numpy implementation of the Frechet Distance.
Taken from https://github.com/bioinf-jku/TTUR
The Frechet distance between two multivariate Gaussians X_1 ~ N(mu_1, C_1)
and X_2 ~ N(mu_2, C_2) is
d^2 = ||mu_1 - mu_2||^2 + Tr(C_1 + C_2 - 2*sqrt(C_1*C_2)).
Stable version by Dougal J. Sutherland.
Params:
-- mu1 : Numpy array containing the activations of a layer of the
inception net (like returned by the function 'get_predictions')
for generated samples.
-- mu2 : The sample mean over activations, precalculated on an
representive data set.
-- sigma1: The covariance matrix over activations for generated samples.
-- sigma2: The covariance matrix over activations, precalculated on an
representive data set.
Returns:
-- : The Frechet Distance.
"""
mu1 = np.atleast_1d(mu1)
mu2 = np.atleast_1d(mu2)
sigma1 = np.atleast_2d(sigma1)
sigma2 = np.atleast_2d(sigma2)
assert mu1.shape == mu2.shape, \
'Training and test mean vectors have different lengths'
assert sigma1.shape == sigma2.shape, \
'Training and test covariances have different dimensions'
diff = mu1 - mu2
# Product might be almost singular
covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False)
if not np.isfinite(covmean).all():
msg = ('fid calculation produces singular product; '
'adding %s to diagonal of cov estimates') % eps
print(msg)
offset = np.eye(sigma1.shape[0]) * eps
covmean = linalg.sqrtm((sigma1 + offset).dot(sigma2 + offset))
# Numerical error might give slight imaginary component
if np.iscomplexobj(covmean):
print('wat')
if not np.allclose(np.diagonal(covmean).imag, 0, atol=1e-3):
m = np.max(np.abs(covmean.imag))
raise ValueError('Imaginary component {}'.format(m))
covmean = covmean.real
tr_covmean = np.trace(covmean)
out = diff.dot(diff) + np.trace(sigma1) + np.trace(sigma2) - 2 * tr_covmean
return out
def torch_calculate_frechet_distance(mu1, sigma1, mu2, sigma2, eps=1e-6):
"""Pytorch implementation of the Frechet Distance.
Taken from https://github.com/bioinf-jku/TTUR
The Frechet distance between two multivariate Gaussians X_1 ~ N(mu_1, C_1)
and X_2 ~ N(mu_2, C_2) is
d^2 = ||mu_1 - mu_2||^2 + Tr(C_1 + C_2 - 2*sqrt(C_1*C_2)).
Stable version by Dougal J. Sutherland.
Params:
-- mu1 : Numpy array containing the activations of a layer of the
inception net (like returned by the function 'get_predictions')
for generated samples.
-- mu2 : The sample mean over activations, precalculated on an
representive data set.
-- sigma1: The covariance matrix over activations for generated samples.
-- sigma2: The covariance matrix over activations, precalculated on an
representive data set.
Returns:
-- : The Frechet Distance.
"""
assert mu1.shape == mu2.shape, \
'Training and test mean vectors have different lengths'
assert sigma1.shape == sigma2.shape, \
'Training and test covariances have different dimensions'
diff = mu1 - mu2
# Run 50 itrs of newton-schulz to get the matrix sqrt of sigma1 dot sigma2
covmean = sqrt_newton_schulz(sigma1.mm(sigma2).unsqueeze(0), 50).squeeze()
out = (diff.dot(diff) + torch.trace(sigma1) + torch.trace(sigma2)
- 2 * torch.trace(covmean))
return out
# Calculate Inception Score mean + std given softmax'd logits and number of splits
def calculate_inception_score(pred, num_splits=10):
scores = []
for index in range(num_splits):
pred_chunk = pred[index * (pred.shape[0] // num_splits): (index + 1) * (pred.shape[0] // num_splits), :]
kl_inception = pred_chunk * (np.log(pred_chunk) - np.log(np.expand_dims(np.mean(pred_chunk, 0), 0)))
kl_inception = np.mean(np.sum(kl_inception, 1))
scores.append(np.exp(kl_inception))
return np.mean(scores), np.std(scores)
# Loop and run the sampler and the net until it accumulates num_inception_images
# activations. Return the pool, the logits, and the labels (if one wants
# Inception Accuracy the labels of the generated class will be needed)
def accumulate_inception_activations(sample, net, num_inception_images=50000):
pool, logits, labels = [], [], []
while (torch.cat(logits, 0).shape[0] if len(logits) else 0) < num_inception_images:
with torch.no_grad():
images, labels_val = sample()
pool_val, logits_val = net(images.float())
pool += [pool_val]
logits += [F.softmax(logits_val, 1)]
labels += [labels_val]
return torch.cat(pool, 0), torch.cat(logits, 0), torch.cat(labels, 0)
# Load and wrap the Inception model
def load_inception_net(parallel=False):
inception_model = inception_v3(pretrained=True, transform_input=False)
inception_model = WrapInception(inception_model.eval()).cuda()
if parallel:
print('Parallelizing Inception module...')
inception_model = nn.DataParallel(inception_model)
return inception_model
# This produces a function which takes in an iterator which returns a set number of samples
# and iterates until it accumulates config['num_inception_images'] images.
# The iterator can return samples with a different batch size than used in
# training, using the setting confg['inception_batchsize']
def prepare_inception_metrics(dataset, parallel, no_fid=False):
# Load metrics; this is intentionally not in a try-except loop so that
# the script will crash here if it cannot find the Inception moments.
# By default, remove the "hdf5" from dataset
dataset = dataset.strip('_hdf5')
data_mu = np.load(dataset+'_inception_moments.npz')['mu']
data_sigma = np.load(dataset+'_inception_moments.npz')['sigma']
# Load network
net = load_inception_net(parallel)
def get_inception_metrics(sample, num_inception_images, num_splits=10,
prints=True, use_torch=False):
if prints:
print('Gathering activations...')
pool, logits, labels = accumulate_inception_activations(sample, net, num_inception_images)
if prints:
print('Calculating Inception Score...')
IS_mean, IS_std = calculate_inception_score(logits.cpu().numpy(), num_splits)
if no_fid:
FID = 9999.0
else:
if prints:
print('Calculating means and covariances...')
if use_torch:
mu, sigma = torch.mean(pool, 0), torch_cov(pool, rowvar=False)
else:
mu, sigma = np.mean(pool.cpu().numpy(), axis=0), np.cov(pool.cpu().numpy(), rowvar=False)
if prints:
print('Covariances calculated, getting FID...')
if use_torch:
FID = torch_calculate_frechet_distance(mu, sigma, torch.tensor(data_mu).float().cuda(), torch.tensor(data_sigma).float().cuda())
FID = float(FID.cpu().numpy())
else:
FID = numpy_calculate_frechet_distance(mu, sigma, data_mu, data_sigma)
# Delete mu, sigma, pool, logits, and labels, just in case
del mu, sigma, pool, logits, labels
return IS_mean, IS_std, FID
return get_inception_metrics
================================================
FILE: FQ-BigGAN/layers.py
================================================
''' Layers
This file contains various layers for the BigGAN models.
'''
import numpy as np
import torch
import torch.nn as nn
from torch.nn import init
import torch.optim as optim
import torch.nn.functional as F
from torch.nn import Parameter as P
from sync_batchnorm import SynchronizedBatchNorm2d as SyncBN2d
# Projection of x onto y
def proj(x, y):
return torch.mm(y, x.t()) * y / torch.mm(y, y.t())
# Orthogonalize x wrt list of vectors ys
def gram_schmidt(x, ys):
for y in ys:
x = x - proj(x, y)
return x
# Apply num_itrs steps of the power method to estimate top N singular values.
def power_iteration(W, u_, update=True, eps=1e-12):
# Lists holding singular vectors and values
us, vs, svs = [], [], []
for i, u in enumerate(u_):
# Run one step of the power iteration
with torch.no_grad():
v = torch.matmul(u, W)
# Run Gram-Schmidt to subtract components of all other singular vectors
v = F.normalize(gram_schmidt(v, vs), eps=eps)
# Add to the list
vs += [v]
# Update the other singular vector
u = torch.matmul(v, W.t())
# Run Gram-Schmidt to subtract components of all other singular vectors
u = F.normalize(gram_schmidt(u, us), eps=eps)
# Add to the list
us += [u]
if update:
u_[i][:] = u
# Compute this singular value and add it to the list
svs += [torch.squeeze(torch.matmul(torch.matmul(v, W.t()), u.t()))]
#svs += [torch.sum(F.linear(u, W.transpose(0, 1)) * v)]
return svs, us, vs
# Convenience passthrough function
class identity(nn.Module):
def forward(self, input):
return input
# Spectral normalization base class
class SN(object):
def __init__(self, num_svs, num_itrs, num_outputs, transpose=False, eps=1e-12):
# Number of power iterations per step
self.num_itrs = num_itrs
# Number of singular values
self.num_svs = num_svs
# Transposed?
self.transpose = transpose
# Epsilon value for avoiding divide-by-0
self.eps = eps
# Register a singular vector for each sv
for i in range(self.num_svs):
self.register_buffer('u%d' % i, torch.randn(1, num_outputs))
self.register_buffer('sv%d' % i, torch.ones(1))
# Singular vectors (u side)
@property
def u(self):
return [getattr(self, 'u%d' % i) for i in range(self.num_svs)]
# Singular values;
# note that these buffers are just for logging and are not used in training.
@property
def sv(self):
return [getattr(self, 'sv%d' % i) for i in range(self.num_svs)]
# Compute the spectrally-normalized weight
def W_(self):
W_mat = self.weight.view(self.weight.size(0), -1)
if self.transpose:
W_mat = W_mat.t()
# Apply num_itrs power iterations
for _ in range(self.num_itrs):
svs, us, vs = power_iteration(W_mat, self.u, update=self.training, eps=self.eps)
# Update the svs
if self.training:
with torch.no_grad(): # Make sure to do this in a no_grad() context or you'll get memory leaks!
for i, sv in enumerate(svs):
self.sv[i][:] = sv
return self.weight / svs[0]
# 2D Conv layer with spectral norm
class SNConv2d(nn.Conv2d, SN):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True,
num_svs=1, num_itrs=1, eps=1e-12):
nn.Conv2d.__init__(self, in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias)
SN.__init__(self, num_svs, num_itrs, out_channels, eps=eps)
def forward(self, x):
return F.conv2d(x, self.W_(), self.bias, self.stride,
self.padding, self.dilation, self.groups)
# Linear layer with spectral norm
class SNLinear(nn.Linear, SN):
def __init__(self, in_features, out_features, bias=True,
num_svs=1, num_itrs=1, eps=1e-12):
nn.Linear.__init__(self, in_features, out_features, bias)
SN.__init__(self, num_svs, num_itrs, out_features, eps=eps)
def forward(self, x):
return F.linear(x, self.W_(), self.bias)
# Embedding layer with spectral norm
# We use num_embeddings as the dim instead of embedding_dim here
# for convenience sake
class SNEmbedding(nn.Embedding, SN):
def __init__(self, num_embeddings, embedding_dim, padding_idx=None,
max_norm=None, norm_type=2, scale_grad_by_freq=False,
sparse=False, _weight=None,
num_svs=1, num_itrs=1, eps=1e-12):
nn.Embedding.__init__(self, num_embeddings, embedding_dim, padding_idx,
max_norm, norm_type, scale_grad_by_freq,
sparse, _weight)
SN.__init__(self, num_svs, num_itrs, num_embeddings, eps=eps)
def forward(self, x):
return F.embedding(x, self.W_())
# A non-local block as used in SA-GAN
# Note that the implementation as described in the paper is largely incorrect;
# refer to the released code for the actual implementation.
class Attention(nn.Module):
def __init__(self, ch, which_conv=SNConv2d, name='attention'):
super(Attention, self).__init__()
# Channel multiplier
self.ch = ch
self.which_conv = which_conv
self.theta = self.which_conv(self.ch, self.ch // 8, kernel_size=1, padding=0, bias=False)
self.phi = self.which_conv(self.ch, self.ch // 8, kernel_size=1, padding=0, bias=False)
self.g = self.which_conv(self.ch, self.ch // 2, kernel_size=1, padding=0, bias=False)
self.o = self.which_conv(self.ch // 2, self.ch, kernel_size=1, padding=0, bias=False)
# Learnable gain parameter
self.gamma = P(torch.tensor(0.), requires_grad=True)
def forward(self, x, y=None):
# Apply convs
theta = self.theta(x)
phi = F.max_pool2d(self.phi(x), [2,2])
g = F.max_pool2d(self.g(x), [2,2])
# Perform reshapes
theta = theta.view(-1, self. ch // 8, x.shape[2] * x.shape[3])
phi = phi.view(-1, self. ch // 8, x.shape[2] * x.shape[3] // 4)
g = g.view(-1, self. ch // 2, x.shape[2] * x.shape[3] // 4)
# Matmul and softmax to get attention maps
beta = F.softmax(torch.bmm(theta.transpose(1, 2), phi), -1)
# Attention map times g path
o = self.o(torch.bmm(g, beta.transpose(1,2)).view(-1, self.ch // 2, x.shape[2], x.shape[3]))
return self.gamma * o + x
# Fused batchnorm op
def fused_bn(x, mean, var, gain=None, bias=None, eps=1e-5):
# Apply scale and shift--if gain and bias are provided, fuse them here
# Prepare scale
scale = torch.rsqrt(var + eps)
# If a gain is provided, use it
if gain is not None:
scale = scale * gain
# Prepare shift
shift = mean * scale
# If bias is provided, use it
if bias is not None:
shift = shift - bias
return x * scale - shift
#return ((x - mean) / ((var + eps) ** 0.5)) * gain + bias # The unfused way.
# Manual BN
# Calculate means and variances using mean-of-squares minus mean-squared
def manual_bn(x, gain=None, bias=None, return_mean_var=False, eps=1e-5):
# Cast x to float32 if necessary
float_x = x.float()
# Calculate expected value of x (m) and expected value of x**2 (m2)
# Mean of x
m = torch.mean(float_x, [0, 2, 3], keepdim=True)
# Mean of x squared
m2 = torch.mean(float_x ** 2, [0, 2, 3], keepdim=True)
# Calculate variance as mean of squared minus mean squared.
var = (m2 - m **2)
# Cast back to float 16 if necessary
var = var.type(x.type())
m = m.type(x.type())
# Return mean and variance for updating stored mean/var if requested
if return_mean_var:
return fused_bn(x, m, var, gain, bias, eps), m.squeeze(), var.squeeze()
else:
return fused_bn(x, m, var, gain, bias, eps)
# My batchnorm, supports standing stats
class myBN(nn.Module):
def __init__(self, num_channels, eps=1e-5, momentum=0.1):
super(myBN, self).__init__()
# momentum for updating running stats
self.momentum = momentum
# epsilon to avoid dividing by 0
self.eps = eps
# Momentum
self.momentum = momentum
# Register buffers
self.register_buffer('stored_mean', torch.zeros(num_channels))
self.register_buffer('stored_var', torch.ones(num_channels))
self.register_buffer('accumulation_counter', torch.zeros(1))
# Accumulate running means and vars
self.accumulate_standing = False
# reset standing stats
def reset_stats(self):
self.stored_mean[:] = 0
self.stored_var[:] = 0
self.accumulation_counter[:] = 0
def forward(self, x, gain, bias):
if self.training:
out, mean, var = manual_bn(x, gain, bias, return_mean_var=True, eps=self.eps)
# If accumulating standing stats, increment them
if self.accumulate_standing:
self.stored_mean[:] = self.stored_mean + mean.data
self.stored_var[:] = self.stored_var + var.data
self.accumulation_counter += 1.0
# If not accumulating standing stats, take running averages
else:
self.stored_mean[:] = self.stored_mean * (1 - self.momentum) + mean * self.momentum
self.stored_var[:] = self.stored_var * (1 - self.momentum) + var * self.momentum
return out
# If not in training mode, use the stored statistics
else:
mean = self.stored_mean.view(1, -1, 1, 1)
var = self.stored_var.view(1, -1, 1, 1)
# If using standing stats, divide them by the accumulation counter
if self.accumulate_standing:
mean = mean / self.accumulation_counter
var = var / self.accumulation_counter
return fused_bn(x, mean, var, gain, bias, self.eps)
# Simple function to handle groupnorm norm stylization
def groupnorm(x, norm_style):
# If number of channels specified in norm_style:
if 'ch' in norm_style:
ch = int(norm_style.split('_')[-1])
groups = max(int(x.shape[1]) // ch, 1)
# If number of groups specified in norm style
elif 'grp' in norm_style:
groups = int(norm_style.split('_')[-1])
# If neither, default to groups = 16
else:
groups = 16
return F.group_norm(x, groups)
# Class-conditional bn
# output size is the number of channels, input size is for the linear layers
# Andy's Note: this class feels messy but I'm not really sure how to clean it up
# Suggestions welcome! (By which I mean, refactor this and make a pull request
# if you want to make this more readable/usable).
class ccbn(nn.Module):
def __init__(self, output_size, input_size, which_linear, eps=1e-5, momentum=0.1,
cross_replica=False, mybn=False, norm_style='bn',):
super(ccbn, self).__init__()
self.output_size, self.input_size = output_size, input_size
# Prepare gain and bias layers
self.gain = which_linear(input_size, output_size)
self.bias = which_linear(input_size, output_size)
# epsilon to avoid dividing by 0
self.eps = eps
# Momentum
self.momentum = momentum
# Use cross-replica batchnorm?
self.cross_replica = cross_replica
# Use my batchnorm?
self.mybn = mybn
# Norm style?
self.norm_style = norm_style
if self.cross_replica:
self.bn = SyncBN2d(output_size, eps=self.eps, momentum=self.momentum, affine=False)
elif self.mybn:
self.bn = myBN(output_size, self.eps, self.momentum)
elif self.norm_style in ['bn', 'in']:
self.register_buffer('stored_mean', torch.zeros(output_size))
self.register_buffer('stored_var', torch.ones(output_size))
def forward(self, x, y):
# Calculate class-conditional gains and biases
gain = (1 + self.gain(y)).view(y.size(0), -1, 1, 1)
bias = self.bias(y).view(y.size(0), -1, 1, 1)
# If using my batchnorm
if self.mybn or self.cross_replica:
return self.bn(x, gain=gain, bias=bias)
# else:
else:
if self.norm_style == 'bn':
out = F.batch_norm(x, self.stored_mean, self.stored_var, None, None,
self.training, 0.1, self.eps)
elif self.norm_style == 'in':
out = F.instance_norm(x, self.stored_mean, self.stored_var, None, None,
self.training, 0.1, self.eps)
elif self.norm_style == 'gn':
out = groupnorm(x, self.normstyle)
elif self.norm_style == 'nonorm':
out = x
return out * gain + bias
def extra_repr(self):
s = 'out: {output_size}, in: {input_size},'
s +=' cross_replica={cross_replica}'
return s.format(**self.__dict__)
# Normal, non-class-conditional BN
class bn(nn.Module):
def __init__(self, output_size, eps=1e-5, momentum=0.1,
cross_replica=False, mybn=False):
super(bn, self).__init__()
self.output_size= output_size
# Prepare gain and bias layers
self.gain = P(torch.ones(output_size), requires_grad=True)
self.bias = P(torch.zeros(output_size), requires_grad=True)
# epsilon to avoid dividing by 0
self.eps = eps
# Momentum
self.momentum = momentum
# Use cross-replica batchnorm?
self.cross_replica = cross_replica
# Use my batchnorm?
self.mybn = mybn
if self.cross_replica:
self.bn = SyncBN2d(output_size, eps=self.eps, momentum=self.momentum, affine=False)
elif mybn:
self.bn = myBN(output_size, self.eps, self.momentum)
# Register buffers if neither of the above
else:
self.register_buffer('stored_mean', torch.zeros(output_size))
self.register_buffer('stored_var', torch.ones(output_size))
def forward(self, x, y=None):
if self.cross_replica or self.mybn:
gain = self.gain.view(1,-1,1,1)
bias = self.bias.view(1,-1,1,1)
return self.bn(x, gain=gain, bias=bias)
else:
return F.batch_norm(x, self.stored_mean, self.stored_var, self.gain,
self.bias, self.training, self.momentum, self.eps)
# Generator blocks
# Note that this class assumes the kernel size and padding (and any other
# settings) have been selected in the main generator module and passed in
# through the which_conv arg. Similar rules apply with which_bn (the input
# size [which is actually the number of channels of the conditional info] must
# be preselected)
class GBlock(nn.Module):
def __init__(self, in_channels, out_channels,
which_conv=nn.Conv2d, which_bn=bn, activation=None,
upsample=None):
super(GBlock, self).__init__()
self.in_channels, self.out_channels = in_channels, out_channels
self.which_conv, self.which_bn = which_conv, which_bn
self.activation = activation
self.upsample = upsample
# Conv layers
self.conv1 = self.which_conv(self.in_channels, self.out_channels)
self.conv2 = self.which_conv(self.out_channels, self.out_channels)
self.learnable_sc = in_channels != out_channels or upsample
if self.learnable_sc:
self.conv_sc = self.which_conv(in_channels, out_channels,
kernel_size=1, padding=0)
# Batchnorm layers
self.bn1 = self.which_bn(in_channels)
self.bn2 = self.which_bn(out_channels)
# upsample layers
self.upsample = upsample
def forward(self, x, y):
h = self.activation(self.bn1(x, y))
if self.upsample:
h = self.upsample(h)
x = self.upsample(x)
h = self.conv1(h)
h = self.activation(self.bn2(h, y))
h = self.conv2(h)
if self.learnable_sc:
x = self.conv_sc(x)
return h + x
# Residual block for the discriminator
class DBlock(nn.Module):
def __init__(self, in_channels, out_channels, which_conv=SNConv2d, wide=True,
preactivation=False, activation=None, downsample=None,):
super(DBlock, self).__init__()
self.in_channels, self.out_channels = in_channels, out_channels
# If using wide D (as in SA-GAN and BigGAN), change the channel pattern
self.hidden_channels = self.out_channels if wide else self.in_channels
self.which_conv = which_conv
self.preactivation = preactivation
self.activation = activation
self.downsample = downsample
# Conv layers
self.conv1 = self.which_conv(self.in_channels, self.hidden_channels)
self.conv2 = self.which_conv(self.hidden_channels, self.out_channels)
self.learnable_sc = True if (in_channels != out_channels) or downsample else False
if self.learnable_sc:
self.conv_sc = self.which_conv(in_channels, out_channels,
kernel_size=1, padding=0)
def shortcut(self, x):
if self.preactivation:
if self.learnable_sc:
x = self.conv_sc(x)
if self.downsample:
x = self.downsample(x)
else:
if self.downsample:
x = self.downsample(x)
if self.learnable_sc:
x = self.conv_sc(x)
return x
def forward(self, x):
if self.preactivation:
# h = self.activation(x) # NOT TODAY SATAN
# Andy's note: This line *must* be an out-of-place ReLU or it
# will negatively affect the shortcut connection.
h = F.relu(x)
else:
h = x
h = self.conv1(h)
h = self.conv2(self.activation(h))
if self.downsample:
h = self.downsample(h)
return h + self.shortcut(x)
# dogball
================================================
FILE: FQ-BigGAN/losses.py
================================================
import torch
import torch.nn.functional as F
# DCGAN loss
def loss_dcgan_dis(dis_fake, dis_real):
L1 = torch.mean(F.softplus(-dis_real))
L2 = torch.mean(F.softplus(dis_fake))
return L1, L2
def loss_dcgan_gen(dis_fake):
loss = torch.mean(F.softplus(-dis_fake))
return loss
# Hinge Loss
def loss_hinge_dis(dis_fake, dis_real):
loss_real = torch.mean(F.relu(1. - dis_real))
loss_fake = torch.mean(F.relu(1. + dis_fake))
return loss_real, loss_fake
# def loss_hinge_dis(dis_fake, dis_real): # This version returns a single loss
# loss = torch.mean(F.relu(1. - dis_real))
# loss += torch.mean(F.relu(1. + dis_fake))
# return loss
def loss_hinge_gen(dis_fake):
loss = -torch.mean(dis_fake)
return loss
# Default to hinge loss
generator_loss = loss_hinge_gen
discriminator_loss = loss_hinge_dis
================================================
FILE: FQ-BigGAN/make_hdf5.py
================================================
""" Convert dataset to HDF5
This script preprocesses a dataset and saves it (images and labels) to
an HDF5 file for improved I/O. """
import os
import sys
from argparse import ArgumentParser
from tqdm import tqdm, trange
import h5py as h5
import numpy as np
import torch
import torchvision.datasets as dset
import torchvision.transforms as transforms
from torchvision.utils import save_image
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
import utils
def prepare_parser():
usage = 'Parser for ImageNet HDF5 scripts.'
parser = ArgumentParser(description=usage)
parser.add_argument(
'--dataset', type=str, default='I128',
help='Which Dataset to train on, out of I128, I256, C10, C100;'
'Append "_hdf5" to use the hdf5 version for ISLVRC (default: %(default)s)')
parser.add_argument(
'--data_root', type=str, default='data',
help='Default location where data is stored (default: %(default)s)')
parser.add_argument(
'--batch_size', type=int, default=256,
help='Default overall batchsize (default: %(default)s)')
parser.add_argument(
'--num_workers', type=int, default=16,
help='Number of dataloader workers (default: %(default)s)')
parser.add_argument(
'--chunk_size', type=int, default=500,
help='Default overall batchsize (default: %(default)s)')
parser.add_argument(
'--compression', action='store_true', default=False,
help='Use LZF compression? (default: %(default)s)')
return parser
def run(config):
if 'hdf5' in config['dataset']:
raise ValueError('Reading from an HDF5 file which you will probably be '
'about to overwrite! Override this error only if you know '
'what you''re doing!')
# Get image size
config['image_size'] = utils.imsize_dict[config['dataset']]
# Update compression entry
config['compression'] = 'lzf' if config['compression'] else None #No compression; can also use 'lzf'
# Get dataset
kwargs = {'num_workers': config['num_workers'], 'pin_memory': False, 'drop_last': False}
train_loader = utils.get_data_loaders(dataset=config['dataset'],
batch_size=config['batch_size'],
shuffle=False,
data_root=config['data_root'],
use_multiepoch_sampler=False,
**kwargs)[0]
# HDF5 supports chunking and compression. You may want to experiment
# with different chunk sizes to see how it runs on your machines.
# Chunk Size/compression Read speed @ 256x256 Read speed @ 128x128 Filesize @ 128x128 Time to write @128x128
# 1 / None 20/s
# 500 / None ramps up to 77/s 102/s 61GB 23min
# 500 / LZF 8/s 56GB 23min
# 1000 / None 78/s
# 5000 / None 81/s
# auto:(125,1,16,32) / None 11/s 61GB
print('Starting to load %s into an HDF5 file with chunk size %i and compression %s...' % (config['dataset'], config['chunk_size'], config['compression']))
# Loop over train loader
for i,(x,y) in enumerate(tqdm(train_loader)):
# Stick X into the range [0, 255] since it's coming from the train loader
x = (255 * ((x + 1) / 2.0)).byte().numpy()
# Numpyify y
y = y.numpy()
# If we're on the first batch, prepare the hdf5
if i==0:
with h5.File(config['data_root'] + '/ILSVRC%i.hdf5' % config['image_size'], 'w') as f:
print('Producing dataset of len %d' % len(train_loader.dataset))
imgs_dset = f.create_dataset('imgs', x.shape,dtype='uint8', maxshape=(len(train_loader.dataset), 3, config['image_size'], config['image_size']),
chunks=(config['chunk_size'], 3, config['image_size'], config['image_size']), compression=config['compression'])
print('Image chunks chosen as ' + str(imgs_dset.chunks))
imgs_dset[...] = x
labels_dset = f.create_dataset('labels', y.shape, dtype='int64', maxshape=(len(train_loader.dataset),), chunks=(config['chunk_size'],), compression=config['compression'])
print('Label chunks chosen as ' + str(labels_dset.chunks))
labels_dset[...] = y
# Else append to the hdf5
else:
with h5.File(config['data_root'] + '/ILSVRC%i.hdf5' % config['image_size'], 'a') as f:
f['imgs'].resize(f['imgs'].shape[0] + x.shape[0], axis=0)
f['imgs'][-x.shape[0]:] = x
f['labels'].resize(f['labels'].shape[0] + y.shape[0], axis=0)
f['labels'][-y.shape[0]:] = y
def main():
# parse command line and run
parser = prepare_parser()
config = vars(parser.parse_args())
print(config)
run(config)
if __name__ == '__main__':
main()
================================================
FILE: FQ-BigGAN/sample.py
================================================
''' Sample
This script loads a pretrained net and a weightsfile and sample '''
import functools
import math
import numpy as np
from tqdm import tqdm, trange
import torch
import torch.nn as nn
from torch.nn import init
import torch.optim as optim
import torch.nn.functional as F
from torch.nn import Parameter as P
import torchvision
# Import my stuff
import inception_utils
import utils
import losses
def run(config):
# Prepare state dict, which holds things like epoch # and itr #
state_dict = {'itr': 0, 'epoch': 0, 'save_num': 0, 'save_best_num': 0,
'best_IS': 0, 'best_FID': 999999, 'config': config}
# Optionally, get the configuration from the state dict. This allows for
# recovery of the config provided only a state dict and experiment name,
# and can be convenient for writing less verbose sample shell scripts.
if config['config_from_name']:
utils.load_weights(None, None, state_dict, config['weights_root'],
config['experiment_name'], config['load_weights'], None,
strict=False, load_optim=False)
# Ignore items which we might want to overwrite from the command line
for item in state_dict['config']:
if item not in ['z_var', 'base_root', 'batch_size', 'G_batch_size', 'use_ema', 'G_eval_mode']:
config[item] = state_dict['config'][item]
# update config (see train.py for explanation)
config['resolution'] = utils.imsize_dict[config['dataset']]
config['n_classes'] = utils.nclass_dict[config['dataset']]
config['G_activation'] = utils.activation_dict[config['G_nl']]
config['D_activation'] = utils.activation_dict[config['D_nl']]
config = utils.update_config_roots(config)
config['skip_init'] = True
config['no_optim'] = True
device = 'cuda'
# Seed RNG
utils.seed_rng(config['seed'])
# Setup cudnn.benchmark for free speed
torch.backends.cudnn.benchmark = True
# Import the model--this line allows us to dynamically select different files.
model = __import__(config['model'])
experiment_name = (config['experiment_name'] if config['experiment_name']
else utils.name_from_config(config))
print('Experiment name is %s' % experiment_name)
G = model.Generator(**config).cuda()
utils.count_parameters(G)
# Load weights
print('Loading weights...')
# Here is where we deal with the ema--load ema weights or load normal weights
utils.load_weights(G if not (config['use_ema']) else None, None, state_dict,
config['weights_root'], experiment_name, config['load_weights'],
G if config['ema'] and config['use_ema'] else None,
strict=False, load_optim=False)
# Update batch size setting used for G
G_batch_size = max(config['G_batch_size'], config['batch_size'])
z_, y_ = utils.prepare_z_y(G_batch_size, G.dim_z, config['n_classes'],
device=device, fp16=config['G_fp16'],
z_var=config['z_var'])
if config['G_eval_mode']:
print('Putting G in eval mode..')
G.eval()
else:
print('G is in %s mode...' % ('training' if G.training else 'eval'))
#Sample function
sample = functools.partial(utils.sample, G=G, z_=z_, y_=y_, config=config)
if config['accumulate_stats']:
print('Accumulating standing stats across %d accumulations...' % config['num_standing_accumulations'])
utils.accumulate_standing_stats(G, z_, y_, config['n_classes'],
config['num_standing_accumulations'])
# Sample a number of images and save them to an NPZ, for use with TF-Inception
if config['sample_npz']:
# Lists to hold images and labels for images
x, y = [], []
print('Sampling %d images and saving them to npz...' % config['sample_num_npz'])
for i in trange(int(np.ceil(config['sample_num_npz'] / float(G_batch_size)))):
with torch.no_grad():
images, labels = sample()
x += [np.uint8(255 * (images.cpu().numpy() + 1) / 2.)]
y += [labels.cpu().numpy()]
x = np.concatenate(x, 0)[:config['sample_num_npz']]
y = np.concatenate(y, 0)[:config['sample_num_npz']]
print('Images shape: %s, Labels shape: %s' % (x.shape, y.shape))
npz_filename = '%s/%s/samples.npz' % (config['samples_root'], experiment_name)
print('Saving npz to %s...' % npz_filename)
np.savez(npz_filename, **{'x' : x, 'y' : y})
# Prepare sample sheets
if config['sample_sheets']:
print('Preparing conditional sample sheets...')
utils.sample_sheet(G, classes_per_sheet=utils.classes_per_sheet_dict[config['dataset']],
num_classes=config['n_classes'],
samples_per_class=10, parallel=config['parallel'],
samples_root=config['samples_root'],
experiment_name=experiment_name,
folder_number=config['sample_sheet_folder_num'],
z_=z_,)
# Sample interp sheets
if config['sample_interps']:
print('Preparing interp sheets...')
for fix_z, fix_y in zip([False, False, True], [False, True, False]):
utils.interp_sheet(G, num_per_sheet=16, num_midpoints=8,
num_classes=config['n_classes'],
parallel=config['parallel'],
samples_root=config['samples_root'],
experiment_name=experiment_name,
folder_number=config['sample_sheet_folder_num'],
sheet_number=0,
fix_z=fix_z, fix_y=fix_y, device='cuda')
# Sample random sheet
if config['sample_random']:
print('Preparing random sample sheet...')
images, labels = sample()
torchvision.utils.save_image(images.float(),
'%s/%s/random_samples.jpg' % (config['samples_root'], experiment_name),
nrow=int(G_batch_size**0.5),
normalize=True)
# Get Inception Score and FID
get_inception_metrics = inception_utils.prepare_inception_metrics(config['dataset'], config['parallel'], config['no_fid'])
# Prepare a simple function get metrics that we use for trunc curves
def get_metrics():
sample = functools.partial(utils.sample, G=G, z_=z_, y_=y_, config=config)
IS_mean, IS_std, FID = get_inception_metrics(sample, config['num_inception_images'], num_splits=10, prints=False)
# Prepare output string
outstring = 'Using %s weights ' % ('ema' if config['use_ema'] else 'non-ema')
outstring += 'in %s mode, ' % ('eval' if config['G_eval_mode'] else 'training')
outstring += 'with noise variance %3.3f, ' % z_.var
outstring += 'over %d images, ' % config['num_inception_images']
if config['accumulate_stats'] or not config['G_eval_mode']:
outstring += 'with batch size %d, ' % G_batch_size
if config['accumulate_stats']:
outstring += 'using %d standing stat accumulations, ' % config['num_standing_accumulations']
outstring += 'Itr %d: PYTORCH UNOFFICIAL Inception Score is %3.3f +/- %3.3f, PYTORCH UNOFFICIAL FID is %5.4f' % (state_dict['itr'], IS_mean, IS_std, FID)
print(outstring)
if config['sample_inception_metrics']:
print('Calculating Inception metrics...')
get_metrics()
# Sample truncation curve stuff. This is basically the same as the inception metrics code
if config['sample_trunc_curves']:
start, step, end = [float(item) for item in config['sample_trunc_curves'].split('_')]
print('Getting truncation values for variance in range (%3.3f:%3.3f:%3.3f)...' % (start, step, end))
for var in np.arange(start, end + step, step):
z_.var = var
# Optionally comment this out if you want to run with standing stats
# accumulated at one z variance setting
if config['accumulate_stats']:
utils.accumulate_standing_stats(G, z_, y_, config['n_classes'],
config['num_standing_accumulations'])
get_metrics()
def main():
# parse command line and run
parser = utils.prepare_parser()
parser = utils.add_sample_parser(parser)
config = vars(parser.parse_args())
print(config)
run(config)
if __name__ == '__main__':
main()
================================================
FILE: FQ-BigGAN/scripts/launch_C10.sh
================================================
#!/bin/bash
#export CUDA_VISIBLE_DEVICES=0,1
python3 train.py --shuffle --batch_size 64 --parallel \
--num_G_accumulations 1 --num_D_accumulations 1 --num_epochs 500 \
--num_D_steps 4 --G_lr 2e-4 \
--D_lr 2e-4 --dataset C10 --G_ortho 0.0 \
--G_attn 0 --D_attn 0 --G_init N02 --D_init N02 \
--ema --use_ema --ema_start 1000 \
--test_every 1000 --save_every 1000 \
--num_best_copies 5 --num_save_copies 2 --seed 0 \
--discrete_layer 0123 --commitment 1.0 --dict_size 10 --dict_decay 0.8 \
--name_suffix quant
================================================
FILE: FQ-BigGAN/scripts/launch_C100.sh
================================================
#!/bin/bash
#export CUDA_VISIBLE_DEVICES=2
python3 train.py --shuffle --batch_size 64 --parallel \
--num_G_accumulations 1 --num_D_accumulations 1 --num_epochs 500 \
--num_D_steps 4 --G_lr 2e-4 \
--D_lr 2e-4 --dataset C100 --G_ortho 0.0 \
--G_attn 0 --D_attn 0 --G_init N02 --D_init N02 \
--ema --use_ema --ema_start 1000 \
--test_every 2000 --save_every 1000 \
--num_best_copies 5 --num_save_copies 2 --seed 0 \
--discrete_layer 0123 --commitment 10.0 --dict_size 6 --dict_decay 0.9 \
--name_suffix quant
================================================
FILE: FQ-BigGAN/scripts/launch_I128_bs256x4.sh
================================================
#!/bin/bash
# export CUDA_VISIBLE_DEVICES=1,2
python train.py \
--dataset I128_hdf5 --parallel --shuffle --num_workers 8 --batch_size 256 --load_in_mem \
--num_G_accumulations 4 --num_D_accumulations 4 \
--num_D_steps 1 --G_lr 1e-4 --D_lr 4e-4 --D_B2 0.999 --G_B2 0.999 \
--G_attn 64 --D_attn 64 \
--G_nl inplace_relu --D_nl inplace_relu \
--SN_eps 1e-6 --BN_eps 1e-5 --adam_eps 1e-6 \
--G_ortho 0.0 \
--hier --dim_z 120 \
--G_eval_mode \
--G_ch 64 --D_ch 64 \
--ema --use_ema --ema_start 20000 \
--test_every 1000 --save_every 1000 --num_best_copies 5 --num_save_copies 2 --seed 0 \
--discrete_layer 0123 --commitment 15.0 --dict_size 10 --dict_decay 0.8 \
--use_multiepoch_sampler --name_suffix quant
================================================
FILE: FQ-BigGAN/scripts/launch_I64_bs128x4.sh
================================================
#!/bin/bash
export CUDA_VISIBLE_DEVICES=1,2
python train.py \
--dataset I64_hdf5 --parallel --shuffle --num_workers 8 --batch_size 128 --load_in_mem \
--num_G_accumulations 4 --num_D_accumulations 4 \
--num_D_steps 1 --G_lr 1e-4 --D_lr 4e-4 --D_B2 0.999 --G_B2 0.999 \
--G_attn 32 --D_attn 32 \
--G_nl inplace_relu --D_nl inplace_relu \
--SN_eps 1e-6 --BN_eps 1e-5 --adam_eps 1e-6 \
--G_ortho 0.0 \
--G_shared \
--G_init ortho --D_init ortho \
--hier --dim_z 120 --shared_dim 128 \
--G_eval_mode \
--G_ch 64 --D_ch 64 \
--ema --use_ema --ema_start 20000 \
--test_every 1000 --save_every 1000 --num_best_copies 5 --num_save_copies 2 --seed 0 \
--discrete_layer 2 --commitment 0.5 --dict_size 10 --dict_decay 0.7 \
--use_multiepoch_sampler --name_suffix test
================================================
FILE: FQ-BigGAN/scripts/utils/duplicate.sh
================================================
#duplicate.sh
source=BigGAN_I128_hdf5_seed0_Gch64_Dch64_bs256_Glr1.0e-04_Dlr4.0e-04_Gnlinplace_relu_Dnlinplace_relu_Ginitxavier_Dinitxavier_Gshared_alex0
target=BigGAN_I128_hdf5_seed0_Gch64_Dch64_bs256_Glr1.0e-04_Dlr4.0e-04_Gnlinplace_relu_Dnlinplace_relu_Ginitxavier_Dinitxavier_Gshared_alex0A
logs_root=logs
weights_root=weights
echo "copying ${source} to ${target}"
cp -r ${logs_root}/${source} ${logs_root}/${target}
cp ${logs_root}/${source}_log.jsonl ${logs_root}/${target}_log.jsonl
cp ${weights_root}/${source}_G.pth ${weights_root}/${target}_G.pth
cp ${weights_root}/${source}_G_ema.pth ${weights_root}/${target}_G_ema.pth
cp ${weights_root}/${source}_D.pth ${weights_root}/${target}_D.pth
cp ${weights_root}/${source}_G_optim.pth ${weights_root}/${target}_G_optim.pth
cp ${weights_root}/${source}_D_optim.pth ${weights_root}/${target}_D_optim.pth
cp ${weights_root}/${source}_state_dict.pth ${weights_root}/${target}_state_dict.pth
================================================
FILE: FQ-BigGAN/scripts/utils/prepare_data.sh
================================================
#!/bin/bash
# export CUDA_VISIBLE_DEVICES=3
python make_hdf5.py --dataset C100 --batch_size 256 --data_root data
python calculate_inception_moments.py --dataset C100 --data_root data --batch_size 128
================================================
FILE: FQ-BigGAN/scripts/utils/trans.py
================================================
filename = 'prepare_data.sh'
fileCont = open(filename, 'r').read()
f = open(filename, 'w', newline='\n')
f.write(fileCont)
f.close()
================================================
FILE: FQ-BigGAN/sync_batchnorm/__init__.py
================================================
# -*- coding: utf-8 -*-
# File : __init__.py
# Author : Jiayuan Mao
# Email : maojiayuan@gmail.com
# Date : 27/01/2018
#
# This file is part of Synchronized-BatchNorm-PyTorch.
# https://github.com/vacancy/Synchronized-BatchNorm-PyTorch
# Distributed under MIT License.
from .batchnorm import SynchronizedBatchNorm1d, SynchronizedBatchNorm2d, SynchronizedBatchNorm3d
from .replicate import DataParallelWithCallback, patch_replication_callback
================================================
FILE: FQ-BigGAN/sync_batchnorm/batchnorm.py
================================================
# -*- coding: utf-8 -*-
# File : batchnorm.py
# Author : Jiayuan Mao
# Email : maojiayuan@gmail.com
# Date : 27/01/2018
#
# This file is part of Synchronized-BatchNorm-PyTorch.
# https://github.com/vacancy/Synchronized-BatchNorm-PyTorch
# Distributed under MIT License.
import collections
import torch
import torch.nn.functional as F
from torch.nn.modules.batchnorm import _BatchNorm
from torch.nn.parallel._functions import ReduceAddCoalesced, Broadcast
from .comm import SyncMaster
__all__ = ['SynchronizedBatchNorm1d', 'SynchronizedBatchNorm2d', 'SynchronizedBatchNorm3d']
def _sum_ft(tensor):
"""sum over the first and last dimention"""
return tensor.sum(dim=0).sum(dim=-1)
def _unsqueeze_ft(tensor):
"""add new dementions at the front and the tail"""
return tensor.unsqueeze(0).unsqueeze(-1)
_ChildMessage = collections.namedtuple('_ChildMessage', ['sum', 'ssum', 'sum_size'])
_MasterMessage = collections.namedtuple('_MasterMessage', ['sum', 'inv_std'])
# _MasterMessage = collections.namedtuple('_MasterMessage', ['sum', 'ssum', 'sum_size'])
class _SynchronizedBatchNorm(_BatchNorm):
def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True):
super(_SynchronizedBatchNorm, self).__init__(num_features, eps=eps, momentum=momentum, affine=affine)
self._sync_master = SyncMaster(self._data_parallel_master)
self._is_parallel = False
self._parallel_id = None
self._slave_pipe = None
def forward(self, input, gain=None, bias=None):
# If it is not parallel computation or is in evaluation mode, use PyTorch's implementation.
if not (self._is_parallel and self.training):
out = F.batch_norm(
input, self.running_mean, self.running_var, self.weight, self.bias,
self.training, self.momentum, self.eps)
if gain is not None:
out = out + gain
if bias is not None:
out = out + bias
return out
# Resize the input to (B, C, -1).
input_shape = input.size()
# print(input_shape)
input = input.view(input.size(0), input.size(1), -1)
# Compute the sum and square-sum.
sum_size = input.size(0) * input.size(2)
input_sum = _sum_ft(input)
input_ssum = _sum_ft(input ** 2)
# Reduce-and-broadcast the statistics.
# print('it begins')
if self._parallel_id == 0:
mean, inv_std = self._sync_master.run_master(_ChildMessage(input_sum, input_ssum, sum_size))
else:
mean, inv_std = self._slave_pipe.run_slave(_ChildMessage(input_sum, input_ssum, sum_size))
# if self._parallel_id == 0:
# # print('here')
# sum, ssum, num = self._sync_master.run_master(_ChildMessage(input_sum, input_ssum, sum_size))
# else:
# # print('there')
# sum, ssum, num = self._slave_pipe.run_slave(_ChildMessage(input_sum, input_ssum, sum_size))
# print('how2')
# num = sum_size
# print('Sum: %f, ssum: %f, sumsize: %f, insum: %f' %(float(sum.sum().cpu()), float(ssum.sum().cpu()), float(sum_size), float(input_sum.sum().cpu())))
# Fix the graph
# sum = (sum.detach() - input_sum.detach()) + input_sum
# ssum = (ssum.detach() - input_ssum.detach()) + input_ssum
# mean = sum / num
# var = ssum / num - mean ** 2
# # var = (ssum - mean * sum) / num
# inv_std = torch.rsqrt(var + self.eps)
# Compute the output.
if gain is not None:
# print('gaining')
# scale = _unsqueeze_ft(inv_std) * gain.squeeze(-1)
# shift = _unsqueeze_ft(mean) * scale - bias.squeeze(-1)
# output = input * scale - shift
output = (input - _unsqueeze_ft(mean)) * (_unsqueeze_ft(inv_std) * gain.squeeze(-1)) + bias.squeeze(-1)
elif self.affine:
# MJY:: Fuse the multiplication for speed.
output = (input - _unsqueeze_ft(mean)) * _unsqueeze_ft(inv_std * self.weight) + _unsqueeze_ft(self.bias)
else:
output = (input - _unsqueeze_ft(mean)) * _unsqueeze_ft(inv_std)
# Reshape it.
return output.view(input_shape)
def __data_parallel_replicate__(self, ctx, copy_id):
self._is_parallel = True
self._parallel_id = copy_id
# parallel_id == 0 means master device.
if self._parallel_id == 0:
ctx.sync_master = self._sync_master
else:
self._slave_pipe = ctx.sync_master.register_slave(copy_id)
def _data_parallel_master(self, intermediates):
"""Reduce the sum and square-sum, compute the statistics, and broadcast it."""
# Always using same "device order" makes the ReduceAdd operation faster.
# Thanks to:: Tete Xiao (http://tetexiao.com/)
intermediates = sorted(intermediates, key=lambda i: i[1].sum.get_device())
to_reduce = [i[1][:2] for i in intermediates]
to_reduce = [j for i in to_reduce for j in i] # flatten
target_gpus = [i[1].sum.get_device() for i in intermediates]
sum_size = sum([i[1].sum_size for i in intermediates])
sum_, ssum = ReduceAddCoalesced.apply(target_gpus[0], 2, *to_reduce)
mean, inv_std = self._compute_mean_std(sum_, ssum, sum_size)
broadcasted = Broadcast.apply(target_gpus, mean, inv_std)
# print('a')
# print(type(sum_), type(ssum), type(sum_size), sum_.shape, ssum.shape, sum_size)
# broadcasted = Broadcast.apply(target_gpus, sum_, ssum, torch.tensor(sum_size).float().to(sum_.device))
# print('b')
outputs = []
for i, rec in enumerate(intermediates):
outputs.append((rec[0], _MasterMessage(*broadcasted[i*2:i*2+2])))
# outputs.append((rec[0], _MasterMessage(*broadcasted[i*3:i*3+3])))
return outputs
def _compute_mean_std(self, sum_, ssum, size):
"""Compute the mean and standard-deviation with sum and square-sum. This method
also maintains the moving average on the master device."""
assert size > 1, 'BatchNorm computes unbiased standard-deviation, which requires size > 1.'
mean = sum_ / size
sumvar = ssum - sum_ * mean
unbias_var = sumvar / (size - 1)
bias_var = sumvar / size
self.running_mean = (1 - self.momentum) * self.running_mean + self.momentum * mean.data
self.running_var = (1 - self.momentum) * self.running_var + self.momentum * unbias_var.data
return mean, torch.rsqrt(bias_var + self.eps)
# return mean, bias_var.clamp(self.eps) ** -0.5
class SynchronizedBatchNorm1d(_SynchronizedBatchNorm):
r"""Applies Synchronized Batch Normalization over a 2d or 3d input that is seen as a
mini-batch.
.. math::
y = \frac{x - mean[x]}{ \sqrt{Var[x] + \epsilon}} * gamma + beta
This module differs from the built-in PyTorch BatchNorm1d as the mean and
standard-deviation are reduced across all devices during training.
For example, when one uses `nn.DataParallel` to wrap the network during
training, PyTorch's implementation normalize the tensor on each device using
the statistics only on that device, which accelerated the computation and
is also easy to implement, but the statistics might be inaccurate.
Instead, in this synchronized version, the statistics will be computed
over all training samples distributed on multiple devices.
Note that, for one-GPU or CPU-only case, this module behaves exactly same
as the built-in PyTorch implementation.
The mean and standard-deviation are calculated per-dimension over
the mini-batches and gamma and beta are learnable parameter vectors
of size C (where C is the input size).
During training, this layer keeps a running estimate of its computed mean
and variance. The running sum is kept with a default momentum of 0.1.
During evaluation, this running mean/variance is used for normalization.
Because the BatchNorm is done over the `C` dimension, computing statistics
on `(N, L)` slices, it's common terminology to call this Temporal BatchNorm
Args:
num_features: num_features from an expected input of size
`batch_size x num_features [x width]`
eps: a value added to the denominator for numerical stability.
Default: 1e-5
momentum: the value used for the running_mean and running_var
computation. Default: 0.1
affine: a boolean value that when set to ``True``, gives the layer learnable
affine parameters. Default: ``True``
Shape:
- Input: :math:`(N, C)` or :math:`(N, C, L)`
- Output: :math:`(N, C)` or :math:`(N, C, L)` (same shape as input)
Examples:
>>> # With Learnable Parameters
>>> m = SynchronizedBatchNorm1d(100)
>>> # Without Learnable Parameters
>>> m = SynchronizedBatchNorm1d(100, affine=False)
>>> input = torch.autograd.Variable(torch.randn(20, 100))
>>> output = m(input)
"""
def _check_input_dim(self, input):
if input.dim() != 2 and input.dim() != 3:
raise ValueError('expected 2D or 3D input (got {}D input)'
.format(input.dim()))
super(SynchronizedBatchNorm1d, self)._check_input_dim(input)
class SynchronizedBatchNorm2d(_SynchronizedBatchNorm):
r"""Applies Batch Normalization over a 4d input that is seen as a mini-batch
of 3d inputs
.. math::
y = \frac{x - mean[x]}{ \sqrt{Var[x] + \epsilon}} * gamma + beta
This module differs from the built-in PyTorch BatchNorm2d as the mean and
standard-deviation are reduced across all devices during training.
For example, when one uses `nn.DataParallel` to wrap the network during
training, PyTorch's implementation normalize the tensor on each device using
the statistics only on that device, which accelerated the computation and
is also easy to implement, but the statistics might be inaccurate.
Instead, in this synchronized version, the statistics will be computed
over all training samples distributed on multiple devices.
Note that, for one-GPU or CPU-only case, this module behaves exactly same
as the built-in PyTorch implementation.
The mean and standard-deviation are calculated per-dimension over
the mini-batches and gamma and beta are learnable parameter vectors
of size C (where C is the input size).
During training, this layer keeps a running estimate of its computed mean
and variance. The running sum is kept with a default momentum of 0.1.
During evaluation, this running mean/variance is used for normalization.
Because the BatchNorm is done over the `C` dimension, computing statistics
on `(N, H, W)` slices, it's common terminology to call this Spatial BatchNorm
Args:
num_features: num_features from an expected input of
size batch_size x num_features x height x width
eps: a value added to the denominator for numerical stability.
Default: 1e-5
momentum: the value used for the running_mean and running_var
computation. Default: 0.1
affine: a boolean value that when set to ``True``, gives the layer learnable
affine parameters. Default: ``True``
Shape:
- Input: :math:`(N, C, H, W)`
- Output: :math:`(N, C, H, W)` (same shape as input)
Examples:
>>> # With Learnable Parameters
>>> m = SynchronizedBatchNorm2d(100)
>>> # Without Learnable Parameters
>>> m = SynchronizedBatchNorm2d(100, affine=False)
>>> input = torch.autograd.Variable(torch.randn(20, 100, 35, 45))
>>> output = m(input)
"""
def _check_input_dim(self, input):
if input.dim() != 4:
raise ValueError('expected 4D input (got {}D input)'
.format(input.dim()))
super(SynchronizedBatchNorm2d, self)._check_input_dim(input)
class SynchronizedBatchNorm3d(_SynchronizedBatchNorm):
r"""Applies Batch Normalization over a 5d input that is seen as a mini-batch
of 4d inputs
.. math::
y = \frac{x - mean[x]}{ \sqrt{Var[x] + \epsilon}} * gamma + beta
This module differs from the built-in PyTorch BatchNorm3d as the mean and
standard-deviation are reduced across all devices during training.
For example, when one uses `nn.DataParallel` to wrap the network during
training, PyTorch's implementation normalize the tensor on each device using
the statistics only on that device, which accelerated the computation and
is also easy to implement, but the statistics might be inaccurate.
Instead, in this synchronized version, the statistics will be computed
over all training samples distributed on multiple devices.
Note that, for one-GPU or CPU-only case, this module behaves exactly same
as the built-in PyTorch implementation.
The mean and standard-deviation are calculated per-dimension over
the mini-batches and gamma and beta are learnable parameter vectors
of size C (where C is the input size).
During training, this layer keeps a running estimate of its computed mean
and variance. The running sum is kept with a default momentum of 0.1.
During evaluation, this running mean/variance is used for normalization.
Because the BatchNorm is done over the `C` dimension, computing statistics
on `(N, D, H, W)` slices, it's common terminology to call this Volumetric BatchNorm
or Spatio-temporal BatchNorm
Args:
num_features: num_features from an expected input of
size batch_size x num_features x depth x height x width
eps: a value added to the denominator for numerical stability.
Default: 1e-5
momentum: the value used for the running_mean and running_var
computation. Default: 0.1
affine: a boolean value that when set to ``True``, gives the layer learnable
affine parameters. Default: ``True``
Shape:
- Input: :math:`(N, C, D, H, W)`
- Output: :math:`(N, C, D, H, W)` (same shape as input)
Examples:
>>> # With Learnable Parameters
>>> m = SynchronizedBatchNorm3d(100)
>>> # Without Learnable Parameters
>>> m = SynchronizedBatchNorm3d(100, affine=False)
>>> input = torch.autograd.Variable(torch.randn(20, 100, 35, 45, 10))
>>> output = m(input)
"""
def _check_input_dim(self, input):
if input.dim() != 5:
raise ValueError('expected 5D input (got {}D input)'
.format(input.dim()))
super(SynchronizedBatchNorm3d, self)._check_input_dim(input)
================================================
FILE: FQ-BigGAN/sync_batchnorm/batchnorm_reimpl.py
================================================
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# File : batchnorm_reimpl.py
# Author : acgtyrant
# Date : 11/01/2018
#
# This file is part of Synchronized-BatchNorm-PyTorch.
# https://github.com/vacancy/Synchronized-BatchNorm-PyTorch
# Distributed under MIT License.
import torch
import torch.nn as nn
import torch.nn.init as init
__all__ = ['BatchNormReimpl']
class BatchNorm2dReimpl(nn.Module):
"""
A re-implementation of batch normalization, used for testing the numerical
stability.
Author: acgtyrant
See also:
https://github.com/vacancy/Synchronized-BatchNorm-PyTorch/issues/14
"""
def __init__(self, num_features, eps=1e-5, momentum=0.1):
super().__init__()
self.num_features = num_features
self.eps = eps
self.momentum = momentum
self.weight = nn.Parameter(torch.empty(num_features))
self.bias = nn.Parameter(torch.empty(num_features))
self.register_buffer('running_mean', torch.zeros(num_features))
self.register_buffer('running_var', torch.ones(num_features))
self.reset_parameters()
def reset_running_stats(self):
self.running_mean.zero_()
self.running_var.fill_(1)
def reset_parameters(self):
self.reset_running_stats()
init.uniform_(self.weight)
init.zeros_(self.bias)
def forward(self, input_):
batchsize, channels, height, width = input_.size()
numel = batchsize * height * width
input_ = input_.permute(1, 0, 2, 3).contiguous().view(channels, numel)
sum_ = input_.sum(1)
sum_of_square = input_.pow(2).sum(1)
mean = sum_ / numel
sumvar = sum_of_square - sum_ * mean
self.running_mean = (
(1 - self.momentum) * self.running_mean
+ self.momentum * mean.detach()
)
unbias_var = sumvar / (numel - 1)
self.running_var = (
(1 - self.momentum) * self.running_var
+ self.momentum * unbias_var.detach()
)
bias_var = sumvar / numel
inv_std = 1 / (bias_var + self.eps).pow(0.5)
output = (
(input_ - mean.unsqueeze(1)) * inv_std.unsqueeze(1) *
self.weight.unsqueeze(1) + self.bias.unsqueeze(1))
return output.view(channels, batchsize, height, width).permute(1, 0, 2, 3).contiguous()
================================================
FILE: FQ-BigGAN/sync_batchnorm/comm.py
================================================
# -*- coding: utf-8 -*-
# File : comm.py
# Author : Jiayuan Mao
# Email : maojiayuan@gmail.com
# Date : 27/01/2018
#
# This file is part of Synchronized-BatchNorm-PyTorch.
# https://github.com/vacancy/Synchronized-BatchNorm-PyTorch
# Distributed under MIT License.
import queue
import collections
import threading
__all__ = ['FutureResult', 'SlavePipe', 'SyncMaster']
class FutureResult(object):
"""A thread-safe future implementation. Used only as one-to-one pipe."""
def __init__(self):
self._result = None
self._lock = threading.Lock()
self._cond = threading.Condition(self._lock)
def put(self, result):
with self._lock:
assert self._result is None, 'Previous result has\'t been fetched.'
self._result = result
self._cond.notify()
def get(self):
with self._lock:
if self._result is None:
self._cond.wait()
res = self._result
self._result = None
return res
_MasterRegistry = collections.namedtuple('MasterRegistry', ['result'])
_SlavePipeBase = collections.namedtuple('_SlavePipeBase', ['identifier', 'queue', 'result'])
class SlavePipe(_SlavePipeBase):
"""Pipe for master-slave communication."""
def run_slave(self, msg):
self.queue.put((self.identifier, msg))
ret = self.result.get()
self.queue.put(True)
return ret
class SyncMaster(object):
"""An abstract `SyncMaster` object.
- During the replication, as the data parallel will trigger an callback of each module, all slave devices should
call `register(id)` and obtain an `SlavePipe` to communicate with the master.
- During the forward pass, master device invokes `run_master`, all messages from slave devices will be collected,
and passed to a registered callback.
- After receiving the messages, the master device should gather the information and determine to message passed
back to each slave devices.
"""
def __init__(self, master_callback):
"""
Args:
master_callback: a callback to be invoked after having collected messages from slave devices.
"""
self._master_callback = master_callback
self._queue = queue.Queue()
self._registry = collections.OrderedDict()
self._activated = False
def __getstate__(self):
return {'master_callback': self._master_callback}
def __setstate__(self, state):
self.__init__(state['master_callback'])
def register_slave(self, identifier):
"""
Register an slave device.
Args:
identifier: an identifier, usually is the device id.
Returns: a `SlavePipe` object which can be used to communicate with the master device.
"""
if self._activated:
assert self._queue.empty(), 'Queue is not clean before next initialization.'
self._activated = False
self._registry.clear()
future = FutureResult()
self._registry[identifier] = _MasterRegistry(future)
return SlavePipe(identifier, self._queue, future)
def run_master(self, master_msg):
"""
Main entry for the master device in each forward pass.
The messages were first collected from each devices (including the master device), and then
an callback will be invoked to compute the message to be sent back to each devices
(including the master device).
Args:
master_msg: the message that the master want to send to itself. This will be placed as the first
message when calling `master_callback`. For detailed usage, see `_SynchronizedBatchNorm` for an example.
Returns: the message to be sent back to the master device.
"""
self._activated = True
intermediates = [(0, master_msg)]
for i in range(self.nr_slaves):
intermediates.append(
gitextract_joocyhq2/ ├── FQ-BigGAN/ │ ├── BigGAN.py │ ├── BigGANdeep.py │ ├── LICENSE │ ├── TFHub/ │ │ ├── README.md │ │ ├── biggan_v1.py │ │ └── converter.py │ ├── animal_hash.py │ ├── calculate_inception_moments.py │ ├── datasets.py │ ├── inception_tf13.py │ ├── inception_utils.py │ ├── layers.py │ ├── losses.py │ ├── make_hdf5.py │ ├── sample.py │ ├── scripts/ │ │ ├── launch_C10.sh │ │ ├── launch_C100.sh │ │ ├── launch_I128_bs256x4.sh │ │ ├── launch_I64_bs128x4.sh │ │ └── utils/ │ │ ├── duplicate.sh │ │ ├── prepare_data.sh │ │ └── trans.py │ ├── sync_batchnorm/ │ │ ├── __init__.py │ │ ├── batchnorm.py │ │ ├── batchnorm_reimpl.py │ │ ├── comm.py │ │ ├── replicate.py │ │ └── unittest.py │ ├── train.py │ ├── train_fns.py │ ├── utility/ │ │ ├── extract_imagenet.py │ │ └── untar.py │ ├── utils.py │ └── vq_layer.py ├── FQ-StyleGAN/ │ ├── LICENSE.txt │ ├── dataset_tool.py │ ├── dnnlib/ │ │ ├── __init__.py │ │ ├── submission/ │ │ │ ├── __init__.py │ │ │ ├── internal/ │ │ │ │ ├── __init__.py │ │ │ │ └── local.py │ │ │ ├── run_context.py │ │ │ └── submit.py │ │ ├── tflib/ │ │ │ ├── __init__.py │ │ │ ├── autosummary.py │ │ │ ├── custom_ops.py │ │ │ ├── network.py │ │ │ ├── ops/ │ │ │ │ ├── __init__.py │ │ │ │ ├── fused_bias_act.cu │ │ │ │ ├── fused_bias_act.py │ │ │ │ ├── upfirdn_2d.cu │ │ │ │ └── upfirdn_2d.py │ │ │ ├── optimizer.py │ │ │ └── tfutil.py │ │ └── util.py │ ├── metrics/ │ │ ├── __init__.py │ │ ├── frechet_inception_distance.py │ │ ├── inception_score.py │ │ ├── linear_separability.py │ │ ├── metric_base.py │ │ ├── metric_defaults.py │ │ ├── perceptual_path_length.py │ │ └── precision_recall.py │ ├── pretrained_networks.py │ ├── projector.py │ ├── run_generator.py │ ├── run_metrics.py │ ├── run_projector.py │ ├── run_training.py │ ├── test_nvcc │ ├── test_nvcc.cu │ └── training/ │ ├── __init__.py │ ├── dataset.py │ ├── loss.py │ ├── misc.py │ ├── networks_stylegan.py │ ├── networks_stylegan2.py │ └── training_loop.py ├── FQ-U-GAT-IT/ │ ├── LICENSE │ ├── UGATIT.py │ ├── dataset/ │ │ └── download_dataset_1.sh │ ├── download_dataset_2.sh │ ├── logger.py │ ├── main.py │ ├── ops.py │ ├── utils.py │ └── vq_layer.py └── README.md
SYMBOL INDEX (681 symbols across 58 files)
FILE: FQ-BigGAN/BigGAN.py
function G_arch (line 19) | def G_arch(ch=64, attention='64', ksize='333333', dilation='111111'):
class Generator (line 54) | class Generator(nn.Module):
method __init__ (line 55) | def __init__(self, G_ch=64, dim_z=128, bottom_width=4, resolution=128,
method init_weights (line 214) | def init_weights(self):
method forward (line 235) | def forward(self, z, y):
function D_arch (line 260) | def D_arch(ch=64, attention='64',ksize='333333', dilation='111111'):
class Discriminator (line 288) | class Discriminator(nn.Module):
method __init__ (line 289) | def __init__(self, D_ch=64, D_wide=True, resolution=128,
method init_weights (line 385) | def init_weights(self):
method forward (line 402) | def forward(self, x, y=None):
class G_D (line 430) | class G_D(nn.Module):
method __init__ (line 431) | def __init__(self, G, D):
method forward (line 436) | def forward(self, z, gy, x=None, dy=None, train_G=False, return_G_z=Fa...
FILE: FQ-BigGAN/BigGANdeep.py
class GBlock (line 23) | class GBlock(nn.Module):
method __init__ (line 24) | def __init__(self, in_channels, out_channels,
method forward (line 48) | def forward(self, x, y):
function G_arch (line 67) | def G_arch(ch=64, attention='64', ksize='333333', dilation='111111'):
class Generator (line 96) | class Generator(nn.Module):
method __init__ (line 97) | def __init__(self, G_ch=64, G_depth=2, dim_z=128, bottom_width=4, reso...
method init_weights (line 243) | def init_weights(self):
method forward (line 265) | def forward(self, z, y):
class DBlock (line 283) | class DBlock(nn.Module):
method __init__ (line 284) | def __init__(self, in_channels, out_channels, which_conv=layers.SNConv...
method shortcut (line 308) | def shortcut(self, x):
method forward (line 315) | def forward(self, x):
function D_arch (line 331) | def D_arch(ch=64, attention='64',ksize='333333', dilation='111111'):
class Discriminator (line 359) | class Discriminator(nn.Module):
method __init__ (line 361) | def __init__(self, D_ch=64, D_wide=True, D_depth=2, resolution=128,
method init_weights (line 458) | def init_weights(self):
method forward (line 475) | def forward(self, x, y=None):
class G_D (line 492) | class G_D(nn.Module):
method __init__ (line 493) | def __init__(self, G, D):
method forward (line 498) | def forward(self, z, gy, x=None, dy=None, train_G=False, return_G_z=Fa...
FILE: FQ-BigGAN/TFHub/biggan_v1.py
function l2normalize (line 12) | def l2normalize(v, eps=1e-4):
function truncated_z_sample (line 16) | def truncated_z_sample(batch_size, z_dim, truncation=0.5, seed=None):
function denorm (line 22) | def denorm(x):
class SpectralNorm (line 27) | class SpectralNorm(nn.Module):
method __init__ (line 28) | def __init__(self, module, name='weight', power_iterations=1):
method _update_u_v (line 36) | def _update_u_v(self):
method _made_params (line 50) | def _made_params(self):
method _make_params (line 59) | def _make_params(self):
method forward (line 76) | def forward(self, *args):
class SelfAttention (line 81) | class SelfAttention(nn.Module):
method __init__ (line 84) | def __init__(self, in_dim, activation=F.relu):
method forward (line 98) | def forward(self, x):
class ConditionalBatchNorm2d (line 115) | class ConditionalBatchNorm2d(nn.Module):
method __init__ (line 116) | def __init__(self, num_features, num_classes, eps=1e-4, momentum=0.1):
method forward (line 123) | def forward(self, x, y):
class GBlock (line 131) | class GBlock(nn.Module):
method __init__ (line 132) | def __init__(
method forward (line 168) | def forward(self, input, condition=None):
class Generator128 (line 197) | class Generator128(nn.Module):
method __init__ (line 198) | def __init__(self, code_dim=120, n_class=1000, chn=96, debug=False):
method forward (line 226) | def forward(self, input, class_id):
class Generator256 (line 244) | class Generator256(nn.Module):
method __init__ (line 245) | def __init__(self, code_dim=140, n_class=1000, chn=96, debug=False):
method forward (line 272) | def forward(self, input, class_id):
class Generator512 (line 290) | class Generator512(nn.Module):
method __init__ (line 291) | def __init__(self, code_dim=128, n_class=1000, chn=96, debug=False):
method forward (line 321) | def forward(self, input, class_id):
class Discriminator (line 339) | class Discriminator(nn.Module):
method __init__ (line 340) | def __init__(self, n_class=1000, chn=96, debug=False):
method forward (line 376) | def forward(self, input, class_id):
FILE: FQ-BigGAN/TFHub/converter.py
function dump_tfhub_to_hdf5 (line 47) | def dump_tfhub_to_hdf5(module_path, hdf5_path, redownload=False):
class TFHub2Pytorch (line 80) | class TFHub2Pytorch(object):
method __init__ (line 97) | def __init__(self, state_dict, tf_weights, resolution=256, load_ema=Tr...
method load (line 106) | def load(self):
method load_generator (line 110) | def load_generator(self):
method load_linear (line 125) | def load_linear(self, name_pth, name_tf, bias=True):
method load_snlinear (line 130) | def load_snlinear(self, name_pth, name_tf, bias=True):
method load_colorize (line 137) | def load_colorize(self, name_pth, name_tf):
method load_GBlock (line 140) | def load_GBlock(self, name_pth, name_tf):
method load_convs (line 144) | def load_convs(self, name_pth, name_tf):
method load_snconv (line 149) | def load_snconv(self, name_pth, name_tf, bias=True):
method load_conv (line 158) | def load_conv(self, name_pth, name_tf, bias=True):
method load_HyperBNs (line 166) | def load_HyperBNs(self, name_pth, name_tf):
method load_ScaledCrossReplicaBNs (line 170) | def load_ScaledCrossReplicaBNs(self, name_pth, name_tf):
method load_HyperBN (line 178) | def load_HyperBN(self, name_pth, name_tf):
method load_attention (line 197) | def load_attention(self, name_pth, name_tf):
method load_tf_tensor (line 205) | def load_tf_tensor(self, prefix, var, device='0'):
function convert_from_v1 (line 210) | def convert_from_v1(hub_dict, resolution=128):
function get_config (line 289) | def get_config(resolution):
function convert_biggan (line 310) | def convert_biggan(resolution, weight_dir, redownload=False, no_ema=Fals...
function generate_sample (line 334) | def generate_sample(G, z_dim, batch_size, filename, parallel=False):
function parse_args (line 348) | def parse_args():
FILE: FQ-BigGAN/calculate_inception_moments.py
function prepare_parser (line 19) | def prepare_parser():
function run (line 49) | def run(config):
function main (line 82) | def main():
FILE: FQ-BigGAN/datasets.py
function is_image_file (line 20) | def is_image_file(filename):
function find_classes (line 33) | def find_classes(dir):
function make_dataset (line 40) | def make_dataset(dir, class_to_idx):
function pil_loader (line 58) | def pil_loader(path):
function accimage_loader (line 65) | def accimage_loader(path):
function default_loader (line 74) | def default_loader(path):
class ImageFolder (line 82) | class ImageFolder(data.Dataset):
method __init__ (line 107) | def __init__(self, root, transform=None, target_transform=None,
method __getitem__ (line 143) | def __getitem__(self, index):
method __len__ (line 166) | def __len__(self):
method __repr__ (line 169) | def __repr__(self):
class ILSVRC_HDF5 (line 184) | class ILSVRC_HDF5(data.Dataset):
method __init__ (line 185) | def __init__(self, root, transform=None, target_transform=None,
method __getitem__ (line 208) | def __getitem__(self, index):
method __len__ (line 238) | def __len__(self):
class CIFAR10 (line 243) | class CIFAR10(dset.CIFAR10):
method __init__ (line 245) | def __init__(self, root, train=True,
method __getitem__ (line 326) | def __getitem__(self, index):
method __len__ (line 347) | def __len__(self):
class CIFAR100 (line 351) | class CIFAR100(CIFAR10):
FILE: FQ-BigGAN/inception_tf13.py
function prepare_parser (line 29) | def prepare_parser():
function run (line 44) | def run(config):
function main (line 130) | def main():
FILE: FQ-BigGAN/inception_utils.py
class WrapInception (line 27) | class WrapInception(nn.Module):
method __init__ (line 28) | def __init__(self, net):
method forward (line 35) | def forward(self, x):
function torch_cov (line 89) | def torch_cov(m, rowvar=False):
function sqrt_newton_schulz (line 124) | def sqrt_newton_schulz(A, numIters, dtype=None):
function numpy_calculate_frechet_distance (line 144) | def numpy_calculate_frechet_distance(mu1, sigma1, mu2, sigma2, eps=1e-6):
function torch_calculate_frechet_distance (line 200) | def torch_calculate_frechet_distance(mu1, sigma1, mu2, sigma2, eps=1e-6):
function calculate_inception_score (line 235) | def calculate_inception_score(pred, num_splits=10):
function accumulate_inception_activations (line 248) | def accumulate_inception_activations(sample, net, num_inception_images=5...
function load_inception_net (line 261) | def load_inception_net(parallel=False):
function prepare_inception_metrics (line 274) | def prepare_inception_metrics(dataset, parallel, no_fid=False):
FILE: FQ-BigGAN/layers.py
function proj (line 16) | def proj(x, y):
function gram_schmidt (line 21) | def gram_schmidt(x, ys):
function power_iteration (line 28) | def power_iteration(W, u_, update=True, eps=1e-12):
class identity (line 54) | class identity(nn.Module):
method forward (line 55) | def forward(self, input):
class SN (line 60) | class SN(object):
method __init__ (line 61) | def __init__(self, num_svs, num_itrs, num_outputs, transpose=False, ep...
method u (line 77) | def u(self):
method sv (line 83) | def sv(self):
method W_ (line 87) | def W_(self):
class SNConv2d (line 103) | class SNConv2d(nn.Conv2d, SN):
method __init__ (line 104) | def __init__(self, in_channels, out_channels, kernel_size, stride=1,
method forward (line 110) | def forward(self, x):
class SNLinear (line 116) | class SNLinear(nn.Linear, SN):
method __init__ (line 117) | def __init__(self, in_features, out_features, bias=True,
method forward (line 121) | def forward(self, x):
class SNEmbedding (line 128) | class SNEmbedding(nn.Embedding, SN):
method __init__ (line 129) | def __init__(self, num_embeddings, embedding_dim, padding_idx=None,
method forward (line 137) | def forward(self, x):
class Attention (line 144) | class Attention(nn.Module):
method __init__ (line 145) | def __init__(self, ch, which_conv=SNConv2d, name='attention'):
method forward (line 156) | def forward(self, x, y=None):
function fused_bn (line 173) | def fused_bn(x, mean, var, gain=None, bias=None, eps=1e-5):
function manual_bn (line 191) | def manual_bn(x, gain=None, bias=None, return_mean_var=False, eps=1e-5):
class myBN (line 212) | class myBN(nn.Module):
method __init__ (line 213) | def __init__(self, num_channels, eps=1e-5, momentum=0.1):
method reset_stats (line 229) | def reset_stats(self):
method forward (line 234) | def forward(self, x, gain, bias):
function groupnorm (line 259) | def groupnorm(x, norm_style):
class ccbn (line 278) | class ccbn(nn.Module):
method __init__ (line 279) | def __init__(self, output_size, input_size, which_linear, eps=1e-5, mo...
method forward (line 306) | def forward(self, x, y):
method extra_repr (line 327) | def extra_repr(self):
class bn (line 334) | class bn(nn.Module):
method __init__ (line 335) | def __init__(self, output_size, eps=1e-5, momentum=0.1,
method forward (line 360) | def forward(self, x, y=None):
class GBlock (line 376) | class GBlock(nn.Module):
method __init__ (line 377) | def __init__(self, in_channels, out_channels,
method forward (line 399) | def forward(self, x, y):
class DBlock (line 413) | class DBlock(nn.Module):
method __init__ (line 414) | def __init__(self, in_channels, out_channels, which_conv=SNConv2d, wid...
method shortcut (line 432) | def shortcut(self, x):
method forward (line 445) | def forward(self, x):
FILE: FQ-BigGAN/losses.py
function loss_dcgan_dis (line 5) | def loss_dcgan_dis(dis_fake, dis_real):
function loss_dcgan_gen (line 11) | def loss_dcgan_gen(dis_fake):
function loss_hinge_dis (line 17) | def loss_hinge_dis(dis_fake, dis_real):
function loss_hinge_gen (line 27) | def loss_hinge_gen(dis_fake):
FILE: FQ-BigGAN/make_hdf5.py
function prepare_parser (line 20) | def prepare_parser():
function run (line 45) | def run(config):
function main (line 102) | def main():
FILE: FQ-BigGAN/sample.py
function run (line 24) | def run(config):
function main (line 174) | def main():
FILE: FQ-BigGAN/sync_batchnorm/batchnorm.py
function _sum_ft (line 24) | def _sum_ft(tensor):
function _unsqueeze_ft (line 29) | def _unsqueeze_ft(tensor):
class _SynchronizedBatchNorm (line 38) | class _SynchronizedBatchNorm(_BatchNorm):
method __init__ (line 39) | def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True):
method forward (line 48) | def forward(self, input, gain=None, bias=None):
method __data_parallel_replicate__ (line 110) | def __data_parallel_replicate__(self, ctx, copy_id):
method _data_parallel_master (line 120) | def _data_parallel_master(self, intermediates):
method _compute_mean_std (line 147) | def _compute_mean_std(self, sum_, ssum, size):
class SynchronizedBatchNorm1d (line 162) | class SynchronizedBatchNorm1d(_SynchronizedBatchNorm):
method _check_input_dim (line 218) | def _check_input_dim(self, input):
class SynchronizedBatchNorm2d (line 225) | class SynchronizedBatchNorm2d(_SynchronizedBatchNorm):
method _check_input_dim (line 281) | def _check_input_dim(self, input):
class SynchronizedBatchNorm3d (line 288) | class SynchronizedBatchNorm3d(_SynchronizedBatchNorm):
method _check_input_dim (line 345) | def _check_input_dim(self, input):
FILE: FQ-BigGAN/sync_batchnorm/batchnorm_reimpl.py
class BatchNorm2dReimpl (line 18) | class BatchNorm2dReimpl(nn.Module):
method __init__ (line 27) | def __init__(self, num_features, eps=1e-5, momentum=0.1):
method reset_running_stats (line 39) | def reset_running_stats(self):
method reset_parameters (line 43) | def reset_parameters(self):
method forward (line 48) | def forward(self, input_):
FILE: FQ-BigGAN/sync_batchnorm/comm.py
class FutureResult (line 18) | class FutureResult(object):
method __init__ (line 21) | def __init__(self):
method put (line 26) | def put(self, result):
method get (line 32) | def get(self):
class SlavePipe (line 46) | class SlavePipe(_SlavePipeBase):
method run_slave (line 49) | def run_slave(self, msg):
class SyncMaster (line 56) | class SyncMaster(object):
method __init__ (line 67) | def __init__(self, master_callback):
method __getstate__ (line 78) | def __getstate__(self):
method __setstate__ (line 81) | def __setstate__(self, state):
method register_slave (line 84) | def register_slave(self, identifier):
method run_master (line 102) | def run_master(self, master_msg):
method nr_slaves (line 136) | def nr_slaves(self):
FILE: FQ-BigGAN/sync_batchnorm/replicate.py
class CallbackContext (line 23) | class CallbackContext(object):
function execute_replication_callbacks (line 27) | def execute_replication_callbacks(modules):
class DataParallelWithCallback (line 50) | class DataParallelWithCallback(DataParallel):
method replicate (line 64) | def replicate(self, module, device_ids):
function patch_replication_callback (line 70) | def patch_replication_callback(data_parallel):
FILE: FQ-BigGAN/sync_batchnorm/unittest.py
class TorchTestCase (line 15) | class TorchTestCase(unittest.TestCase):
method assertTensorClose (line 16) | def assertTensorClose(self, x, y):
FILE: FQ-BigGAN/train.py
function run (line 34) | def run(config):
function main (line 219) | def main():
FILE: FQ-BigGAN/train_fns.py
function dummy_training_function (line 14) | def dummy_training_function():
function GAN_training_function (line 19) | def GAN_training_function(G, D, GD, z_, y_, ema, state_dict, config):
function save_and_sample (line 106) | def save_and_sample(G, D, G_ema, z_, y_, fixed_z, fixed_y,
function test (line 169) | def test(G, D, G_ema, z_, y_, state_dict, config, sample, get_inception_...
FILE: FQ-BigGAN/utility/extract_imagenet.py
function create_imagenet_ext (line 5) | def create_imagenet_ext(src_path, tgt_path, num_class=20):
FILE: FQ-BigGAN/utils.py
function prepare_parser (line 30) | def prepare_parser():
function add_sample_parser (line 379) | def add_sample_parser(parser):
class CenterCropLongEdge (line 457) | class CenterCropLongEdge(object):
method __call__ (line 464) | def __call__(self, img):
method __repr__ (line 473) | def __repr__(self):
class RandomCropLongEdge (line 476) | class RandomCropLongEdge(object):
method __call__ (line 483) | def __call__(self, img):
method __repr__ (line 498) | def __repr__(self):
class MultiEpochSampler (line 504) | class MultiEpochSampler(torch.utils.data.Sampler):
method __init__ (line 513) | def __init__(self, data_source, num_epochs, start_itr=0, batch_size=128):
method __iter__ (line 524) | def __iter__(self):
method __len__ (line 542) | def __len__(self):
function get_data_loaders (line 547) | def get_data_loaders(dataset, data_root=None, augment=False, batch_size=64,
function seed_rng (line 610) | def seed_rng(seed):
function update_config_roots (line 618) | def update_config_roots(config):
function prepare_root (line 627) | def prepare_root(config):
class ema (line 637) | class ema(object):
method __init__ (line 638) | def __init__(self, source, target, decay=0.9999, start_itr=0):
method update (line 653) | def update(self, itr=None):
function ortho (line 669) | def ortho(model, strength=1e-4, blacklist=[]):
function default_ortho (line 684) | def default_ortho(model, strength=1e-4, blacklist=[]):
function toggle_grad (line 697) | def toggle_grad(model, on_or_off):
function join_strings (line 705) | def join_strings(base_string, strings):
function save_weights (line 710) | def save_weights(G, D, state_dict, weights_root, experiment_name,
function load_weights (line 735) | def load_weights(G, D, state_dict, weights_root, experiment_name,
class MetricsLogger (line 767) | class MetricsLogger(object):
method __init__ (line 768) | def __init__(self, fname, reinitialize=False):
method log (line 776) | def log(self, record=None, **kwargs):
class MyLogger (line 794) | class MyLogger(object):
method __init__ (line 795) | def __init__(self, fname, reinitialize=False, logstyle='%3.3f'):
method reinit (line 804) | def reinit(self, item):
method log (line 816) | def log(self, itr, **kwargs):
function write_metadata (line 834) | def write_metadata(logs_root, experiment_name, config, state_dict):
function progress (line 849) | def progress(items, desc='', total=None, min_delay=0.1, displaytype='s1k'):
function sample (line 888) | def sample(G, z_, y_, config):
function sample_sheet (line 900) | def sample_sheet(G, classes_per_sheet, num_classes, samples_per_class, p...
function interp (line 934) | def interp(x0, x1, num_midpoints):
function interp_sheet (line 941) | def interp_sheet(G, num_per_sheet, num_midpoints, num_classes, parallel,
function print_grad_norms (line 978) | def print_grad_norms(net):
function get_SVs (line 991) | def get_SVs(net, prefix):
function name_from_config (line 999) | def name_from_config(config):
function hashname (line 1052) | def hashname(name):
function query_gpu (line 1063) | def query_gpu(indices):
function count_parameters (line 1068) | def count_parameters(module):
function sample_1hot (line 1074) | def sample_1hot(batch_size, num_classes, device='cuda'):
class Distribution (line 1086) | class Distribution(torch.Tensor):
method init_distribution (line 1088) | def init_distribution(self, dist_type, **kwargs):
method sample_ (line 1096) | def sample_(self):
method to (line 1105) | def to(self, *args, **kwargs):
function prepare_z_y (line 1113) | def prepare_z_y(G_batch_size, dim_z, nclasses, device='cuda',
function initiate_standing_stats (line 1128) | def initiate_standing_stats(net):
function accumulate_standing_stats (line 1135) | def accumulate_standing_stats(net, z, y, nclasses, num_accumulations=16):
class Adam16 (line 1155) | class Adam16(Optimizer):
method __init__ (line 1156) | def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8,weigh...
method load_state_dict (line 1163) | def load_state_dict(self, state_dict):
method step (line 1171) | def step(self, closure=None):
FILE: FQ-BigGAN/vq_layer.py
class Quantize (line 6) | class Quantize(nn.Module):
method __init__ (line 7) | def __init__(self, dim, n_embed, commitment=1.0, decay=0.8, eps=1e-5):
method forward (line 21) | def forward(self, x, y=None):
method embed_code (line 56) | def embed_code(self, embed_id):
FILE: FQ-StyleGAN/dataset_tool.py
function error (line 26) | def error(msg):
class TFRecordExporter (line 32) | class TFRecordExporter:
method __init__ (line 33) | def __init__(self, tfrecord_dir, expected_images, print_progress=True,...
method close (line 50) | def close(self):
method choose_shuffled_order (line 60) | def choose_shuffled_order(self): # Note: Images and labels must be add...
method add_image (line 65) | def add_image(self, img):
method add_labels (line 90) | def add_labels(self, labels):
method __enter__ (line 97) | def __enter__(self):
method __exit__ (line 100) | def __exit__(self, *args):
class ExceptionInfo (line 105) | class ExceptionInfo(object):
method __init__ (line 106) | def __init__(self):
class WorkerThread (line 112) | class WorkerThread(threading.Thread):
method __init__ (line 113) | def __init__(self, task_queue):
method run (line 117) | def run(self):
class ThreadPool (line 130) | class ThreadPool(object):
method __init__ (line 131) | def __init__(self, num_threads):
method add_task (line 141) | def add_task(self, func, args=()):
method get_result (line 147) | def get_result(self, func): # returns (result, args)
method finish (line 154) | def finish(self):
method __enter__ (line 158) | def __enter__(self): # for 'with' statement
method __exit__ (line 161) | def __exit__(self, *excinfo):
method process_items_concurrently (line 164) | def process_items_concurrently(self, item_iterator, process_func=lambd...
function display (line 192) | def display(tfrecord_dir):
function extract (line 218) | def extract(tfrecord_dir, output_dir):
function compare (line 245) | def compare(tfrecord_dir_a, tfrecord_dir_b, ignore_labels):
function create_mnist (line 288) | def create_mnist(tfrecord_dir, mnist_dir):
function create_mnistrgb (line 312) | def create_mnistrgb(tfrecord_dir, mnist_dir, num_images=1000000, random_...
function create_cifar10 (line 329) | def create_cifar10(tfrecord_dir, cifar10_dir):
function create_cifar100 (line 356) | def create_cifar100(tfrecord_dir, cifar100_dir):
function create_svhn (line 378) | def create_svhn(tfrecord_dir, svhn_dir):
function create_lsun (line 405) | def create_lsun(tfrecord_dir, lmdb_dir, resolution=256, max_images=None):
function create_lsun_wide (line 438) | def create_lsun_wide(tfrecord_dir, lmdb_dir, width=512, height=384, max_...
function create_celeba (line 483) | def create_celeba(tfrecord_dir, celeba_dir, cx=89, cy=121):
function create_from_images (line 502) | def create_from_images(tfrecord_dir, image_dir, shuffle):
function create_from_hdf5 (line 530) | def create_from_hdf5(tfrecord_dir, hdf5_filename, shuffle):
function execute_cmdline (line 545) | def execute_cmdline(argv):
FILE: FQ-StyleGAN/dnnlib/submission/internal/local.py
class TargetOptions (line 7) | class TargetOptions():
method __init__ (line 8) | def __init__(self):
class Target (line 11) | class Target():
method __init__ (line 12) | def __init__(self):
method finalize_submit_config (line 15) | def finalize_submit_config(self, submit_config, host_run_dir):
method submit (line 19) | def submit(self, submit_config, host_run_dir):
FILE: FQ-StyleGAN/dnnlib/submission/run_context.py
class RunContext (line 23) | class RunContext(object):
method __init__ (line 35) | def __init__(self, submit_config: submit.SubmitConfig, config_module: ...
method __enter__ (line 57) | def __enter__(self) -> "RunContext":
method __exit__ (line 60) | def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> N...
method update (line 63) | def update(self, loss: Any = 0, cur_epoch: Any = 0, max_epoch: Any = N...
method should_stop (line 74) | def should_stop(self) -> bool:
method get_time_since_start (line 78) | def get_time_since_start(self) -> float:
method get_time_since_last_update (line 82) | def get_time_since_last_update(self) -> float:
method get_last_update_interval (line 86) | def get_last_update_interval(self) -> float:
method close (line 90) | def close(self) -> None:
method get (line 106) | def get():
FILE: FQ-StyleGAN/dnnlib/submission/submit.py
class SubmitTarget (line 29) | class SubmitTarget(Enum):
class PathType (line 37) | class PathType(Enum):
class PlatformExtras (line 49) | class PlatformExtras:
method __init__ (line 57) | def __init__(self):
class SubmitConfig (line 64) | class SubmitConfig(util.EasyDict):
method __init__ (line 87) | def __init__(self):
function get_path_from_template (line 116) | def get_path_from_template(path_template: str, path_type: PathType = Pat...
function get_template_from_path (line 138) | def get_template_from_path(path: str) -> str:
function convert_path (line 144) | def convert_path(path: str, path_type: PathType = PathType.AUTO) -> str:
function set_user_name_override (line 151) | def set_user_name_override(name: str) -> None:
function get_user_name (line 157) | def get_user_name():
function make_run_dir_path (line 173) | def make_run_dir_path(*paths):
function _create_run_dir_local (line 192) | def _create_run_dir_local(submit_config: SubmitConfig) -> str:
function _get_next_run_id_local (line 211) | def _get_next_run_id_local(run_dir_root: str) -> int:
function _populate_run_dir (line 227) | def _populate_run_dir(submit_config: SubmitConfig, run_dir: str) -> None:
function run_wrapper (line 256) | def run_wrapper(submit_config: SubmitConfig) -> None:
function submit_run (line 310) | def submit_run(submit_config: SubmitConfig, run_func_name: str, **run_fu...
FILE: FQ-StyleGAN/dnnlib/tflib/autosummary.py
function _create_var (line 45) | def _create_var(name: str, value_expr: TfExpression) -> TfExpression:
function autosummary (line 77) | def autosummary(name: str, value: TfExpressionEx, passthru: TfExpression...
function finalize_autosummaries (line 118) | def finalize_autosummaries() -> None:
function save_summaries (line 177) | def save_summaries(file_writer, global_step=None):
FILE: FQ-StyleGAN/dnnlib/tflib/custom_ops.py
function _find_compiler_bindir (line 36) | def _find_compiler_bindir():
function _get_compute_cap (line 42) | def _get_compute_cap(device):
function _get_cuda_gpu_arch_string (line 49) | def _get_cuda_gpu_arch_string():
function _run_cmd (line 56) | def _run_cmd(cmd):
function _prepare_nvcc_cli (line 63) | def _prepare_nvcc_cli(opts):
function get_plugin (line 87) | def get_plugin(cuda_file):
FILE: FQ-StyleGAN/dnnlib/tflib/network.py
function import_handler (line 29) | def import_handler(handler_func):
class Network (line 35) | class Network:
method __init__ (line 73) | def __init__(self, name: str = None, func_name: Any = None, **static_k...
method _init_fields (line 100) | def _init_fields(self) -> None:
method _init_graph (line 125) | def _init_graph(self) -> None:
method reset_own_vars (line 187) | def reset_own_vars(self) -> None:
method reset_vars (line 191) | def reset_vars(self) -> None:
method reset_trainables (line 195) | def reset_trainables(self) -> None:
method get_output_for (line 199) | def get_output_for(self, *in_expr: TfExpression, return_as_list: bool ...
method get_var_local_name (line 234) | def get_var_local_name(self, var_or_global_name: Union[TfExpression, s...
method find_var (line 240) | def find_var(self, var_or_local_name: Union[TfExpression, str]) -> TfE...
method get_var (line 245) | def get_var(self, var_or_local_name: Union[TfExpression, str]) -> np.n...
method set_var (line 250) | def set_var(self, var_or_local_name: Union[TfExpression, str], new_val...
method __getstate__ (line 255) | def __getstate__(self) -> dict:
method __setstate__ (line 267) | def __setstate__(self, state: dict) -> None:
method clone (line 301) | def clone(self, name: str = None, **new_static_kwargs) -> "Network":
method copy_own_vars_from (line 316) | def copy_own_vars_from(self, src_net: "Network") -> None:
method copy_vars_from (line 321) | def copy_vars_from(self, src_net: "Network") -> None:
method copy_trainables_from (line 326) | def copy_trainables_from(self, src_net: "Network") -> None:
method convert (line 331) | def convert(self, new_func_name: str, new_name: str = None, **new_stat...
method setup_as_moving_average_of (line 341) | def setup_as_moving_average_of(self, src_net: "Network", beta: TfExpre...
method run (line 353) | def run(self,
method list_ops (line 455) | def list_ops(self) -> List[TfExpression]:
method list_layers (line 463) | def list_layers(self) -> List[Tuple[str, TfExpression, List[TfExpressi...
method print_layers (line 506) | def print_layers(self, title: str = None, hide_layers_with_no_params: ...
method setup_weight_histograms (line 535) | def setup_weight_histograms(self, title: str = None) -> None:
function _handle_legacy_output_transforms (line 555) | def _handle_legacy_output_transforms(output_transform, dynamic_kwargs):
function _legacy_output_transform_func (line 575) | def _legacy_output_transform_func(*expr, out_mul=1.0, out_add=0.0, out_s...
FILE: FQ-StyleGAN/dnnlib/tflib/ops/fused_bias_act.py
function _get_plugin (line 15) | def _get_plugin():
function fused_bias_act (line 34) | def fused_bias_act(x, b=None, axis=1, act='linear', alpha=None, gain=Non...
function _fused_bias_act_ref (line 72) | def _fused_bias_act_ref(x, b, axis, act, alpha, gain):
function _fused_bias_act_cuda (line 100) | def _fused_bias_act_cuda(x, b, axis, act, alpha, gain):
FILE: FQ-StyleGAN/dnnlib/tflib/ops/upfirdn_2d.py
function _get_plugin (line 14) | def _get_plugin():
function upfirdn_2d (line 19) | def upfirdn_2d(x, k, upx=1, upy=1, downx=1, downy=1, padx0=0, padx1=0, p...
function _upfirdn_2d_ref (line 66) | def _upfirdn_2d_ref(x, k, upx, upy, downx, downy, padx0, padx1, pady0, p...
function _upfirdn_2d_cuda (line 105) | def _upfirdn_2d_cuda(x, k, upx, upy, downx, downy, padx0, padx1, pady0, ...
function filter_2d (line 144) | def filter_2d(x, k, gain=1, data_format='NCHW', impl='cuda'):
function upsample_2d (line 169) | def upsample_2d(x, k=None, factor=2, gain=1, data_format='NCHW', impl='c...
function downsample_2d (line 202) | def downsample_2d(x, k=None, factor=2, gain=1, data_format='NCHW', impl=...
function upsample_conv_2d (line 234) | def upsample_conv_2d(x, w, k=None, factor=2, gain=1, data_format='NCHW',...
function conv_downsample_2d (line 296) | def conv_downsample_2d(x, w, k=None, factor=2, gain=1, data_format='NCHW...
function _shape (line 337) | def _shape(tf_expr, dim_idx):
function _setup_kernel (line 344) | def _setup_kernel(k):
function _simple_upfirdn_2d (line 353) | def _simple_upfirdn_2d(x, k, up=1, down=1, pad0=0, pad1=0, data_format='...
FILE: FQ-StyleGAN/dnnlib/tflib/optimizer.py
class Optimizer (line 28) | class Optimizer:
method __init__ (line 40) | def __init__(self,
method _get_device (line 84) | def _get_device(self, device_name: str):
method register_gradients (line 114) | def register_gradients(self, loss: TfExpression, trainable_vars: Union...
method apply_updates (line 156) | def apply_updates(self, allow_no_op: bool = False) -> tf.Operation:
method reset_optimizer_state (line 266) | def reset_optimizer_state(self) -> None:
method get_loss_scaling_var (line 271) | def get_loss_scaling_var(self, device: str) -> Union[tf.Variable, None]:
method apply_loss_scaling (line 275) | def apply_loss_scaling(self, value: TfExpression) -> TfExpression:
method undo_loss_scaling (line 282) | def undo_loss_scaling(self, value: TfExpression) -> TfExpression:
class SimpleAdam (line 290) | class SimpleAdam:
method __init__ (line 293) | def __init__(self, name="Adam", learning_rate=0.001, beta1=0.9, beta2=...
method variables (line 301) | def variables(self):
method compute_gradients (line 304) | def compute_gradients(self, loss, var_list, gate_gradients=tf.train.Op...
method apply_gradients (line 308) | def apply_gradients(self, grads_and_vars):
FILE: FQ-StyleGAN/dnnlib/tflib/tfutil.py
function run (line 28) | def run(*args, **kwargs) -> Any:
function is_tf_expression (line 34) | def is_tf_expression(x: Any) -> bool:
function shape_to_list (line 39) | def shape_to_list(shape: Iterable[tf.Dimension]) -> List[Union[int, None]]:
function flatten (line 44) | def flatten(x: TfExpressionEx) -> TfExpression:
function log2 (line 50) | def log2(x: TfExpressionEx) -> TfExpression:
function exp2 (line 56) | def exp2(x: TfExpressionEx) -> TfExpression:
function lerp (line 62) | def lerp(a: TfExpressionEx, b: TfExpressionEx, t: TfExpressionEx) -> TfE...
function lerp_clip (line 68) | def lerp_clip(a: TfExpressionEx, b: TfExpressionEx, t: TfExpressionEx) -...
function absolute_name_scope (line 74) | def absolute_name_scope(scope: str) -> tf.name_scope:
function absolute_variable_scope (line 79) | def absolute_variable_scope(scope: str, **kwargs) -> tf.variable_scope:
function _sanitize_tf_config (line 84) | def _sanitize_tf_config(config_dict: dict = None) -> dict:
function init_tf (line 107) | def init_tf(config_dict: dict = None) -> None:
function assert_tf_initialized (line 135) | def assert_tf_initialized():
function create_session (line 141) | def create_session(config_dict: dict = None, force_as_default: bool = Fa...
function init_uninitialized_vars (line 164) | def init_uninitialized_vars(target_vars: List[tf.Variable] = None) -> None:
function set_vars (line 194) | def set_vars(var_to_value_dict: dict) -> None:
function create_var_with_large_initial_value (line 220) | def create_var_with_large_initial_value(initial_value: np.ndarray, *args...
function convert_images_from_uint8 (line 230) | def convert_images_from_uint8(images, drange=[-1,1], nhwc_to_nchw=False):
function convert_images_to_uint8 (line 240) | def convert_images_to_uint8(images, drange=[-1,1], nchw_to_nhwc=False, s...
FILE: FQ-StyleGAN/dnnlib/util.py
class EasyDict (line 35) | class EasyDict(dict):
method __getattr__ (line 38) | def __getattr__(self, name: str) -> Any:
method __setattr__ (line 44) | def __setattr__(self, name: str, value: Any) -> None:
method __delattr__ (line 47) | def __delattr__(self, name: str) -> None:
class Logger (line 51) | class Logger(object):
method __init__ (line 54) | def __init__(self, file_name: str = None, file_mode: str = "w", should...
method __enter__ (line 67) | def __enter__(self) -> "Logger":
method __exit__ (line 70) | def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> N...
method write (line 73) | def write(self, text: str) -> None:
method flush (line 86) | def flush(self) -> None:
method close (line 93) | def close(self) -> None:
function format_time (line 111) | def format_time(seconds: Union[int, float]) -> str:
function ask_yes_no (line 125) | def ask_yes_no(question: str) -> bool:
function tuple_product (line 135) | def tuple_product(t: Tuple) -> Any:
function get_dtype_and_ctype (line 159) | def get_dtype_and_ctype(type_obj: Any) -> Tuple[np.dtype, Any]:
function is_pickleable (line 182) | def is_pickleable(obj: Any) -> bool:
function get_module_from_obj_name (line 194) | def get_module_from_obj_name(obj_name: str) -> Tuple[types.ModuleType, s...
function get_obj_from_module (line 235) | def get_obj_from_module(module: types.ModuleType, obj_name: str) -> Any:
function get_obj_by_name (line 245) | def get_obj_by_name(name: str) -> Any:
function call_func_by_name (line 251) | def call_func_by_name(*args, func_name: str = None, **kwargs) -> Any:
function get_module_dir_by_obj_name (line 259) | def get_module_dir_by_obj_name(obj_name: str) -> str:
function is_top_level_function (line 265) | def is_top_level_function(obj: Any) -> bool:
function get_top_level_function_name (line 270) | def get_top_level_function_name(obj: Any) -> str:
function list_dir_recursively_with_ignore (line 279) | def list_dir_recursively_with_ignore(dir_path: str, ignores: List[str] =...
function copy_files_and_create_dirs (line 312) | def copy_files_and_create_dirs(files: List[Tuple[str, str]]) -> None:
function is_url (line 328) | def is_url(obj: Any, allow_file_urls: bool = False) -> bool:
function open_url (line 346) | def open_url(url: str, cache_dir: str = None, num_attempts: int = 10, ve...
FILE: FQ-StyleGAN/metrics/frechet_inception_distance.py
class FID (line 20) | class FID(metric_base.MetricBase):
method __init__ (line 21) | def __init__(self, num_images, minibatch_per_gpu, **kwargs):
method _evaluate (line 26) | def _evaluate(self, Gs, Gs_kwargs, num_gpus):
FILE: FQ-StyleGAN/metrics/inception_score.py
class IS (line 18) | class IS(metric_base.MetricBase):
method __init__ (line 19) | def __init__(self, num_images, num_splits, minibatch_per_gpu, **kwargs):
method _evaluate (line 25) | def _evaluate(self, Gs, Gs_kwargs, num_gpus):
FILE: FQ-StyleGAN/metrics/linear_separability.py
function prob_normalize (line 65) | def prob_normalize(p):
function mutual_information (line 70) | def mutual_information(p):
function entropy (line 84) | def entropy(p):
function conditional_entropy (line 94) | def conditional_entropy(p):
class LS (line 103) | class LS(metric_base.MetricBase):
method __init__ (line 104) | def __init__(self, num_samples, num_keep, attrib_indices, minibatch_pe...
method _evaluate (line 112) | def _evaluate(self, Gs, Gs_kwargs, num_gpus):
FILE: FQ-StyleGAN/metrics/metric_base.py
class MetricBase (line 23) | class MetricBase:
method __init__ (line 24) | def __init__(self, name):
method close (line 34) | def close(self):
method _reset (line 37) | def _reset(self, network_pkl=None, run_dir=None, data_dir=None, datase...
method configure_progress_reports (line 55) | def configure_progress_reports(self, plo, phi, pmax, psec=15):
method run (line 61) | def run(self, network_pkl, run_dir=None, data_dir=None, dataset_args=N...
method get_result_str (line 79) | def get_result_str(self):
method update_autosummaries (line 90) | def update_autosummaries(self):
method _evaluate (line 94) | def _evaluate(self, Gs, Gs_kwargs, num_gpus):
method _report_result (line 97) | def _report_result(self, value, suffix='', fmt='%-10.4f'):
method _report_progress (line 100) | def _report_progress(self, pcur, pmax, status_str=''):
method _get_cache_file_for_reals (line 110) | def _get_cache_file_for_reals(self, extension='pkl', **kwargs):
method _get_dataset_obj (line 119) | def _get_dataset_obj(self):
method _iterate_reals (line 124) | def _iterate_reals(self, minibatch_size):
method _iterate_fakes (line 132) | def _iterate_fakes(self, Gs, minibatch_size, num_gpus):
method _get_random_labels_tf (line 139) | def _get_random_labels_tf(self, minibatch_size):
class MetricGroup (line 145) | class MetricGroup:
method __init__ (line 146) | def __init__(self, metric_kwarg_list):
method run (line 149) | def run(self, *args, **kwargs):
method get_result_str (line 153) | def get_result_str(self):
method update_autosummaries (line 156) | def update_autosummaries(self):
class DummyMetric (line 163) | class DummyMetric(MetricBase):
method _evaluate (line 164) | def _evaluate(self, Gs, Gs_kwargs, num_gpus):
FILE: FQ-StyleGAN/metrics/perceptual_path_length.py
function normalize (line 19) | def normalize(v):
function slerp (line 23) | def slerp(a, b, t):
class PPL (line 34) | class PPL(metric_base.MetricBase):
method __init__ (line 35) | def __init__(self, num_samples, epsilon, space, sampling, crop, miniba...
method _evaluate (line 47) | def _evaluate(self, Gs, Gs_kwargs, num_gpus):
FILE: FQ-StyleGAN/metrics/precision_recall.py
function batch_pairwise_distances (line 20) | def batch_pairwise_distances(U, V):
class DistanceBlock (line 38) | class DistanceBlock():
method __init__ (line 40) | def __init__(self, num_features, num_gpus):
method pairwise_distances (line 55) | def pairwise_distances(self, U, V):
class ManifoldEstimator (line 61) | class ManifoldEstimator():
method __init__ (line 63) | def __init__(self, distance_block, features, row_batch_size, col_batch...
method evaluate (line 96) | def evaluate(self, eval_features, return_realism=False, return_neighbo...
function knn_precision_recall_features (line 138) | def knn_precision_recall_features(ref_features, eval_features, feature_n...
class PR (line 171) | class PR(metric_base.MetricBase):
method __init__ (line 172) | def __init__(self, num_images, nhood_size, minibatch_per_gpu, row_batc...
method _evaluate (line 180) | def _evaluate(self, Gs, Gs_kwargs, num_gpus):
FILE: FQ-StyleGAN/pretrained_networks.py
function get_path_or_url (line 57) | def get_path_or_url(path_or_gdrive_path):
function load_networks (line 64) | def load_networks(path_or_gdrive_path):
FILE: FQ-StyleGAN/projector.py
class Projector (line 16) | class Projector:
method __init__ (line 17) | def __init__(self):
method _info (line 50) | def _info(self, *args):
method set_network (line 54) | def set_network(self, Gs, minibatch_size=1):
method run (line 134) | def run(self, target_images):
method start (line 147) | def start(self, target_images):
method step (line 167) | def step(self):
method get_cur_step (line 194) | def get_cur_step(self):
method get_dlatents (line 197) | def get_dlatents(self):
method get_noises (line 200) | def get_noises(self):
method get_images (line 203) | def get_images(self):
FILE: FQ-StyleGAN/run_generator.py
function generate_images (line 19) | def generate_images(network_pkl, seeds, truncation_psi):
function style_mixing_example (line 40) | def style_mixing_example(network_pkl, row_seeds, col_seeds, truncation_p...
function _parse_num_range (line 90) | def _parse_num_range(s):
function main (line 119) | def main():
FILE: FQ-StyleGAN/run_metrics.py
function run (line 20) | def run(network_pkl, metrics, dataset, data_dir, mirror_augment):
function _str_to_bool (line 31) | def _str_to_bool(v):
function main (line 52) | def main():
FILE: FQ-StyleGAN/run_projector.py
function project_image (line 21) | def project_image(proj, targets, png_prefix, num_snapshots):
function project_generated_images (line 34) | def project_generated_images(network_pkl, seeds, num_snapshots, truncati...
function project_real_images (line 55) | def project_real_images(network_pkl, dataset_name, data_dir, num_images,...
function _parse_num_range (line 73) | def _parse_num_range(s):
function main (line 97) | def main():
FILE: FQ-StyleGAN/run_training.py
function run (line 36) | def run(dataset, data_dir, result_dir, config_id, num_gpus, total_kimg, ...
function _str_to_bool (line 134) | def _str_to_bool(v):
function _parse_comma_sep (line 144) | def _parse_comma_sep(s):
function main (line 166) | def main():
FILE: FQ-StyleGAN/training/dataset.py
class TFRecordDataset (line 19) | class TFRecordDataset:
method __init__ (line 20) | def __init__(self,
method close (line 122) | def close(self):
method configure (line 126) | def configure(self, minibatch_size, lod=0):
method get_minibatch_tf (line 135) | def get_minibatch_tf(self): # => images, labels
method get_minibatch_np (line 139) | def get_minibatch_np(self, minibatch_size, lod=0): # => images, labels
method get_random_labels_tf (line 147) | def get_random_labels_tf(self, minibatch_size): # => labels
method get_random_labels_np (line 155) | def get_random_labels_np(self, minibatch_size): # => labels
method parse_tfrecord_tf (line 162) | def parse_tfrecord_tf(record):
method parse_tfrecord_np (line 171) | def parse_tfrecord_np(record):
function load_dataset (line 181) | def load_dataset(class_name=None, data_dir=None, verbose=False, **kwargs):
FILE: FQ-StyleGAN/training/loss.py
function G_logistic (line 18) | def G_logistic(G, D, opt, training_set, minibatch_size):
function G_logistic_ns (line 27) | def G_logistic_ns(G, D, opt, training_set, minibatch_size):
function D_logistic (line 36) | def D_logistic(G, D, opt, training_set, minibatch_size, reals, labels):
function D_logistic_r1 (line 52) | def D_logistic_r1(G, D, opt, training_set, minibatch_size, reals, labels...
function D_logistic_r2 (line 85) | def D_logistic_r2(G, D, opt, training_set, minibatch_size, reals, labels...
function G_wgan (line 107) | def G_wgan(G, D, opt, training_set, minibatch_size):
function D_wgan (line 116) | def D_wgan(G, D, opt, training_set, minibatch_size, reals, labels, wgan_...
function D_wgan_gp (line 134) | def D_wgan_gp(G, D, opt, training_set, minibatch_size, reals, labels, wg...
function G_logistic_ns_pathreg (line 163) | def G_logistic_ns_pathreg(G, D, opt, training_set, minibatch_size, pl_mi...
FILE: FQ-StyleGAN/training/misc.py
function open_file_or_url (line 20) | def open_file_or_url(file_or_url):
function load_pkl (line 25) | def load_pkl(file_or_url):
function save_pkl (line 29) | def save_pkl(obj, filename):
function adjust_dynamic_range (line 36) | def adjust_dynamic_range(data, drange_in, drange_out):
function create_image_grid (line 43) | def create_image_grid(images, grid_size=None):
function convert_to_pil_image (line 60) | def convert_to_pil_image(image, drange=[0,1]):
function save_image_grid (line 73) | def save_image_grid(images, filename, drange=[0,1], grid_size=None):
function apply_mirror_augment (line 76) | def apply_mirror_augment(minibatch):
function parse_config_for_previous_run (line 85) | def parse_config_for_previous_run(run_dir):
function setup_snapshot_image_grid (line 95) | def setup_snapshot_image_grid(training_set,
FILE: FQ-StyleGAN/training/networks_stylegan.py
function _blur2d (line 21) | def _blur2d(x, f=[1,2,1], normalize=True, flip=False, stride=1):
function _upscale2d (line 50) | def _upscale2d(x, factor=2, gain=1):
function _downscale2d (line 69) | def _downscale2d(x, factor=2, gain=1):
function blur2d (line 95) | def blur2d(x, f=[1,2,1], normalize=True):
function upscale2d (line 107) | def upscale2d(x, factor=2):
function downscale2d (line 119) | def downscale2d(x, factor=2):
function get_weight (line 134) | def get_weight(shape, gain=np.sqrt(2), use_wscale=False, lrmul=1):
function dense (line 153) | def dense(x, fmaps, **kwargs):
function conv2d (line 163) | def conv2d(x, fmaps, kernel, **kwargs):
function upscale2d_conv2d (line 173) | def upscale2d_conv2d(x, fmaps, kernel, fused_scale='auto', **kwargs):
function conv2d_downscale2d (line 192) | def conv2d_downscale2d(x, fmaps, kernel, fused_scale='auto', **kwargs):
function apply_bias (line 212) | def apply_bias(x, lrmul=1):
function leaky_relu (line 222) | def leaky_relu(x, alpha=0.2):
function pixel_norm (line 238) | def pixel_norm(x, epsilon=1e-8):
function instance_norm (line 246) | def instance_norm(x, epsilon=1e-8):
function style_mod (line 260) | def style_mod(x, dlatent, **kwargs):
function apply_noise (line 269) | def apply_noise(x, noise_var=None, randomize_noise=True):
function minibatch_stddev_layer (line 282) | def minibatch_stddev_layer(x, group_size=4, num_new_features=1):
function G_style (line 301) | def G_style(
function G_mapping (line 383) | def G_mapping(
function G_synthesis (line 439) | def G_synthesis(
function D_basic (line 563) | def D_basic(
FILE: FQ-StyleGAN/training/networks_stylegan2.py
function get_weight (line 22) | def get_weight(shape, gain=1, use_wscale=True, lrmul=1, weight_var='weig...
function dense_layer (line 41) | def dense_layer(x, fmaps, gain=1, use_wscale=True, lrmul=1, weight_var='...
function conv2d_layer (line 51) | def conv2d_layer(x, fmaps, kernel, up=False, down=False, resample_kernel...
function apply_bias_act (line 66) | def apply_bias_act(x, act='linear', alpha=None, gain=None, lrmul=1, bias...
function naive_upsample_2d (line 73) | def naive_upsample_2d(x, factor=2):
function naive_downsample_2d (line 80) | def naive_downsample_2d(x, factor=2):
function modulated_conv2d_layer (line 89) | def modulated_conv2d_layer(x, y, fmaps, kernel, up=False, down=False, de...
function minibatch_stddev_layer (line 132) | def minibatch_stddev_layer(x, group_size=4, num_new_features=1):
function G_main (line 151) | def G_main(
function G_mapping (line 251) | def G_mapping(
function G_synthesis_stylegan_revised (line 307) | def G_synthesis_stylegan_revised(
function G_synthesis_stylegan2 (line 417) | def G_synthesis_stylegan2(
function VectorQuantizerEMA (line 512) | def VectorQuantizerEMA(inputs, is_training=True, embedding_dim=512,
function D_stylegan (line 593) | def D_stylegan(
function D_stylegan2 (line 694) | def D_stylegan2(
function D_stylegan2_quant (line 782) | def D_stylegan2_quant(
FILE: FQ-StyleGAN/training/training_loop.py
function process_reals (line 22) | def process_reals(x, labels, lod, mirror_augment, drange_data, drange_net):
function training_schedule (line 47) | def training_schedule(
function training_loop (line 105) | def training_loop(
FILE: FQ-U-GAT-IT/UGATIT.py
class UGATIT (line 12) | class UGATIT(object) :
method __init__ (line 13) | def __init__(self, sess, args):
method model_dir (line 127) | def model_dir(self):
method generator (line 162) | def generator(self, x_init, reuse=False, scope="generator"):
method MLP (line 220) | def MLP(self, x, use_bias=True, reuse=False, scope='MLP'):
method discriminator (line 243) | def discriminator(self, x_init, reuse=False, scope="discriminator"):
method discriminator_global (line 256) | def discriminator_global(self, x_init, reuse=False, scope='discriminat...
method discriminator_local (line 297) | def discriminator_local(self, x_init, reuse=False, scope='discriminato...
method generate_a2b (line 338) | def generate_a2b(self, x_A, reuse=False):
method generate_b2a (line 343) | def generate_b2a(self, x_B, reuse=False):
method discriminate_real (line 348) | def discriminate_real(self, x_A, x_B):
method discriminate_fake (line 357) | def discriminate_fake(self, x_ba, x_ab):
method gradient_panalty (line 367) | def gradient_panalty(self, real, fake, scope="discriminator_A"):
method build_model (line 410) | def build_model(self):
method train (line 564) | def train(self):
method save (line 653) | def save(self, checkpoint_dir, step):
method load (line 667) | def load(self, checkpoint_dir):
method test (line 682) | def test(self, epoch):
FILE: FQ-U-GAT-IT/logger.py
class Logger (line 3) | class Logger(object):
method __init__ (line 4) | def __init__(self, output_file):
method write (line 8) | def write(self, message):
method flush (line 12) | def flush(self):
FILE: FQ-U-GAT-IT/main.py
function parse_args (line 8) | def parse_args():
function check_args (line 66) | def check_args(args):
function main (line 99) | def main():
FILE: FQ-U-GAT-IT/ops.py
function conv (line 16) | def conv(x, channels, kernel=4, stride=2, pad=0, pad_type='zero', use_bi...
function fully_connected_with_w (line 54) | def fully_connected_with_w(x, use_bias=True, sn=False, reuse=False, scop...
function fully_connected (line 82) | def fully_connected(x, units, use_bias=True, sn=False, scope='linear'):
function flatten (line 104) | def flatten(x) :
function resblock (line 111) | def resblock(x_init, channels, use_bias=True, scope='resblock_0'):
function adaptive_ins_layer_resblock (line 124) | def adaptive_ins_layer_resblock(x_init, channels, gamma, beta, use_bias=...
function up_sample (line 142) | def up_sample(x, scale_factor=2):
function global_avg_pooling (line 148) | def global_avg_pooling(x):
function global_max_pooling (line 152) | def global_max_pooling(x):
function lrelu (line 160) | def lrelu(x, alpha=0.01):
function relu (line 165) | def relu(x):
function tanh (line 169) | def tanh(x):
function sigmoid (line 172) | def sigmoid(x) :
function adaptive_instance_layer_norm (line 179) | def adaptive_instance_layer_norm(x, gamma, beta, smoothing=True, scope='...
function instance_norm (line 202) | def instance_norm(x, scope='instance_norm'):
function layer_norm (line 208) | def layer_norm(x, scope='layer_norm') :
function layer_instance_norm (line 213) | def layer_instance_norm(x, scope='layer_instance_norm') :
function spectral_norm (line 235) | def spectral_norm(w, iteration=1):
function L1_loss (line 270) | def L1_loss(x, y):
function cam_loss (line 275) | def cam_loss(source, non_source) :
function regularization_loss (line 284) | def regularization_loss(scope_name) :
function discriminator_loss (line 300) | def discriminator_loss(loss_func, real, fake):
function generator_loss (line 326) | def generator_loss(loss_func, fake):
FILE: FQ-U-GAT-IT/utils.py
class ImageData (line 7) | class ImageData:
method __init__ (line 9) | def __init__(self, load_size, channels, augment_flag):
method image_processing (line 14) | def image_processing(self, filename):
function load_test_data (line 28) | def load_test_data(image_path, size=256):
function augmentation (line 39) | def augmentation(image, augment_size):
function save_images (line 47) | def save_images(images, size, image_path):
function inverse_transform (line 50) | def inverse_transform(images):
function imsave (line 54) | def imsave(images, size, path):
function merge (line 60) | def merge(images, size):
function show_all_variables (line 70) | def show_all_variables():
function check_folder (line 74) | def check_folder(log_dir):
function str2bool (line 79) | def str2bool(x):
FILE: FQ-U-GAT-IT/vq_layer.py
class VectorQuantizerEMA (line 4) | class VectorQuantizerEMA:
method __init__ (line 17) | def __init__(self, embedding_dim, num_embeddings, commitment_cost, decay,
method __call__ (line 27) | def __call__(self, inputs, reuse=False, layer=None, is_training=True):
method embeddings (line 111) | def embeddings(self):
method quantize (line 114) | def quantize(self, encoding_indices):
Condensed preview — 87 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (776K chars).
[
{
"path": "FQ-BigGAN/BigGAN.py",
"chars": 20542,
"preview": "import numpy as np\nimport math\nimport functools\n\nimport torch\nimport torch.nn as nn\nfrom torch.nn import init\nimport tor"
},
{
"path": "FQ-BigGAN/BigGANdeep.py",
"chars": 22982,
"preview": "import numpy as np\nimport math\nimport functools\n\nimport torch\nimport torch.nn as nn\nfrom torch.nn import init\nimport tor"
},
{
"path": "FQ-BigGAN/LICENSE",
"chars": 1067,
"preview": "MIT License\n\nCopyright (c) 2019 Andy Brock\n\nPermission is hereby granted, free of charge, to any person obtaining a copy"
},
{
"path": "FQ-BigGAN/TFHub/README.md",
"chars": 670,
"preview": "# BigGAN-PyTorch TFHub converter\nThis dir contains scripts for taking the [pre-trained generator weights from TFHub](htt"
},
{
"path": "FQ-BigGAN/TFHub/biggan_v1.py",
"chars": 12173,
"preview": "# BigGAN V1:\n# This is now deprecated code used for porting the TFHub modules to pytorch,\n# included here for reference "
},
{
"path": "FQ-BigGAN/TFHub/converter.py",
"chars": 17428,
"preview": "\"\"\"Utilities for converting TFHub BigGAN generator weights to PyTorch.\n\nRecommended usage:\n\nTo convert all BigGAN varian"
},
{
"path": "FQ-BigGAN/animal_hash.py",
"chars": 32285,
"preview": "c = ['Aardvark', 'Abyssinian', 'Affenpinscher', 'Akbash', 'Akita', 'Albatross',\n 'Alligator', 'Alpaca', 'Angelfish',"
},
{
"path": "FQ-BigGAN/calculate_inception_moments.py",
"chars": 3551,
"preview": "''' Calculate Inception Moments\n This script iterates over the dataset and calculates the moments of the \n activations o"
},
{
"path": "FQ-BigGAN/datasets.py",
"chars": 11416,
"preview": "''' Datasets\n This file contains definitions for our CIFAR, ImageFolder, and HDF5 datasets\n'''\nimport os\nimport os.pa"
},
{
"path": "FQ-BigGAN/inception_tf13.py",
"chars": 5363,
"preview": "''' Tensorflow inception score code\nDerived from https://github.com/openai/improved-gan\nCode derived from tensorflow/ten"
},
{
"path": "FQ-BigGAN/inception_utils.py",
"chars": 12283,
"preview": "''' Inception utilities\n This file contains methods for calculating IS and FID, using either\n the original numpy c"
},
{
"path": "FQ-BigGAN/layers.py",
"chars": 17130,
"preview": "''' Layers\n This file contains various layers for the BigGAN models.\n'''\nimport numpy as np\nimport torch\nimport torch"
},
{
"path": "FQ-BigGAN/losses.py",
"chars": 821,
"preview": "import torch\nimport torch.nn.functional as F\n\n# DCGAN loss\ndef loss_dcgan_dis(dis_fake, dis_real):\n L1 = torch.mean(F.s"
},
{
"path": "FQ-BigGAN/make_hdf5.py",
"chars": 4971,
"preview": "\"\"\" Convert dataset to HDF5\n This script preprocesses a dataset and saves it (images and labels) to \n an HDF5 file"
},
{
"path": "FQ-BigGAN/sample.py",
"chars": 8346,
"preview": "''' Sample\n This script loads a pretrained net and a weightsfile and sample '''\nimport functools\nimport math\nimport nu"
},
{
"path": "FQ-BigGAN/scripts/launch_C10.sh",
"chars": 507,
"preview": "#!/bin/bash\n#export CUDA_VISIBLE_DEVICES=0,1\npython3 train.py --shuffle --batch_size 64 --parallel \\\n--num_G_accumulatio"
},
{
"path": "FQ-BigGAN/scripts/launch_C100.sh",
"chars": 506,
"preview": "#!/bin/bash\n#export CUDA_VISIBLE_DEVICES=2\npython3 train.py --shuffle --batch_size 64 --parallel \\\n--num_G_accumulations"
},
{
"path": "FQ-BigGAN/scripts/launch_I128_bs256x4.sh",
"chars": 704,
"preview": "#!/bin/bash\n# export CUDA_VISIBLE_DEVICES=1,2\npython train.py \\\n--dataset I128_hdf5 --parallel --shuffle --num_workers "
},
{
"path": "FQ-BigGAN/scripts/launch_I64_bs128x4.sh",
"chars": 758,
"preview": "#!/bin/bash\nexport CUDA_VISIBLE_DEVICES=1,2\npython train.py \\\n--dataset I64_hdf5 --parallel --shuffle --num_workers 8 -"
},
{
"path": "FQ-BigGAN/scripts/utils/duplicate.sh",
"chars": 941,
"preview": "#duplicate.sh\nsource=BigGAN_I128_hdf5_seed0_Gch64_Dch64_bs256_Glr1.0e-04_Dlr4.0e-04_Gnlinplace_relu_Dnlinplace_relu_Gini"
},
{
"path": "FQ-BigGAN/scripts/utils/prepare_data.sh",
"chars": 200,
"preview": "#!/bin/bash\n# export CUDA_VISIBLE_DEVICES=3\npython make_hdf5.py --dataset C100 --batch_size 256 --data_root data\npython "
},
{
"path": "FQ-BigGAN/scripts/utils/trans.py",
"chars": 132,
"preview": "filename = 'prepare_data.sh'\nfileCont = open(filename, 'r').read()\nf = open(filename, 'w', newline='\\n')\nf.write(fileCon"
},
{
"path": "FQ-BigGAN/sync_batchnorm/__init__.py",
"chars": 449,
"preview": "# -*- coding: utf-8 -*-\n# File : __init__.py\n# Author : Jiayuan Mao\n# Email : maojiayuan@gmail.com\n# Date : 27/01/2"
},
{
"path": "FQ-BigGAN/sync_batchnorm/batchnorm.py",
"chars": 14882,
"preview": "# -*- coding: utf-8 -*-\n# File : batchnorm.py\n# Author : Jiayuan Mao\n# Email : maojiayuan@gmail.com\n# Date : 27/01/"
},
{
"path": "FQ-BigGAN/sync_batchnorm/batchnorm_reimpl.py",
"chars": 2383,
"preview": "#! /usr/bin/env python3\n# -*- coding: utf-8 -*-\n# File : batchnorm_reimpl.py\n# Author : acgtyrant\n# Date : 11/01/201"
},
{
"path": "FQ-BigGAN/sync_batchnorm/comm.py",
"chars": 4449,
"preview": "# -*- coding: utf-8 -*-\n# File : comm.py\n# Author : Jiayuan Mao\n# Email : maojiayuan@gmail.com\n# Date : 27/01/2018\n"
},
{
"path": "FQ-BigGAN/sync_batchnorm/replicate.py",
"chars": 3226,
"preview": "# -*- coding: utf-8 -*-\n# File : replicate.py\n# Author : Jiayuan Mao\n# Email : maojiayuan@gmail.com\n# Date : 27/01/"
},
{
"path": "FQ-BigGAN/sync_batchnorm/unittest.py",
"chars": 746,
"preview": "# -*- coding: utf-8 -*-\n# File : unittest.py\n# Author : Jiayuan Mao\n# Email : maojiayuan@gmail.com\n# Date : 27/01/2"
},
{
"path": "FQ-BigGAN/train.py",
"chars": 9153,
"preview": "\"\"\" BigGAN: The Authorized Unofficial PyTorch release\n Code by A. Brock and A. Andonian\n This code is an unofficia"
},
{
"path": "FQ-BigGAN/train_fns.py",
"chars": 8727,
"preview": "''' train_fns.py\nFunctions for the main loop of training different conditional image models\n'''\nimport torch\nimport torc"
},
{
"path": "FQ-BigGAN/utility/extract_imagenet.py",
"chars": 526,
"preview": "import os\nimport sys\nimport shutil\n\ndef create_imagenet_ext(src_path, tgt_path, num_class=20):\n\tif not os.path.exists(tg"
},
{
"path": "FQ-BigGAN/utility/untar.py",
"chars": 307,
"preview": "import tarfile\n\nsrc_path = '/media/cchen/StorageDisk/yzhao/datasets/images/ImageNet/'\nfor fname in src_path:\n\tif (fname."
},
{
"path": "FQ-BigGAN/utils.py",
"chars": 50054,
"preview": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n\n''' Utilities file\nThis file contains utility functions for bookkeeping, "
},
{
"path": "FQ-BigGAN/vq_layer.py",
"chars": 4536,
"preview": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\n\nclass Quantize(nn.Module):\n\tdef __init__(self, dim,"
},
{
"path": "FQ-StyleGAN/LICENSE.txt",
"chars": 4666,
"preview": "Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n\n\nNvidia Source Code License-NC\n\n=========================="
},
{
"path": "FQ-StyleGAN/dataset_tool.py",
"chars": 30282,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/__init__.py",
"chars": 774,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/submission/__init__.py",
"chars": 274,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/submission/internal/__init__.py",
"chars": 247,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/submission/internal/local.py",
"chars": 769,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/submission/run_context.py",
"chars": 4245,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/submission/submit.py",
"chars": 13502,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/__init__.py",
"chars": 467,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/autosummary.py",
"chars": 8038,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/custom_ops.py",
"chars": 7244,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/network.py",
"chars": 29982,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/ops/__init__.py",
"chars": 235,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/ops/fused_bias_act.cu",
"chars": 7792,
"preview": "// Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n//\n// This work is made available under the Nvidia Sourc"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/ops/fused_bias_act.py",
"chars": 8539,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/ops/upfirdn_2d.cu",
"chars": 15142,
"preview": "// Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n//\n// This work is made available under the Nvidia Sourc"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/ops/upfirdn_2d.py",
"chars": 16588,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/optimizer.py",
"chars": 17969,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/tflib/tfutil.py",
"chars": 9675,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/dnnlib/util.py",
"chars": 13914,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source "
},
{
"path": "FQ-StyleGAN/metrics/__init__.py",
"chars": 235,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/metrics/frechet_inception_distance.py",
"chars": 3335,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/metrics/inception_score.py",
"chars": 2592,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/metrics/linear_separability.py",
"chars": 10178,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/metrics/metric_base.py",
"chars": 7055,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/metrics/metric_defaults.py",
"chars": 2311,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/metrics/perceptual_path_length.py",
"chars": 5610,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/metrics/precision_recall.py",
"chars": 11442,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/pretrained_networks.py",
"chars": 7665,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/projector.py",
"chars": 8980,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/run_generator.py",
"chars": 8239,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/run_metrics.py",
"chars": 3447,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/run_projector.py",
"chars": 6957,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/run_training.py",
"chars": 9043,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/test_nvcc.cu",
"chars": 686,
"preview": "// Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n//\n// This work is made available under the Nvidia Sourc"
},
{
"path": "FQ-StyleGAN/training/__init__.py",
"chars": 235,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/training/dataset.py",
"chars": 9881,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/training/loss.py",
"chars": 11855,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/training/misc.py",
"chars": 5706,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/training/networks_stylegan.py",
"chars": 33273,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/training/networks_stylegan2.py",
"chars": 47739,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-StyleGAN/training/training_loop.py",
"chars": 20198,
"preview": "# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.\n#\n# This work is made available under the Nvidia Source C"
},
{
"path": "FQ-U-GAT-IT/LICENSE",
"chars": 1066,
"preview": "MIT License\n\nCopyright (c) 2019 Junho Kim\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\n"
},
{
"path": "FQ-U-GAT-IT/UGATIT.py",
"chars": 32638,
"preview": "from ops import *\nfrom utils import *\nfrom glob import glob\nimport time\nfrom tensorflow.contrib.data import prefetch_to_"
},
{
"path": "FQ-U-GAT-IT/dataset/download_dataset_1.sh",
"chars": 289,
"preview": "DATASET=$1\n\nif [[$DATASET != \"portrait\" && $DATASET != \"cat2dog\"]]; then\n echo \"dataset not available\"\n exit\nfi\n\nURL=h"
},
{
"path": "FQ-U-GAT-IT/download_dataset_2.sh",
"chars": 901,
"preview": "#!/bin/bash\n# https://github.com/junyanz/CycleGAN/blob/master/datasets/download_dataset.sh\n\nFILE=$1\n\nif [[ $FILE != \"ae_"
},
{
"path": "FQ-U-GAT-IT/logger.py",
"chars": 346,
"preview": "import sys\n\nclass Logger(object):\n def __init__(self, output_file):\n self.terminal = sys.stdout\n self.log = open("
},
{
"path": "FQ-U-GAT-IT/main.py",
"chars": 5525,
"preview": "from UGATIT import UGATIT\nimport argparse\nfrom utils import *\nfrom logger import Logger\nimport sys\n\"\"\"parsing and config"
},
{
"path": "FQ-U-GAT-IT/ops.py",
"chars": 12069,
"preview": "import tensorflow as tf\nimport tensorflow.contrib as tf_contrib\n\n# Xavier : tf_contrib.layers.xavier_initializer()\n# He "
},
{
"path": "FQ-U-GAT-IT/utils.py",
"chars": 2349,
"preview": "import tensorflow as tf\nfrom tensorflow.contrib import slim\nimport cv2\nimport os, random\nimport numpy as np\n\nclass Image"
},
{
"path": "FQ-U-GAT-IT/vq_layer.py",
"chars": 5522,
"preview": "import tensorflow as tf\nfrom tensorflow.python.training import moving_averages\n\nclass VectorQuantizerEMA:\n \"\"\"Sonnet mo"
},
{
"path": "README.md",
"chars": 7896,
"preview": "# FQ-GAN\n\n### Recent Update \n\n* May 22, 2020 Releasing the pre-trained FQ-BigGAN/BigGAN at resolution 64x64 and their t"
}
]
// ... and 1 more files (download for full content)
About this extraction
This page contains the full source code of the YangNaruto/FQ-GAN GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 87 files (732.2 KB), approximately 196.8k tokens, and a symbol index with 681 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.