Retour au classement

XifengGuo/CapsNet-Keras

Python

A Keras implementation of CapsNet in NIPS2017 paper "Dynamic Routing Between Capsules". Now test error = 0.34%.

kerascapsnetcapsulecapsnet-kerascapsule-networkdynamic-routing
Croissance des étoiles
Étoiles
2.5k
Forks
645
Croissance hebdomadaire
Issues
29
1k2k
oct. 2017sept. 2020août 2023juil. 2026
ArtefactsPyPIpip install capsnet-keras
README

CapsNet-Keras

License

A Keras (branch tf2.2 supports TensorFlow 2) implementation of CapsNet in the paper:
Sara Sabour, Nicholas Frosst, Geoffrey E Hinton. Dynamic Routing Between Capsules. NIPS 2017
The current average test error = 0.34% and best test error = 0.30%.

Differences with the paper:

  • We use the learning rate decay with decay factor = 0.9 and step = 1 epoch,
    while the paper did not give the detailed parameters (or they didn't use it?).
  • We only report the test errors after 50 epochs training.
    In the paper, I suppose they trained for 1250 epochs according to Figure A.1? Sounds crazy, maybe I misunderstood.
  • We use MSE (mean squared error) as the reconstruction loss and the coefficient for the loss is lam_recon=0.0005*784=0.392.
    This should be equivalent with using SSE (sum squared error) and lam_recon=0.0005 as in the paper.

Warnning

Please use Keras==2.0.7 with TensorFlow==1.2 backend, or the K.batch_dot function may not work correctly.

However, if you use Tensorflow>=2.0, then checkout branch tf2.2

Usage

Step 1. Clone this repository to local.

git clone https://github.com/XifengGuo/CapsNet-Keras.git capsnet-keras
cd capsnet-keras
git checkout tf2.2 # Only if use Tensorflow>=2.0

Step 2. Install Keras==2.0.7 with TensorFlow==1.2 backend.

pip install tensorflow-gpu==1.2
pip install keras==2.0.7

or install Tensorflow>=2.0

pip install tensorflow==2.2

Step 3. Train a CapsNet on MNIST

Training with default settings:

python capsulenet.py

More detailed usage run for help:

python capsulenet.py -h

Step 4. Test a pre-trained CapsNet model

Suppose you have trained a model using the above command, then the trained model will be saved to result/trained_model.h5. Now just launch the following command to get test results.

$ python capsulenet.py -t -w result/trained_model.h5

It will output the testing accuracy and show the reconstructed images. The testing data is same as the validation data. It will be easy to test on new data, just change the code as you want.

You can also just download a model I trained from https://pan.baidu.com/s/1sldqQo1 or https://drive.google.com/open?id=1A7pRxH7iWzYZekzr-O0nrwqdUUpUpkik

Step 5. Train on multi gpus

This requires Keras>=2.0.9. After updating Keras:

python capsulenet-multi-gpu.py --gpus 2

It will automatically train on multi gpus for 50 epochs and then output the performance on test dataset. But during training, no validation accuracy is reported.

Results

Test Errors

CapsNet classification test error on MNIST. Average and standard deviation results are reported by 3 trials. The results can be reproduced by launching the following commands.

python capsulenet.py --routings 1 --lam_recon 0.0    #CapsNet-v1   
python capsulenet.py --routings 1 --lam_recon 0.392  #CapsNet-v2
python capsulenet.py --routings 3 --lam_recon 0.0    #CapsNet-v3 
python capsulenet.py --routings 3 --lam_recon 0.392  #CapsNet-v4
Method Routing Reconstruction MNIST (%) Paper
Baseline -- -- -- 0.39
CapsNet-v1 1 no 0.39 (0.024) 0.34 (0.032)
CapsNet-v2 1 yes 0.36 (0.009) 0.29 (0.011)
CapsNet-v3 3 no 0.40 (0.016) 0.35 (0.036)
CapsNet-v4 3 yes 0.34 (0.016) 0.25 (0.005)

Losses and accuracies:

Training Speed

About 100s / epoch on a single GTX 1070 GPU.
About 80s / epoch on a single GTX 1080Ti GPU.
About 55s / epoch on two GTX 1080Ti GPU by using capsulenet-multi-gpu.py.

Reconstruction result

The result of CapsNet-v4 by launching

python capsulenet.py -t -w result/trained_model.h5

Digits at top 5 rows are real images from MNIST and digits at bottom are corresponding reconstructed images.

Manipulate latent code

python capsulenet.py -t --digit 5 -w result/trained_model.h5 

For each digit, the ith row corresponds to the ith dimension of the capsule, and columns from left to right correspond to adding [-0.25, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25] to the value of one dimension of the capsule.

As we can see, each dimension has caught some characteristics of a digit. The same dimension of different digit capsules may represent different characteristics. This is because that different digits are reconstructed from different feature vectors (digit capsules). These vectors are mutually independent during reconstruction.

Other Implementations

Dépôts similaires
lutzroeder/netron

Visualizer for neural network, deep learning and machine learning models

JavaScriptnpmMIT Licenseneural-networkdeep-learning
netron.app
33.3k3.2k
tqdm/tqdm

:zap: A Fast, Extensible Progress Bar for Python and CLI

PythonPyPIOtherprogressbarprogressmeter
tqdm.github.io
31.2k1.5k
donnemartin/data-science-ipython-notebooks

Data science Python notebooks: Deep learning (TensorFlow, Theano, Caffe, Keras), scikit-learn, Kaggle, big data (Spark, Hadoop MapReduce, HDFS), matplotlib, pandas, NumPy, SciPy, Python essentials, AWS, and various command lines.

PythonPyPIOtherpythonmachine-learning
29.3k8k
d2l-ai/d2l-en

Interactive deep learning book with multi-framework code, math, and discussions. Adopted at 500 universities from 70 countries including Stanford, MIT, Harvard, and Cambridge.

PythonPyPIOtherdeep-learningmachine-learning
d2l.ai
29.2k5.1k
matterport/Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow

PythonPyPIOthermask-rcnntensorflow
25.6k11.6k
lukasmasuch/best-of-ml-python

🏆 A ranked list of awesome machine learning Python libraries. Updated weekly.

Creative Commons Attribution Share Alike 4.0 Internationalpythonmachine-learning
ml-python.best-of.org
23.7k3.1k
Tencent/ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform

C++Otherinferencehigh-preformance
23.6k4.5k
MLEveryday/100-Days-Of-ML-Code

100-Days-Of-ML-Code中文版

Jupyter NotebookMIT Licensemachine-learningpython
22.2k5.5k
onnx/onnx

Open standard for machine learning interoperability

PythonPyPIApache License 2.0deep-learningdeep-neural-networks
onnx.ai
21.2k4k
ml-tooling/best-of-ml-python

🏆 A ranked list of awesome machine learning Python libraries. Updated weekly.

Creative Commons Attribution Share Alike 4.0 Internationalpythonmachine-learning
ml-python.best-of.org
19.8k2.7k
emilwallner/Screenshot-to-code

A neural network that transforms a design mock-up into a static website.

HTMLOtherkerasdeep-learning
16.5k1.5k
kailashahirwar/cheatsheets-ai

Essential Cheat Sheets for deep learning and machine learning researchers https://medium.com/@kailashahirwar/essential-cheat-sheets-for-machine-learning-and-deep-learning-researchers-efb6a8ebd2e5

MIT Licensedeep-learningartificial-intelligence
aicheatsheets.com
15.4k3.4k