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.
- myosotis_researches/CcGAN/train/__init__.py +4 -0
- myosotis_researches/CcGAN/{train_128_output_10 → train}/train_ccgan.py +4 -4
- myosotis_researches/CcGAN/{train_128 → train}/train_cgan.py +1 -3
- myosotis_researches/CcGAN/{train_128 → train}/train_cgan_concat.py +1 -3
- myosotis_researches/CcGAN/utils/__init__.py +2 -1
- myosotis_researches/CcGAN/utils/train.py +94 -3
- {myosotis_researches-0.1.7.dist-info → myosotis_researches-0.1.9.dist-info}/METADATA +1 -1
- myosotis_researches-0.1.9.dist-info/RECORD +24 -0
- myosotis_researches/CcGAN/models_128/CcGAN_SAGAN.py +0 -301
- myosotis_researches/CcGAN/models_128/ResNet_class_eval.py +0 -141
- myosotis_researches/CcGAN/models_128/ResNet_embed.py +0 -188
- myosotis_researches/CcGAN/models_128/ResNet_regre_eval.py +0 -175
- myosotis_researches/CcGAN/models_128/__init__.py +0 -7
- myosotis_researches/CcGAN/models_128/autoencoder.py +0 -119
- myosotis_researches/CcGAN/models_128/cGAN_SAGAN.py +0 -276
- myosotis_researches/CcGAN/models_128/cGAN_concat_SAGAN.py +0 -245
- myosotis_researches/CcGAN/models_256/CcGAN_SAGAN.py +0 -303
- myosotis_researches/CcGAN/models_256/ResNet_class_eval.py +0 -142
- myosotis_researches/CcGAN/models_256/ResNet_embed.py +0 -188
- myosotis_researches/CcGAN/models_256/ResNet_regre_eval.py +0 -178
- myosotis_researches/CcGAN/models_256/__init__.py +0 -7
- myosotis_researches/CcGAN/models_256/autoencoder.py +0 -133
- myosotis_researches/CcGAN/models_256/cGAN_SAGAN.py +0 -280
- myosotis_researches/CcGAN/models_256/cGAN_concat_SAGAN.py +0 -249
- myosotis_researches/CcGAN/train_128/DiffAugment_pytorch.py +0 -76
- myosotis_researches/CcGAN/train_128/__init__.py +0 -0
- myosotis_researches/CcGAN/train_128/eval_metrics.py +0 -205
- myosotis_researches/CcGAN/train_128/opts.py +0 -87
- myosotis_researches/CcGAN/train_128/pretrain_AE.py +0 -268
- myosotis_researches/CcGAN/train_128/pretrain_CNN_class.py +0 -251
- myosotis_researches/CcGAN/train_128/pretrain_CNN_regre.py +0 -255
- myosotis_researches/CcGAN/train_128/train_ccgan.py +0 -303
- myosotis_researches/CcGAN/train_128/utils.py +0 -120
- myosotis_researches/CcGAN/train_128_output_10/DiffAugment_pytorch.py +0 -76
- myosotis_researches/CcGAN/train_128_output_10/__init__.py +0 -0
- myosotis_researches/CcGAN/train_128_output_10/eval_metrics.py +0 -205
- myosotis_researches/CcGAN/train_128_output_10/opts.py +0 -87
- myosotis_researches/CcGAN/train_128_output_10/pretrain_AE.py +0 -268
- myosotis_researches/CcGAN/train_128_output_10/pretrain_CNN_class.py +0 -251
- myosotis_researches/CcGAN/train_128_output_10/pretrain_CNN_regre.py +0 -255
- myosotis_researches/CcGAN/train_128_output_10/train_cgan.py +0 -254
- myosotis_researches/CcGAN/train_128_output_10/train_cgan_concat.py +0 -242
- myosotis_researches/CcGAN/train_128_output_10/train_net_for_label_embed.py +0 -181
- myosotis_researches/CcGAN/train_128_output_10/utils.py +0 -120
- myosotis_researches-0.1.7.dist-info/RECORD +0 -59
- /myosotis_researches/CcGAN/{train_128 → train}/train_net_for_label_embed.py +0 -0
- {myosotis_researches-0.1.7.dist-info → myosotis_researches-0.1.9.dist-info}/WHEEL +0 -0
- {myosotis_researches-0.1.7.dist-info → myosotis_researches-0.1.9.dist-info}/licenses/LICENSE +0 -0
- {myosotis_researches-0.1.7.dist-info → myosotis_researches-0.1.9.dist-info}/top_level.txt +0 -0
@@ -1,303 +0,0 @@
|
|
1
|
-
'''
|
2
|
-
|
3
|
-
Adapted from https://github.com/voletiv/self-attention-GAN-pytorch/blob/master/sagan_models.py
|
4
|
-
|
5
|
-
|
6
|
-
'''
|
7
|
-
|
8
|
-
|
9
|
-
import numpy as np
|
10
|
-
import torch
|
11
|
-
import torch.nn as nn
|
12
|
-
import torch.nn.functional as F
|
13
|
-
|
14
|
-
from torch.nn.utils import spectral_norm
|
15
|
-
from torch.nn.init import xavier_uniform_
|
16
|
-
|
17
|
-
|
18
|
-
def init_weights(m):
|
19
|
-
if type(m) == nn.Linear or type(m) == nn.Conv2d:
|
20
|
-
xavier_uniform_(m.weight)
|
21
|
-
if m.bias is not None:
|
22
|
-
m.bias.data.fill_(0.)
|
23
|
-
|
24
|
-
|
25
|
-
def snconv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True):
|
26
|
-
return spectral_norm(nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size,
|
27
|
-
stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias))
|
28
|
-
|
29
|
-
def snlinear(in_features, out_features, bias=True):
|
30
|
-
return spectral_norm(nn.Linear(in_features=in_features, out_features=out_features, bias=bias))
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
class Self_Attn(nn.Module):
|
35
|
-
""" Self attention Layer"""
|
36
|
-
|
37
|
-
def __init__(self, in_channels):
|
38
|
-
super(Self_Attn, self).__init__()
|
39
|
-
self.in_channels = in_channels
|
40
|
-
self.snconv1x1_theta = snconv2d(in_channels=in_channels, out_channels=in_channels//8, kernel_size=1, stride=1, padding=0)
|
41
|
-
self.snconv1x1_phi = snconv2d(in_channels=in_channels, out_channels=in_channels//8, kernel_size=1, stride=1, padding=0)
|
42
|
-
self.snconv1x1_g = snconv2d(in_channels=in_channels, out_channels=in_channels//2, kernel_size=1, stride=1, padding=0)
|
43
|
-
self.snconv1x1_attn = snconv2d(in_channels=in_channels//2, out_channels=in_channels, kernel_size=1, stride=1, padding=0)
|
44
|
-
self.maxpool = nn.MaxPool2d(2, stride=2, padding=0)
|
45
|
-
self.softmax = nn.Softmax(dim=-1)
|
46
|
-
self.sigma = nn.Parameter(torch.zeros(1))
|
47
|
-
|
48
|
-
def forward(self, x):
|
49
|
-
"""
|
50
|
-
inputs :
|
51
|
-
x : input feature maps(B X C X W X H)
|
52
|
-
returns :
|
53
|
-
out : self attention value + input feature
|
54
|
-
attention: B X N X N (N is Width*Height)
|
55
|
-
"""
|
56
|
-
_, ch, h, w = x.size()
|
57
|
-
# Theta path
|
58
|
-
theta = self.snconv1x1_theta(x)
|
59
|
-
theta = theta.view(-1, ch//8, h*w)
|
60
|
-
# Phi path
|
61
|
-
phi = self.snconv1x1_phi(x)
|
62
|
-
phi = self.maxpool(phi)
|
63
|
-
phi = phi.view(-1, ch//8, h*w//4)
|
64
|
-
# Attn map
|
65
|
-
attn = torch.bmm(theta.permute(0, 2, 1), phi)
|
66
|
-
attn = self.softmax(attn)
|
67
|
-
# g path
|
68
|
-
g = self.snconv1x1_g(x)
|
69
|
-
g = self.maxpool(g)
|
70
|
-
g = g.view(-1, ch//2, h*w//4)
|
71
|
-
# Attn_g
|
72
|
-
attn_g = torch.bmm(g, attn.permute(0, 2, 1))
|
73
|
-
attn_g = attn_g.view(-1, ch//2, h, w)
|
74
|
-
attn_g = self.snconv1x1_attn(attn_g)
|
75
|
-
# Out
|
76
|
-
out = x + self.sigma*attn_g
|
77
|
-
return out
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
'''
|
83
|
-
|
84
|
-
Generator
|
85
|
-
|
86
|
-
'''
|
87
|
-
|
88
|
-
|
89
|
-
class ConditionalBatchNorm2d(nn.Module):
|
90
|
-
def __init__(self, num_features, dim_embed):
|
91
|
-
super().__init__()
|
92
|
-
self.num_features = num_features
|
93
|
-
self.bn = nn.BatchNorm2d(num_features, momentum=0.001, affine=False)
|
94
|
-
self.embed_gamma = nn.Linear(dim_embed, num_features, bias=False)
|
95
|
-
self.embed_beta = nn.Linear(dim_embed, num_features, bias=False)
|
96
|
-
|
97
|
-
def forward(self, x, y):
|
98
|
-
out = self.bn(x)
|
99
|
-
gamma = self.embed_gamma(y).view(-1, self.num_features, 1, 1)
|
100
|
-
beta = self.embed_beta(y).view(-1, self.num_features, 1, 1)
|
101
|
-
out = out + gamma*out + beta
|
102
|
-
return out
|
103
|
-
|
104
|
-
|
105
|
-
class GenBlock(nn.Module):
|
106
|
-
def __init__(self, in_channels, out_channels, dim_embed):
|
107
|
-
super(GenBlock, self).__init__()
|
108
|
-
self.cond_bn1 = ConditionalBatchNorm2d(in_channels, dim_embed)
|
109
|
-
self.relu = nn.ReLU(inplace=True)
|
110
|
-
self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
111
|
-
self.cond_bn2 = ConditionalBatchNorm2d(out_channels, dim_embed)
|
112
|
-
self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
113
|
-
self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
|
114
|
-
|
115
|
-
def forward(self, x, labels):
|
116
|
-
x0 = x
|
117
|
-
|
118
|
-
x = self.cond_bn1(x, labels)
|
119
|
-
x = self.relu(x)
|
120
|
-
x = F.interpolate(x, scale_factor=2, mode='nearest') # upsample
|
121
|
-
x = self.snconv2d1(x)
|
122
|
-
x = self.cond_bn2(x, labels)
|
123
|
-
x = self.relu(x)
|
124
|
-
x = self.snconv2d2(x)
|
125
|
-
|
126
|
-
x0 = F.interpolate(x0, scale_factor=2, mode='nearest') # upsample
|
127
|
-
x0 = self.snconv2d0(x0)
|
128
|
-
|
129
|
-
out = x + x0
|
130
|
-
return out
|
131
|
-
|
132
|
-
|
133
|
-
class CcGAN_SAGAN_Generator(nn.Module):
|
134
|
-
"""Generator."""
|
135
|
-
|
136
|
-
def __init__(self, dim_z, dim_embed=128, nc=3, gene_ch=64):
|
137
|
-
super(CcGAN_SAGAN_Generator, self).__init__()
|
138
|
-
|
139
|
-
self.dim_z = dim_z
|
140
|
-
self.gene_ch = gene_ch
|
141
|
-
|
142
|
-
self.snlinear0 = snlinear(in_features=dim_z, out_features=gene_ch*16*4*4)
|
143
|
-
self.block1 = GenBlock(gene_ch*16, gene_ch*16, dim_embed)
|
144
|
-
self.block2 = GenBlock(gene_ch*16, gene_ch*8, dim_embed)
|
145
|
-
self.block3 = GenBlock(gene_ch*8, gene_ch*4, dim_embed)
|
146
|
-
self.block4 = GenBlock(gene_ch*4, gene_ch*2, dim_embed)
|
147
|
-
self.self_attn = Self_Attn(gene_ch*2)
|
148
|
-
self.block5 = GenBlock(gene_ch*2, gene_ch*2, dim_embed)
|
149
|
-
self.block6 = GenBlock(gene_ch*2, gene_ch, dim_embed)
|
150
|
-
self.bn = nn.BatchNorm2d(gene_ch, eps=1e-5, momentum=0.0001, affine=True)
|
151
|
-
self.relu = nn.ReLU(inplace=True)
|
152
|
-
self.snconv2d1 = snconv2d(in_channels=gene_ch, out_channels=nc, kernel_size=3, stride=1, padding=1)
|
153
|
-
self.tanh = nn.Tanh()
|
154
|
-
|
155
|
-
# Weight init
|
156
|
-
self.apply(init_weights)
|
157
|
-
|
158
|
-
def forward(self, z, labels):
|
159
|
-
# n x dim_z
|
160
|
-
out = self.snlinear0(z) # 4*4
|
161
|
-
out = out.view(-1, self.gene_ch*16, 4, 4) # 4 x 4
|
162
|
-
out = self.block1(out, labels) # 8 x 8
|
163
|
-
out = self.block2(out, labels) # 16 x 16
|
164
|
-
out = self.block3(out, labels) # 32 x 32
|
165
|
-
out = self.block4(out, labels) # 64 x 64
|
166
|
-
out = self.self_attn(out) # 64 x 64
|
167
|
-
out = self.block5(out, labels) # 128 x 128
|
168
|
-
out = self.block6(out, labels) # 256 x 256
|
169
|
-
out = self.bn(out)
|
170
|
-
out = self.relu(out)
|
171
|
-
out = self.snconv2d1(out)
|
172
|
-
out = self.tanh(out)
|
173
|
-
return out
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
'''
|
178
|
-
|
179
|
-
Discriminator
|
180
|
-
|
181
|
-
'''
|
182
|
-
|
183
|
-
class DiscOptBlock(nn.Module):
|
184
|
-
def __init__(self, in_channels, out_channels):
|
185
|
-
super(DiscOptBlock, self).__init__()
|
186
|
-
self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
187
|
-
self.relu = nn.ReLU(inplace=True)
|
188
|
-
self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
189
|
-
self.downsample = nn.AvgPool2d(2)
|
190
|
-
self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
|
191
|
-
|
192
|
-
def forward(self, x):
|
193
|
-
x0 = x
|
194
|
-
|
195
|
-
x = self.snconv2d1(x)
|
196
|
-
x = self.relu(x)
|
197
|
-
x = self.snconv2d2(x)
|
198
|
-
x = self.downsample(x)
|
199
|
-
|
200
|
-
x0 = self.downsample(x0)
|
201
|
-
x0 = self.snconv2d0(x0)
|
202
|
-
|
203
|
-
out = x + x0
|
204
|
-
return out
|
205
|
-
|
206
|
-
|
207
|
-
class DiscBlock(nn.Module):
|
208
|
-
def __init__(self, in_channels, out_channels):
|
209
|
-
super(DiscBlock, self).__init__()
|
210
|
-
self.relu = nn.ReLU(inplace=True)
|
211
|
-
self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
212
|
-
self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
|
213
|
-
self.downsample = nn.AvgPool2d(2)
|
214
|
-
self.ch_mismatch = False
|
215
|
-
if in_channels != out_channels:
|
216
|
-
self.ch_mismatch = True
|
217
|
-
self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
|
218
|
-
|
219
|
-
def forward(self, x, downsample=True):
|
220
|
-
x0 = x
|
221
|
-
|
222
|
-
x = self.relu(x)
|
223
|
-
x = self.snconv2d1(x)
|
224
|
-
x = self.relu(x)
|
225
|
-
x = self.snconv2d2(x)
|
226
|
-
if downsample:
|
227
|
-
x = self.downsample(x)
|
228
|
-
|
229
|
-
if downsample or self.ch_mismatch:
|
230
|
-
x0 = self.snconv2d0(x0)
|
231
|
-
if downsample:
|
232
|
-
x0 = self.downsample(x0)
|
233
|
-
|
234
|
-
out = x + x0
|
235
|
-
return out
|
236
|
-
|
237
|
-
|
238
|
-
class CcGAN_SAGAN_Discriminator(nn.Module):
|
239
|
-
"""Discriminator."""
|
240
|
-
|
241
|
-
def __init__(self, dim_embed=128, nc=3, disc_ch=64):
|
242
|
-
super(CcGAN_SAGAN_Discriminator, self).__init__()
|
243
|
-
self.disc_ch = disc_ch
|
244
|
-
self.opt_block1 = DiscOptBlock(nc, disc_ch)
|
245
|
-
self.block1 = DiscBlock(disc_ch, disc_ch*2)
|
246
|
-
self.self_attn = Self_Attn(disc_ch*2)
|
247
|
-
self.block2 = DiscBlock(disc_ch*2, disc_ch*4)
|
248
|
-
self.block3 = DiscBlock(disc_ch*4, disc_ch*6)
|
249
|
-
self.block4 = DiscBlock(disc_ch*6, disc_ch*12)
|
250
|
-
self.block5 = DiscBlock(disc_ch*12, disc_ch*12)
|
251
|
-
self.block6 = DiscBlock(disc_ch*12, disc_ch*16)
|
252
|
-
self.relu = nn.ReLU(inplace=True)
|
253
|
-
self.snlinear1 = snlinear(in_features=disc_ch*16*4*4, out_features=1)
|
254
|
-
self.sn_embedding1 = snlinear(dim_embed, disc_ch*16*4*4, bias=False)
|
255
|
-
|
256
|
-
# Weight init
|
257
|
-
self.apply(init_weights)
|
258
|
-
xavier_uniform_(self.sn_embedding1.weight)
|
259
|
-
|
260
|
-
def forward(self, x, labels):
|
261
|
-
# 256x256
|
262
|
-
out = self.opt_block1(x) # 128 x 128
|
263
|
-
out = self.block1(out) # 64 x 64
|
264
|
-
out = self.self_attn(out) # 64 x 64
|
265
|
-
out = self.block2(out) # 32 x 32
|
266
|
-
out = self.block3(out) # 16 x 16
|
267
|
-
out = self.block4(out) # 8 x 8
|
268
|
-
out = self.block5(out) # 4 x 4
|
269
|
-
out = self.block6(out, downsample=False) # 4 x 4
|
270
|
-
out = self.relu(out) # n x disc_ch*16 x 4 x 4
|
271
|
-
out = out.view(-1, self.disc_ch*16*4*4)
|
272
|
-
output1 = torch.squeeze(self.snlinear1(out)) # n
|
273
|
-
# Projection
|
274
|
-
h_labels = self.sn_embedding1(labels) # n x disc_ch*16 x 4 x 4
|
275
|
-
proj = torch.mul(out, h_labels) # n x disc_ch*16 x 4 x 4
|
276
|
-
output2 = torch.sum(proj, dim=[1]) # n
|
277
|
-
# Out
|
278
|
-
output = output1 + output2 # n
|
279
|
-
return output
|
280
|
-
|
281
|
-
|
282
|
-
if __name__ == "__main__":
|
283
|
-
def get_parameter_number(net):
|
284
|
-
total_num = sum(p.numel() for p in net.parameters())
|
285
|
-
trainable_num = sum(p.numel() for p in net.parameters() if p.requires_grad)
|
286
|
-
return {'Total': total_num, 'Trainable': trainable_num}
|
287
|
-
|
288
|
-
netG = CcGAN_SAGAN_Generator(dim_z=256, dim_embed=128, gene_ch=128).cuda()
|
289
|
-
netD = CcGAN_SAGAN_Discriminator(dim_embed=128, disc_ch=128).cuda()
|
290
|
-
|
291
|
-
# netG = nn.DataParallel(netG)
|
292
|
-
# netD = nn.DataParallel(netD)
|
293
|
-
|
294
|
-
N=4
|
295
|
-
z = torch.randn(N, 256).cuda()
|
296
|
-
y = torch.randn(N, 128).cuda()
|
297
|
-
x = netG(z,y)
|
298
|
-
o = netD(x,y)
|
299
|
-
print(x.size())
|
300
|
-
print(o.size())
|
301
|
-
|
302
|
-
print('G:', get_parameter_number(netG))
|
303
|
-
print('D:', get_parameter_number(netD))
|
@@ -1,142 +0,0 @@
|
|
1
|
-
'''
|
2
|
-
Regular ResNet
|
3
|
-
|
4
|
-
codes are based on
|
5
|
-
@article{
|
6
|
-
zhang2018mixup,
|
7
|
-
title={mixup: Beyond Empirical Risk Minimization},
|
8
|
-
author={Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin, David Lopez-Paz},
|
9
|
-
journal={International Conference on Learning Representations},
|
10
|
-
year={2018},
|
11
|
-
url={https://openreview.net/forum?id=r1Ddp1-Rb},
|
12
|
-
}
|
13
|
-
'''
|
14
|
-
|
15
|
-
|
16
|
-
import torch
|
17
|
-
import torch.nn as nn
|
18
|
-
import torch.nn.functional as F
|
19
|
-
|
20
|
-
from torch.autograd import Variable
|
21
|
-
|
22
|
-
IMG_SIZE=256
|
23
|
-
NC=3
|
24
|
-
|
25
|
-
|
26
|
-
class BasicBlock(nn.Module):
|
27
|
-
expansion = 1
|
28
|
-
|
29
|
-
def __init__(self, in_planes, planes, stride=1):
|
30
|
-
super(BasicBlock, self).__init__()
|
31
|
-
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
|
32
|
-
self.bn1 = nn.BatchNorm2d(planes)
|
33
|
-
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
|
34
|
-
self.bn2 = nn.BatchNorm2d(planes)
|
35
|
-
|
36
|
-
self.shortcut = nn.Sequential()
|
37
|
-
if stride != 1 or in_planes != self.expansion*planes:
|
38
|
-
self.shortcut = nn.Sequential(
|
39
|
-
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
|
40
|
-
nn.BatchNorm2d(self.expansion*planes)
|
41
|
-
)
|
42
|
-
|
43
|
-
def forward(self, x):
|
44
|
-
out = F.relu(self.bn1(self.conv1(x)))
|
45
|
-
out = self.bn2(self.conv2(out))
|
46
|
-
out += self.shortcut(x)
|
47
|
-
out = F.relu(out)
|
48
|
-
return out
|
49
|
-
|
50
|
-
|
51
|
-
class Bottleneck(nn.Module):
|
52
|
-
expansion = 4
|
53
|
-
|
54
|
-
def __init__(self, in_planes, planes, stride=1):
|
55
|
-
super(Bottleneck, self).__init__()
|
56
|
-
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
|
57
|
-
self.bn1 = nn.BatchNorm2d(planes)
|
58
|
-
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
|
59
|
-
self.bn2 = nn.BatchNorm2d(planes)
|
60
|
-
self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)
|
61
|
-
self.bn3 = nn.BatchNorm2d(self.expansion*planes)
|
62
|
-
|
63
|
-
self.shortcut = nn.Sequential()
|
64
|
-
if stride != 1 or in_planes != self.expansion*planes:
|
65
|
-
self.shortcut = nn.Sequential(
|
66
|
-
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
|
67
|
-
nn.BatchNorm2d(self.expansion*planes)
|
68
|
-
)
|
69
|
-
|
70
|
-
def forward(self, x):
|
71
|
-
out = F.relu(self.bn1(self.conv1(x)))
|
72
|
-
out = F.relu(self.bn2(self.conv2(out)))
|
73
|
-
out = self.bn3(self.conv3(out))
|
74
|
-
out += self.shortcut(x)
|
75
|
-
out = F.relu(out)
|
76
|
-
return out
|
77
|
-
|
78
|
-
|
79
|
-
class ResNet_class_eval(nn.Module):
|
80
|
-
def __init__(self, block, num_blocks, num_classes=49, nc=NC, ngpu = 1):
|
81
|
-
super(ResNet_class_eval, self).__init__()
|
82
|
-
self.in_planes = 64
|
83
|
-
self.ngpu = ngpu
|
84
|
-
|
85
|
-
self.main = nn.Sequential(
|
86
|
-
nn.Conv2d(nc, 64, kernel_size=3, stride=1, padding=1, bias=False), # h=h #256
|
87
|
-
nn.BatchNorm2d(64),
|
88
|
-
nn.ReLU(),
|
89
|
-
nn.MaxPool2d(2,2), #h=h/2 128
|
90
|
-
# self._make_layer(block, 64, num_blocks[0], stride=1), # h=h
|
91
|
-
self._make_layer(block, 64, num_blocks[0], stride=2), # h=h/2 64
|
92
|
-
nn.MaxPool2d(2,2), #h=h/2 32
|
93
|
-
self._make_layer(block, 128, num_blocks[1], stride=2),
|
94
|
-
self._make_layer(block, 256, num_blocks[2], stride=2),
|
95
|
-
self._make_layer(block, 512, num_blocks[3], stride=2),
|
96
|
-
nn.AvgPool2d(kernel_size=4)
|
97
|
-
)
|
98
|
-
self.classifier = nn.Linear(512*block.expansion, num_classes)
|
99
|
-
|
100
|
-
def _make_layer(self, block, planes, num_blocks, stride):
|
101
|
-
strides = [stride] + [1]*(num_blocks-1)
|
102
|
-
layers = []
|
103
|
-
for stride in strides:
|
104
|
-
layers.append(block(self.in_planes, planes, stride))
|
105
|
-
self.in_planes = planes * block.expansion
|
106
|
-
return nn.Sequential(*layers)
|
107
|
-
|
108
|
-
def forward(self, x):
|
109
|
-
|
110
|
-
if x.is_cuda and self.ngpu > 1:
|
111
|
-
features = nn.parallel.data_parallel(self.main, x, range(self.ngpu))
|
112
|
-
features = features.view(features.size(0), -1)
|
113
|
-
out = nn.parallel.data_parallel(self.classifier, features, range(self.ngpu))
|
114
|
-
else:
|
115
|
-
features = self.main(x)
|
116
|
-
features = features.view(features.size(0), -1)
|
117
|
-
out = self.classifier(features)
|
118
|
-
return out, features
|
119
|
-
|
120
|
-
|
121
|
-
def ResNet18_class_eval(num_classes=49, ngpu = 1):
|
122
|
-
return ResNet_class_eval(BasicBlock, [2,2,2,2], num_classes=num_classes, ngpu = ngpu)
|
123
|
-
|
124
|
-
def ResNet34_class_eval(num_classes=49, ngpu = 1):
|
125
|
-
return ResNet_class_eval(BasicBlock, [3,4,6,3], num_classes=num_classes, ngpu = ngpu)
|
126
|
-
|
127
|
-
def ResNet50_class_eval(num_classes=49, ngpu = 1):
|
128
|
-
return ResNet_class_eval(Bottleneck, [3,4,6,3], num_classes=num_classes, ngpu = ngpu)
|
129
|
-
|
130
|
-
def ResNet101_class_eval(num_classes=49, ngpu = 1):
|
131
|
-
return ResNet_class_eval(Bottleneck, [3,4,23,3], num_classes=num_classes, ngpu = ngpu)
|
132
|
-
|
133
|
-
def ResNet152_class_eval(num_classes=49, ngpu = 1):
|
134
|
-
return ResNet_class_eval(Bottleneck, [3,8,36,3], num_classes=num_classes, ngpu = ngpu)
|
135
|
-
|
136
|
-
|
137
|
-
if __name__ == "__main__":
|
138
|
-
net = ResNet50_class_eval(num_classes=5, ngpu = 1).cuda()
|
139
|
-
x = torch.randn(16,NC,IMG_SIZE,IMG_SIZE).cuda()
|
140
|
-
out, features = net(x)
|
141
|
-
print(out.size())
|
142
|
-
print(features.size())
|
@@ -1,188 +0,0 @@
|
|
1
|
-
'''
|
2
|
-
ResNet-based model to map an image from pixel space to a features space.
|
3
|
-
Need to be pretrained on the dataset.
|
4
|
-
|
5
|
-
if isometric_map = True, there is an extra step (elf.classifier_1 = nn.Linear(512, 32*32*3)) to increase the dimension of the feature map from 512 to 32*32*3. This selection is for desity-ratio estimation in feature space.
|
6
|
-
|
7
|
-
codes are based on
|
8
|
-
@article{
|
9
|
-
zhang2018mixup,
|
10
|
-
title={mixup: Beyond Empirical Risk Minimization},
|
11
|
-
author={Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin, David Lopez-Paz},
|
12
|
-
journal={International Conference on Learning Representations},
|
13
|
-
year={2018},
|
14
|
-
url={https://openreview.net/forum?id=r1Ddp1-Rb},
|
15
|
-
}
|
16
|
-
'''
|
17
|
-
|
18
|
-
|
19
|
-
import torch
|
20
|
-
import torch.nn as nn
|
21
|
-
import torch.nn.functional as F
|
22
|
-
|
23
|
-
NC = 3
|
24
|
-
IMG_SIZE = 256
|
25
|
-
DIM_EMBED = 128
|
26
|
-
|
27
|
-
|
28
|
-
#------------------------------------------------------------------------------
|
29
|
-
class BasicBlock(nn.Module):
|
30
|
-
expansion = 1
|
31
|
-
|
32
|
-
def __init__(self, in_planes, planes, stride=1):
|
33
|
-
super(BasicBlock, self).__init__()
|
34
|
-
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
|
35
|
-
self.bn1 = nn.BatchNorm2d(planes)
|
36
|
-
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
|
37
|
-
self.bn2 = nn.BatchNorm2d(planes)
|
38
|
-
|
39
|
-
self.shortcut = nn.Sequential()
|
40
|
-
if stride != 1 or in_planes != self.expansion*planes:
|
41
|
-
self.shortcut = nn.Sequential(
|
42
|
-
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
|
43
|
-
nn.BatchNorm2d(self.expansion*planes)
|
44
|
-
)
|
45
|
-
|
46
|
-
def forward(self, x):
|
47
|
-
out = F.relu(self.bn1(self.conv1(x)))
|
48
|
-
out = self.bn2(self.conv2(out))
|
49
|
-
out += self.shortcut(x)
|
50
|
-
out = F.relu(out)
|
51
|
-
return out
|
52
|
-
|
53
|
-
|
54
|
-
class Bottleneck(nn.Module):
|
55
|
-
expansion = 4
|
56
|
-
|
57
|
-
def __init__(self, in_planes, planes, stride=1):
|
58
|
-
super(Bottleneck, self).__init__()
|
59
|
-
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
|
60
|
-
self.bn1 = nn.BatchNorm2d(planes)
|
61
|
-
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
|
62
|
-
self.bn2 = nn.BatchNorm2d(planes)
|
63
|
-
self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)
|
64
|
-
self.bn3 = nn.BatchNorm2d(self.expansion*planes)
|
65
|
-
|
66
|
-
self.shortcut = nn.Sequential()
|
67
|
-
if stride != 1 or in_planes != self.expansion*planes:
|
68
|
-
self.shortcut = nn.Sequential(
|
69
|
-
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
|
70
|
-
nn.BatchNorm2d(self.expansion*planes)
|
71
|
-
)
|
72
|
-
|
73
|
-
def forward(self, x):
|
74
|
-
out = F.relu(self.bn1(self.conv1(x)))
|
75
|
-
out = F.relu(self.bn2(self.conv2(out)))
|
76
|
-
out = self.bn3(self.conv3(out))
|
77
|
-
out += self.shortcut(x)
|
78
|
-
out = F.relu(out)
|
79
|
-
return out
|
80
|
-
|
81
|
-
|
82
|
-
class ResNet_embed(nn.Module):
|
83
|
-
def __init__(self, block, num_blocks, nc=NC, dim_embed=DIM_EMBED):
|
84
|
-
super(ResNet_embed, self).__init__()
|
85
|
-
self.in_planes = 64
|
86
|
-
|
87
|
-
self.main = nn.Sequential(
|
88
|
-
nn.Conv2d(nc, 64, kernel_size=3, stride=1, padding=1, bias=False), # h=h 256
|
89
|
-
nn.BatchNorm2d(64),
|
90
|
-
nn.ReLU(),
|
91
|
-
nn.MaxPool2d(2,2), #h=h/2 128
|
92
|
-
# self._make_layer(block, 64, num_blocks[0], stride=1), # h=h
|
93
|
-
self._make_layer(block, 64, num_blocks[0], stride=2), # h=h/2 64
|
94
|
-
nn.MaxPool2d(2,2), # 32
|
95
|
-
self._make_layer(block, 128, num_blocks[1], stride=2), # h=h/2 16
|
96
|
-
self._make_layer(block, 256, num_blocks[2], stride=2), # h=h/2 8
|
97
|
-
self._make_layer(block, 512, num_blocks[3], stride=2), # h=h/2 4
|
98
|
-
# nn.AvgPool2d(kernel_size=4)
|
99
|
-
nn.AdaptiveAvgPool2d((1, 1))
|
100
|
-
)
|
101
|
-
|
102
|
-
self.x2h_res = nn.Sequential(
|
103
|
-
nn.Linear(512, 512),
|
104
|
-
nn.BatchNorm1d(512),
|
105
|
-
nn.ReLU(),
|
106
|
-
|
107
|
-
nn.Linear(512, dim_embed),
|
108
|
-
nn.BatchNorm1d(dim_embed),
|
109
|
-
nn.ReLU(),
|
110
|
-
)
|
111
|
-
|
112
|
-
self.h2y = nn.Sequential(
|
113
|
-
nn.Linear(dim_embed, 1),
|
114
|
-
nn.ReLU()
|
115
|
-
)
|
116
|
-
|
117
|
-
def _make_layer(self, block, planes, num_blocks, stride):
|
118
|
-
strides = [stride] + [1]*(num_blocks-1)
|
119
|
-
layers = []
|
120
|
-
for stride in strides:
|
121
|
-
layers.append(block(self.in_planes, planes, stride))
|
122
|
-
self.in_planes = planes * block.expansion
|
123
|
-
return nn.Sequential(*layers)
|
124
|
-
|
125
|
-
def forward(self, x):
|
126
|
-
|
127
|
-
features = self.main(x)
|
128
|
-
features = features.view(features.size(0), -1)
|
129
|
-
features = self.x2h_res(features)
|
130
|
-
out = self.h2y(features)
|
131
|
-
|
132
|
-
return out, features
|
133
|
-
|
134
|
-
|
135
|
-
def ResNet18_embed(dim_embed=DIM_EMBED):
|
136
|
-
return ResNet_embed(BasicBlock, [2,2,2,2], dim_embed=dim_embed)
|
137
|
-
|
138
|
-
def ResNet34_embed(dim_embed=DIM_EMBED):
|
139
|
-
return ResNet_embed(BasicBlock, [3,4,6,3], dim_embed=dim_embed)
|
140
|
-
|
141
|
-
def ResNet50_embed(dim_embed=DIM_EMBED):
|
142
|
-
return ResNet_embed(Bottleneck, [3,4,6,3], dim_embed=dim_embed)
|
143
|
-
|
144
|
-
#------------------------------------------------------------------------------
|
145
|
-
# map labels to the embedding space
|
146
|
-
class model_y2h(nn.Module):
|
147
|
-
def __init__(self, dim_embed=DIM_EMBED):
|
148
|
-
super(model_y2h, self).__init__()
|
149
|
-
self.main = nn.Sequential(
|
150
|
-
nn.Linear(1, dim_embed),
|
151
|
-
# nn.BatchNorm1d(dim_embed),
|
152
|
-
nn.GroupNorm(8, dim_embed),
|
153
|
-
nn.ReLU(),
|
154
|
-
|
155
|
-
nn.Linear(dim_embed, dim_embed),
|
156
|
-
# nn.BatchNorm1d(dim_embed),
|
157
|
-
nn.GroupNorm(8, dim_embed),
|
158
|
-
nn.ReLU(),
|
159
|
-
|
160
|
-
nn.Linear(dim_embed, dim_embed),
|
161
|
-
# nn.BatchNorm1d(dim_embed),
|
162
|
-
nn.GroupNorm(8, dim_embed),
|
163
|
-
nn.ReLU(),
|
164
|
-
|
165
|
-
nn.Linear(dim_embed, dim_embed),
|
166
|
-
# nn.BatchNorm1d(dim_embed),
|
167
|
-
nn.GroupNorm(8, dim_embed),
|
168
|
-
nn.ReLU(),
|
169
|
-
|
170
|
-
nn.Linear(dim_embed, dim_embed),
|
171
|
-
nn.ReLU()
|
172
|
-
)
|
173
|
-
|
174
|
-
def forward(self, y):
|
175
|
-
y = y.view(-1, 1) +1e-8
|
176
|
-
# y = torch.exp(y.view(-1, 1))
|
177
|
-
return self.main(y)
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
if __name__ == "__main__":
|
182
|
-
net = ResNet34_embed(dim_embed=128).cuda()
|
183
|
-
x = torch.randn(16,NC,IMG_SIZE,IMG_SIZE).cuda()
|
184
|
-
out, features = net(x)
|
185
|
-
print(out.size())
|
186
|
-
print(features.size())
|
187
|
-
|
188
|
-
net_y2h = model_y2h()
|