-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
84 lines (62 loc) · 1.72 KB
/
model.py
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
import torch
from torch import nn
from compressai.layers import GDN1
from compressai.models.utils import conv, deconv
from compressai.entropy_models import EntropyBottleneck
class Encoder(nn.Module):
def __init__(self, N, M):
super().__init__()
layers = [
conv(3, N),
GDN1(N),
]
for _ in range(M):
layers.extend(
[
conv(N, N),
GDN1(N),
]
)
layers.append(conv(N, N))
self.encoder = nn.Sequential(*layers)
def forward(self, x):
return self.encoder(x)
class Decoder(nn.Module):
def __init__(self, N, M):
super().__init__()
layers = [
deconv(N, N),
GDN1(N, inverse=True),
]
for _ in range(M):
layers.extend(
[
deconv(N, N),
GDN1(N, inverse=True),
]
)
layers.extend(
[
deconv(N, 3),
nn.Sigmoid(),
]
)
self.decoder = nn.Sequential(*layers)
def forward(self, x):
return self.decoder(x)
class AutoEncoder(nn.Module):
def __init__(self, N=256, M=2):
super().__init__()
self.N = N
self.M = M
self._encoder = Encoder(N, M)
self._decoder = Decoder(N, M)
self._bottleneck = EntropyBottleneck(channels=N)
def forward(self, x):
y = self._encoder(x)
y_, self.likelihoods = self._bottleneck(y, training=self.training)
return self._decoder(y_)
def encoder(self):
return self._encoder
def decoder(self):
return self._decoder