-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
125 lines (90 loc) · 3.76 KB
/
models.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import torch
import torch.nn as nn
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(ResidualBlock, self).__init__()
layer = lambda in_channels, out_channels: [
nn.Conv2d(in_channels, out_channels, 3, 1, 1, padding_mode="reflect"),
nn.InstanceNorm2d(out_channels),
nn.GELU()]
self.model = nn.Sequential(*(layer(in_channels, out_channels) + layer(in_channels, out_channels)))
def forward(self, x):
return self.model(x) + x
class DownBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3):
super(DownBlock, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size, 2, 1, padding_mode="reflect"),
nn.InstanceNorm2d(out_channels),
nn.GELU()
)
def forward(self, x):
return self.model(x)
class UpBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(UpBlock, self).__init__()
self.model = nn.Sequential(
nn.ConvTranspose2d(in_channels, out_channels, 3, 2, 1, output_padding=1),
nn.InstanceNorm2d(out_channels),
nn.GELU()
)
def forward(self, x):
return self.model(x)
class Generator(nn.Module):
def __init__(self, in_channels, hidden_channels, n_downs=2, n_residuals=9):
super(Generator, self).__init__()
layers = []
# Define initial conv layer
layers.append(nn.Sequential(nn.Conv2d(in_channels, hidden_channels, 7, 1, 3, padding_mode="reflect"), nn.GELU()))
# Define down blocks
for i in range(n_downs):
layers.append(DownBlock(2**i*hidden_channels, 2**(i+1)*hidden_channels))
# Define residual blocks
for _ in range(n_residuals):
layers.append(ResidualBlock(2**n_downs*hidden_channels, 2**n_downs*hidden_channels))
# Define up blocks
for i in range(n_downs, 0, -1):
layers.append(UpBlock(2**i*hidden_channels, 2**(i-1)*hidden_channels))
# Define final conv layer
layers.append(nn.Sequential(nn.Conv2d(hidden_channels, in_channels, 7, 1, 3, padding_mode="reflect"), nn.Tanh()))
# Sequentialize model
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)
class Discriminator(nn.Module):
def __init__(self, in_channels, hidden_channels, n_downs=3):
super(Discriminator, self).__init__()
layers = []
# Define initial conv block
layers.append(nn.Sequential(nn.Conv2d(in_channels, hidden_channels, 7, 1, 3, padding_mode="reflect"), nn.GELU()))
# Define down blocks
for i in range(n_downs):
layers.append(DownBlock(2**i*hidden_channels, 2**(i+1)*hidden_channels, 4))
# Define final conv block
layers.append(nn.Conv2d(2**n_downs*hidden_channels, 1, 4, 1, 1))
# Sequentialize model
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)
if __name__ == "__main__":
x = torch.randn(2, 8, 16, 16)
res_block = ResidualBlock(8, 8)
print(x.shape)
print(res_block(x).shape)
down_block = DownBlock(8, 16)
print(x.shape)
print(down_block(x).shape)
x = torch.randn(4, 2, 16, 16)
up_block = UpBlock(2, 1)
print(x.shape)
print(up_block(x).shape)
device = torch.device("mps")
x = torch.randn(4, 3, 16, 16).to(device)
gen = Generator(3, 16).to(device)
print(x.shape)
print(gen(x).shape)
print("Disc")
x = torch.randn(4, 3, 256, 256, device=device)
disc = Discriminator(3, 64).to(device)
print(x.shape)
print(disc(x).shape)