|
| 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