Cycle-Consistent Adversarial Networks (CycleGAN)¶
Discriminator¶
-
dalib.translation.cyclegan.
pixel
(ndf, input_nc=3, norm='batch', init_type='normal', init_gain=0.02)[source]¶ 1x1 PixelGAN discriminator can classify whether a pixel is real or not. It encourages greater color diversity but has no effect on spatial statistics.
- Parameters
ndf (int) – the number of filters in the first conv layer
input_nc (int) – the number of channels in input images. Default: 3
norm (str) – the type of normalization layers used in the network. Default: ‘batch’
init_type (str) – the name of the initialization method. Choices includes:
normal
|xavier
|kaiming
|orthogonal
. Default: ‘normal’init_gain (float) – scaling factor for normal, xavier and orthogonal. Default: 0.02
-
dalib.translation.cyclegan.
patch
(ndf, input_nc=3, norm='batch', n_layers=3, init_type='normal', init_gain=0.02)[source]¶ PatchGAN classifier described in the original pix2pix paper. It can classify whether 70×70 overlapping patches are real or fake. Such a patch-level discriminator architecture has fewer parameters than a full-image discriminator and can work on arbitrarily-sized images in a fully convolutional fashion.
- Parameters
ndf (int) – the number of filters in the first conv layer
input_nc (int) – the number of channels in input images. Default: 3
norm (str) – the type of normalization layers used in the network. Default: ‘batch’
n_layers (int) – the number of conv layers in the discriminator. Default: 3
init_type (str) – the name of the initialization method. Choices includes:
normal
|xavier
|kaiming
|orthogonal
. Default: ‘normal’init_gain (float) – scaling factor for normal, xavier and orthogonal. Default: 0.02
Generator¶
-
dalib.translation.cyclegan.
resnet_9
(ngf, input_nc=3, output_nc=3, norm='batch', use_dropout=False, init_type='normal', init_gain=0.02)[source]¶ Resnet-based generator with 9 Resnet blocks.
- Parameters
ngf (int) – the number of filters in the last conv layer
input_nc (int) – the number of channels in input images. Default: 3
output_nc (int) – the number of channels in output images. Default: 3
norm (str) – the type of normalization layers used in the network. Default: ‘batch’
use_dropout (bool) – whether use dropout. Default: False
init_type (str) – the name of the initialization method. Choices includes:
normal
|xavier
|kaiming
|orthogonal
. Default: ‘normal’init_gain (float) – scaling factor for normal, xavier and orthogonal. Default: 0.02
-
dalib.translation.cyclegan.
resnet_6
(ngf, input_nc=3, output_nc=3, norm='batch', use_dropout=False, init_type='normal', init_gain=0.02)[source]¶ Resnet-based generator with 6 Resnet blocks.
- Parameters
ngf (int) – the number of filters in the last conv layer
input_nc (int) – the number of channels in input images. Default: 3
output_nc (int) – the number of channels in output images. Default: 3
norm (str) – the type of normalization layers used in the network. Default: ‘batch’
use_dropout (bool) – whether use dropout. Default: False
init_type (str) – the name of the initialization method. Choices includes:
normal
|xavier
|kaiming
|orthogonal
. Default: ‘normal’init_gain (float) – scaling factor for normal, xavier and orthogonal. Default: 0.02
-
dalib.translation.cyclegan.
unet_256
(ngf, input_nc=3, output_nc=3, norm='batch', use_dropout=False, init_type='normal', init_gain=0.02)[source]¶ U-Net generator for 256x256 input images. The size of the input image should be a multiple of 256.
- Parameters
ngf (int) – the number of filters in the last conv layer
input_nc (int) – the number of channels in input images. Default: 3
output_nc (int) – the number of channels in output images. Default: 3
norm (str) – the type of normalization layers used in the network. Default: ‘batch’
use_dropout (bool) – whether use dropout. Default: False
init_type (str) – the name of the initialization method. Choices includes:
normal
|xavier
|kaiming
|orthogonal
. Default: ‘normal’init_gain (float) – scaling factor for normal, xavier and orthogonal. Default: 0.02
-
dalib.translation.cyclegan.
unet_128
(ngf, input_nc=3, output_nc=3, norm='batch', use_dropout=False, init_type='normal', init_gain=0.02)[source]¶ U-Net generator for 128x128 input images. The size of the input image should be a multiple of 128.
- Parameters
ngf (int) – the number of filters in the last conv layer
input_nc (int) – the number of channels in input images. Default: 3
output_nc (int) – the number of channels in output images. Default: 3
norm (str) – the type of normalization layers used in the network. Default: ‘batch’
use_dropout (bool) – whether use dropout. Default: False
init_type (str) – the name of the initialization method. Choices includes:
normal
|xavier
|kaiming
|orthogonal
. Default: ‘normal’init_gain (float) – scaling factor for normal, xavier and orthogonal. Default: 0.02
GAN Loss¶
-
class
dalib.translation.cyclegan.
LeastSquaresGenerativeAdversarialLoss
(reduction='mean')[source]¶ Loss for Least Squares Generative Adversarial Network (LSGAN)
- Parameters
reduction (str, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the sum of the output will be divided by the number of elements in the output,'sum'
: the output will be summed. Default:'mean'
- Inputs:
prediction (tensor): unnormalized discriminator predictions
real (bool): if the ground truth label is for real images or fake images. Default: true
Warning
Do not use sigmoid as the last layer of Discriminator.
-
class
dalib.translation.cyclegan.
VanillaGenerativeAdversarialLoss
(reduction='mean')[source]¶ Loss for Vanilla Generative Adversarial Network
- Parameters
reduction (str, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the sum of the output will be divided by the number of elements in the output,'sum'
: the output will be summed. Default:'mean'
- Inputs:
prediction (tensor): unnormalized discriminator predictions
real (bool): if the ground truth label is for real images or fake images. Default: true
Warning
Do not use sigmoid as the last layer of Discriminator.
-
class
dalib.translation.cyclegan.
WassersteinGenerativeAdversarialLoss
(reduction='mean')[source]¶ Loss for Wasserstein Generative Adversarial Network
- Parameters
reduction (str, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the sum of the output will be divided by the number of elements in the output,'sum'
: the output will be summed. Default:'mean'
- Inputs:
prediction (tensor): unnormalized discriminator predictions
real (bool): if the ground truth label is for real images or fake images. Default: true
Warning
Do not use sigmoid as the last layer of Discriminator.
Translation¶
-
class
dalib.translation.cyclegan.
Translation
(generator, device=device(type='cpu'), mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))[source]¶ Image Translation Transform Module
- Parameters
generator (torch.nn.Module) – An image generator, e.g.
resnet_9_generator()
device (torch.device) – device to put the generator. Default: ‘cpu’
mean (tuple) – the normalized mean for image
std (tuple) – the normalized std for image
- Input:
image (PIL.Image): raw image in shape H x W x C
- Output:
raw image in shape H x W x 3
Util¶
-
class
dalib.translation.cyclegan.util.
ImagePool
(pool_size)[source]¶ An image buffer that stores previously generated images.
This buffer enables us to update discriminators using a history of generated images rather than the ones produced by the latest generators.
- Parameters
pool_size (int) – the size of image buffer, if pool_size=0, no buffer will be created
-
query
(images)[source]¶ Return an image from the pool.
- Parameters
images (torch.Tensor) – the latest generated images from the generator
- Returns
By 50/100, the buffer will return input images. By 50/100, the buffer will return images previously stored in the buffer, and insert the current images to the buffer.