Home

Awesome

Dynamic Routing Between Capsules

reference: Dynamic routing between capsules by Sara Sabour, Nicholas Frosst, Geoffrey E Hinton

Note: this implementation strictly follow the instructions of the paper, check the paper for details.

Takeaways

The key of the paper is not how accurate the CapsNet is, but the novel idea of representation of image with capsule.

Dependencies

six>=1.11
matplotlib>=2.0.2
numpy>=1.7.1
scipy>=0.13.2
easydict>=1.6
tqdm>=4.17.1

install by running

$ cd $ROOT
$ pip install -r requirements.txt

Experiments

NOTE: all the experiments conducted on the checkpoint: Jbox(SJTU) or Google_Drive

reconstruction

By running:

$ cd code
$ python eval.py --ckpt 'path/to/ckpt' --mode reconstruct

reconstruct results: (Note: the float numbers on the row with even number are max norm of the 10 digit capsules)

1 2

capsule unit representation

By running:

$ cd code
$ python eval.py --ckpt 'path/to/ckpt' --mode cap_tweak

results:

Note: images along x-axis are representations of units of 16-D vector, and y-axis corresponds to the tweak range of [-0.25, 0.25] with stride 0.05.

cap_tweak-1 cap_tweak-1

adversarial test

By running:

$ cd code
$ python eval.py --ckpt 'path/to/ckpt' --mode adversarial

result:

adver-1 adver-2 adver-3

the adversarial result is not as good as i expected, I was hoping that capsule representation would be more robust to adversarial attack.

training

Note: all trained with batch_size = 100

latest commit with 3 iterations of dynamic routing:

1. update dynamic routing with tf.while_loop and static way
2. fix margin loss issue

result:

Iterations1k2k3k4k5k
val_acc98.9099.1699.0999.3099.24
test_acc----99.21

commit 8e3785d.

with bugs:
1. wrong implementation of margin loss
2. updating `prior` during routing 

result:

Iterations2k4k5k7k9k10k
val_acc98.0298.58-98.8298.96-
test_acc--98.89--99.09

Train

$ cd $ROOT/code
$ python train.py --data_dir 'path/to/data' --max_iters 10000 --ckpt 'OPTIONAL:path/to/ckpt' --batch_size 100

or train with logs by runing(NOTE: set extra arguments in train.sh accordingly):

$ cd $ROOT/code
$ bash train.sh

TODO

Reference