-
Notifications
You must be signed in to change notification settings - Fork 16
/
convae.py
48 lines (37 loc) · 1.58 KB
/
convae.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
############################################
# Semi-Adversarial Network #
# (convolutional autoencoder) #
# iPRoBe lab #
# #
############################################
import torch
import torch.nn as nn
class AutoEncoder(nn.Module):
def __init__(self):
super(AutoEncoder, self).__init__()
self.autoencoder = nn.Sequential(
## Encoder
nn.Conv2d(4, 8, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(inplace=True),
nn.AvgPool2d(kernel_size=2, stride=2),
nn.Conv2d(8, 12, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(inplace=True),
nn.AvgPool2d(kernel_size=2, stride=2),
## Decoder
nn.Conv2d(12, 256, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(inplace=True),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(inplace=True),
nn.Upsample(scale_factor=2, mode='nearest')
)
self.protocombiner = nn.Sequential(
nn.Conv2d(131, 1, kernel_size=1, stride=1, padding=0),
nn.Sigmoid()
)
def forward(self, imgs, same_proto, oppo_proto):
x = torch.cat([imgs, same_proto], dim=1)
x = self.autoencoder(x)
rec_same = torch.cat([x, same_proto], dim=1)
rec_oppo = torch.cat([x, oppo_proto], dim=1)
return self.protocombiner(rec_same), self.protocombiner(rec_oppo)