-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcapsnet_v2.py
More file actions
170 lines (134 loc) · 6.6 KB
/
capsnet_v2.py
File metadata and controls
170 lines (134 loc) · 6.6 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import torch
import torch.nn as nn
import torch.nn.functional as F
from config import options
import numpy as np
def squash(input_tensor, dim=-1, epsilon=1e-7):
squared_norm = (input_tensor ** 2).sum(dim=dim, keepdim=True)
safe_norm = torch.sqrt(squared_norm + epsilon)
scale = squared_norm / (1 + squared_norm)
unit_vector = input_tensor / safe_norm
return scale * unit_vector
class SimNet(nn.Module):
def __init__(self, args):
super(SimNet, self).__init__()
self.args = args
self.sim_net = nn.Sequential(
nn.Linear(options.digit_cap_dim, args.h1),
nn.ReLU(inplace=True),
nn.Linear(args.h1, args.h2),
nn.ReLU(inplace=True),
nn.Linear(args.h2, args.img_h * args.img_w),
nn.Sigmoid()
)
def forward(self, imgs):
x = self.sim_net()
return x
class PrimaryCapsLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, cap_dim, num_cap_map):
super(PrimaryCapsLayer, self).__init__()
self.capsule_dim = cap_dim
self.num_cap_map = num_cap_map
self.conv_out = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding=0)
def forward(self, x):
batch_size = x.size(0)
outputs = self.conv_out(x)
map_dim = outputs.size(-1)
outputs = outputs.view(batch_size, self.capsule_dim, self.num_cap_map, map_dim, map_dim)
# [bs, 8 (or 10), 32, 6, 6]
outputs = outputs.view(batch_size, self.capsule_dim, self.num_cap_map, -1).transpose(1, 2).transpose(2, 3)
# [bs, 32, 36, 8]
outputs = squash(outputs)
return outputs
class DigitCapsLayer(nn.Module):
def __init__(self, num_digit_cap, num_prim_cap, num_prim_map, in_cap_dim, out_cap_dim, num_iterations):
super(DigitCapsLayer, self).__init__()
self.num_prim_cap = num_prim_cap
self.num_prim_map = num_prim_map
self.num_digit_cap = num_digit_cap
self.num_iterations = num_iterations
self.out_cap_dim = out_cap_dim
if options.share_weight:
self.W = nn.Parameter(0.01 * torch.randn(1, num_digit_cap, num_prim_map, 1, out_cap_dim, in_cap_dim))
# [1, 10, 32, 1, 16, 8]
else:
self.W = nn.Parameter(0.01 * torch.randn(1, num_digit_cap, num_prim_map, num_prim_cap, out_cap_dim, in_cap_dim))
# [1, 10, 32, 36, 16, 8]
def forward(self, x):
batch_size = x.size(0) # [bs, 32, 36, 8]
u = x[:, None, :, :, :, None] # [bs, 1, 32, 36, 8, 1]
u_hat = torch.matmul(self.W, u).squeeze(-1) # [bs, 10, 32, 36, 16]
# detach u_hat during routing iterations to prevent gradients from flowing
temp_u_hat = u_hat.detach()
b = torch.zeros(batch_size, self.num_digit_cap, u_hat.size(2), u_hat.size(3), 1).cuda()
# [bs, 10, 32, 36, 1]
for i in range(self.num_iterations - 1):
c = F.softmax(b, dim=1)
s = (c * temp_u_hat).sum(dim=2).sum(dim=2) # [bs, 10, 16]
v = squash(s)
# [bs, 10, 1152, 16] . [batch_size, 10, 16, 1]
uv = torch.matmul(temp_u_hat.view(batch_size, self.num_digit_cap, -1, self.out_cap_dim), v.unsqueeze(-1)) # [batch_size, 10, 1152, 1]
b += uv.view(b.shape)
c = F.softmax(b, dim=3)
s = (c * u_hat).sum(dim=2).sum(dim=2)
v = squash(s)
return v
class CapsuleNet(nn.Module):
def __init__(self, args):
super(CapsuleNet, self).__init__()
self.args = args
# convolution layer
self.conv1 = nn.Conv2d(in_channels=args.img_c, out_channels=args.f_conv1,
kernel_size=args.k_conv1, stride=args.s_conv1)
# primary capsule layer
assert args.f_prim % args.primary_cap_dim == 0
self.num_prim_map = int(args.f_prim / args.primary_cap_dim)
self.primary_capsules = PrimaryCapsLayer(in_channels=args.f_conv1, out_channels=args.f_prim,
kernel_size=args.k_prim, stride=args.s_prim,
cap_dim=args.primary_cap_dim,
num_cap_map=self.num_prim_map)
num_prim_cap = int((args.img_h - 2*(args.k_prim-1)) * (args.img_h - 2*(args.k_prim-1))
/ (args.s_prim*args.s_prim))
self.digit_capsules = DigitCapsLayer(num_digit_cap=args.num_classes,
num_prim_cap=num_prim_cap,
num_prim_map=self.num_prim_map,
in_cap_dim=args.primary_cap_dim,
out_cap_dim=args.digit_cap_dim,
num_iterations=args.num_iterations)
if args.add_decoder:
self.decoder = nn.Sequential(
nn.Linear(16 * args.num_classes, args.h1),
nn.ReLU(inplace=True),
nn.Linear(args.h1, args.h2),
nn.ReLU(inplace=True),
nn.Linear(args.h2, args.img_h * args.img_w),
nn.Sigmoid()
)
def forward(self, imgs, y=None):
x = F.relu(self.conv1(imgs), inplace=True)
x = self.primary_capsules(x)
x = self.digit_capsules(x)
v_length = (x ** 2).sum(dim=-1) ** 0.5
_, y_pred = v_length.max(dim=1)
y_pred_ohe = F.one_hot(y_pred, self.args.num_classes)
if y is None:
y = y_pred_ohe
img_reconst = torch.zeros_like(imgs)
if self.args.add_decoder:
img_reconst = self.decoder((x * y[:, :, None].float()).view(x.size(0), -1))
return y_pred_ohe, img_reconst, v_length
class CapsuleLoss(nn.Module):
def __init__(self, args):
super(CapsuleLoss, self).__init__()
self.args = args
def forward(self, images, labels, v_c, reconstructions):
present_error = F.relu(self.args.m_plus - v_c, inplace=True) ** 2 # max(0, m_plus-||v_c||)^2
absent_error = F.relu(v_c - self.args.m_minus, inplace=True) ** 2 # max(0, ||v_c||-m_minus)^2
l_c = labels.float() * present_error + self.args.lambda_val * (1. - labels.float()) * absent_error
margin_loss = l_c.sum(dim=1).mean()
reconstruction_loss = 0
if self.args.add_decoder:
assert torch.numel(images) == torch.numel(reconstructions)
images = images.view(reconstructions.size()[0], -1)
reconstruction_loss = torch.mean((reconstructions - images) ** 2)
return margin_loss + self.args.alpha * reconstruction_loss