Blogs / Capsule Networks: The Intelligent Architecture for Machine Visual Understanding

Capsule Networks: The Intelligent Architecture for Machine Visual Understanding

شبکه‌های عصبی کپسولی: معماری هوشمند درک بصری ماشین‌ها

Introduction

Imagine your child sees a coffee cup for the first time. They can recognize that same cup from any angle - from above, from the side, or even when it's tilted - and still identify it as a cup. But traditional neural networks struggle with this task. If you train a Convolutional Neural Network (CNN) with images of upright cups, it might recognize a tilted cup as a completely different object.
Or consider fooling a face recognition system. With regular CNNs, you could trick the system into thinking there's a real face by placing a picture with an eye, a nose, and a mouth in completely wrong positions on a piece of paper! This is exactly the weakness that Capsule Networks aim to fix.
Capsule Networks (CapsNets) were introduced by Geoffrey Hinton - the father of deep learning - and his team to solve a fundamental problem: understanding spatial relationships between parts of an object. These networks not only understand what an object is, but also know how its components are arranged relative to each other.

What's Wrong with Convolutional Networks?

Convolutional Neural Networks perform exceptionally well in many tasks, but they have a fundamental flaw: the Pooling operation. This operation, used to reduce data dimensions, destroys precise spatial information.
When a CNN processes a face image, it can detect that "there are two eyes, there's a nose, there's a mouth" - but it doesn't care much about how these parts are arranged relative to each other. This makes CNNs vulnerable to:
  • Viewpoint changes: When an object rotates or is seen from a different angle
  • Adversarial attacks: Small changes in the image that are invisible to humans but fool the network
  • Unrealistic objects: Incorrect arrangements of parts that logically shouldn't exist

How Do Capsules Work?

In traditional neural networks, each neuron outputs a number (scalar) indicating whether a specific feature exists or not. But capsules output vectors.

Structure of a Capsule

A capsule is a group of neurons that together produce a vector:
  • Vector length: Indicates the probability of a specific feature existing (a number between 0 and 1)
  • Vector direction: Encodes the properties of that object (like angle, size, position, color, texture)
For example, a capsule responsible for detecting an "eye" might output:
[0.9, 45°, 20px, location (150,80), brown color, almond shape]
This means: "With 90% probability, there's an eye that's rotated 45 degrees, 20 pixels wide, at position (150,80), brown colored, and almond-shaped."

Dynamic Routing Mechanism

The beating heart of CapsNets is the Routing by Agreement algorithm. This mechanism determines which capsule in the higher layer should receive information from lower-layer capsules.
The process works like this:
  1. Prediction: Each lower-layer capsule predicts what the higher-layer capsule's output should be
  2. Agreement: If a capsule's prediction matches the actual output of the higher capsule, the connection strengthens
  3. Weighting: Capsules with the most agreement get higher weights
This mechanism is like democratic voting: capsules that "agree" have their votes counted more.
To understand better, imagine three capsules that have detected "left eye," "right eye," and "nose." All three predict that a "face" capsule should be activated in the higher layer. If these three predictions are consistent (e.g., they have logical positions and angles), the "face" capsule activates. But if the eyes and nose are in illogical positions, there's no agreement and the face capsule doesn't activate.

Squashing Activation Function

To ensure the capsule vector length is between 0 and 1 (to indicate probability), a function called Squashing is used:
v = (||s||² / (1 + ||s||²)) × (s / ||s||)
This function compresses short vectors toward zero and keeps long vectors close to 1, while preserving the vector direction.

Classic CapsNet Architecture

The original CapsNet architecture introduced by Sabour and colleagues in 2017 was designed for the MNIST database (handwritten digit recognition):

Network Layers:

1. Initial Convolutional Layer
  • 256 filters of 9×9 with stride=1
  • Extracts basic features like edges and corners
2. PrimaryCaps Layer
  • 32 channels of 8-dimensional capsules
  • Each capsule represents a simple feature
  • Total of 6.5 million capsules
3. DigitCaps Layer
  • 10 capsules of 16 dimensions (each representing a digit from 0 to 9)
  • Length of each vector indicates the probability of that digit
  • Vector direction indicates digit features (angle, line thickness, etc.)
4. Reconstruction Network (Decoder)
  • A three-layer fully connected neural network
  • Input: winning capsule vector (recognized digit)
  • Output: reconstruction of original image
  • Goal: forcing capsules to learn meaningful features

Loss Function

CapsNet uses Margin Loss:
L_k = T_k max(0, m⁺ - ||v_k||)² + λ(1 - T_k) max(0, ||v_k|| - m⁻)²
Where:
  • T_k = 1 if class k exists, otherwise 0
  • m⁺ = 0.9 (minimum length for existing class)
  • m⁻ = 0.1 (maximum length for absent class)
  • λ = 0.5 (weight for absent classes)
Additionally, reconstruction loss is added to force capsules to learn meaningful features.

Amazing Advantages of CapsNets

1. Robustness to Rotation and Viewpoint Changes

One of the most powerful features of CapsNets is their ability to generalize to geometric transformations. If you train the network with upright images of an object, it can recognize that object even when it's rotated 45 degrees or seen from a different angle.
This is because capsules store rotation and position information in their vectors. When an object rotates, only the vector direction changes, not the existence of the object itself.
Real-world example: In autonomous vehicle systems, recognizing road signs from different angles is critical. CapsNets can correctly identify a "STOP" sign even when seen from the side or at an angle.

2. Data Efficiency

Traditional CNNs need millions of images for training to see all possible angles and states. But CapsNets can learn with less data because they understand spatial relationships.
If a CapsNet sees a face from the front, it can infer what that same face looks like from the side - without needing to have seen it!
Medical application: In AI medical diagnosis, labeled data is scarce. CapsNets can train with fewer MRI or CT scan images and recognize tumors from different angles.

3. Resistance to Adversarial Attacks

Adversarial attacks are one of the biggest threats to AI security systems. In these attacks, very small changes in an image (invisible to humans) can fool CNNs.
But CapsNets are harder to fool because they care about spatial relationships. You can't just mislead them by changing a few pixels - you'd have to change the entire spatial structure, which is much harder.
Security example: In face recognition systems, attackers try to fool the system with special masks or printed patterns. CapsNets can detect that the spatial relationships between facial features aren't natural.

4. Better Interpretability

One of the big problems with deep neural networks is that they're "black boxes" - we don't know exactly what they've learned. But in CapsNets, you can examine capsule vectors and see exactly what features the network has detected.
By changing different dimensions of a capsule vector and reconstructing the image, you can see what feature each dimension encodes (angle? thickness? size?).
This is very important for explainable AI, especially in fields like medicine where doctors need to know why the system gave a specific diagnosis.

5. Overlapping Object Recognition

CNNs typically struggle with detecting objects that are on top of each other or overlapping. But CapsNets, because they have separate capsules for each object, can simultaneously detect multiple overlapping objects.
Retail application: In cashier-less stores (like Amazon Go), cameras must be able to identify different products in a shopping basket even when they're stacked on each other.

CapsNets Challenges and Limitations

1. High Computational Cost

The biggest problem with CapsNets is their low speed. The Routing by Agreement algorithm needs several iterations, and this process is very time-consuming.
While a CNN can process an image in a fraction of a second, a CapsNet might take several seconds. This is problematic for real-time applications like autonomous vehicles.

2. High Memory Requirements

Capsules produce vectors instead of a single number. This means more memory is needed. For high-resolution images, the number of capsules grows rapidly.

3. Performance on Complex Images

While CapsNets perform excellently on simple databases like MNIST, they're still not as good as modern CNNs on more complex databases like ImageNet (millions of images with thousands of classes).

4. Lack of Scalability

Adding more layers to CapsNet is difficult because computational cost grows exponentially. While regular deep neural networks can have hundreds of layers.

Real-World Applications of CapsNets

1. Medical Diagnosis

In medicine, CapsNets can be used for:
  • Brain tumor detection: From MRI images at different angles
  • Skin cancer detection: From images of skin lesions
  • Chest X-ray analysis: Detecting lung diseases
The main advantage is that doctors can see exactly what features the network paid attention to (like shape, size, tumor location).

2. Document and ID Recognition

  • Detect fake passports
  • Read handwritten text
  • Verify signatures
Even if the document is scanned crooked or photographed at an angle.

3. Robotics and Machine Vision

In robotics, robots must recognize objects from different angles:
  • Warehouse robots: Arranging and picking items
  • Surgical robots: Detecting organs and tools
  • Home robots: Identifying everyday objects

4. Augmented Reality (AR)

In augmented reality, CapsNets can:
  • Recognize real objects from different angles
  • Accurately calculate object position and orientation
  • Properly place virtual objects on real ones

5. E-commerce Item Recognition

Online stores can use CapsNets for:
  • Visual product search
  • Recommending similar products
  • Recognizing products from user photos
Even if the photo is taken from a bad angle or in poor lighting.

Recent Advances in CapsNets

EM Routing

In 2018, Hinton introduced a new algorithm called EM Routing (Expectation-Maximization) that's faster than Routing by Agreement.
This algorithm uses statistical methods to find agreement and can scale to deeper networks.

Self-Attention Routing

Researchers have recently implemented the Attention mechanism in CapsNets. This approach is inspired by the success of Transformer models.
Self-Attention Routing is both faster and has better accuracy.

Capsules for Time Series Data

CapsNets aren't just for images anymore! Researchers have adapted them for time series forecasting like stock prices, weather patterns, and medical signals.

Efficient CapsNets

Many efforts have been made to reduce computational cost:
  • Fast Routing: Using mathematical approximations
  • Sparse Capsules: Activating only necessary capsules
  • Quantized CapsNets: Using lower precision (like 8-bit instead of 32-bit)
These improvements make CapsNets more usable for mobile devices and Internet of Things.

3D Capsules

For applications like medicine (CT scans), robotics, and virtual reality, three-dimensional CapsNets have been developed that can work with volumetric data.

Comparing CapsNet with Other Architectures

CapsNet vs CNN

Feature CNN CapsNet
Understanding spatial relationships Weak Excellent
Speed Very fast Slow
Data requirement High Lower
Rotation resistance Weak Excellent
Scalability Excellent Limited
Memory usage Low High

CapsNet vs Vision Transformers

Vision Transformers (ViT) have become powerful competitors to both CNNs and CapsNets in recent years. ViTs use the Attention mechanism and can learn long-range relationships in images.
ViT advantages: Higher speed, better scalability, excellent performance on large databases
CapsNet advantages: Better understanding of spatial relationships, less data requirement, more interpretable

CapsNet and Graph Neural Networks

Graph Neural Networks (GNN) also care about relationships between components like CapsNets. Some researchers view CapsNets as a type of GNN for image data.
Combining these two approaches could create more powerful networks.

Implementing CapsNet

For those who want to try CapsNet, the TensorFlow, PyTorch, and Keras libraries offer good support.

Simple PyTorch Code Example

python
import torch
import torch.nn as nn
import torch.nn.functional as F

class PrimaryCaps(nn.Module):
def __init__(self, num_capsules=8, in_channels=256, out_channels=32):
super(PrimaryCaps, self).__init__()
self.capsules = nn.ModuleList([
nn.Conv2d(in_channels, out_channels, kernel_size=9, stride=2)
for _ in range(num_capsules)
])
def forward(self, x):
outputs = [capsule(x).view(x.size(0), -1, 1) for capsule in self.capsules]
outputs = torch.cat(outputs, dim=-1)
return self.squash(outputs)
def squash(self, tensor):
squared_norm = (tensor ** 2).sum(dim=-1, keepdim=True)
scale = squared_norm / (1 + squared_norm)
return scale * tensor / torch.sqrt(squared_norm + 1e-8)
class DigitCaps(nn.Module):
def __init__(self, num_capsules=10, num_routes=32 * 6 * 6,
in_channels=8, out_channels=16):
super(DigitCaps, self).__init__()
self.num_routes = num_routes
self.num_capsules = num_capsules
self.W = nn.Parameter(torch.randn(1, num_routes, num_capsules,
out_channels, in_channels))
def forward(self, x, num_iterations=3):
batch_size = x.size(0)
x = x.unsqueeze(2).unsqueeze(4)
u_hat = torch.matmul(self.W, x).squeeze(4)
b = torch.zeros(batch_size, self.num_routes, self.num_capsules, 1)
for iteration in range(num_iterations):
c = F.softmax(b, dim=2)
s = (c * u_hat).sum(dim=1, keepdim=True)
v = self.squash(s)
if iteration < num_iterations - 1:
agreement = torch.matmul(u_hat, v.transpose(3, 4))
b = b + agreement
return v.squeeze(1)
def squash(self, tensor):
squared_norm = (tensor ** 2).sum(dim=-1, keepdim=True)
scale = squared_norm / (1 + squared_norm)
return scale * tensor / torch.sqrt(squared_norm + 1e-8)
This example code shows how PrimaryCaps and DigitCaps layers are implemented.

Tools and Learning Resources

To get started with CapsNets:
  1. Hinton's original code: The original paper has complete TensorFlow code
  2. PyTorch implementations: Several open-source implementations available on GitHub
  3. Online courses: Deep learning courses that cover CapsNets
  4. Kaggle notebooks: Practical examples on real data
For those who want to get more serious, I recommend starting with Google Colab which provides free GPU.

What's the Future of CapsNets?

Integration with Emerging Technologies

Researchers are working on combining CapsNets with:

Future Applications

In the future, CapsNets could revolutionize these areas:
1. Autonomous vehicles: More accurate detection of pedestrians, vehicles, and signs from any angle
2. Robotic surgery: Helping surgical robots detect organs and tissues in any position
3. Virtual/Augmented Reality: More accurate motion tracking and more natural interaction with virtual objects
4. Early disease detection: Analyzing medical images with higher accuracy and less data requirement
5. Cybersecurity: Detecting attack patterns considering their structure and relationships

Remaining Challenges

For CapsNets to join the mainstream of artificial intelligence, these problems must be solved:
  1. Speed: Ways to increase speed must be found
  2. Scalability: They must be able to scale to larger networks
  3. Standardization: Need for standard architectures and best practices
  4. Tools: More optimized and user-friendly libraries
  5. Education: More educational resources for developers

Key Points for Learning CapsNets

If you want to work with CapsNets, consider these points:
1. Solid foundation: First learn Convolutional Neural Networks well
2. Mathematics: Understanding linear algebra and vectors is essential
3. Start small: Begin with implementation on MNIST, then move to more complex data
4. Practical experience: Write code and experiment - theoretical reading isn't enough
5. Scientific community: Follow new papers and participate in scientific discussions

Conclusion

Capsule Networks show that there are still many ways to improve machine learning. The main idea of CapsNets - understanding spatial relationships - is very powerful and could have a big impact in the future.
Although CapsNets still have challenges, they have tremendous potential to solve problems that CNNs cannot. With advances in hardware and more efficient algorithms, we'll likely see wider applications of CapsNets in the near future.
For those working in computer vision and deep learning, familiarity with CapsNets can open new horizons. This technology might be the key to solving some fundamental challenges in artificial intelligence.
Will CapsNets completely replace CNNs? Probably not. But they can be a powerful tool alongside other techniques. The future likely belongs to hybrid architectures that combine the best features of CapsNets, Transformers, and other innovations.
Now it's time for you to try this technology and see what wonders you can build with it!