{"id":13520219,"url":"https://github.com/arogozhnikov/readable_capsnet","last_synced_at":"2025-05-01T14:43:27.982Z","repository":{"id":46820490,"uuid":"294852677","full_name":"arogozhnikov/readable_capsnet","owner":"arogozhnikov","description":"Blazingly fast capsule networks in 75 lines of pytorch+einops","archived":false,"fork":false,"pushed_at":"2021-09-23T15:12:00.000Z","size":34,"stargazers_count":26,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T21:23:44.073Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/arogozhnikov.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}},"created_at":"2020-09-12T02:17:37.000Z","updated_at":"2024-09-14T16:11:15.000Z","dependencies_parsed_at":"2022-09-23T04:51:55.783Z","dependency_job_id":null,"html_url":"https://github.com/arogozhnikov/readable_capsnet","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/arogozhnikov%2Freadable_capsnet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arogozhnikov%2Freadable_capsnet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arogozhnikov%2Freadable_capsnet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arogozhnikov%2Freadable_capsnet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arogozhnikov","download_url":"https://codeload.github.com/arogozhnikov/readable_capsnet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251892520,"owners_count":21660983,"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-08-01T05:02:14.427Z","updated_at":"2025-05-01T14:43:27.965Z","avatar_url":"https://github.com/arogozhnikov.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Readable Capsule Networks\n\nCapsule network in \u003c 80 lines.\n\nResearch-friendly implementation of capsule networks from [Dynamic Routing Between Capsules](https://papers.nips.cc/paper/6975-dynamic-routing-between-capsules.pdf)\nin pytorch.\n\n\n\n### What are capsules?\n\nCapsules are groups of neurons each describing an entity with positional characteristics. \n\nCapsules are meant to form a 'soft parsing tree' for a scene with strength of confidence encoded by norm of capsule's activations.\n\n\n### Intuition behind CapsNets\n\n\u003ca href='https://www.youtube.com/watch?v=x5Vxk9twXlE'\u003e\n\u003cimg src='http://arogozhnikov.github.io/images/etc/hinton_explains_capsnets.png' width=500 /\u003e\n\u003c/a\u003e\n\nVideo of lecture: \u003chttps://www.youtube.com/watch?v=x5Vxk9twXlE\u003e\n\n\n## What is different about this implementation\n\n- completely readable and very compact\n- capsule layers are perfectly stackable\n- auto inference of number of capsules after convolutional stem\n- **memory efficiency:** \u003cbr /\u003e\n  almost 3x less GPU memory foorprint compared to other [implementation](https://github.com/cedrickchee/capsule-net-pytorch) (2Gb vs 5.9Gb for batch size of 256)\n- **blazingly fast:** \u003cbr /\u003e \n  15 sec/epoch vs 747 sec/epoch on single V100 vs other [implementation](https://github.com/cedrickchee/capsule-net-pytorch)\n\nand, well, I didn't even use `torch.jit.trace` and did not use `fp16`, which will provide an additional boost in efficiency.\n\n\nTwo most important changes that made this possible are:\n\n- all convolutional capsules are packed into one fat convolution instead of splitting into several convolutions and then reshaping and concatenating, \n  `einops` takes care of making that efficiently. Additionally `einops` resolves weight management for capsules.\n- ugly routing implementation made efficient - all split/concat and all unnecessary repeats are eliminated by proper usage of `einsum` and `einops`\n\n\n## Project origins:\n\nThere is a ton of implementations for CapsNets. \n\nMost of them are hardly readable, almost all are inefficient and a large fraction is simply wrong.\nAll of them require to make some computations specific for each dataset and disallow easy tweaking of parts.   \n\n@michaelklachko [suggested](https://github.com/arogozhnikov/einops/issues/53)\nto rewrite \nhis [implementation](https://github.com/michaelklachko/CapsNet/blob/master/capsnet_cifar.py#L68-L91) \nof routing algorithm to pytorch using ein-notation.\n\nThis turned out to be a very nice exercise. \nAdditionally to improvements in routing done by Michael, \nI've minified all tricky places in encoder/decoder with `einops` layers.   \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farogozhnikov%2Freadable_capsnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farogozhnikov%2Freadable_capsnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farogozhnikov%2Freadable_capsnet/lists"}