-
Notifications
You must be signed in to change notification settings - Fork 37
/
primary_net.py
82 lines (58 loc) · 2.53 KB
/
primary_net.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
from hypernetwork_modules import HyperNetwork
from resnet_blocks import ResNetBlock
class Embedding(nn.Module):
def __init__(self, z_num, z_dim):
super(Embedding, self).__init__()
self.z_list = nn.ParameterList()
self.z_num = z_num
self.z_dim = z_dim
h,k = self.z_num
for i in range(h):
for j in range(k):
self.z_list.append(Parameter(torch.fmod(torch.randn(self.z_dim).cuda(), 2)))
def forward(self, hyper_net):
ww = []
h, k = self.z_num
for i in range(h):
w = []
for j in range(k):
w.append(hyper_net(self.z_list[i*k + j]))
ww.append(torch.cat(w, dim=1))
return torch.cat(ww, dim=0)
class PrimaryNetwork(nn.Module):
def __init__(self, z_dim=64):
super(PrimaryNetwork, self).__init__()
self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
self.bn1 = nn.BatchNorm2d(16)
self.z_dim = z_dim
self.hope = HyperNetwork(z_dim=self.z_dim)
self.zs_size = [[1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1], [1, 1],
[2, 1], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2],
[4, 2], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4]]
self.filter_size = [[16,16], [16,16], [16,16], [16,16], [16,16], [16,16], [16,32], [32,32], [32,32], [32,32],
[32,32], [32,32], [32,64], [64,64], [64,64], [64,64], [64,64], [64,64]]
self.res_net = nn.ModuleList()
for i in range(18):
down_sample = False
if i > 5 and i % 6 == 0:
down_sample = True
self.res_net.append(ResNetBlock(self.filter_size[i][0], self.filter_size[i][1], downsample=down_sample))
self.zs = nn.ModuleList()
for i in range(36):
self.zs.append(Embedding(self.zs_size[i], self.z_dim))
self.global_avg = nn.AvgPool2d(8)
self.final = nn.Linear(64,10)
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
for i in range(18):
# if i != 15 and i != 17:
w1 = self.zs[2*i](self.hope)
w2 = self.zs[2*i+1](self.hope)
x = self.res_net[i](x, w1, w2)
x = self.global_avg(x)
x = self.final(x.view(-1,64))
return x