-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmnist_LeNet.py
84 lines (71 loc) · 3 KB
/
mnist_LeNet.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
"""
Title: mnist_LeNet.py
Reference: https://github.com/lukasruff/Deep-SAD-PyTorch/tree/master/src/networks
"""
from .base_net import BaseNet
import torch
import torch.nn as nn
import torch.nn.functional as F
# #########################################################################
# 1. Encoder
# #########################################################################
class MNISTLeNet(BaseNet):
def __init__(self, rep_dim=64):
super().__init__()
self.rep_dim = rep_dim
self.pool = nn.MaxPool2d(2, 2)
self.conv1 = nn.Conv2d(1, 16, 5, bias=False, padding=2)
self.bn2d1 = nn.BatchNorm2d(16, eps=1e-04, affine=False)
self.conv2 = nn.Conv2d(16, 32, 5, bias=False, padding=2)
self.bn2d2 = nn.BatchNorm2d(32, eps=1e-04, affine=False)
self.fc1 = nn.Linear(32 * 7 * 7, 128, bias=False)
self.bn1d1 = nn.BatchNorm1d(128, eps=1e-04, affine=False)
self.fc2 = nn.Linear(128, self.rep_dim, bias=False)
def forward(self, x):
x = x.view(-1, 1, 28, 28)
x = self.conv1(x)
x = self.pool(F.leaky_relu(self.bn2d1(x)))
x = self.conv2(x)
x = self.pool(F.leaky_relu(self.bn2d2(x)))
x = x.view(int(x.size(0)), -1)
x = F.leaky_relu(self.bn1d1(self.fc1(x)))
x = self.fc2(x)
return x
# #########################################################################
# 2. Decoder
# #########################################################################
class MNISTLeNetDecoder(BaseNet):
def __init__(self, rep_dim=64):
super().__init__()
self.rep_dim = rep_dim
self.fc3 = nn.Linear(self.rep_dim, 128, bias=False)
self.bn1d2 = nn.BatchNorm1d(128, eps=1e-04, affine=False)
self.deconv1 = nn.ConvTranspose2d(8, 32, 5, bias=False, padding=2)
self.bn2d3 = nn.BatchNorm2d(32, eps=1e-04, affine=False)
self.deconv2 = nn.ConvTranspose2d(32, 16, 5, bias=False, padding=3)
self.bn2d4 = nn.BatchNorm2d(16, eps=1e-04, affine=False)
self.deconv3 = nn.ConvTranspose2d(16, 1, 5, bias=False, padding=2)
def forward(self, x):
x = self.bn1d2(self.fc3(x))
x = x.view(int(x.size(0)), int(128 / 16), 4, 4)
x = F.interpolate(F.leaky_relu(x), scale_factor=2)
x = self.deconv1(x)
x = F.interpolate(F.leaky_relu(self.bn2d3(x)), scale_factor=2)
x = self.deconv2(x)
x = F.interpolate(F.leaky_relu(self.bn2d4(x)), scale_factor=2)
x = self.deconv3(x)
x = torch.sigmoid(x)
return x
# #########################################################################
# 3. Autoencoder
# #########################################################################
class MNISTLeNetAutoencoder(BaseNet):
def __init__(self, rep_dim=64):
super().__init__()
self.rep_dim = rep_dim
self.encoder = MNISTLeNet(rep_dim=rep_dim)
self.decoder = MNISTLeNetDecoder(rep_dim=rep_dim)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x