-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
42 lines (38 loc) · 1.77 KB
/
model.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
import torch.nn as nn
# I used this class to create an autoencoder, decoder and encoder can be implemented as different classes
class DenoisingAutoEnc(nn.Module):
def __init__(self):
super(DenoisingAutoEnc, self).__init__()
self.img_size = 32
self.create_encoder()
self.create_decoder()
# downsampling to obtain latent space(encoding)
def create_encoder(self):
self.encoder = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3,stride=2, padding=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(32),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=2, padding=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(64),
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=2, padding=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(128)
)
# upsampling model to recreate the original resolution from the latent space representation
def create_decoder(self):
self.decoder = nn.Sequential(
nn.ConvTranspose2d(in_channels=128, out_channels=64, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(64),
nn.ConvTranspose2d(in_channels=64, out_channels=32, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(32),
nn.ConvTranspose2d(in_channels=32, out_channels=3, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.Sigmoid()
)
# model workflow
def forward(self,input_image):
latent_space = self.encoder(input_image)
output_image = self.decoder(latent_space)
return output_image