Myosotis-Researches 0.1.7__py3-none-any.whl → 0.1.9__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.
Files changed (49) hide show
  1. myosotis_researches/CcGAN/train/__init__.py +4 -0
  2. myosotis_researches/CcGAN/{train_128_output_10 → train}/train_ccgan.py +4 -4
  3. myosotis_researches/CcGAN/{train_128 → train}/train_cgan.py +1 -3
  4. myosotis_researches/CcGAN/{train_128 → train}/train_cgan_concat.py +1 -3
  5. myosotis_researches/CcGAN/utils/__init__.py +2 -1
  6. myosotis_researches/CcGAN/utils/train.py +94 -3
  7. {myosotis_researches-0.1.7.dist-info → myosotis_researches-0.1.9.dist-info}/METADATA +1 -1
  8. myosotis_researches-0.1.9.dist-info/RECORD +24 -0
  9. myosotis_researches/CcGAN/models_128/CcGAN_SAGAN.py +0 -301
  10. myosotis_researches/CcGAN/models_128/ResNet_class_eval.py +0 -141
  11. myosotis_researches/CcGAN/models_128/ResNet_embed.py +0 -188
  12. myosotis_researches/CcGAN/models_128/ResNet_regre_eval.py +0 -175
  13. myosotis_researches/CcGAN/models_128/__init__.py +0 -7
  14. myosotis_researches/CcGAN/models_128/autoencoder.py +0 -119
  15. myosotis_researches/CcGAN/models_128/cGAN_SAGAN.py +0 -276
  16. myosotis_researches/CcGAN/models_128/cGAN_concat_SAGAN.py +0 -245
  17. myosotis_researches/CcGAN/models_256/CcGAN_SAGAN.py +0 -303
  18. myosotis_researches/CcGAN/models_256/ResNet_class_eval.py +0 -142
  19. myosotis_researches/CcGAN/models_256/ResNet_embed.py +0 -188
  20. myosotis_researches/CcGAN/models_256/ResNet_regre_eval.py +0 -178
  21. myosotis_researches/CcGAN/models_256/__init__.py +0 -7
  22. myosotis_researches/CcGAN/models_256/autoencoder.py +0 -133
  23. myosotis_researches/CcGAN/models_256/cGAN_SAGAN.py +0 -280
  24. myosotis_researches/CcGAN/models_256/cGAN_concat_SAGAN.py +0 -249
  25. myosotis_researches/CcGAN/train_128/DiffAugment_pytorch.py +0 -76
  26. myosotis_researches/CcGAN/train_128/__init__.py +0 -0
  27. myosotis_researches/CcGAN/train_128/eval_metrics.py +0 -205
  28. myosotis_researches/CcGAN/train_128/opts.py +0 -87
  29. myosotis_researches/CcGAN/train_128/pretrain_AE.py +0 -268
  30. myosotis_researches/CcGAN/train_128/pretrain_CNN_class.py +0 -251
  31. myosotis_researches/CcGAN/train_128/pretrain_CNN_regre.py +0 -255
  32. myosotis_researches/CcGAN/train_128/train_ccgan.py +0 -303
  33. myosotis_researches/CcGAN/train_128/utils.py +0 -120
  34. myosotis_researches/CcGAN/train_128_output_10/DiffAugment_pytorch.py +0 -76
  35. myosotis_researches/CcGAN/train_128_output_10/__init__.py +0 -0
  36. myosotis_researches/CcGAN/train_128_output_10/eval_metrics.py +0 -205
  37. myosotis_researches/CcGAN/train_128_output_10/opts.py +0 -87
  38. myosotis_researches/CcGAN/train_128_output_10/pretrain_AE.py +0 -268
  39. myosotis_researches/CcGAN/train_128_output_10/pretrain_CNN_class.py +0 -251
  40. myosotis_researches/CcGAN/train_128_output_10/pretrain_CNN_regre.py +0 -255
  41. myosotis_researches/CcGAN/train_128_output_10/train_cgan.py +0 -254
  42. myosotis_researches/CcGAN/train_128_output_10/train_cgan_concat.py +0 -242
  43. myosotis_researches/CcGAN/train_128_output_10/train_net_for_label_embed.py +0 -181
  44. myosotis_researches/CcGAN/train_128_output_10/utils.py +0 -120
  45. myosotis_researches-0.1.7.dist-info/RECORD +0 -59
  46. /myosotis_researches/CcGAN/{train_128 → train}/train_net_for_label_embed.py +0 -0
  47. {myosotis_researches-0.1.7.dist-info → myosotis_researches-0.1.9.dist-info}/WHEEL +0 -0
  48. {myosotis_researches-0.1.7.dist-info → myosotis_researches-0.1.9.dist-info}/licenses/LICENSE +0 -0
  49. {myosotis_researches-0.1.7.dist-info → myosotis_researches-0.1.9.dist-info}/top_level.txt +0 -0
@@ -1,178 +0,0 @@
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 = 256
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 128
87
- self._make_layer(block, 64, num_blocks[0], stride=2), # h=h/2 64
88
- )
89
- self.block2 = nn.Sequential(
90
- nn.MaxPool2d(2,2), #h=h/2 32
91
- self._make_layer(block, 128, num_blocks[1], stride=2) # h=h/2 16
92
- )
93
- self.block3 = self._make_layer(block, 256, num_blocks[2], stride=2) # h=h/2 8
94
- self.block4 = self._make_layer(block, 512, num_blocks[3], stride=2) # h=h/2 4
95
-
96
- self.pool1 = nn.AvgPool2d(kernel_size=4)
97
- if self.feature_layer == 'f2':
98
- self.pool2 = nn.AdaptiveAvgPool2d((2,2))
99
- elif self.feature_layer == 'f3':
100
- self.pool2 = nn.AdaptiveAvgPool2d((2,2))
101
- else:
102
- self.pool2 = nn.AdaptiveAvgPool2d((1,1))
103
-
104
- linear_layers = [
105
- nn.Linear(512*block.expansion, 128),
106
- nn.BatchNorm1d(128),
107
- nn.ReLU(),
108
- nn.Linear(128, 128),
109
- nn.BatchNorm1d(128),
110
- nn.ReLU(),
111
- nn.Linear(128, 1),
112
- # nn.Sigmoid()
113
- nn.ReLU(),
114
- ]
115
- self.linear = nn.Sequential(*linear_layers)
116
-
117
-
118
- def _make_layer(self, block, planes, num_blocks, stride):
119
- strides = [stride] + [1]*(num_blocks-1)
120
- layers = []
121
- for stride in strides:
122
- layers.append(block(self.in_planes, planes, stride))
123
- self.in_planes = planes * block.expansion
124
- return nn.Sequential(*layers)
125
-
126
- def forward(self, x):
127
-
128
- if x.is_cuda and self.ngpu > 1:
129
- ft1 = nn.parallel.data_parallel(self.block1, x, range(self.ngpu))
130
- ft2 = nn.parallel.data_parallel(self.block2, ft1, range(self.ngpu))
131
- ft3 = nn.parallel.data_parallel(self.block3, ft2, range(self.ngpu))
132
- ft4 = nn.parallel.data_parallel(self.block4, ft3, range(self.ngpu))
133
- out = nn.parallel.data_parallel(self.pool1, ft4, range(self.ngpu))
134
- out = out.view(out.size(0), -1)
135
- out = nn.parallel.data_parallel(self.linear, out, range(self.ngpu))
136
- else:
137
- ft1 = self.block1(x)
138
- ft2 = self.block2(ft1)
139
- ft3 = self.block3(ft2)
140
- ft4 = self.block4(ft3)
141
- out = self.pool1(ft4)
142
- out = out.view(out.size(0), -1)
143
- out = self.linear(out)
144
-
145
- if self.feature_layer == 'f2':
146
- ext_features = self.pool2(ft2)
147
- elif self.feature_layer == 'f3':
148
- ext_features = self.pool2(ft3)
149
- else:
150
- ext_features = self.pool2(ft4)
151
-
152
- ext_features = ext_features.view(ext_features.size(0), -1)
153
-
154
- return out, ext_features
155
-
156
-
157
- def ResNet18_regre_eval(ngpu = 1):
158
- return ResNet_regre_eval(BasicBlock, [2,2,2,2], ngpu = ngpu)
159
-
160
- def ResNet34_regre_eval(ngpu = 1):
161
- return ResNet_regre_eval(BasicBlock, [3,4,6,3], ngpu = ngpu)
162
-
163
- def ResNet50_regre_eval(ngpu = 1):
164
- return ResNet_regre_eval(Bottleneck, [3,4,6,3], ngpu = ngpu)
165
-
166
- def ResNet101_regre_eval(ngpu = 1):
167
- return ResNet_regre_eval(Bottleneck, [3,4,23,3], ngpu = ngpu)
168
-
169
- def ResNet152_regre_eval(ngpu = 1):
170
- return ResNet_regre_eval(Bottleneck, [3,8,36,3], ngpu = ngpu)
171
-
172
-
173
- if __name__ == "__main__":
174
- net = ResNet34_regre_eval(ngpu = 1).cuda()
175
- x = torch.randn(4,NC,IMG_SIZE,IMG_SIZE).cuda()
176
- out, features = net(x)
177
- print(out.size())
178
- print(features.size())
@@ -1,7 +0,0 @@
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
@@ -1,133 +0,0 @@
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; 128
14
- nn.BatchNorm2d(ch),
15
- nn.ReLU(),
16
- nn.MaxPool2d(2,2), #h=h/2; 64
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, kernel_size=4, stride=2, padding=1), #h=h/2; 32
22
- nn.BatchNorm2d(ch),
23
- nn.ReLU(),
24
- nn.Conv2d(ch, 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*2, kernel_size=4, stride=2, padding=1), #h=h/2; 16
29
- nn.BatchNorm2d(ch*2),
30
- nn.ReLU(),
31
- nn.Conv2d(ch*2, 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*4, kernel_size=4, stride=2, padding=1), #h=h/2; 8
36
- nn.BatchNorm2d(ch*4),
37
- nn.ReLU(),
38
- nn.Conv2d(ch*4, ch*8, kernel_size=3, stride=1, padding=1), #h=h
39
- nn.BatchNorm2d(ch*8),
40
- nn.ReLU(),
41
-
42
- nn.Conv2d(ch*8, ch*8, kernel_size=4, stride=2, padding=1), #h=h/2; 4
43
- nn.BatchNorm2d(ch*8),
44
- nn.ReLU(),
45
- nn.Conv2d(ch*8, ch*16, kernel_size=3, stride=1, padding=1), #h=h
46
- nn.BatchNorm2d(ch*16),
47
- nn.ReLU(),
48
- )
49
-
50
- self.linear = nn.Sequential(
51
- nn.Linear(ch*16*4*4, dim_bottleneck),
52
- # nn.ReLU()
53
- )
54
-
55
- def forward(self, x):
56
- feature = self.conv(x)
57
- feature = feature.view(-1, self.ch*16*4*4)
58
- feature = self.linear(feature)
59
- return feature
60
-
61
-
62
-
63
- class decoder(nn.Module):
64
- def __init__(self, dim_bottleneck=512, ch=64):
65
- super(decoder, self).__init__()
66
- self.ch = ch
67
- self.dim_bottleneck = dim_bottleneck
68
-
69
- self.linear = nn.Sequential(
70
- nn.Linear(dim_bottleneck, ch*16*4*4)
71
- )
72
-
73
- self.deconv = nn.Sequential(
74
- nn.ConvTranspose2d(ch*16, ch*16, kernel_size=4, stride=2, padding=1), #h=2h; 8
75
- nn.BatchNorm2d(ch*16),
76
- nn.ReLU(True),
77
- nn.Conv2d(ch*16, ch*8, kernel_size=3, stride=1, padding=1), #h=h
78
- nn.BatchNorm2d(ch*8),
79
- nn.ReLU(),
80
-
81
- nn.ConvTranspose2d(ch*8, ch*8, kernel_size=4, stride=2, padding=1), #h=2h; 16
82
- nn.BatchNorm2d(ch*8),
83
- nn.ReLU(True),
84
- nn.Conv2d(ch*8, ch*4, kernel_size=3, stride=1, padding=1), #h=h
85
- nn.BatchNorm2d(ch*4),
86
- nn.ReLU(),
87
-
88
- nn.ConvTranspose2d(ch*4, ch*4, kernel_size=4, stride=2, padding=1), #h=2h; 32
89
- nn.BatchNorm2d(ch*4),
90
- nn.ReLU(True),
91
- nn.Conv2d(ch*4, ch*2, kernel_size=3, stride=1, padding=1), #h=h
92
- nn.BatchNorm2d(ch*2),
93
- nn.ReLU(),
94
-
95
- nn.ConvTranspose2d(ch*2, ch*2, kernel_size=4, stride=2, padding=1), #h=2h; 64
96
- nn.BatchNorm2d(ch*2),
97
- nn.ReLU(True),
98
- nn.Conv2d(ch*2, ch, kernel_size=3, stride=1, padding=1), #h=h
99
- nn.BatchNorm2d(ch),
100
- nn.ReLU(),
101
-
102
- nn.ConvTranspose2d(ch, ch, kernel_size=4, stride=2, padding=1), #h=2h; 128
103
- nn.BatchNorm2d(ch),
104
- nn.ReLU(True),
105
- nn.Conv2d(ch, ch, kernel_size=3, stride=1, padding=1), #h=h
106
- nn.BatchNorm2d(ch),
107
- nn.ReLU(),
108
-
109
- nn.ConvTranspose2d(ch, ch, kernel_size=4, stride=2, padding=1), #h=2h; 256
110
- nn.BatchNorm2d(ch),
111
- nn.ReLU(True),
112
- nn.Conv2d(ch, 3, kernel_size=3, stride=1, padding=1), #h=h
113
- nn.Tanh()
114
- )
115
-
116
- def forward(self, feature):
117
- out = self.linear(feature)
118
- out = out.view(-1, self.ch*16, 4, 4)
119
- out = self.deconv(out)
120
- return out
121
-
122
-
123
- if __name__=="__main__":
124
- #test
125
-
126
- net_encoder = encoder(dim_bottleneck=512, ch=64).cuda()
127
- net_decoder = decoder(dim_bottleneck=512, ch=64).cuda()
128
-
129
- x = torch.randn(10, 3, 256, 256).cuda()
130
- f = net_encoder(x)
131
- xh = net_decoder(f)
132
- print(f.size())
133
- print(xh.size())
@@ -1,280 +0,0 @@
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.block4 = GenBlock(g_conv_dim*4, g_conv_dim*2, num_classes)
134
- self.self_attn = Self_Attn(g_conv_dim*2)
135
- self.block5 = GenBlock(g_conv_dim*2, g_conv_dim*2, num_classes)
136
- self.block6 = GenBlock(g_conv_dim*2, g_conv_dim, num_classes)
137
- self.bn = nn.BatchNorm2d(g_conv_dim, eps=1e-5, momentum=0.0001, affine=True)
138
- self.relu = nn.ReLU(inplace=True)
139
- self.snconv2d1 = snconv2d(in_channels=g_conv_dim, out_channels=3, kernel_size=3, stride=1, padding=1)
140
- self.tanh = nn.Tanh()
141
-
142
- # Weight init
143
- self.apply(init_weights)
144
-
145
- def forward(self, z, labels):
146
- # n x z_dim
147
- act0 = self.snlinear0(z) # n x g_conv_dim*16*4*4
148
- act0 = act0.view(-1, self.g_conv_dim*16, 4, 4) # n x g_conv_dim*16 x 4 x 4
149
- act1 = self.block1(act0, labels) # n x g_conv_dim*16 x 8 x 8
150
- act2 = self.block2(act1, labels) # n x g_conv_dim*8 x 16 x 16
151
- act3 = self.block3(act2, labels) # 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
- act4 = self.self_attn(act4) # n x g_conv_dim*2 x 64 x 64
154
- act5 = self.block5(act4, labels) # n x g_conv_dim x 128 x 128
155
- act6 = self.block6(act5, labels) # n x g_conv_dim x 256 x 256
156
- act6 = self.bn(act6) # n x g_conv_dim x 256 x 256
157
- act6 = self.relu(act6) # n x g_conv_dim x 256 x 256
158
- act7 = self.snconv2d1(act6) # n x 3 x 256 x 256
159
- act7 = self.tanh(act7) # n x 3 x 256 x 256
160
- return act7
161
-
162
-
163
- class DiscOptBlock(nn.Module):
164
- def __init__(self, in_channels, out_channels):
165
- super(DiscOptBlock, self).__init__()
166
- self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
167
- self.relu = nn.ReLU(inplace=True)
168
- self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
169
- self.downsample = nn.AvgPool2d(2)
170
- self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
171
-
172
- def forward(self, x):
173
- x0 = x
174
-
175
- x = self.snconv2d1(x)
176
- x = self.relu(x)
177
- x = self.snconv2d2(x)
178
- x = self.downsample(x)
179
-
180
- x0 = self.downsample(x0)
181
- x0 = self.snconv2d0(x0)
182
-
183
- out = x + x0
184
- return out
185
-
186
-
187
- class DiscBlock(nn.Module):
188
- def __init__(self, in_channels, out_channels):
189
- super(DiscBlock, self).__init__()
190
- self.relu = nn.ReLU(inplace=True)
191
- self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
192
- self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
193
- self.downsample = nn.AvgPool2d(2)
194
- self.ch_mismatch = False
195
- if in_channels != out_channels:
196
- self.ch_mismatch = True
197
- self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
198
-
199
- def forward(self, x, downsample=True):
200
- x0 = x
201
-
202
- x = self.relu(x)
203
- x = self.snconv2d1(x)
204
- x = self.relu(x)
205
- x = self.snconv2d2(x)
206
- if downsample:
207
- x = self.downsample(x)
208
-
209
- if downsample or self.ch_mismatch:
210
- x0 = self.snconv2d0(x0)
211
- if downsample:
212
- x0 = self.downsample(x0)
213
-
214
- out = x + x0
215
- return out
216
-
217
-
218
- class cGAN_SAGAN_Discriminator(nn.Module):
219
- """Discriminator."""
220
-
221
- def __init__(self, num_classes, d_conv_dim=64):
222
- super(cGAN_SAGAN_Discriminator, self).__init__()
223
- self.d_conv_dim = d_conv_dim
224
- self.opt_block1 = DiscOptBlock(3, d_conv_dim)
225
- self.block1 = DiscBlock(d_conv_dim, d_conv_dim*2)
226
- self.self_attn = Self_Attn(d_conv_dim*2)
227
- self.block2 = DiscBlock(d_conv_dim*2, d_conv_dim*4)
228
- self.block3 = DiscBlock(d_conv_dim*4, d_conv_dim*6)
229
- self.block4 = DiscBlock(d_conv_dim*6, d_conv_dim*12)
230
- self.block5 = DiscBlock(d_conv_dim*12, d_conv_dim*12)
231
- self.block6 = DiscBlock(d_conv_dim*12, d_conv_dim*16)
232
- self.relu = nn.ReLU(inplace=True)
233
- self.snlinear1 = snlinear(in_features=d_conv_dim*16, out_features=1)
234
- self.sn_embedding1 = sn_embedding(num_classes, d_conv_dim*16)
235
-
236
- # Weight init
237
- self.apply(init_weights)
238
- xavier_uniform_(self.sn_embedding1.weight)
239
-
240
- def forward(self, x, labels):
241
- # n x 3 x 256 x 256
242
- h0 = self.opt_block1(x) # n x d_conv_dim x 128 x 128
243
- h1 = self.block1(h0) # n x d_conv_dim*2 x 64 x 64
244
- h1 = self.self_attn(h1) # n x d_conv_dim*2 x 64 x 64
245
- h2 = self.block2(h1) # n x d_conv_dim*4 x 32 x 32
246
- h3 = self.block3(h2) # n x d_conv_dim*8 x 16 x 16
247
- h4 = self.block4(h3) # n x d_conv_dim*16 x 8 x 8
248
- h5 = self.block5(h4) # n x d_conv_dim*16 x 4 x 4
249
- h6 = self.block6(h5, downsample=False) # n x d_conv_dim*16 x 4 x 4
250
- h6 = self.relu(h6) # n x d_conv_dim*16 x 4 x 4
251
- h6 = torch.sum(h6, dim=[2,3]) # n x d_conv_dim*16
252
- output1 = torch.squeeze(self.snlinear1(h6)) # n
253
- # Projection
254
- h_labels = self.sn_embedding1(labels) # n x d_conv_dim*16
255
- proj = torch.mul(h6, h_labels) # n x d_conv_dim*16
256
- output2 = torch.sum(proj, dim=[1]) # n
257
- # Out
258
- output = output1 + output2 # n
259
- return output
260
-
261
-
262
-
263
- if __name__ == "__main__":
264
-
265
- num_classes = 10
266
-
267
- netG = cGAN_SAGAN_Generator(z_dim=128, num_classes=num_classes, g_conv_dim=128).cuda()
268
- netD = cGAN_SAGAN_Discriminator(num_classes=num_classes, d_conv_dim=128).cuda()
269
-
270
- n = 4
271
- # target = torch.randint(high=num_classes, size=(1,n)) # set size (2,10) for MHE
272
- # y = torch.zeros(n, num_classes)
273
- # y[range(y.shape[0]), target]=1
274
- # y = y.type(torch.long).cuda()
275
- y = torch.randint(high=num_classes, size=(n,)).type(torch.long).cuda()
276
- z = torch.randn(n, 128).cuda()
277
- x = netG(z,y)
278
- o = netD(x,y)
279
- print(x.size())
280
- print(o.size())