
--- model/AttentiveRNN GAN.py
+++ model/AttentiveRNN GAN.py
... | ... | @@ -10,7 +10,8 @@ |
10 | 10 |
if kernel_size is None: |
11 | 11 |
kernel_size = [3, 3] |
12 | 12 |
self.attentiveRNN = AttentiveRNN( repetition, |
13 |
- blocks=3, layers=1, input_ch=3, out_ch=32, kernel_size=None, stride=1, padding=1, groups=1, dilation=1 |
|
13 |
+ blocks=blocks, layers=layers, input_ch=input_ch, out_ch=out_ch, |
|
14 |
+ kernel_size=None, stride=stride, padding=padding, groups=groups, dilation=dilation |
|
14 | 15 |
) |
15 | 16 |
self.autoencoder = AutoEncoder() |
16 | 17 |
self.blocks = blocks |
... | ... | @@ -27,6 +28,7 @@ |
27 | 28 |
def forward(self, x): |
28 | 29 |
x, attention_map = self.attentiveRNN(x) |
29 | 30 |
x = self.autoencoder(x * attention_map) |
31 |
+ return x |
|
30 | 32 |
|
31 | 33 |
if __name__ == "__main__": |
32 | 34 |
import torch |
--- model/attentivernn.py
+++ model/attentivernn.py
... | ... | @@ -218,43 +218,6 @@ |
218 | 218 |
return loss, inference_ret['final_attention_map'] |
219 | 219 |
|
220 | 220 |
# Need work |
221 |
-class DiscriminativeNet(nn.Module): |
|
222 |
- def __init__(self, W, H): |
|
223 |
- super(DiscriminativeNet, self).__init__() |
|
224 |
- self.conv1 = nn.Conv2d(in_channels=3, out_channels=8, kernel_size=5, stride=1, padding=2) |
|
225 |
- self.conv2 = nn.Conv2d(in_channels=8, out_channels=16, kernel_size=5, stride=1, padding=2) |
|
226 |
- self.conv3 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=2) |
|
227 |
- self.conv4 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2) |
|
228 |
- self.conv5 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=5, stride=1, padding=2) |
|
229 |
- self.conv6 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=5, stride=1, padding=2) |
|
230 |
- self.conv_map = nn.Conv2d(in_channels=128, out_channels=1, kernel_size=5, stride=1, padding=2, bias=False) |
|
231 |
- self.conv7 = nn.Conv2d(in_channels=128, out_channels=64, kernel_size=5, stride=4, padding=2) |
|
232 |
- self.conv8 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=5, stride=4, padding=2) |
|
233 |
- self.conv9 = nn.Conv2d(in_channels=64, out_channels=32, kernel_size=5, stride=4, padding=2) |
|
234 |
- self.fc1 = nn.Linear(32 * W * H, |
|
235 |
- 1024) # You need to adjust the input dimension here depending on your input size |
|
236 |
- self.fc2 = nn.Linear(1024, 1) |
|
237 |
- |
|
238 |
- def forward(self, x): |
|
239 |
- x1 = F.leaky_relu(self.conv1(x)) |
|
240 |
- x2 = F.leaky_relu(self.conv2(x1)) |
|
241 |
- x3 = F.leaky_relu(self.conv3(x2)) |
|
242 |
- x4 = F.leaky_relu(self.conv4(x3)) |
|
243 |
- x5 = F.leaky_relu(self.conv5(x4)) |
|
244 |
- x6 = F.leaky_relu(self.conv6(x5)) |
|
245 |
- attention_map = self.conv_map(x6) |
|
246 |
- x7 = F.leaky_relu(self.conv7(attention_map * x6)) |
|
247 |
- x8 = F.leaky_relu(self.conv8(x7)) |
|
248 |
- x9 = F.leaky_relu(self.conv9(x8)) |
|
249 |
- x9 = x9.view(x9.size(0), -1) # flatten the tensor |
|
250 |
- fc1 = self.fc1(x9) |
|
251 |
- fc2 = self.fc2(fc1) |
|
252 |
- fc_out = torch.sigmoid(fc2) |
|
253 |
- |
|
254 |
- # Ensure fc_out is not exactly 0 or 1 for stability of log operation in loss |
|
255 |
- fc_out = torch.clamp(fc_out, min=1e-7, max=1 - 1e-7) |
|
256 |
- |
|
257 |
- return fc_out, attention_map, fc2 |
|
258 | 221 |
|
259 | 222 |
if __name__ == "__main__": |
260 | 223 |
from torchinfo import summary |
+++ model/discriminator.py
... | ... | @@ -0,0 +1,40 @@ |
1 | +from torch import nn, clamp | |
2 | +from torch.functional import F | |
3 | + | |
4 | +class DiscriminativeNet(nn.Module): | |
5 | + def __init__(self, W, H): | |
6 | + super(DiscriminativeNet, self).__init__() | |
7 | + self.conv1 = nn.Conv2d(in_channels=3, out_channels=8, kernel_size=5, stride=1, padding=2) | |
8 | + self.conv2 = nn.Conv2d(in_channels=8, out_channels=16, kernel_size=5, stride=1, padding=2) | |
9 | + self.conv3 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=2) | |
10 | + self.conv4 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2) | |
11 | + self.conv5 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=5, stride=1, padding=2) | |
12 | + self.conv6 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=5, stride=1, padding=2) | |
13 | + self.conv_map = nn.Conv2d(in_channels=128, out_channels=1, kernel_size=5, stride=1, padding=2, bias=False) | |
14 | + self.conv7 = nn.Conv2d(in_channels=128, out_channels=64, kernel_size=5, stride=4, padding=2) | |
15 | + self.conv8 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=5, stride=4, padding=2) | |
16 | + self.conv9 = nn.Conv2d(in_channels=64, out_channels=32, kernel_size=5, stride=4, padding=2) | |
17 | + self.fc1 = nn.Linear(32 * W * H, | |
18 | + 1024) # You need to adjust the input dimension here depending on your input size | |
19 | + self.fc2 = nn.Linear(1024, 1) | |
20 | + | |
21 | + def forward(self, x): | |
22 | + x1 = F.leaky_relu(self.conv1(x)) | |
23 | + x2 = F.leaky_relu(self.conv2(x1)) | |
24 | + x3 = F.leaky_relu(self.conv3(x2)) | |
25 | + x4 = F.leaky_relu(self.conv4(x3)) | |
26 | + x5 = F.leaky_relu(self.conv5(x4)) | |
27 | + x6 = F.leaky_relu(self.conv6(x5)) | |
28 | + attention_map = self.conv_map(x6) | |
29 | + x7 = F.leaky_relu(self.conv7(attention_map * x6)) | |
30 | + x8 = F.leaky_relu(self.conv8(x7)) | |
31 | + x9 = F.leaky_relu(self.conv9(x8)) | |
32 | + x9 = x9.view(x9.size(0), -1) # flatten the tensor | |
33 | + fc1 = self.fc1(x9) | |
34 | + fc2 = self.fc2(fc1) | |
35 | + fc_out = F.sigmoid(fc2) | |
36 | + | |
37 | + # Ensure fc_out is not exactly 0 or 1 for stability of log operation in loss | |
38 | + fc_out = clamp(fc_out, min=1e-7, max=1 - 1e-7) | |
39 | + | |
40 | + return fc_out, attention_map, fc2 |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?