-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
76 lines (54 loc) · 2.32 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
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
import torch
import numpy
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
class Residual_block(nn.Module):
def __init__(self, input_nc):
super(Residual_block, self).__init__()
model = [nn.Conv2d(input_nc, input_nc, kernel_size=3, stride=1, padding=1, bias=False),
nn.InstanceNorm2d(input_nc, affine=True, track_running_stats=True),
nn.ReLU(inplace=False),
nn.Conv2d(input_nc, input_nc, kernel_size =3, stride=1, padding=1, bias=False),
nn.InstanceNorm2d(input_nc, affine=True, track_running_stats=True)]
self.model = nn.Sequential(*model)
def forward(self, x):
return self.model(x)
class Encoder(nn.Module):
def __init__(self, input_nc, output_nc, resblocks=6):
super(Encoder, self).__init__()
model = [nn.Conv2d(input_nc, 64, kernel_size=3, stride=3, padding=3),
nn.ReLU(inplace=False),
nn.MaxPool2d(2, stride=2),]
in_features = 64
out_features = in_features*2
for i in range(resblocks):
model += [Residual_block(in_features)]
model += [nn.Conv2d(in_features, 8, kernel_size=3, stride=2, padding=1),
nn.ReLU(inplace=False),
nn.MaxPool2d(2, stride=1)]
self.model = nn.Sequential(*model)
def forward(self, x):
torch.autograd.set_detect_anomaly(True)
return self.model(x)
class Decoder(nn.Module):
def __init__(self, input_nc, output_nc, resblocks=6):
super(Decoder, self).__init__()
in_features = 64
out_features = in_features*2
model = [nn.ConvTranspose2d(8, in_features, kernel_size=3, stride=2 ),
nn.ReLU(inplace=False)]
for _ in range(resblocks):
model += [Residual_block(in_features)]
model += [nn.ConvTranspose2d(in_features, 8, kernel_size=5, stride=3, padding=1),
nn.ReLU(inplace=False),
nn.ConvTranspose2d(8, output_nc, kernel_size=2, stride=2, padding=1),
nn.Tanh()]
self.model = nn.Sequential(*model)
def forward(self, x):
torch.autograd.set_detect_anomaly(True)
return self.model(x)
if __name__ == "__main__":
D = Decoder(3,3)
E = Encoder(3,3)
print(D,E)