PatchGANDiscriminator<T>
Layers · AiDotNet.NeuralNetworks.Layers
PatchGAN: the convolutional “Markovian” discriminator introduced by pix2pix (Isola et al., 2017).
Inheritance: LayerBase<T> → PatchGANDiscriminator<T>
Implements: IShapeContract
For Beginners
when a generator is trained with an adversarial loss, a second network — the discriminator — has to say whether an image looks real. A PatchGAN does not output one verdict for the whole image. It slides over the image and outputs a GRID of verdicts, one per square patch, then the loss averages them. Two things follow: the discriminator is much smaller than a whole-image classifier (it only ever looks at one patch at a time), and it works on any input size, because a bigger image simply produces a bigger grid.
How It Works
Paper (section 3.2.2, “Markovian discriminator (PatchGAN)”). The discriminator “tries to classify if each N x N patch in an image is real or fake”, is “run convolutionally across the image, averaging all responses”, and models the image as a Markov random field — pixels further apart than one patch diameter are treated as independent. The L1/L2 reconstruction term is what enforces low-frequency correctness; the PatchGAN’s job is only the high frequencies.
Architecture (section 6.1.2, verbatim). The 70x70 discriminator is C64-C128-C256-C512;
“after the last layer, a convolution is applied to map to a 1-dimensional output, followed by a
Sigmoid function”; “as an exception to the above notation, BatchNorm is not applied to the first C64
layer”; “all ReLUs are leaky, with slope 0.2”. Ck is a 4x4 Convolution-BatchNorm-LeakyReLU
block with k filters. Other receptive fields “follow the same basic architecture, with depth varied
to modify the receptive field size” — hence PatchGANReceptiveField selects a depth
rather than a different design.
Why depth alone sets the patch size. Every stride-2 block doubles the distance an output unit
can see. Applying the receptive-field recurrence r_in = r_out * stride + (kernel - stride) backwards
through C64(s2) C128(s2) C256(s2) C512(s1) plus the 1-channel output convolution (s1) gives
1 -> 4 -> 7 -> 16 -> 34 -> 70, which is exactly the paper’s 70x70. Adding two more stride-2
C512 blocks continues 70 -> 142 -> 286, its ImageGAN. This class therefore makes the LAST block
stride 1 and every earlier block stride 2, and sizes channels as
numFilters * min(2^i, 8) — which yields 64, 128, 256, 512, 512, 512.
Usage:
Every sub-layer is a normal library layer registered via RegisterSubLayer, and the forward
pass is composed purely from those layers’ own Forward calls. Gradients therefore flow
through the autodiff tape with no manual backward pass and no raw buffer writes, which is what lets
this be dropped into any model’s adversarial objective.
Constructors
| Constructor | Summary |
|---|---|
PatchGANDiscriminator(PatchGANReceptiveField receptiveField, int numFilters, double leakySlope, bool applySigmoid) |
Preserves the constructor signature used by binaries compiled before BiasMode was introduced. |
PatchGANDiscriminator(PatchGANReceptiveField receptiveField, int numFilters, double leakySlope, bool applySigmoid, BiasMode biasMode) |
Preserves the explicit-depth constructor signature used by binaries compiled before BiasMode was introduced. |
PatchGANDiscriminator(int numLayers, int numFilters, int kernelSize, double leakySlope, bool applySigmoid) |
Preserves the explicit-depth constructor signature used by binaries compiled before BiasMode was introduced. |
PatchGANDiscriminator(int numLayers, int numFilters, int kernelSize, double leakySlope, bool applySigmoid, BiasMode biasMode) |
Creates a PatchGAN discriminator with an explicit depth, for receptive fields other than the four the paper tabulates. |
Properties
| Property | Summary |
|---|---|
bool SupportsTraining { get; } |
|
int NumLayers { get; } |
Gets the number of Ck blocks preceding the output convolution. |
int ReceptiveField { get; } |
Gets the receptive field, in pixels, of one output unit — the “N” in “N x N patch”. |
Methods
| Method | Summary |
|---|---|
IReadOnlyList<ILayer<T>> GetSubLayers() |
|
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank) |
|
void ClearGradients() |
|
void ResetState() |
|
void UpdateParameters(T learningRate) |
Fields
| Field | Summary |
|---|---|
double DefaultLeakySlope |
Paper default LeakyReLU slope: “all ReLUs are leaky, with slope 0.2” (section 6.1.2). |
int DefaultKernelSize |
Paper default convolution kernel size. |
int DefaultNumFilters |
Paper default filter count for the first block (the “64” in C64), section 6.1.2. |