-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModels.py
48 lines (40 loc) · 1.48 KB
/
Models.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
from torch import nn
class autoencoder(nn.Module):
def __init__(self):
super(autoencoder, self).__init__()
self.conv1 = nn.Conv2d(3, 6, kernel_size=(5,5))
self.maxpool1 = nn.MaxPool2d(kernel_size=(2,2), return_indices=True)
self.maxpool2 = nn.MaxPool2d(kernel_size=(2,2), return_indices=True)
self.unconv1 = nn.ConvTranspose2d(6,3,kernel_size=(5,5))
self.maxunpool1 = nn.MaxUnpool2d(kernel_size=(2,2))
self.unmaxunpool2 = nn.MaxUnpool2d(kernel_size=(2,2))
self.encoder1 = nn.Sequential(
nn.Tanh(),
nn.Conv2d(6, 12,kernel_size=(5,5)),
)
self.encoder2 = nn.Sequential(
nn.Tanh(),
nn.Conv2d(12, 16, kernel_size=(5,5)),
nn.Tanh()
)
self.decoder2 = nn.Sequential(
nn.ConvTranspose2d(16, 12, kernel_size=(5,5)),
nn.Tanh()
)
self.decoder1 = nn.Sequential(
nn.ConvTranspose2d(12,6,kernel_size=(5,5)),
nn.Tanh(),
)
def forward(self, x):
x = self.conv1(x)
x,indices1 = self.maxpool1(x)
x = self.encoder1(x)
x,indices2 = self.maxpool2(x)
coding = self.encoder2(x)
x = self.decoder2(coding)
x = self.unmaxunpool2(x, indices2)
x = self.decoder1(x)
x = self.maxunpool1(x,indices1)
x = self.unconv1(x)
output = nn.Tanh()(x)
return coding, output