Myosotis-Researches 0.0.12__py3-none-any.whl → 0.0.14__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- myosotis_researches/CcGAN/models_128/CcGAN_SAGAN.py +301 -0
- myosotis_researches/CcGAN/models_128/ResNet_class_eval.py +141 -0
- myosotis_researches/CcGAN/models_128/ResNet_embed.py +188 -0
- myosotis_researches/CcGAN/models_128/ResNet_regre_eval.py +175 -0
- myosotis_researches/CcGAN/models_128/__init__.py +8 -0
- myosotis_researches/CcGAN/models_128/autoencoder.py +119 -0
- myosotis_researches/CcGAN/models_128/cGAN_SAGAN.py +276 -0
- myosotis_researches/CcGAN/models_128/cGAN_concat_SAGAN.py +245 -0
- myosotis_researches/CcGAN/models_256/CcGAN_SAGAN.py +303 -0
- myosotis_researches/CcGAN/models_256/ResNet_class_eval.py +142 -0
- myosotis_researches/CcGAN/models_256/ResNet_embed.py +188 -0
- myosotis_researches/CcGAN/models_256/ResNet_regre_eval.py +178 -0
- myosotis_researches/CcGAN/models_256/__init__.py +8 -0
- myosotis_researches/CcGAN/models_256/autoencoder.py +133 -0
- myosotis_researches/CcGAN/models_256/cGAN_SAGAN.py +280 -0
- myosotis_researches/CcGAN/models_256/cGAN_concat_SAGAN.py +249 -0
- myosotis_researches/CcGAN/utils/make_h5.py +13 -9
- {myosotis_researches-0.0.12.dist-info → myosotis_researches-0.0.14.dist-info}/METADATA +1 -1
- myosotis_researches-0.0.14.dist-info/RECORD +28 -0
- myosotis_researches-0.0.12.dist-info/RECORD +0 -12
- {myosotis_researches-0.0.12.dist-info → myosotis_researches-0.0.14.dist-info}/WHEEL +0 -0
- {myosotis_researches-0.0.12.dist-info → myosotis_researches-0.0.14.dist-info}/licenses/LICENSE +0 -0
- {myosotis_researches-0.0.12.dist-info → myosotis_researches-0.0.14.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,175 @@
|
|
1
|
+
'''
|
2
|
+
codes are based on
|
3
|
+
@article{
|
4
|
+
zhang2018mixup,
|
5
|
+
title={mixup: Beyond Empirical Risk Minimization},
|
6
|
+
author={Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin, David Lopez-Paz},
|
7
|
+
journal={International Conference on Learning Representations},
|
8
|
+
year={2018},
|
9
|
+
url={https://openreview.net/forum?id=r1Ddp1-Rb},
|
10
|
+
}
|
11
|
+
'''
|
12
|
+
|
13
|
+
|
14
|
+
import torch
|
15
|
+
import torch.nn as nn
|
16
|
+
import torch.nn.functional as F
|
17
|
+
|
18
|
+
NC = 3
|
19
|
+
IMG_SIZE = 128
|
20
|
+
|
21
|
+
|
22
|
+
class BasicBlock(nn.Module):
|
23
|
+
expansion = 1
|
24
|
+
|
25
|
+
def __init__(self, in_planes, planes, stride=1):
|
26
|
+
super(BasicBlock, self).__init__()
|
27
|
+
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
|
28
|
+
self.bn1 = nn.BatchNorm2d(planes)
|
29
|
+
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
|
30
|
+
self.bn2 = nn.BatchNorm2d(planes)
|
31
|
+
|
32
|
+
self.shortcut = nn.Sequential()
|
33
|
+
if stride != 1 or in_planes != self.expansion*planes:
|
34
|
+
self.shortcut = nn.Sequential(
|
35
|
+
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
|
36
|
+
nn.BatchNorm2d(self.expansion*planes)
|
37
|
+
)
|
38
|
+
|
39
|
+
def forward(self, x):
|
40
|
+
out = F.relu(self.bn1(self.conv1(x)))
|
41
|
+
out = self.bn2(self.conv2(out))
|
42
|
+
out += self.shortcut(x)
|
43
|
+
out = F.relu(out)
|
44
|
+
return out
|
45
|
+
|
46
|
+
|
47
|
+
class Bottleneck(nn.Module):
|
48
|
+
expansion = 4
|
49
|
+
|
50
|
+
def __init__(self, in_planes, planes, stride=1):
|
51
|
+
super(Bottleneck, self).__init__()
|
52
|
+
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
|
53
|
+
self.bn1 = nn.BatchNorm2d(planes)
|
54
|
+
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
|
55
|
+
self.bn2 = nn.BatchNorm2d(planes)
|
56
|
+
self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)
|
57
|
+
self.bn3 = nn.BatchNorm2d(self.expansion*planes)
|
58
|
+
|
59
|
+
self.shortcut = nn.Sequential()
|
60
|
+
if stride != 1 or in_planes != self.expansion*planes:
|
61
|
+
self.shortcut = nn.Sequential(
|
62
|
+
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
|
63
|
+
nn.BatchNorm2d(self.expansion*planes)
|
64
|
+
)
|
65
|
+
|
66
|
+
def forward(self, x):
|
67
|
+
out = F.relu(self.bn1(self.conv1(x)))
|
68
|
+
out = F.relu(self.bn2(self.conv2(out)))
|
69
|
+
out = self.bn3(self.conv3(out))
|
70
|
+
out += self.shortcut(x)
|
71
|
+
out = F.relu(out)
|
72
|
+
return out
|
73
|
+
|
74
|
+
|
75
|
+
class ResNet_regre_eval(nn.Module):
|
76
|
+
def __init__(self, block, num_blocks, nc=NC, ngpu = 1, feature_layer='f3'):
|
77
|
+
super(ResNet_regre_eval, self).__init__()
|
78
|
+
self.in_planes = 64
|
79
|
+
self.ngpu = ngpu
|
80
|
+
self.feature_layer=feature_layer
|
81
|
+
|
82
|
+
self.block1 = nn.Sequential(
|
83
|
+
nn.Conv2d(nc, 64, kernel_size=3, stride=1, padding=1, bias=False), # h=h
|
84
|
+
nn.BatchNorm2d(64),
|
85
|
+
nn.ReLU(),
|
86
|
+
nn.MaxPool2d(2,2), #h=h/2 64
|
87
|
+
self._make_layer(block, 64, num_blocks[0], stride=2), # h=h/2 32
|
88
|
+
)
|
89
|
+
self.block2 = self._make_layer(block, 128, num_blocks[1], stride=2) # h=h/2 16
|
90
|
+
self.block3 = self._make_layer(block, 256, num_blocks[2], stride=2) # h=h/2 8
|
91
|
+
self.block4 = self._make_layer(block, 512, num_blocks[3], stride=2) # h=h/2 4
|
92
|
+
|
93
|
+
self.pool1 = nn.AvgPool2d(kernel_size=4)
|
94
|
+
if self.feature_layer == 'f2':
|
95
|
+
self.pool2 = nn.AdaptiveAvgPool2d((2,2))
|
96
|
+
elif self.feature_layer == 'f3':
|
97
|
+
self.pool2 = nn.AdaptiveAvgPool2d((2,2))
|
98
|
+
else:
|
99
|
+
self.pool2 = nn.AdaptiveAvgPool2d((1,1))
|
100
|
+
|
101
|
+
linear_layers = [
|
102
|
+
nn.Linear(512*block.expansion, 128),
|
103
|
+
nn.BatchNorm1d(128),
|
104
|
+
nn.ReLU(),
|
105
|
+
nn.Linear(128, 128),
|
106
|
+
nn.BatchNorm1d(128),
|
107
|
+
nn.ReLU(),
|
108
|
+
nn.Linear(128, 1),
|
109
|
+
# nn.Sigmoid()
|
110
|
+
nn.ReLU(),
|
111
|
+
]
|
112
|
+
self.linear = nn.Sequential(*linear_layers)
|
113
|
+
|
114
|
+
|
115
|
+
def _make_layer(self, block, planes, num_blocks, stride):
|
116
|
+
strides = [stride] + [1]*(num_blocks-1)
|
117
|
+
layers = []
|
118
|
+
for stride in strides:
|
119
|
+
layers.append(block(self.in_planes, planes, stride))
|
120
|
+
self.in_planes = planes * block.expansion
|
121
|
+
return nn.Sequential(*layers)
|
122
|
+
|
123
|
+
def forward(self, x):
|
124
|
+
|
125
|
+
if x.is_cuda and self.ngpu > 1:
|
126
|
+
ft1 = nn.parallel.data_parallel(self.block1, x, range(self.ngpu))
|
127
|
+
ft2 = nn.parallel.data_parallel(self.block2, ft1, range(self.ngpu))
|
128
|
+
ft3 = nn.parallel.data_parallel(self.block3, ft2, range(self.ngpu))
|
129
|
+
ft4 = nn.parallel.data_parallel(self.block4, ft3, range(self.ngpu))
|
130
|
+
out = nn.parallel.data_parallel(self.pool1, ft4, range(self.ngpu))
|
131
|
+
out = out.view(out.size(0), -1)
|
132
|
+
out = nn.parallel.data_parallel(self.linear, out, range(self.ngpu))
|
133
|
+
else:
|
134
|
+
ft1 = self.block1(x)
|
135
|
+
ft2 = self.block2(ft1)
|
136
|
+
ft3 = self.block3(ft2)
|
137
|
+
ft4 = self.block4(ft3)
|
138
|
+
out = self.pool1(ft4)
|
139
|
+
out = out.view(out.size(0), -1)
|
140
|
+
out = self.linear(out)
|
141
|
+
|
142
|
+
if self.feature_layer == 'f2':
|
143
|
+
ext_features = self.pool2(ft2)
|
144
|
+
elif self.feature_layer == 'f3':
|
145
|
+
ext_features = self.pool2(ft3)
|
146
|
+
else:
|
147
|
+
ext_features = self.pool2(ft4)
|
148
|
+
|
149
|
+
ext_features = ext_features.view(ext_features.size(0), -1)
|
150
|
+
|
151
|
+
return out, ext_features
|
152
|
+
|
153
|
+
|
154
|
+
def ResNet18_regre_eval(ngpu = 1):
|
155
|
+
return ResNet_regre_eval(BasicBlock, [2,2,2,2], ngpu = ngpu)
|
156
|
+
|
157
|
+
def ResNet34_regre_eval(ngpu = 1):
|
158
|
+
return ResNet_regre_eval(BasicBlock, [3,4,6,3], ngpu = ngpu)
|
159
|
+
|
160
|
+
def ResNet50_regre_eval(ngpu = 1):
|
161
|
+
return ResNet_regre_eval(Bottleneck, [3,4,6,3], ngpu = ngpu)
|
162
|
+
|
163
|
+
def ResNet101_regre_eval(ngpu = 1):
|
164
|
+
return ResNet_regre_eval(Bottleneck, [3,4,23,3], ngpu = ngpu)
|
165
|
+
|
166
|
+
def ResNet152_regre_eval(ngpu = 1):
|
167
|
+
return ResNet_regre_eval(Bottleneck, [3,8,36,3], ngpu = ngpu)
|
168
|
+
|
169
|
+
|
170
|
+
if __name__ == "__main__":
|
171
|
+
net = ResNet34_regre_eval(ngpu = 1).cuda()
|
172
|
+
x = torch.randn(4,NC,IMG_SIZE,IMG_SIZE).cuda()
|
173
|
+
out, features = net(x)
|
174
|
+
print(out.size())
|
175
|
+
print(features.size())
|
@@ -0,0 +1,8 @@
|
|
1
|
+
from .autoencoder import *
|
2
|
+
from .cGAN_SAGAN import cGAN_SAGAN_Generator, cGAN_SAGAN_Discriminator
|
3
|
+
from .cGAN_concat_SAGAN import cGAN_concat_SAGAN_Generator, cGAN_concat_SAGAN_Discriminator
|
4
|
+
from .CcGAN_SAGAN import CcGAN_SAGAN_Generator, CcGAN_SAGAN_Discriminator
|
5
|
+
from .ResNet_embed import ResNet18_embed, ResNet34_embed, ResNet50_embed, model_y2h
|
6
|
+
from .ResNet_regre_eval import ResNet34_regre_eval
|
7
|
+
from .ResNet_class_eval import ResNet34_class_eval
|
8
|
+
|
@@ -0,0 +1,119 @@
|
|
1
|
+
import torch
|
2
|
+
from torch import nn
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
class encoder(nn.Module):
|
7
|
+
def __init__(self, dim_bottleneck=512, ch=64):
|
8
|
+
super(encoder, self).__init__()
|
9
|
+
self.ch = ch
|
10
|
+
self.dim_bottleneck = dim_bottleneck
|
11
|
+
|
12
|
+
self.conv = nn.Sequential(
|
13
|
+
nn.Conv2d(3, ch, kernel_size=4, stride=2, padding=1), #h=h/2; 64
|
14
|
+
nn.BatchNorm2d(ch),
|
15
|
+
nn.ReLU(),
|
16
|
+
nn.MaxPool2d(2,2), #h=h/2; 32
|
17
|
+
nn.Conv2d(ch, ch, kernel_size=3, stride=1, padding=1), #h=h
|
18
|
+
nn.BatchNorm2d(ch),
|
19
|
+
nn.ReLU(),
|
20
|
+
|
21
|
+
nn.Conv2d(ch, ch*2, kernel_size=4, stride=2, padding=1), #h=h/2; 16
|
22
|
+
nn.BatchNorm2d(ch*2),
|
23
|
+
nn.ReLU(),
|
24
|
+
nn.Conv2d(ch*2, ch*2, kernel_size=3, stride=1, padding=1), #h=h
|
25
|
+
nn.BatchNorm2d(ch*2),
|
26
|
+
nn.ReLU(),
|
27
|
+
|
28
|
+
nn.Conv2d(ch*2, ch*4, kernel_size=4, stride=2, padding=1), #h=h/2; 8
|
29
|
+
nn.BatchNorm2d(ch*4),
|
30
|
+
nn.ReLU(),
|
31
|
+
nn.Conv2d(ch*4, ch*4, kernel_size=3, stride=1, padding=1), #h=h
|
32
|
+
nn.BatchNorm2d(ch*4),
|
33
|
+
nn.ReLU(),
|
34
|
+
|
35
|
+
nn.Conv2d(ch*4, ch*8, kernel_size=4, stride=2, padding=1), #h=h/2; 4
|
36
|
+
nn.BatchNorm2d(ch*8),
|
37
|
+
nn.ReLU(),
|
38
|
+
nn.Conv2d(ch*8, ch*8, kernel_size=3, stride=1, padding=1), #h=h
|
39
|
+
nn.BatchNorm2d(ch*8),
|
40
|
+
nn.ReLU(),
|
41
|
+
)
|
42
|
+
|
43
|
+
self.linear = nn.Sequential(
|
44
|
+
nn.Linear(ch*8*4*4, dim_bottleneck),
|
45
|
+
# nn.ReLU()
|
46
|
+
)
|
47
|
+
|
48
|
+
def forward(self, x):
|
49
|
+
feature = self.conv(x)
|
50
|
+
feature = feature.view(-1, self.ch*8*4*4)
|
51
|
+
feature = self.linear(feature)
|
52
|
+
return feature
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
class decoder(nn.Module):
|
57
|
+
def __init__(self, dim_bottleneck=512, ch=64):
|
58
|
+
super(decoder, self).__init__()
|
59
|
+
self.ch = ch
|
60
|
+
self.dim_bottleneck = dim_bottleneck
|
61
|
+
|
62
|
+
self.linear = nn.Sequential(
|
63
|
+
nn.Linear(dim_bottleneck, ch*16*4*4)
|
64
|
+
)
|
65
|
+
|
66
|
+
self.deconv = nn.Sequential(
|
67
|
+
nn.ConvTranspose2d(ch*16, ch*8, kernel_size=4, stride=2, padding=1), #h=2h; 8
|
68
|
+
nn.BatchNorm2d(ch*8),
|
69
|
+
nn.ReLU(True),
|
70
|
+
nn.Conv2d(ch*8, ch*8, kernel_size=3, stride=1, padding=1), #h=h
|
71
|
+
nn.BatchNorm2d(ch*8),
|
72
|
+
nn.ReLU(),
|
73
|
+
|
74
|
+
nn.ConvTranspose2d(ch*8, ch*4, kernel_size=4, stride=2, padding=1), #h=2h; 16
|
75
|
+
nn.BatchNorm2d(ch*4),
|
76
|
+
nn.ReLU(True),
|
77
|
+
nn.Conv2d(ch*4, ch*4, kernel_size=3, stride=1, padding=1), #h=h
|
78
|
+
nn.BatchNorm2d(ch*4),
|
79
|
+
nn.ReLU(),
|
80
|
+
|
81
|
+
nn.ConvTranspose2d(ch*4, ch*2, kernel_size=4, stride=2, padding=1), #h=2h; 32
|
82
|
+
nn.BatchNorm2d(ch*2),
|
83
|
+
nn.ReLU(True),
|
84
|
+
nn.Conv2d(ch*2, ch*2, kernel_size=3, stride=1, padding=1), #h=h
|
85
|
+
nn.BatchNorm2d(ch*2),
|
86
|
+
nn.ReLU(),
|
87
|
+
|
88
|
+
nn.ConvTranspose2d(ch*2, ch, kernel_size=4, stride=2, padding=1), #h=2h; 64
|
89
|
+
nn.BatchNorm2d(ch),
|
90
|
+
nn.ReLU(True),
|
91
|
+
nn.Conv2d(ch, ch, kernel_size=3, stride=1, padding=1), #h=h
|
92
|
+
nn.BatchNorm2d(ch),
|
93
|
+
nn.ReLU(),
|
94
|
+
|
95
|
+
nn.ConvTranspose2d(ch, ch, kernel_size=4, stride=2, padding=1), #h=2h; 128
|
96
|
+
nn.BatchNorm2d(ch),
|
97
|
+
nn.ReLU(True),
|
98
|
+
nn.Conv2d(ch, 3, kernel_size=3, stride=1, padding=1), #h=h
|
99
|
+
nn.Tanh()
|
100
|
+
)
|
101
|
+
|
102
|
+
def forward(self, feature):
|
103
|
+
out = self.linear(feature)
|
104
|
+
out = out.view(-1, self.ch*16, 4, 4)
|
105
|
+
out = self.deconv(out)
|
106
|
+
return out
|
107
|
+
|
108
|
+
|
109
|
+
if __name__=="__main__":
|
110
|
+
#test
|
111
|
+
|
112
|
+
net_encoder = encoder(dim_bottleneck=512, ch=64).cuda()
|
113
|
+
net_decoder = decoder(dim_bottleneck=512, ch=64).cuda()
|
114
|
+
|
115
|
+
x = torch.randn(10, 3, 128, 128).cuda()
|
116
|
+
f = net_encoder(x)
|
117
|
+
xh = net_decoder(f)
|
118
|
+
print(f.size())
|
119
|
+
print(xh.size())
|
@@ -0,0 +1,276 @@
|
|
1
|
+
import numpy as np
|
2
|
+
import torch
|
3
|
+
import torch.nn as nn
|
4
|
+
import torch.nn.functional as F
|
5
|
+
|
6
|
+
from torch.nn.utils import spectral_norm
|
7
|
+
from torch.nn.init import xavier_uniform_
|
8
|
+
|
9
|
+
|
10
|
+
def init_weights(m):
|
11
|
+
if type(m) == nn.Linear or type(m) == nn.Conv2d:
|
12
|
+
xavier_uniform_(m.weight)
|
13
|
+
m.bias.data.fill_(0.)
|
14
|
+
|
15
|
+
|
16
|
+
def snconv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True):
|
17
|
+
return spectral_norm(nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size,
|
18
|
+
stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias))
|
19
|
+
|
20
|
+
|
21
|
+
def snlinear(in_features, out_features):
|
22
|
+
return spectral_norm(nn.Linear(in_features=in_features, out_features=out_features))
|
23
|
+
|
24
|
+
|
25
|
+
def sn_embedding(num_embeddings, embedding_dim):
|
26
|
+
return spectral_norm(nn.Embedding(num_embeddings=num_embeddings, embedding_dim=embedding_dim))
|
27
|
+
|
28
|
+
|
29
|
+
class Self_Attn(nn.Module):
|
30
|
+
""" Self attention Layer"""
|
31
|
+
|
32
|
+
def __init__(self, in_channels):
|
33
|
+
super(Self_Attn, self).__init__()
|
34
|
+
self.in_channels = in_channels
|
35
|
+
self.snconv1x1_theta = snconv2d(in_channels=in_channels, out_channels=in_channels//8, kernel_size=1, stride=1, padding=0)
|
36
|
+
self.snconv1x1_phi = snconv2d(in_channels=in_channels, out_channels=in_channels//8, kernel_size=1, stride=1, padding=0)
|
37
|
+
self.snconv1x1_g = snconv2d(in_channels=in_channels, out_channels=in_channels//2, kernel_size=1, stride=1, padding=0)
|
38
|
+
self.snconv1x1_attn = snconv2d(in_channels=in_channels//2, out_channels=in_channels, kernel_size=1, stride=1, padding=0)
|
39
|
+
self.maxpool = nn.MaxPool2d(2, stride=2, padding=0)
|
40
|
+
self.softmax = nn.Softmax(dim=-1)
|
41
|
+
self.sigma = nn.Parameter(torch.zeros(1))
|
42
|
+
|
43
|
+
def forward(self, x):
|
44
|
+
"""
|
45
|
+
inputs :
|
46
|
+
x : input feature maps(B X C X W X H)
|
47
|
+
returns :
|
48
|
+
out : self attention value + input feature
|
49
|
+
attention: B X N X N (N is Width*Height)
|
50
|
+
"""
|
51
|
+
_, ch, h, w = x.size()
|
52
|
+
# Theta path
|
53
|
+
theta = self.snconv1x1_theta(x)
|
54
|
+
theta = theta.view(-1, ch//8, h*w)
|
55
|
+
# Phi path
|
56
|
+
phi = self.snconv1x1_phi(x)
|
57
|
+
phi = self.maxpool(phi)
|
58
|
+
phi = phi.view(-1, ch//8, h*w//4)
|
59
|
+
# Attn map
|
60
|
+
attn = torch.bmm(theta.permute(0, 2, 1), phi)
|
61
|
+
attn = self.softmax(attn)
|
62
|
+
# g path
|
63
|
+
g = self.snconv1x1_g(x)
|
64
|
+
g = self.maxpool(g)
|
65
|
+
g = g.view(-1, ch//2, h*w//4)
|
66
|
+
# Attn_g
|
67
|
+
attn_g = torch.bmm(g, attn.permute(0, 2, 1))
|
68
|
+
attn_g = attn_g.view(-1, ch//2, h, w)
|
69
|
+
attn_g = self.snconv1x1_attn(attn_g)
|
70
|
+
# Out
|
71
|
+
out = x + self.sigma*attn_g
|
72
|
+
return out
|
73
|
+
|
74
|
+
|
75
|
+
class ConditionalBatchNorm2d(nn.Module):
|
76
|
+
# https://github.com/pytorch/pytorch/issues/8985#issuecomment-405080775
|
77
|
+
def __init__(self, num_features, num_classes):
|
78
|
+
super().__init__()
|
79
|
+
self.num_features = num_features
|
80
|
+
self.bn = nn.BatchNorm2d(num_features, momentum=0.001, affine=False)
|
81
|
+
self.embed = nn.Embedding(num_classes, num_features * 2)
|
82
|
+
# self.embed.weight.data[:, :num_features].normal_(1, 0.02) # Initialise scale at N(1, 0.02)
|
83
|
+
self.embed.weight.data[:, :num_features].fill_(1.) # Initialize scale to 1
|
84
|
+
self.embed.weight.data[:, num_features:].zero_() # Initialize bias at 0
|
85
|
+
|
86
|
+
def forward(self, x, y):
|
87
|
+
out = self.bn(x)
|
88
|
+
gamma, beta = self.embed(y).chunk(2, 1)
|
89
|
+
out = gamma.view(-1, self.num_features, 1, 1) * out + beta.view(-1, self.num_features, 1, 1)
|
90
|
+
return out
|
91
|
+
|
92
|
+
|
93
|
+
class GenBlock(nn.Module):
|
94
|
+
def __init__(self, in_channels, out_channels, num_classes):
|
95
|
+
super(GenBlock, self).__init__()
|
96
|
+
self.cond_bn1 = ConditionalBatchNorm2d(in_channels, num_classes)
|
97
|
+
self.relu = nn.ReLU(inplace=True)
|
98
|
+
self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
99
|
+
self.cond_bn2 = ConditionalBatchNorm2d(out_channels, num_classes)
|
100
|
+
self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
101
|
+
self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
|
102
|
+
|
103
|
+
def forward(self, x, labels):
|
104
|
+
x0 = x
|
105
|
+
|
106
|
+
x = self.cond_bn1(x, labels)
|
107
|
+
x = self.relu(x)
|
108
|
+
x = F.interpolate(x, scale_factor=2, mode='nearest') # upsample
|
109
|
+
x = self.snconv2d1(x)
|
110
|
+
x = self.cond_bn2(x, labels)
|
111
|
+
x = self.relu(x)
|
112
|
+
x = self.snconv2d2(x)
|
113
|
+
|
114
|
+
x0 = F.interpolate(x0, scale_factor=2, mode='nearest') # upsample
|
115
|
+
x0 = self.snconv2d0(x0)
|
116
|
+
|
117
|
+
out = x + x0
|
118
|
+
return out
|
119
|
+
|
120
|
+
|
121
|
+
class cGAN_SAGAN_Generator(nn.Module):
|
122
|
+
"""Generator."""
|
123
|
+
|
124
|
+
def __init__(self, z_dim, num_classes, g_conv_dim=64):
|
125
|
+
super(cGAN_SAGAN_Generator, self).__init__()
|
126
|
+
|
127
|
+
self.z_dim = z_dim
|
128
|
+
self.g_conv_dim = g_conv_dim
|
129
|
+
self.snlinear0 = snlinear(in_features=z_dim, out_features=g_conv_dim*16*4*4)
|
130
|
+
self.block1 = GenBlock(g_conv_dim*16, g_conv_dim*16, num_classes)
|
131
|
+
self.block2 = GenBlock(g_conv_dim*16, g_conv_dim*8, num_classes)
|
132
|
+
self.block3 = GenBlock(g_conv_dim*8, g_conv_dim*4, num_classes)
|
133
|
+
self.self_attn = Self_Attn(g_conv_dim*4)
|
134
|
+
self.block4 = GenBlock(g_conv_dim*4, g_conv_dim*2, num_classes)
|
135
|
+
self.block5 = GenBlock(g_conv_dim*2, g_conv_dim, num_classes)
|
136
|
+
self.bn = nn.BatchNorm2d(g_conv_dim, eps=1e-5, momentum=0.0001, affine=True)
|
137
|
+
self.relu = nn.ReLU(inplace=True)
|
138
|
+
self.snconv2d1 = snconv2d(in_channels=g_conv_dim, out_channels=3, kernel_size=3, stride=1, padding=1)
|
139
|
+
self.tanh = nn.Tanh()
|
140
|
+
|
141
|
+
# Weight init
|
142
|
+
self.apply(init_weights)
|
143
|
+
|
144
|
+
def forward(self, z, labels):
|
145
|
+
# n x z_dim
|
146
|
+
act0 = self.snlinear0(z) # n x g_conv_dim*16*4*4
|
147
|
+
act0 = act0.view(-1, self.g_conv_dim*16, 4, 4) # n x g_conv_dim*16 x 4 x 4
|
148
|
+
act1 = self.block1(act0, labels) # n x g_conv_dim*16 x 8 x 8
|
149
|
+
act2 = self.block2(act1, labels) # n x g_conv_dim*8 x 16 x 16
|
150
|
+
act3 = self.block3(act2, labels) # n x g_conv_dim*4 x 32 x 32
|
151
|
+
act3 = self.self_attn(act3) # n x g_conv_dim*4 x 32 x 32
|
152
|
+
act4 = self.block4(act3, labels) # n x g_conv_dim*2 x 64 x 64
|
153
|
+
act5 = self.block5(act4, labels) # n x g_conv_dim x 128 x 128
|
154
|
+
act5 = self.bn(act5) # n x g_conv_dim x 128 x 128
|
155
|
+
act5 = self.relu(act5) # n x g_conv_dim x 128 x 128
|
156
|
+
act6 = self.snconv2d1(act5) # n x 3 x 128 x 128
|
157
|
+
act6 = self.tanh(act6) # n x 3 x 128 x 128
|
158
|
+
return act6
|
159
|
+
|
160
|
+
|
161
|
+
class DiscOptBlock(nn.Module):
|
162
|
+
def __init__(self, in_channels, out_channels):
|
163
|
+
super(DiscOptBlock, self).__init__()
|
164
|
+
self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
165
|
+
self.relu = nn.ReLU(inplace=True)
|
166
|
+
self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
167
|
+
self.downsample = nn.AvgPool2d(2)
|
168
|
+
self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
|
169
|
+
|
170
|
+
def forward(self, x):
|
171
|
+
x0 = x
|
172
|
+
|
173
|
+
x = self.snconv2d1(x)
|
174
|
+
x = self.relu(x)
|
175
|
+
x = self.snconv2d2(x)
|
176
|
+
x = self.downsample(x)
|
177
|
+
|
178
|
+
x0 = self.downsample(x0)
|
179
|
+
x0 = self.snconv2d0(x0)
|
180
|
+
|
181
|
+
out = x + x0
|
182
|
+
return out
|
183
|
+
|
184
|
+
|
185
|
+
class DiscBlock(nn.Module):
|
186
|
+
def __init__(self, in_channels, out_channels):
|
187
|
+
super(DiscBlock, self).__init__()
|
188
|
+
self.relu = nn.ReLU(inplace=True)
|
189
|
+
self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
190
|
+
self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
191
|
+
self.downsample = nn.AvgPool2d(2)
|
192
|
+
self.ch_mismatch = False
|
193
|
+
if in_channels != out_channels:
|
194
|
+
self.ch_mismatch = True
|
195
|
+
self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
|
196
|
+
|
197
|
+
def forward(self, x, downsample=True):
|
198
|
+
x0 = x
|
199
|
+
|
200
|
+
x = self.relu(x)
|
201
|
+
x = self.snconv2d1(x)
|
202
|
+
x = self.relu(x)
|
203
|
+
x = self.snconv2d2(x)
|
204
|
+
if downsample:
|
205
|
+
x = self.downsample(x)
|
206
|
+
|
207
|
+
if downsample or self.ch_mismatch:
|
208
|
+
x0 = self.snconv2d0(x0)
|
209
|
+
if downsample:
|
210
|
+
x0 = self.downsample(x0)
|
211
|
+
|
212
|
+
out = x + x0
|
213
|
+
return out
|
214
|
+
|
215
|
+
|
216
|
+
class cGAN_SAGAN_Discriminator(nn.Module):
|
217
|
+
"""Discriminator."""
|
218
|
+
|
219
|
+
def __init__(self, num_classes, d_conv_dim=64):
|
220
|
+
super(cGAN_SAGAN_Discriminator, self).__init__()
|
221
|
+
self.d_conv_dim = d_conv_dim
|
222
|
+
self.opt_block1 = DiscOptBlock(3, d_conv_dim)
|
223
|
+
self.block1 = DiscBlock(d_conv_dim, d_conv_dim*2)
|
224
|
+
self.self_attn = Self_Attn(d_conv_dim*2)
|
225
|
+
self.block2 = DiscBlock(d_conv_dim*2, d_conv_dim*4)
|
226
|
+
self.block3 = DiscBlock(d_conv_dim*4, d_conv_dim*8)
|
227
|
+
self.block4 = DiscBlock(d_conv_dim*8, d_conv_dim*16)
|
228
|
+
self.block5 = DiscBlock(d_conv_dim*16, d_conv_dim*16)
|
229
|
+
self.relu = nn.ReLU(inplace=True)
|
230
|
+
self.snlinear1 = snlinear(in_features=d_conv_dim*16, out_features=1)
|
231
|
+
self.sn_embedding1 = sn_embedding(num_classes, d_conv_dim*16)
|
232
|
+
|
233
|
+
# Weight init
|
234
|
+
self.apply(init_weights)
|
235
|
+
xavier_uniform_(self.sn_embedding1.weight)
|
236
|
+
|
237
|
+
def forward(self, x, labels):
|
238
|
+
# n x 3 x 128 x 128
|
239
|
+
h0 = self.opt_block1(x) # n x d_conv_dim x 64 x 64
|
240
|
+
h1 = self.block1(h0) # n x d_conv_dim*2 x 32 x 32
|
241
|
+
h1 = self.self_attn(h1) # n x d_conv_dim*2 x 32 x 32
|
242
|
+
h2 = self.block2(h1) # n x d_conv_dim*4 x 16 x 16
|
243
|
+
h3 = self.block3(h2) # n x d_conv_dim*8 x 8 x 8
|
244
|
+
h4 = self.block4(h3) # n x d_conv_dim*16 x 4 x 4
|
245
|
+
h5 = self.block5(h4, downsample=False) # n x d_conv_dim*16 x 4 x 4
|
246
|
+
h5 = self.relu(h5) # n x d_conv_dim*16 x 4 x 4
|
247
|
+
h6 = torch.sum(h5, dim=[2,3]) # n x d_conv_dim*16
|
248
|
+
output1 = torch.squeeze(self.snlinear1(h6)) # n
|
249
|
+
# Projection
|
250
|
+
h_labels = self.sn_embedding1(labels) # n x d_conv_dim*16
|
251
|
+
proj = torch.mul(h6, h_labels) # n x d_conv_dim*16
|
252
|
+
output2 = torch.sum(proj, dim=[1]) # n
|
253
|
+
# Out
|
254
|
+
output = output1 + output2 # n
|
255
|
+
return output
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
if __name__ == "__main__":
|
260
|
+
|
261
|
+
num_classes = 10
|
262
|
+
|
263
|
+
netG = cGAN_SAGAN_Generator(z_dim=128, num_classes=num_classes, g_conv_dim=128).cuda()
|
264
|
+
netD = cGAN_SAGAN_Discriminator(num_classes=num_classes, d_conv_dim=128).cuda()
|
265
|
+
|
266
|
+
n = 4
|
267
|
+
# target = torch.randint(high=num_classes, size=(1,n)) # set size (2,10) for MHE
|
268
|
+
# y = torch.zeros(n, num_classes)
|
269
|
+
# y[range(y.shape[0]), target]=1
|
270
|
+
# y = y.type(torch.long).cuda()
|
271
|
+
y = torch.randint(high=num_classes, size=(n,)).type(torch.long).cuda()
|
272
|
+
z = torch.randn(n, 128).cuda()
|
273
|
+
x = netG(z,y)
|
274
|
+
o = netD(x,y)
|
275
|
+
print(x.size())
|
276
|
+
print(o.size())
|