forked from mritunjaymusale/EM-matrix-capsule-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCapsNet.py
38 lines (32 loc) · 1.29 KB
/
CapsNet.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
from ConvCaps import ConvCaps
import torch.nn as nn
from PrimaryCaps import PrimaryCaps
from ClassCaps import ClassCaps
class CapsNet(nn.Module):
def __init__(self, A=32, B=32, C=32, D=32, E=10, K=3, P=4, iters=3, cuda=True):
super(CapsNet, self).__init__()
self.cuda = cuda
self.conv1 = nn.Conv2d(in_channels=1, out_channels=A,
kernel_size=5, stride=2, padding=2)
self.bn1 = nn.BatchNorm2d(num_features=A, eps=0.001,
momentum=0.1, affine=True)
self.relu1 = nn.ReLU(inplace=False)
self.primary_caps = PrimaryCaps(A, B, 1, P, stride=1)
self.conv_caps1 = ConvCaps(
B, C, K, P, stride=2, iters=iters, cuda=self.cuda)
self.conv_caps2 = ConvCaps(
C, D, K, P, stride=1, iters=iters, cuda=self.cuda)
self.class_caps = ClassCaps(D, E, 1, P, stride=1, iters=iters,
coor_add=True, w_shared=True, cuda=self.cuda)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.primary_caps(x)
x = self.conv_caps1(x)
x = self.conv_caps2(x)
x = self.class_caps(x)
return x
def capsules(**kwargs):
model = CapsNet(**kwargs)
return model