Skip to content

Commit 967cd49

Browse files
committed
add denoise_autoencoder.py
1 parent 45bccec commit 967cd49

File tree

3 files changed

+284
-2
lines changed

3 files changed

+284
-2
lines changed

chapter5_卷积神经网络/mycnn.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ def forward(self, x):
5959

6060

6161
# 打印模型
62-
print(Cnn)
63-
6462
model = Cnn(1, 10) # 图片大小是28x28, 10
6563

64+
# 打印模型
65+
print(model)
66+
6667
# 定义loss和optimizer
6768
criterion = nn.CrossEntropyLoss()
6869
optimizer = optim.SGD(model.parameters(), lr=learning_rate)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import os
2+
import pdb
3+
import torch
4+
import torchvision
5+
from torch import nn
6+
from torch.autograd import Variable
7+
from torch.utils.data import DataLoader
8+
from torchvision import transforms
9+
from torchvision.datasets import MNIST
10+
from torchvision.utils import save_image
11+
from torchvision import datasets
12+
import matplotlib.pyplot as plt
13+
14+
# 配置参数
15+
torch.manual_seed(1) #设置随机数种子,确保结果可重复
16+
batch_size = 128 #批处理大小
17+
learning_rate = 1e-2 #学习率
18+
num_epochs = 10 #训练次数
19+
20+
#下载训练集 MNIST 手写数字训练集
21+
train_dataset = datasets.MNIST(
22+
root='./data', #数据保持的位置
23+
train=True, # 训练集
24+
transform=transforms.ToTensor(),# 一个取值范围是[0,255]的PIL.Image
25+
# 转化为取值范围是[0,1.0]的torch.FloadTensor
26+
download=True) #下载数据
27+
28+
test_dataset = datasets.MNIST(
29+
root='./data',
30+
train=False, # 测试集
31+
transform=transforms.ToTensor())
32+
33+
#pdb.set_trace()
34+
#数据的批处理,尺寸大小为batch_size,
35+
#在训练集中,shuffle 必须设置为True, 表示次序是随机的
36+
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
37+
test_loader = DataLoader(test_dataset, batch_size=10000, shuffle=False)
38+
39+
class autoencoder(nn.Module):
40+
def __init__(self):
41+
super(autoencoder, self).__init__()
42+
self.encoder = nn.Sequential(
43+
nn.Linear(28 * 28, 1000),
44+
nn.ReLU(True),
45+
nn.Linear(1000, 500),
46+
nn.ReLU(True),
47+
nn.Linear(500, 250),
48+
nn.ReLU(True),
49+
nn.Linear(250, 2)
50+
)
51+
self.decoder = nn.Sequential(
52+
nn.Linear(2, 250),
53+
nn.ReLU(True),
54+
nn.Linear(250, 500),
55+
nn.ReLU(True),
56+
nn.Linear(500, 1000),
57+
nn.ReLU(True),
58+
nn.Linear(1000, 28 * 28),
59+
nn.Tanh())
60+
61+
def forward(self, x):
62+
x = self.encoder(x)
63+
x = self.decoder(x)
64+
return x
65+
66+
#model = autoencoder().cuda()
67+
model = autoencoder()
68+
criterion = nn.MSELoss()
69+
optimizer = torch.optim.Adam(
70+
model.parameters(), lr=learning_rate, weight_decay=1e-5)
71+
72+
for epoch in range(num_epochs):
73+
for data in train_loader:
74+
img, _ = data
75+
img = img.view(img.size(0), -1)
76+
#img = Variable(img).cuda()
77+
img = Variable(img)
78+
# ===================forward=====================
79+
output = model(img)
80+
loss = criterion(output, img)
81+
# ===================backward====================
82+
optimizer.zero_grad()
83+
loss.backward()
84+
optimizer.step()
85+
# ===================log========================
86+
print('epoch [{}/{}], loss:{:.4f}'
87+
.format(epoch + 1, num_epochs, loss.data.item()))
88+
89+
90+
#模型测试, 由于训练和测试 BatchNorm, Dropout配置不同,需要说明是否模型测试
91+
model.eval()
92+
eval_loss = 0
93+
import pdb
94+
#pdb.set_trace()
95+
for data in test_loader: #test set 批处理
96+
img, label = data
97+
98+
img = img.view(img.size(0), -1)
99+
#img = Variable(img, volatile=True).cuda() # volatile 确定你是否不调用.backward(), 测试中不需要
100+
img = Variable(img, volatile=True)
101+
label = Variable(label, volatile=True)
102+
out = model(img) # 前向算法
103+
out = out.detach().numpy()
104+
y = (label.data).numpy()
105+
plt.scatter(out[:, 0], out[:, 1], c = y)
106+
plt.colorbar()
107+
plt.title('audocoder of MNIST test dataset')
108+
plt.show()
109+
110+
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Simple Convolutional Autoencoder
2+
import torch
3+
import torch.nn as nn
4+
import torch.utils as utils
5+
from torch.autograd import Variable
6+
import torchvision.datasets as dset
7+
import torchvision.transforms as transforms
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
# 配置参数
11+
torch.manual_seed(1) #设置随机数种子,确保结果可重复
12+
n_epoch = 200 #训练次数
13+
batch_size = 100 #批处理大小
14+
learning_rate = 0.0002 #学习率
15+
16+
#下载训练集 MNIST 手写数字训练集
17+
mnist_train = dset.MNIST("./", train=True, transform=transforms.ToTensor(), target_transform=None, download=True)
18+
train_loader = torch.utils.data.DataLoader(dataset=mnist_train,batch_size=batch_size,shuffle=True)
19+
20+
# Encoder 模型设置
21+
class Encoder(nn.Module):
22+
def __init__(self):
23+
super(Encoder,self).__init__()
24+
self.layer1 = nn.Sequential(
25+
nn.Conv2d(1,32,3,padding=1), # batch x 32 x 28 x 28
26+
nn.ReLU(),
27+
nn.BatchNorm2d(32),
28+
nn.Conv2d(32,32,3,padding=1), # batch x 32 x 28 x 28
29+
nn.ReLU(),
30+
nn.BatchNorm2d(32),
31+
nn.Conv2d(32,64,3,padding=1), # batch x 64 x 28 x 28
32+
nn.ReLU(),
33+
nn.BatchNorm2d(64),
34+
nn.Conv2d(64,64,3,padding=1), # batch x 64 x 28 x 28
35+
nn.ReLU(),
36+
nn.BatchNorm2d(64),
37+
nn.MaxPool2d(2,2) # batch x 64 x 14 x 14
38+
)
39+
self.layer2 = nn.Sequential(
40+
nn.Conv2d(64,128,3,padding=1), # batch x 128 x 14 x 14
41+
nn.ReLU(),
42+
nn.BatchNorm2d(128),
43+
nn.Conv2d(128,128,3,padding=1), # batch x 128 x 14 x 14
44+
nn.ReLU(),
45+
nn.BatchNorm2d(128),
46+
nn.MaxPool2d(2,2),
47+
nn.Conv2d(128,256,3,padding=1), # batch x 256 x 7 x 7
48+
nn.ReLU()
49+
)
50+
51+
52+
53+
# Encoder 模型设置
54+
class Encoder(nn.Module):
55+
def __init__(self):
56+
super(Encoder,self).__init__()
57+
self.layer1 = nn.Sequential(
58+
nn.Conv2d(1,32,3,padding=1), # batch x 32 x 28 x 28
59+
nn.ReLU(),
60+
nn.BatchNorm2d(32),
61+
nn.Conv2d(32,32,3,padding=1), # batch x 32 x 28 x 28
62+
nn.ReLU(),
63+
nn.BatchNorm2d(32),
64+
nn.Conv2d(32,64,3,padding=1), # batch x 64 x 28 x 28
65+
nn.ReLU(),
66+
nn.BatchNorm2d(64),
67+
nn.Conv2d(64,64,3,padding=1), # batch x 64 x 28 x 28
68+
nn.ReLU(),
69+
nn.BatchNorm2d(64),
70+
nn.MaxPool2d(2,2) # batch x 64 x 14 x 14
71+
)
72+
self.layer2 = nn.Sequential(
73+
nn.Conv2d(64,128,3,padding=1), # batch x 128 x 14 x 14
74+
nn.ReLU(),
75+
nn.BatchNorm2d(128),
76+
nn.Conv2d(128,128,3,padding=1), # batch x 128 x 14 x 14
77+
nn.ReLU(),
78+
nn.BatchNorm2d(128),
79+
nn.MaxPool2d(2,2),
80+
nn.Conv2d(128,256,3,padding=1), # batch x 256 x 7 x 7
81+
nn.ReLU()
82+
)
83+
84+
def forward(self,x):
85+
out = self.layer1(x)
86+
out = self.layer2(out)
87+
out = out.view(batch_size, -1)
88+
return out
89+
90+
#encoder = Encoder().cuda()
91+
encoder = Encoder()
92+
# decoder模型设置
93+
94+
class Decoder(nn.Module):
95+
def __init__(self):
96+
super(Decoder,self).__init__()
97+
self.layer1 = nn.Sequential(
98+
nn.ConvTranspose2d(256,128,3,2,1,1), # batch x 128 x 14 x 14
99+
nn.ReLU(),
100+
nn.BatchNorm2d(128),
101+
nn.ConvTranspose2d(128,128,3,1,1), # batch x 128 x 14 x 14
102+
nn.ReLU(),
103+
nn.BatchNorm2d(128),
104+
nn.ConvTranspose2d(128,64,3,1,1), # batch x 64 x 14 x 14
105+
nn.ReLU(),
106+
nn.BatchNorm2d(64),
107+
nn.ConvTranspose2d(64,64,3,1,1), # batch x 64 x 14 x 14
108+
nn.ReLU(),
109+
nn.BatchNorm2d(64)
110+
)
111+
self.layer2 = nn.Sequential(
112+
nn.ConvTranspose2d(64,32,3,1,1), # batch x 32 x 14 x 14
113+
nn.ReLU(),
114+
nn.BatchNorm2d(32),
115+
nn.ConvTranspose2d(32,32,3,1,1), # batch x 32 x 14 x 14
116+
nn.ReLU(),
117+
nn.BatchNorm2d(32),
118+
nn.ConvTranspose2d(32,1,3,2,1,1), # batch x 1 x 28 x 28
119+
nn.ReLU()
120+
)
121+
122+
def forward(self,x):
123+
out = x.view(batch_size,256,7,7)
124+
out = self.layer1(out)
125+
out = self.layer2(out)
126+
return out
127+
128+
129+
#decoder = Decoder().cuda()
130+
decoder = Decoder()
131+
132+
parameters = list(encoder.parameters())+ list(decoder.parameters())
133+
loss_func = nn.MSELoss()
134+
optimizer = torch.optim.Adam(parameters, lr=learning_rate)
135+
136+
# 噪声
137+
noise = torch.rand(batch_size,1,28,28)
138+
for i in range(n_epoch):
139+
for image,label in train_loader:
140+
image_n = torch.mul(image+0.25, 0.1 * noise)
141+
#image = Variable(image).cuda()
142+
image = Variable(image)
143+
#image_n = Variable(image_n).cuda()
144+
image_n = Variable(image_n)
145+
optimizer.zero_grad()
146+
output = encoder(image_n)
147+
output = decoder(output)
148+
loss = loss_func(output,image)
149+
loss.backward()
150+
optimizer.step()
151+
break
152+
print('epoch [{}/{}], loss:{:.4f}'
153+
.format(i + 1, n_epoch, loss.data.item()))
154+
155+
156+
157+
img = image[0].cpu()
158+
input_img = image_n[0].cpu()
159+
output_img = output[0].cpu()
160+
origin = img.data.numpy()
161+
inp = input_img.data.numpy()
162+
out = output_img.data.numpy()
163+
plt.figure('denoising autodecoder')
164+
plt.subplot(131)
165+
plt.imshow(origin[0],cmap='gray')
166+
plt.subplot(132)
167+
plt.imshow(inp[0],cmap='gray')
168+
plt.subplot(133)
169+
plt.imshow(out[0],cmap="gray")
170+
plt.show()
171+
print(label[0])

0 commit comments

Comments
 (0)