File size: 3,187 Bytes
223d932
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# ------------------------------------------------------------------------------
# OptVQ: Preventing Local Pitfalls in Vector Quantization via Optimal Transport
# Copyright (c) 2024 Borui Zhang. All Rights Reserved.
# Licensed under the MIT License [see LICENSE for details]
# ------------------------------------------------------------------------------

import torch.nn as nn

class PlainCNNEncoder(nn.Module):
    def __init__(self, in_dim: int = 3):
        super(PlainCNNEncoder, self).__init__()

        self.in_dim = in_dim

        self.in_fc = nn.Conv2d(in_channels=in_dim, out_channels=16,
                                kernel_size=3, stride=1, padding=1, bias=True)
        self.act0 = nn.ReLU(inplace=True)

        self.down1 = nn.Conv2d(in_channels=16, out_channels=16, kernel_size=2, stride=2)
        self.conv1 = nn.Conv2d(in_channels=16, out_channels=16, 
                               kernel_size=3, stride=1, padding=1, bias=True)
        self.act1 = nn.ReLU(inplace=True)

        self.down2 = nn.Conv2d(in_channels=16, out_channels=16, kernel_size=2, stride=2)
        self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, 
                               kernel_size=3, stride=1, padding=1, bias=True)
        self.act2 = nn.ReLU(inplace=True)

        self.out_fc = nn.Conv2d(in_channels=32, out_channels=32,
                                kernel_size=3, stride=1, padding=1, bias=True)
    
    @property
    def hidden_dim(self):
        return 32

    def forward(self, x):
        x = self.in_fc(x)
        x = self.act0(x)

        x = self.down1(x)
        x = self.conv1(x)
        x = self.act1(x)

        x = self.down2(x)
        x = self.conv2(x)
        x = self.act2(x)

        x = self.out_fc(x)
        return x

class PlainCNNDecoder(nn.Module):
    def __init__(self, out_dim: int = 3):
        super(PlainCNNDecoder, self).__init__()
        self.out_dim = out_dim

        self.in_fc = nn.Conv2d(in_channels=32, out_channels=32,
                               kernel_size=3, stride=1, padding=1, bias=True)
        
        self.act1 = nn.ReLU(inplace=True)
        self.up1 = nn.ConvTranspose2d(in_channels=32, out_channels=16, kernel_size=2, stride=2)
        self.conv1 = nn.Conv2d(in_channels=16, out_channels=16, 
                               kernel_size=3, stride=1, padding=1, bias=True)
        
        self.act2 = nn.ReLU(inplace=True)
        self.up2 = nn.ConvTranspose2d(in_channels=16, out_channels=16, kernel_size=2, stride=2)
        self.conv2 = nn.Conv2d(in_channels=16, out_channels=16, 
                               kernel_size=3, stride=1, padding=1, bias=True)
        
        self.act3 = nn.ReLU(inplace=True)
        self.out_fc = nn.Conv2d(in_channels=16, out_channels=out_dim,
                               kernel_size=3, stride=1, padding=1, bias=True)
    
    @property
    def hidden_dim(self):
        return 32
    
    def forward(self, x):
        x = self.in_fc(x)

        x = self.act1(x)
        x = self.up1(x)
        x = self.conv1(x)

        x = self.act2(x)
        x = self.up2(x)
        x = self.conv2(x)
        
        x = self.act3(x)
        x = self.out_fc(x)
        return x