-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVLE.py
148 lines (130 loc) · 5.2 KB
/
VLE.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import torch.nn as nn
import torch.nn.functional as F
import torch
from conv_lstm import ConvLSTM
class Encoder(nn.Module):
def __init__(self, in_channels=6, depth=4, width=16):
super(Encoder, self).__init__()
self.depth = depth
self.width = width
self.encoder = nn.ModuleList()
for i in range(depth):
self.encoder.append(
nn.Sequential(
nn.Conv2d(
in_channels if i == 0 else width * 2**i,
width * 2 ** (i + 1),
kernel_size=3,
padding=1,
),
nn.ReLU(inplace=True),
nn.Conv2d(
width * 2 ** (i + 1),
width * 2 ** (i + 1),
kernel_size=3,
padding=1,
),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
)
def forward(self, x):
for encoder in self.encoder:
x = encoder(x)
return x
class Decoder(nn.Module):
def __init__(self, out_channels=3, depth=4, width=16):
super(Decoder, self).__init__()
self.depth = depth
self.width = width
self.decoder = nn.ModuleList()
for i in range(depth, 0, -1):
self.decoder.append(
nn.Sequential(
nn.ConvTranspose2d(
width * 2**i, width * 2 ** (i - 1), kernel_size=2, stride=2
),
nn.Conv2d(
width * 2 ** (i - 1),
width * 2 ** (i - 1),
kernel_size=3,
padding=1,
),
nn.ReLU(inplace=True),
nn.Conv2d(
width * 2 ** (i - 1),
width * 2 ** (i - 1),
kernel_size=3,
padding=1,
),
nn.ReLU(inplace=True),
)
)
self.decoder.append(
nn.Sequential(
nn.ConvTranspose2d(width, out_channels, kernel_size=3, stride=1, padding=0),
nn.Conv2d(out_channels, out_channels=out_channels, kernel_size=3, padding=0,),
#nn.Conv2d(out_channels, out_channels=out_channels, kernel_size=3, padding=0),
#nn.Softmax2d(),
)
)
def forward(self, x):
for decoder in self.decoder:
x = decoder(x)
return x
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, hidden_size, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.conv1 = nn.Conv2d(in_channels=in_channels,
out_channels=hidden_size,
kernel_size=3,
padding=1)
self.conv2 = nn.Conv2d(in_channels=hidden_size,
out_channels=out_channels,
kernel_size=3,
padding=1)
self.bn1 = nn.BatchNorm2d(hidden_size)
self.bn2 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
residual = x
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu(x)
return x + residual
class LSTMBlock(nn.Module):
def __init__(self, in_channels, hidden_dim, kernel_size, num_layers):
super(LSTMBlock, self).__init__()
self.convlstm = ConvLSTM(input_dim=in_channels,
hidden_dim=hidden_dim,
kernel_size=kernel_size,
num_layers=num_layers,
batch_first=True,)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
# Add an extra dimension for the sequence length
mask, _ = self.convlstm(x.unsqueeze(1))
mask = mask[0].squeeze(1)
mask = self.sigmoid(mask)
#mask = (mask > 0.5).float()
return x, mask
class VLE(nn.Module):
def __init__(self, in_channels=3, out_channels=3, max_tokens=5, depth=4, width=16, image_size=512):
super(VLE, self).__init__()
self.encoder = Encoder(in_channels, depth, width)
self.decoder = Decoder(out_channels, depth, width)
self.residual_block = ResidualBlock(out_channels, out_channels, 64)
self.lstm_block = LSTMBlock(out_channels, [64, 128, 1], (3,3), 3)
def forward(self, residual):
# Pass the residual and the current mask through the residual block
residual = self.residual_block(residual)
residual, mask = self.lstm_block(residual)
masked_residual = residual * mask
# Encode the masked residual into a token, conditioned on the mask
token = self.encoder(masked_residual)
# Decode the token into a partial reconstruction
partial_reconstruction = self.decoder(token)
return partial_reconstruction, mask