|
|
| import torch |
| import torch.nn as nn |
|
|
| class Autoencoder(nn.Module): |
| def __init__(self): |
| super(Autoencoder, self).__init__() |
|
|
| self.encoder = nn.Sequential( |
| nn.Conv2d(3, 64, 4, 2, 1), |
| nn.ReLU(), |
| nn.Conv2d(64, 128, 4, 2, 1), |
| nn.BatchNorm2d(128), |
| nn.ReLU(), |
| nn.Conv2d(128, 256, 4, 2, 1), |
| nn.BatchNorm2d(256), |
| nn.ReLU(), |
| nn.Conv2d(256, 512, 4, 2, 1), |
| nn.ReLU() |
| ) |
|
|
| self.decoder = nn.Sequential( |
| nn.ConvTranspose2d(512, 256, 4, 2, 1), |
| nn.BatchNorm2d(256), |
| nn.ReLU(), |
| nn.ConvTranspose2d(256, 128, 4, 2, 1), |
| nn.BatchNorm2d(128), |
| nn.ReLU(), |
| nn.ConvTranspose2d(128, 64, 4, 2, 1), |
| nn.ReLU(), |
| nn.ConvTranspose2d(64, 3, 4, 2, 1), |
| nn.Sigmoid() |
| ) |
|
|
| def forward(self, x): |
| return self.decoder(self.encoder(x)) |
|
|