summaryrefslogtreecommitdiff
path: root/text_recognizer/criterions/n_layer_discriminator.py
blob: a9f47f905ed4060eabd749bdf4d8dc548bd42d8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Pix2pix discriminator loss."""
from torch import nn, Tensor

from text_recognizer.networks.vqvae.norm import Normalize


class NLayerDiscriminator(nn.Module):
    """Defines a PatchGAN discriminator loss in Pix2Pix."""

    def __init__(
        self, in_channels: int = 1, num_channels: int = 32, num_layers: int = 3
    ) -> None:
        super().__init__()
        self.in_channels = in_channels
        self.num_channels = num_channels
        self.num_layers = num_layers
        self.discriminator = self._build_discriminator()

    def _build_discriminator(self) -> nn.Sequential:
        """Builds discriminator."""
        discriminator = [
            nn.Sigmoid(),
            nn.Conv2d(
                in_channels=self.in_channels,
                out_channels=self.num_channels,
                kernel_size=4,
                stride=2,
                padding=1,
            ),
            nn.Mish(inplace=True),
        ]
        in_channels = self.num_channels
        for n in range(1, self.num_layers):
            discriminator += [
                nn.Conv2d(
                    in_channels=in_channels,
                    out_channels=in_channels * n,
                    kernel_size=4,
                    stride=2,
                    padding=1,
                ),
                # Normalize(num_channels=in_channels * n),
                nn.Mish(inplace=True),
            ]
            in_channels *= n

        discriminator += [
            nn.Conv2d(
                in_channels=self.num_channels * (self.num_layers - 1),
                out_channels=1,
                kernel_size=4,
                padding=1,
            )
        ]
        return nn.Sequential(*discriminator)

    def forward(self, x: Tensor) -> Tensor:
        """Forward pass through discriminator."""
        return self.discriminator(x)