{"id":13454798,"url":"https://github.com/www0wwwjs1/Matrix-Capsules-EM-Tensorflow","last_synced_at":"2025-03-24T06:31:28.672Z","repository":{"id":89871484,"uuid":"111348180","full_name":"www0wwwjs1/Matrix-Capsules-EM-Tensorflow","owner":"www0wwwjs1","description":"A Tensorflow implementation of CapsNet based on paper Matrix Capsules with EM Routing","archived":false,"fork":false,"pushed_at":"2018-12-02T18:22:16.000Z","size":787,"stargazers_count":218,"open_issues_count":36,"forks_count":83,"subscribers_count":27,"default_branch":"master","last_synced_at":"2024-10-28T21:41:32.732Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/www0wwwjs1.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}},"created_at":"2017-11-20T01:37:31.000Z","updated_at":"2024-08-12T03:10:41.000Z","dependencies_parsed_at":"2023-03-13T18:03:58.585Z","dependency_job_id":null,"html_url":"https://github.com/www0wwwjs1/Matrix-Capsules-EM-Tensorflow","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/www0wwwjs1%2FMatrix-Capsules-EM-Tensorflow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/www0wwwjs1%2FMatrix-Capsules-EM-Tensorflow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/www0wwwjs1%2FMatrix-Capsules-EM-Tensorflow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/www0wwwjs1%2FMatrix-Capsules-EM-Tensorflow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/www0wwwjs1","download_url":"https://codeload.github.com/www0wwwjs1/Matrix-Capsules-EM-Tensorflow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245222566,"owners_count":20580185,"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-07-31T08:00:58.020Z","updated_at":"2025-03-24T06:31:27.818Z","avatar_url":"https://github.com/www0wwwjs1.png","language":"Python","funding_links":[],"categories":["Implementations"],"sub_categories":["Tensorflow"],"readme":"# MATRIX CAPSULES EM-Tensorflow\n\n[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg?style=plastic)](https://opensource.org/licenses/Apache-2.0)\n![completion](https://img.shields.io/badge/completion%20state-95%25-blue.svg?style=plastic)\n\nA Tensorflow implementation of CapsNet based on paper [Matrix Capsules with EM Routing](https://openreview.net/pdf?id=HJWLfGWRb)\n\n\u003e **Status:**\n\u003e 1. With configuration A=32, B=8, C=16, D=16, batch_size=128, the code can work on a Tesla P40 GPU at a speed of 8s/iteration. The definitions of A-D can be referred to the paper.\n\u003e 2. With configuration A=B=C=D=32, batch_size=64, the code can work on a Tesla P40 GPU at a speed of 25s/iteration. More optimization on implementation structure is required.\n\u003e 3. Some modification and optimization is implemented to prompt the numerical stability of GMM. Specific explanations can be found in the code.\n\u003e 4. With configuration A=32, B=4, D=4, D=4, batch_size=128, each iteration of training takes around 0.6s on a Tesla P40 GPU.\n\n\u003e **Current Results on smallNORB:**\n- Configuration: A=32, B=8, C=16, D=16, batch_size=50, iteration number of EM routing: 2, with Coordinate Addition, spread loss, batch normalization\n- Training loss. Variation of loss is suppressed by batch normalization. However there still exists a gap between our best results and the reported results in the original paper.\n![spread loss](imgs/spread_loss_norb.png)\n\n- Test accuracy(current best result is 91.8%)\n![test_acc](imgs/test_accuracy_norb.png)\n\n\u003e **Ablation Study on smallNORB:**\n- Configuration: A=32, B=8, C=16, D=16, batch_size=32, iteration number of EM routing: 2, with Coordinate Addition, spread loss, test accuracy is 79.8%.\n\n\u003e **Current Results on MNIST:**\n- Configuration: A=32, B=8, C=16, D=16, batch_size=50, iteration number of EM routing: 2, with Coordinate Addition, spread loss, batch normalization, reconstruction loss.\n\n- Training loss.\n![spread loss](imgs/training_loss.png)\n\n- Test accuracy(current best result is 99.3%, only 10% samples are used in test)\n![test_acc](imgs/test_accuracy.png)\n\n\u003e **Ablation Study on MNIST:**\n- Configuration: A=32, B=4, C=4, D=4, batch_size=128, iteration number of EM routing: 2, no Coordinate Addition, cross entropy loss, test accuracy is 96.4%.\n- Configuration: A=32, B=4, C=4, D=4, batch_size=128, iteration number of EM routing: 2, with Coordinate Addition, cross entropy loss, test accuracy is 96.8%.\n- Configuration: A=32, B=8, C=16, D=16, batch_size=32, iteration number of EM routing: 2, with Coordinate Addition, spread loss 99.1%.\n\n\u003e **To Do List:**\n\u003e 1. Experiments on smallNORB as in paper is about to be casted.\n\nAny questions and comments to the code and the original algorithms are welcomed!!! My email: zhangsuofei at njupt.edu.cn\n\n## Requirements\n- Python \u003e= 3.4\n- Numpy\n- Tensorflow \u003e= 1.2.0\n- Keras\n\n```pip install -r requirement.txt```\n\n## Usage\n**Step 1.**\nClone this repository with ``git``.\n\n```\n$ git clone https://github.com/www0wwwjs1/Matrix-Capsules-EM-Tensorflow.git\n$ cd Matrix-Capsules-EM-Tensorflow\n```\n\n**Step 2.**\nDownload the [MNIST dataset](http://yann.lecun.com/exdb/mnist/), ``mv`` and extract it into ``data/mnist`` directory.(Be careful the backslash appeared around the curly braces when you copy the ``wget `` command to your terminal, remove it)\n\n```\n$ mkdir -p data/mnist\n$ wget -c -P data/mnist http://yann.lecun.com/exdb/mnist/{train-images-idx3-ubyte.gz,train-labels-idx1-ubyte.gz,t10k-images-idx3-ubyte.gz,t10k-labels-idx1-ubyte.gz}\n$ gunzip data/mnist/*.gz\n```\n\n***To install smallNORB, follow instructions in ```./data/README.md```***\n\n**Step 3.**\nStart the training(MNIST):\n```\n$ python3 train.py \"mnist\"\n```\n\n**Step 4.**\nDownload the [Fashion MNIST dataset](https://github.com/zalandoresearch/fashion-mnist), ``mv`` and extract it into ``data/fashion_mnist`` directory.(Be careful the backslash appeared around the curly braces when you copy the ``wget `` command to your terminal, remove it)\n\n```\n$ mkdir -p data/fashion_mnist\n$ wget -c -P data/fashion_mnist http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/{train-images-idx3-ubyte.gz,train-labels-idx1-ubyte.gz,t10k-images-idx3-ubyte.gz,t10k-labels-idx1-ubyte.gz}\n$ gunzip data/fashion_mnist/*.gz\n```\n\nStart the training(smallNORB):\n```\n$ python3 train.py \"smallNORB\"\n```\n\nStart the training(CNN baseline):\n```\n$ python3 train_baseline.py \"smallNORB\"\n```\n\n**Step 4.**\nView the status of training:\n```\n$ tensorboard --logdir=./logdir/{model_name}/{dataset_name}/train_log/\n```\nOpen the url tensorboard has shown.\n\n**Step 5.**\nStart the test on MNIST:\n```\n$ python3 eval.py \"mnist\" \"caps\"\n```\n\nStart the test on smallNORB:\n```\n$ python3 eval.py \"smallNORB\" \"caps\"\n```\n\n**Step 6.**\nView the status of test:\n```\n$ tensorboard --logdir=./test_logdir/{model_name}/{dataset_name}/\n```\nOpen the url tensorboard has shown.\n\n### Reference\n- [naturomics/CapsNet-Tensorflow](https://github.com/naturomics/CapsNet-Tensorflow): the implementation of Hinton's paper [Dynamic Routing Between Capsules](https://arxiv.org/abs/1710.09829)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwww0wwwjs1%2FMatrix-Capsules-EM-Tensorflow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwww0wwwjs1%2FMatrix-Capsules-EM-Tensorflow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwww0wwwjs1%2FMatrix-Capsules-EM-Tensorflow/lists"}