pytorchcapsule
Pytorch implementation of Hinton's Dynamic Routing Between Capsules
view repo
A capsule is a group of neurons whose activity vector represents the instantiation parameters of a specific type of entity such as an object or an object part. We use the length of the activity vector to represent the probability that the entity exists and its orientation to represent the instantiation parameters. Active capsules at one level make predictions, via transformation matrices, for the instantiation parameters of higherlevel capsules. When multiple predictions agree, a higher level capsule becomes active. We show that a discrimininatively trained, multilayer capsule system achieves stateoftheart performance on MNIST and is considerably better than a convolutional net at recognizing highly overlapping digits. To achieve these results we use an iterative routingbyagreement mechanism: A lowerlevel capsule prefers to send its output to higher level capsules whose activity vectors have a big scalar product with the prediction coming from the lowerlevel capsule.
READ FULL TEXT VIEW PDFPytorch implementation of Hinton's Dynamic Routing Between Capsules
A tutorialstyle implementation of CapsNet in PyTorch
Prof. Geoffrey Hinton’ın “Dynamic Routing Between Capsules” makalesindeki Kapsül Ağı (Capsule Network: CapsNet) algoritmasının Keras Uygulamasıdır.
None
A lightway systemwrapper of Capsule Layers
Human vision ignores irrelevant details by using a carefully determined sequence of fixation points to ensure that only a tiny fraction of the optic array is ever processed at the highest resolution. Introspection is a poor guide to understanding how much of our knowledge of a scene comes from the sequence of fixations and how much we glean from a single fixation, but in this paper we will assume that a single fixation gives us much more than just a single identified object and its properties. We assume that our multilayer visual system creates a parse treelike structure on each fixation, and we ignore the issue of how these singlefixation parse trees are coordinated over multiple fixations.
Parse trees are generally constructed on the fly by dynamically allocating memory. Following Hinton et al. (2000)
, however, we shall assume that, for a single fixation, a parse tree is carved out of a fixed multilayer neural network like a sculpture is carved from a rock. Each layer will be divided into many small groups of neurons called “capsules” (
Hinton et al. (2011)) and each node in the parse tree will correspond to an active capsule. Using an iterative routing process, each active capsule will choose a capsule in the layer above to be its parent in the tree. For the higher levels of a visual system, this iterative process will be solving the problem of assigning parts to wholes.The activities of the neurons within an active capsule represent the various properties of a particular entity that is present in the image. These properties can include many different types of instantiation parameter such as pose (position, size, orientation), deformation, velocity, albedo, hue, texture, etc. One very special property is the existence of the instantiated entity in the image. An obvious way to represent existence is by using a separate logistic unit whose output is the probability that the entity exists. In this paper we explore an interesting alternative which is to use the overall length of the vector of instantiation parameters to represent the existence of the entity and to force the orientation of the vector to represent the properties of the entity^{1}^{1}1This makes biological sense as it does not use large activities to get accurate representations of things that probably don’t exist.. We ensure that the length of the vector output of a capsule cannot exceed by applying a nonlinearity that leaves the orientation of the vector unchanged but scales down its magnitude.
The fact that the output of a capsule is a vector makes it possible to use a powerful dynamic routing mechanism to ensure that the output of the capsule gets sent to an appropriate parent in the layer above. Initially, the output is routed to all possible parents but is scaled down by coupling coefficients that sum to
. For each possible parent, the capsule computes a “prediction vector” by multiplying its own output by a weight matrix. If this prediction vector has a large scalar product with the output of a possible parent, there is topdown feedback which increases the coupling coefficient for that parent and decreasing it for other parents. This increases the contribution that the capsule makes to that parent thus further increasing the scalar product of the capsule’s prediction with the parent’s output. This type of “routingbyagreement” should be far more effective than the very primitive form of routing implemented by maxpooling, which allows neurons in one layer to ignore all but the most active feature detector in a local pool in the layer below. We demonstrate that our dynamic routing mechanism is an effective way to implement the “explaining away” that is needed for segmenting highly overlapping objects.
Convolutional neural networks (CNNs) use translated replicas of learned feature detectors. This allows them to translate knowledge about good weight values acquired at one position in an image to other positions. This has proven extremely helpful in image interpretation. Even though we are replacing the scalaroutput feature detectors of CNNs with vectoroutput capsules and maxpooling with routingbyagreement, we would still like to replicate learned knowledge across space. To achieve this, we make all but the last layer of capsules be convolutional. As with CNNs, we make higherlevel capsules cover larger regions of the image. Unlike maxpooling however, we do not throw away information about the precise position of the entity within the region. For low level capsules, location information is “placecoded” by which capsule is active. As we ascend the hierarchy, more and more of the positional information is “ratecoded” in the realvalued components of the output vector of a capsule. This shift from placecoding to ratecoding combined with the fact that higherlevel capsules represent more complex entities with more degrees of freedom suggests that the dimensionality of capsules should increase as we ascend the hierarchy.
There are many possible ways to implement the general idea of capsules. The aim of this paper is not to explore this whole space but simply to show that one fairly straightforward implementation works well and that dynamic routing helps.
We want the length of the output vector of a capsule to represent the probability that the entity represented by the capsule is present in the current input. We therefore use a nonlinear "squashing" function to ensure that short vectors get shrunk to almost zero length and long vectors get shrunk to a length slightly below . We leave it to discriminative learning to make good use of this nonlinearity.
(1) 
where is the vector output of capsule and is its total input.
For all but the first layer of capsules, the total input to a capsule is a weighted sum over all “prediction vectors” from the capsules in the layer below and is produced by multiplying the output of a capsule in the layer below by a weight matrix
(2) 
where the are coupling coefficients that are determined by the iterative dynamic routing process.
The coupling coefficients between capsule and all the capsules in the layer above sum to
and are determined by a “routing softmax” whose initial logits
are the log prior probabilities that capsule
should be coupled to capsule .(3) 
The log priors can be learned discriminatively at the same time as all the other weights. They depend on the location and type of the two capsules but not on the current input image^{2}^{2}2For MNIST we found that it was sufficient to set all of these priors to be equal.. The initial coupling coefficients are then iteratively refined by measuring the agreement between the current output of each capsule, , in the layer above and the prediction made by capsule .
The agreement is simply the scalar product . This agreement is treated as if it was a log likelihood and is added to the initial logit, before computing the new values for all the coupling coefficients linking capsule to higher level capsules.
In convolutional capsule layers, each capsule outputs a local grid of vectors to each type of capsule in the layer above using different transformation matrices for each member of the grid as well as for each type of capsule.
We are using the length of the instantiation vector to represent the probability that a capsule’s entity exists. We would like the toplevel capsule for digit class to have a long instantiation vector if and only if that digit is present in the image. To allow for multiple digits, we use a separate margin loss, for each digit capsule, :
(4) 
where iff a digit of class is present^{3}^{3}3We do not allow an image to contain two instances of the same digit class. We address this weakness of capsules in the discussion section. and and . The downweighting of the loss for absent digit classes stops the initial learning from shrinking the lengths of the activity vectors of all the digit capsules. We use . The total loss is simply the sum of the losses of all digit capsules.
A simple CapsNet architecture is shown in Fig. 1. The architecture is shallow with only two convolutional layers and one fully connected layer. Conv has ,
convolution kernels with a stride of 1 and ReLU activation. This layer converts pixel intensities to the activities of local feature detectors that are then used as inputs to the
primary capsules.The primary capsules are the lowest level of multidimensional entities and, from an inverse graphics perspective, activating the primary capsules corresponds to inverting the rendering process. This is a very different type of computation than piecing instantiated parts together to make familiar wholes, which is what capsules are designed to be good at.
The second layer (PrimaryCapsules) is a convolutional capsule layer with channels of convolutional D capsules (i.e. each primary capsule contains 8 convolutional units with a kernel and a stride of 2). Each primary capsule output sees the outputs of all Conv units whose receptive fields overlap with the location of the center of the capsule. In total PrimaryCapsules has capsule outputs (each output is an D vector) and each capsule in the grid is sharing their weights with each other. One can see PrimaryCapsules as a Convolution layer with Eq. 1 as its block nonlinearity. The final Layer (DigitCaps) has one D capsule per digit class and each of these capsules receives input from all the capsules in the layer below.
We have routing only between two consecutive capsule layers (e.g. PrimaryCapsules and DigitCaps). Since Conv output is D, there is no orientation in its space to agree on. Therefore, no routing is used between Conv and PrimaryCapsules. All the routing logits () are initialized to zero. Therefore, initially a capsule output () is sent to all parent capsules () with equal probability ().
Our implementation is in TensorFlow (
Abadi et al. (2016)) and we use the Adam optimizer (Kingma and Ba (2014)) with its TensorFlow default parameters, including the exponentially decaying learning rate, to minimize the sum of the margin losses in Eq. 4.1cmInput  
Output 
We use an additional reconstruction loss to encourage the digit capsules to encode the instantiation parameters of the input digit. During training, we mask out all but the activity vector of the correct digit capsule. Then we use this activity vector to reconstruct the input image. The output of the digit capsule is fed into a decoder consisting of fully connected layers that model the pixel intensities as described in Fig. 2. We minimize the sum of squared differences between the outputs of the logistic units and the pixel intensities. We scale down this reconstruction loss by so that it does not dominate the margin loss during training. As illustrated in Fig. 3 the reconstructions from the D output of the CapsNet are robust while keeping only important details.
Training is performed on MNIST (LeCun et al. (1998)) images that have been shifted by up to
pixels in each direction with zero padding. No other data augmentation/deformation is used. The dataset has
K and K images for training and testing respectively.We test using a single model without any model averaging. Wan et al. (2013) achieves % test error with ensembling and augmenting the data with rotation and scaling. They achieve % without them. We get a low test error (%) on a layer network previously only achieved by deeper networks. Tab. 1 reports the test error rate on MNIST for different CapsNet setups and shows the importance of routing and reconstruction regularizer. Adding the reconstruction regularizer boosts the routing performance by enforcing the pose encoding in the capsule vector.
The baseline is a standard CNN with three convolutional layers of channels. Each has 5x5 kernels and stride of 1. The last convolutional layers are followed by two fully connected layers of size . The last fully connected layer is connected with dropout to a
class softmax layer with cross entropy loss. The baseline is also trained on 2pixel shifted MNIST with Adam optimizer. The baseline is designed to achieve the best performance on MNIST while keeping the computation cost as close as to CapsNet. In terms of number of parameters the baseline has 35.4M while CapsNet has 8.2M parameters and 6.8M parameters without the reconstruction subnetwork.
Method  Routing  Reconstruction  MNIST (%)  MultiMNIST (%) 

Baseline      
CapsNet  1  no    
CapsNet  1  yes  
CapsNet  3  no    
CapsNet  3  yes 
CapsNet classification test accuracy. The MNIST average and standard deviation results are reported from
trials.Scale and thickness  

Localized part  
Stroke thickness  
Localized skew 

Width and translation  
Localized part 
Since we are passing the encoding of only one digit and zeroing out other digits, the dimensions of a digit capsule should learn to span the space of variations in the way digits of that class are instantiated. These variations include stroke thickness, skew and width. They also include digitspecific variations such as the length of the tail of a 2. We can see what the individual dimensions represent by making use of the decoder network. After computing the activity vector for the correct digit capsule, we can feed a perturbed version of this activity vector to the decoder network and see how the perturbation affects the reconstruction. Examples of these perturbations are shown in Fig. 4. We found that one dimension (out of ) of the capsule almost always represents the width of the digit. While some dimensions represent combinations of global variations, there are other dimensions that represent variation in a localized part of the digit. For example, different dimensions are used for the length of the ascender of a 6 and the size of the loop.
Experiments show that each DigitCaps capsule learns a more robust representation for each class than a traditional convolutional network. Because there is natural variance in skew, rotation, style, etc in hand written digits, the trained CapsNet is moderately robust to small affine transformations of the training data.
To test the robustness of CapsNet to affine transformations, we trained a CapsNet and a traditional convolutional network (with MaxPooling and DropOut) on a padded and translated MNIST training set, in which each example is an MNIST digit placed randomly on a black background of pixels. We then tested this network on the affNIST^{4}^{4}4Available at http://www.cs.toronto.edu/~tijmen/affNIST/. data set, in which each example is an MNIST digit with a random small affine transformation. Our models were never trained with affine transformations other than translation and any natural transformation seen in the standard MNIST. An undertrained CapsNet with early stopping which achieved 99.23% accuracy on the expanded MNIST test set achieved 79% accuracy on the affnist test set. A traditional convolutional model with a similar number of parameters which achieved similar accuracy (99.22%) on the expanded mnist test set only achieved 66% on the affnist test set.
Dynamic routing can be viewed as a parallel attention mechanism that allows each capsule at one level to attend to some active capsules at the level below and to ignore others. This should allow the model to recognize multiple objects in the image even if objects overlap. Hinton et al. propose the task of segmenting and recognizing highly overlapping digits (Hinton et al. (2000) and others have tested their networks in a similar domain (Goodfellow et al. (2013), Ba et al. (2014), Greff et al. (2016)). The routingbyagreement should make it possible to use a prior about the shape of objects to help segmentation and it should obviate the need to make higherlevel segmentation decisions in the domain of pixels.
R:  R:  R:  R:  *R:  *R:  R:  R:P: 
L:  L:  L:  L:  L:  L:  L:  L: 
R:  R:  R:  R:  *R:  *R:  R:  R:P: 
L:  L:  L:  L:  L:  L:  L:  L: 
We generate the MultiMNIST training and test dataset by overlaying a digit on top of another digit from the same set (training or test) but different class. Each digit is shifted up to pixels in each direction resulting in a image. Considering a digit in a image is bounded in a box, two digits bounding boxes on average have % overlap. For each digit in the MNIST dataset we generate K MultiMNIST examples. So the training set size is M and the test set size is M.
Our layer CapsNet model trained from scratch on MultiMNIST training data achieves higher test classification accuracy than our baseline convolutional model. We are achieving the same classification error rate of
% on highly overlapping digit pairs as the sequential attention model of
Ba et al. (2014) achieves on a much easier task that has far less overlap (% overlap of the boxes around the two digits in our case vs % for Ba et al. (2014)). On test images, which are composed of pairs of images from the test set, we treat the two most active digit capsules as the classification produced by the capsules network. During reconstruction we pick one digit at a time and use the activity vector of the chosen digit capsule to reconstruct the image of the chosen digit (we know this image because we used it to generate the composite image). The only difference with our MNIST model is that we increased the period of the decay step for the learning rate to be larger because the training dataset is larger.The reconstructions illustrated in Fig. 5 show that CapsNet is able to segment the image into the two original digits. Since this segmentation is not at pixel level we observe that the model is able to deal correctly with the overlaps (a pixel is on in both digits) while accounting for all the pixels. The position and the style of each digit is encoded in DigitCaps. The decoder has learned to reconstruct a digit given the encoding. The fact that it is able to reconstruct digits regardless of the overlap shows that each digit capsule can pick up the style and position from the votes it is receiving from PrimaryCapsules layer.
Tab. 1 emphasizes the importance of capsules with routing on this task. As a baseline for the classification of CapsNet accuracy we trained a convolution network with two convolution layers and two fully connected layers on top of them. The first layer has convolution kernels of size and stride . The second layer has kernels of size and stride . After each convolution layer the model has a pooling layer of size and stride . The third layer is a D fully connected layer. All three layers have ReLU nonlinearities. The final layer of units is fully connected. We use the TensorFlow default Adam optimizer (Kingma and Ba (2014)) to train a sigmoid cross entropy loss on the output of final layer. This model has M parameters which is times more parameters than CapsNet with M parameters. We started with a smaller CNN ( and convolutional kernels of and stride of and a D fully connected layer) and incrementally increased the width of the network until we reached the best test accuracy on a K subset of the MultiMNIST data. We also searched for the right decay step on the K validation set.
We decode the two most active DigitCaps capsules one at a time and get two images. Then by assigning any pixel with nonzero intensity to each digit we get the segmentation results for each digit.
We tested our capsule model on CIFAR10 and achieved 10.6% error with an ensemble of 7 models each of which is trained with routing iterations on patches of the image. Each model has the same architecture as the simple model we used for MNIST except that there are three color channels and we used different types of primary capsule. We also found that it helped to introduce a "noneoftheabove" category for the routing softmaxes, since we do not expect the final layer of ten capsules to explain everything in the image. 10.6% test error is about what standard convolutional nets achieved when they were first applied to CIFAR10 (Zeiler and Fergus (2013)).
One drawback of Capsules which it shares with generative models is that it likes to account for everything in the image so it does better when it can model the clutter than when it just uses an additional “orphan” category in the dynamic routing. In CIFAR10, the backgrounds are much too varied to model in a reasonable sized net which helps to account for the poorer performance.
We also tested the exact same architecture as we used for MNIST on smallNORB (LeCun et al. (2004)) and achieved test error rate, which is onpar with the stateoftheart (Cireşan et al. (2011)). The smallNORB dataset consists of 96x96 stereo greyscale images. We resized the images to 48x48 and during training processed random 32x32 crops of them. We passed the central 32x32 patch during test.
For thirty years, the stateoftheart in speech recognition used hidden Markov models with Gaussian mixtures as output distributions. These models were easy to learn on small computers, but they had a representational limitation that was ultimately fatal: The oneofn representations they use are exponentially inefficient compared with, say, a recurrent neural network that uses distributed representations. To double the amount of information that an HMM can remember about the string it has generated so far, we need to square the number of hidden nodes. For a recurrent net we only need to double the number of hidden neurons.
Now that convolutional neural networks have become the dominant approach to object recognition, it makes sense to ask whether there are any exponential inefficiencies that may lead to their demise. A good candidate is the difficulty that convolutional nets have in generalizing to novel viewpoints. The ability to deal with translation is built in, but for the other dimensions of an affine transformation we have to chose between replicating feature detectors on a grid that grows exponentially with the number of dimensions, or increasing the size of the labelled training set in a similarly exponential way. Capsules (Hinton et al. (2011)) avoid these exponential inefficiencies by converting pixel intensities into vectors of instantiation parameters of recognized fragments and then applying transformation matrices to the fragments to predict the instantiation parameters of larger fragments. Transformation matrices that learn to encode the intrinsic spatial relationship between a part and a whole constitute viewpoint invariant knowledge that automatically generalizes to novel viewpoints. Hinton et al. (2011)
proposed transforming autoencoders to generate the instantiation parameters of the PrimaryCapsule layer and their system required transformation matrices to be supplied externally. We propose a complete system that also answers "how larger and more complex visual entities can be recognized by using agreements of the poses predicted by active, lowerlevel capsules".
Capsules make a very strong representational assumption: At each location in the image, there is at most one instance of the type of entity that a capsule represents. This assumption, which was motivated by the perceptual phenomenon called "crowding" (Pelli et al. (2004)), eliminates the binding problem (Hinton (1981a)) and allows a capsule to use a distributed representation (its activity vector) to encode the instantiation parameters of the entity of that type at a given location. This distributed representation is exponentially more efficient than encoding the instantiation parameters by activating a point on a highdimensional grid and with the right distributed representation, capsules can then take full advantage of the fact that spatial relationships can be modelled by matrix multiplies.
Capsules use neural activities that vary as viewpoint varies rather than trying to eliminate viewpoint variation from the activities. This gives them an advantage over "normalization" methods like spatial transformer networks (
Jaderberg et al. (2015)): They can deal with multiple different affine transformations of different objects or object parts at the same time.Capsules are also very good for dealing with segmentation, which is another of the toughest problems in vision, because the vector of instantiation parameters allows them to use routingbyagreement, as we have demonstrated in this paper. The importance of dynamic routing procedure is also backed by biologically plausible models of invarient pattern recognition in the visual cortex.
Hinton (1981b) proposes dynamic connections and canonical object based frames of reference to generate shape descriptions that can be used for object recognition. Olshausen et al. (1993) improves upon Hinton (1981b) dynamic connections and presents a biologically plausible, position and scale invariant model of object representations.Research on capsules is now at a similar stage to research on recurrent neural networks for speech recognition at the beginning of this century. There are fundamental representational reasons for believing that it is a better approach but it probably requires a lot more small insights before it can outperform a highly developed technology. The fact that a simple capsules system already gives unparalleled performance at segmenting overlapping digits is an early indication that capsules are a direction worth exploring.
Acknowledgement. Of the many who provided us with constructive comments, we are specially grateful to Robert Gens, Eric Langlois, Vincent Vanhoucke, Chris Williams, and the reviewers for their fruitful comments and corrections.
International Joint Conference on Artificial Intelligence Vol 2
, 1981a.Spatial transformer networks.
In Advances in Neural Information Processing Systems, pages 2017–2025, 2015.NIPS workshop on deep learning and unsupervised feature learning
, volume 2011, page 5, 2011.Proceedings of the 30th International Conference on Machine Learning (ICML13)
, pages 1058–1066, 2013.In order to experimentally verify the convergence of the routing algorithm we plot the average change in the routing logits at each routing iteration. Fig. A.1 shows the average change after each routing iteration. Experimentally we observe that there is negligible change in the routing by iteration from the start of training. Average change in the
pass of the routing settles down after 500 epochs of training to 0.007 while at routing iteration 5 the logits only change by
on average.We observed that in general more routing iterations increases the network capacity and tends to overfit to the training dataset. Fig. A.2 shows a comparison of Capsule training loss on Cifar10 when trained with 1 iteration of routing vs iteration of routing. Motivated by Fig. A.2 and Fig. A.1 we suggest 3 iteration of routing for all experiments.