{"id":21105175,"url":"https://github.com/tchaton/capsnet-keras-py2.7","last_synced_at":"2025-10-20T04:21:00.864Z","repository":{"id":134495090,"uuid":"110135648","full_name":"tchaton/CapsNet-Keras-py2.7","owner":"tchaton","description":"XifengGuo/CapsNet-Keras github re-implemented with py2.7","archived":false,"fork":false,"pushed_at":"2017-11-09T16:04:58.000Z","size":425,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-21T03:24:54.789Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tchaton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-09T15:56:28.000Z","updated_at":"2017-11-09T15:59:36.000Z","dependencies_parsed_at":"2023-06-16T11:15:37.734Z","dependency_job_id":null,"html_url":"https://github.com/tchaton/CapsNet-Keras-py2.7","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tchaton%2FCapsNet-Keras-py2.7","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tchaton%2FCapsNet-Keras-py2.7/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tchaton%2FCapsNet-Keras-py2.7/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tchaton%2FCapsNet-Keras-py2.7/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tchaton","download_url":"https://codeload.github.com/tchaton/CapsNet-Keras-py2.7/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243551230,"owners_count":20309291,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T00:04:04.618Z","updated_at":"2025-10-20T04:20:55.810Z","avatar_url":"https://github.com/tchaton.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"This repository is a copy of https://github.com/XifengGuo/CapsNet-Keras where I changed the code in order to make it work on Python 2.7.6.\n\n# CapsNet-Keras\n[![License](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/XifengGuo/CapsNet-Keras/blob/master/LICENSE)\n\nNow `test error \u003c 0.4%`. A Keras implementation of CapsNet in the paper:   \n[Sara Sabour, Nicholas Frosst, Geoffrey E Hinton. Dynamic Routing Between Capsules. NIPS 2017](https://arxiv.org/abs/1710.09829)\n\n**Differences with the paper:**   \n- We use the learning rate decay with `decay factor = 0.9` and `step = 1 epoch`,    \nwhile the paper did not give the detailed parameters.\n- We only report the test errors after `30 epochs` training (still under-fitting).   \nIn the paper, I suppose they trained for `1250 epochs` according to Figure A.1?\n- We use MSE (mean squared error) as the reconstruction loss and \nthe coefficient for the loss is `lam_recon=0.0005*784=0.392`.   \nThis should be **equivalent** with using SSE (sum squared error) and `lam_recon=0.0005` as in the paper.\n\n**Recent updates:**\n- Change the default value of lam_recon from 0.0005 to 0.392. This is because the reconstruction\nloss is SSE in paper but MSE in our implementation. \nWe believe that MSE is more robust to the dimension of input images.\n- Report test errors on MNIST\n\n**TODO**\n- ~~The model has 8M parameters, while the paper said it should be 11M.~~   \nI have figured out the reason: 11M parameters are for the CapsuleNet on MultiMNIST where the\nimage size is 36x36. The CapsuleNet on MNIST should indeed have 8M parameters. \n- I'll stop pursuing higher accuracy on MNIST. \nIt is time to explore the interacting characteristics of CapsuleNet.\n\n**Contacts**\n- Your contributions to the repo are always welcome. \nOpen an issue or contact me with E-mail `guoxifeng1990@163.com` or WeChat `wenlong-guo`.\n\n\n## Usage\n\n**Step 1.\nInstall [Keras](https://github.com/fchollet/keras) \nwith [TensorFlow](https://github.com/tensorflow/tensorflow) backend.**\n```\npip install tensorflow-gpu\npip install keras\n```\n\n**Step 2. Clone this repository to local with higher keras / tensorflow versions.**\n```\ngit clone https://github.com/XifengGuo/CapsNet-Keras.git\ncd CapsNet-Keras\n\n```\n**Step 2. Clone this repository to local with Python2.7.6.**\n```\ngit clone https://github.com/tchaton/CapsNet-Keras-py2.7\ncd CapsNet-Keras\n\n**Step 3. Train a CapsNet on MNIST**  \n\nTraining with default settings:\n```\n$ python capsulenet.py\n```\nTraining with one routing iteration (default 3).   \n```\n$ python capsulenet.py --num_routing 1\n```\n\nOther parameters include `batch_size, epochs, lam_recon, shift_fraction, save_dir` can be\npassed to the function in the same way. Please refer to `capsulenet.py`\n\n**Step 4. Test a pre-trained CapsNet model**\n\nSuppose you have trained a model using the above command, then the trained model will be\nsaved to `result/trained_model.h5`. Now just launch the following command to get test results.\n```\n$ python capsulenet.py --is_training 0 --weights result/trained_model.h5\n```\nIt will output the testing accuracy and show the reconstructed images.\nThe testing data is same as the validation data. It will be easy to test on new data, \njust change the code as you want.\n\nYou can also just *download a model I trained* from https://pan.baidu.com/s/1o7Hb9fO\n\n## Results\n\n**Test Errors**   \n\nCapsNet classification test **error** on MNIST. Average and standard deviation results are\nreported by 3 trials. The results can be reproduced by launching the following commands.   \n ```\n python capsulenet.py --num_routing 1 --lam_recon 0.0    #CapsNet-v1   \n python capsulenet.py --num_routing 1 --lam_recon 0.392  #CapsNet-v2\n python capsulenet.py --num_routing 3 --lam_recon 0.0    #CapsNet-v3 \n python capsulenet.py --num_routing 3 --lam_recon 0.392  #CapsNet-v4\n```\n   Method     |   Routing   |   Reconstruction  |  MNIST (%)  |  *Paper*    \n   :---------|:------:|:---:|:----:|:----:\n   Baseline |  -- | -- | --             | *0.39* \n   CapsNet-v1 |  1 | no | 0.39 (0.024)  | *0.34 (0.032)* \n   CapsNet-v2  |  1 | yes | 0.37 (0.022)| *0.29 (0.011)*\n   CapsNet-v3 |  3 | no | 0.40 (0.016)  | *0.35 (0.036)*\n   CapsNet-v4  |  3 | yes| 0.34 (0.009) | *0.25 (0.005)*\n   \nLosses and accuracies:   \n![](result/log.png)\n\n\n**Training Speed**  \n\nAbout `110s / epoch` on a single GTX 1070 GPU.   \n\n\n**Reconstruction result**  \n\nThe result of CapsNet-v4 by launching   \n```\npython capsulenet.py --is_training 0 --weights result/trained_model.h5\n```\nDigits at top 5 rows are real images from MNIST and \ndigits at bottom are corresponding reconstructed images.\n\n![](real_and_recon.png)\n\n**The model structure:**  \n \n![](result/model.png)\n\n## Other Implementations\n- Kaggle (this version as self-contained notebook):\n  - [MNIST Dataset](https://www.kaggle.com/kmader/capsulenet-on-mnist) running on the standard MNIST and predicting for test data\n  - [MNIST Fashion](https://www.kaggle.com/kmader/capsulenet-on-fashion-mnist) running on the more challenging Fashion images.\n- TensorFlow:\n  - [naturomics/CapsNet-Tensorflow](https://github.com/naturomics/CapsNet-Tensorflow.git)   \n  Very good implementation. I referred to this repository in my code.\n  - [InnerPeace-Wu/CapsNet-tensorflow](https://github.com/InnerPeace-Wu/CapsNet-tensorflow)   \n  I referred to the use of tf.scan when optimizing my CapsuleLayer.\n  - [LaoDar/tf_CapsNet_simple](https://github.com/LaoDar/tf_CapsNet_simple)\n\n- PyTorch:\n  - [nishnik/CapsNet-PyTorch](https://github.com/nishnik/CapsNet-PyTorch.git)\n  - [timomernick/pytorch-capsule](https://github.com/timomernick/pytorch-capsule)\n  - [gram-ai/capsule-networks](https://github.com/gram-ai/capsule-networks)\n  - [andreaazzini/capsnet.pytorch](https://github.com/andreaazzini/capsnet.pytorch.git)\n  - [leftthomas/CapsNet](https://github.com/leftthomas/CapsNet)\n  \n- MXNet:\n  - [AaronLeong/CapsNet_Mxnet](https://github.com/AaronLeong/CapsNet_Mxnet)\n  \n- Lasagne (Theano):\n  - [DeniskaMazur/CapsNet-Lasagne](https://github.com/DeniskaMazur/CapsNet-Lasagne)\n\n- Chainer:\n  - [soskek/dynamic_routing_between_capsules](https://github.com/soskek/dynamic_routing_between_capsules)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftchaton%2Fcapsnet-keras-py2.7","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftchaton%2Fcapsnet-keras-py2.7","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftchaton%2Fcapsnet-keras-py2.7/lists"}