-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathImage_Networks.py
149 lines (121 loc) · 4.68 KB
/
Image_Networks.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import torch
import torch.nn as nn
import torchvision.models as backbone_
class EncoderCNN(nn.Module):
def __init__(self, hp=None):
super(EncoderCNN, self).__init__()
self.feature = backbone_.vgg16(pretrained=True).features
self.pool_method = nn.AdaptiveMaxPool2d(1)
self.fc_mu = nn.Linear(512, 128)
self.fc_std = nn.Linear(512, 128)
def forward(self, x):
backbone_feature = self.feature(x)
x = torch.flatten(self.pool_method(backbone_feature), start_dim=1)
mean = self.fc_mu(x)
log_var = self.fc_std(x)
posterior_dist = torch.distributions.Normal(mean, torch.exp(0.5 * log_var))
return backbone_feature, posterior_dist
# class EncoderCNN(nn.Module):
# def __init__(self, hp=None):
# super(EncoderCNN, self).__init__()
# self.feature = Unet_Encoder(in_channels=3)
# self.fc_mu = nn.Linear(512, 128)
# self.fc_std = nn.Linear(512, 128)
#
# def forward(self, x):
# x = self.feature(x)
# mean = self.fc_mu(x)
# log_var = self.fc_std(x)
# posterior_dist = torch.distributions.Normal(mean, torch.exp(0.5 * log_var))
# return posterior_dist
class DecoderCNN(nn.Module):
def __init__(self, hp=None):
super(DecoderCNN, self).__init__()
self.model = Unet_Decoder(out_channels=3)
def forward(self, x):
return self.model(x)
class Unet_Encoder(nn.Module):
def __init__(self, in_channels=3):
super(Unet_Encoder, self).__init__()
self.down_1 = Unet_DownBlock(in_channels, 32, normalize=False)
self.down_2 = Unet_DownBlock(32, 64)
self.down_3 = Unet_DownBlock(64, 128)
self.down_4 = Unet_DownBlock(128, 256)
self.down_5 = Unet_DownBlock(256, 256)
self.linear_encoder = nn.Linear(256 * 8 * 8, 512)
self.dropout = nn.Dropout(0.5)
def forward(self, x):
x = self.down_1(x)
x = self.down_2(x)
x = self.down_3(x)
x = self.down_4(x)
x = self.down_5(x)
x = torch.flatten(x, start_dim=1)
x = self.linear_encoder(x)
x = self.dropout(x)
return x
class Unet_Decoder(nn.Module):
def __init__(self, out_channels=3):
super(Unet_Decoder, self).__init__()
self.linear_1 = nn.Linear(128, 8*8*256)
self.dropout = nn.Dropout(0.5)
self.deconv_1 = Unet_UpBlock(256, 256)
self.deconv_2 = Unet_UpBlock(256, 128)
self.deconv_3 = Unet_UpBlock(128, 64)
self.deconv_4 = Unet_UpBlock(64, 32)
self.final_image = nn.Sequential(*[nn.ConvTranspose2d(32, out_channels,
kernel_size=4, stride=2,
padding=1), nn.Tanh()])
def forward(self, x):
x = self.linear_1(x)
x = x.view(-1, 256, 8, 8)
x = self.dropout(x)
x = self.deconv_1(x)
x = self.deconv_2(x)
x = self.deconv_3(x)
x = self.deconv_4(x)
x = self.final_image(x)
return x
class Unet_UpBlock(nn.Module):
def __init__(self, inner_nc, outer_nc):
super(Unet_UpBlock, self).__init__()
layers = [
nn.ConvTranspose2d(inner_nc, outer_nc, 4, 2, 1, bias=True),
nn.InstanceNorm2d(outer_nc),
nn.ReLU(inplace=True),
]
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)
class Unet_DownBlock(nn.Module):
def __init__(self, inner_nc, outer_nc, normalize=True):
super(Unet_DownBlock, self).__init__()
layers = [nn.Conv2d(inner_nc, outer_nc, kernel_size=4, stride=2, padding=1, bias=True)]
if normalize:
layers.append(nn.InstanceNorm2d(outer_nc))
layers.append(nn.LeakyReLU(0.2, True))
self.model = nn.Sequential(*layers)
def forward(self, x):
return self.model(x)
class VGG_encoder(nn.Module):
def __init__(self, hp):
super(VGG_encoder, self).__init__()
self.feature = backbone_.vgg16(pretrained=True).features
self.pool_method = nn.AdaptiveMaxPool2d(1)
self.dropout = nn.Dropout(0.5)
def forward(self, x):
x = self.backbone(input)
x = self.pool_method(x).view(-1, 512)
x = self.dropout(x)
return x
def weights_init_normal(m):
classname = m.__class__.__name__
if classname.find("Conv") != -1:
torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
if hasattr(m, "bias") and m.bias is not None:
torch.nn.init.constant_(m.bias.data, 0.0)
elif classname.find("BatchNorm2d") != -1:
torch.nn.init.normal_(m.weight.data, 1.0, 0.02)
torch.nn.init.constant_(m.bias.data, 0.0)
if __name__ == '__main__':
pass