-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAE.py
118 lines (101 loc) · 3.79 KB
/
MAE.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision as tv
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision.transforms import transforms
from tqdm import tqdm
from matplotlib import pyplot as plt
import numpy as np
# If you get 503 while downloading MNIST then download it manually
# wget www.di.ens.fr/~lelarge/MNIST.tar.gz
# tar -zxvf MNIST.tar.gz
BATCH_SIZE = 32
DEVICE = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
trainset = tv.datasets.MNIST(root='.', train=True, download=True, transform=transform)
dataloader = DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=0)
def imshow(axs, inp):
inp = inp.cpu().detach().numpy()
mean = 0.1307
std = 0.3081
inp = ((mean * inp) + std)
axs.imshow(inp, cmap='gray')
def noise_img(img, noise_level, white_value=2):
x = img.clone().reshape(-1)
numNoiseBits = int(x.shape[0] * noise_level)
noise = np.random.permutation(x.shape[0])[0:numNoiseBits]
x[noise] = white_value
x = x.reshape(img.shape)
return x
def bimshow(batch):
plt.clf()
noise_levels = [0, 0.1, 0.2, 0.4]
fig, axs = plt.subplots(1, len(noise_levels))
with torch.no_grad():
for i, noise_level in enumerate(noise_levels):
noisy = noise_img(batch, noise_level)
output = model(noisy.to(DEVICE)).cpu()
imshow(axs[i], torch.cat((noisy.view(-1, 28), output.view(-1, 28)), 1))
plt.show()
class Autoencoder(nn.Module):
def __init__(self, width, height, bottleneck):
self.width = width
self.height = height
super(Autoencoder, self).__init__()
self.conv1 = nn.Conv2d(1, 2, kernel_size=5)
self.conv2 = nn.Conv2d(2, 4, kernel_size=5)
self.conv3 = nn.Conv2d(4, 8, kernel_size=5)
self.hidden_size = (self.width - 4 * 3) * (self.height - 4 * 3) * 8
self.lin1 = nn.Linear(self.hidden_size, bottleneck)
self.lin2 = nn.Linear(bottleneck, self.hidden_size)
self.conv4 = nn.ConvTranspose2d(8, 4, kernel_size=5)
self.conv5 = nn.ConvTranspose2d(4, 2, kernel_size=5)
self.conv6 = nn.ConvTranspose2d(2, 1, kernel_size=5)
def forward(self, x):
batch_size = x.size()[0]
x = self.conv1(x)
x = F.relu(x, True)
x = self.conv2(x)
x = F.relu(x, True)
x = self.conv3(x)
x = F.relu(x, True)
x = x.view(batch_size, self.hidden_size)
x = self.lin1(x)
x = F.relu(x, True)
x = self.lin2(x)
x = F.relu(x, True)
x = x.view(batch_size, 8, self.width - 4 * 3, self.height - 4 * 3)
x = self.conv4(x)
x = F.relu(x, True)
x = self.conv5(x)
x = F.relu(x, True)
x = self.conv6(x)
x = F.relu(x, True)
# x = torch.sigmoid(x)
return x
# Defining Parameters
EPOCHS = 1000
model = Autoencoder(28, 28, 4).to(DEVICE)
distance = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), weight_decay=1e-5)
outer_bar = tqdm(total=EPOCHS, position=0)
inner_bar = tqdm(total=len(trainset), position=1)
outer_bar.set_description("Epochs")
for epoch in range(EPOCHS):
inner_bar.reset()
for data in dataloader:
img, _ = data
img = img.to(DEVICE)
# ===================forward=====================
output = model(img)
loss = distance(output, img)
# ===================backward====================
optimizer.zero_grad()
loss.backward()
optimizer.step()
inner_bar.update(BATCH_SIZE)
inner_bar.set_description("Avg loss %.2f" % (loss.item() / BATCH_SIZE))
outer_bar.update(1)
bimshow(next(iter(dataloader))[0])