{"id":21135231,"url":"https://github.com/cloneofsimo/realformer-pytorch","last_synced_at":"2025-07-09T03:32:42.893Z","repository":{"id":98904003,"uuid":"324715154","full_name":"cloneofsimo/realformer-pytorch","owner":"cloneofsimo","description":"Implementation of RealFormer using pytorch","archived":false,"fork":false,"pushed_at":"2020-12-27T09:52:22.000Z","size":17537,"stargazers_count":102,"open_issues_count":0,"forks_count":13,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-16T05:32:17.932Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cloneofsimo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2020-12-27T08:06:53.000Z","updated_at":"2024-07-22T03:55:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"10cfaa69-7a49-495d-b93b-e03f34daea42","html_url":"https://github.com/cloneofsimo/realformer-pytorch","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/cloneofsimo%2Frealformer-pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloneofsimo%2Frealformer-pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloneofsimo%2Frealformer-pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloneofsimo%2Frealformer-pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloneofsimo","download_url":"https://codeload.github.com/cloneofsimo/realformer-pytorch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225482022,"owners_count":17481179,"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-20T06:46:43.246Z","updated_at":"2024-11-20T06:46:43.763Z","avatar_url":"https://github.com/cloneofsimo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# RealFormer-Pytorch\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./figures/modelfig.PNG\" alt=\"modelfig\"/\u003e\n\u003c/p\u003e\n\nImplementation of RealFormer using pytorch. Includes comparison with classical Transformer on image classification task (ViT) wrt CIFAR-10 dataset.\n\nOriginal Paper of the model : https://arxiv.org/abs/2012.11747\n\n## So how are RealFormers at vision tasks?\n\nRun the train.py with\n```python\nmodel = ViR(\n        image_pix = 32,\n        patch_pix = 4,\n        class_cnt = 10,\n        layer_cnt = 4\n    )\n```\nto Test how RealFormer works on CIFAR-10 dataset compared to just classical ViT, which is\n\n```python\nmodel = ViT(\n        image_pix = 32,\n        patch_pix = 4,\n        class_cnt = 10,\n        layer_cnt = 4\n    )\n```\n...  which is of course, much, much smaller version of ViT compared to the origianl ones ().\n\n## Results\n\n**Model : layers = 4, hidden_dim = 128, feedforward_dim = 512, head_cnt = 4**\n\nTrained 10 epochs\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./figures/ViR_ly4_px4.svg\" alt=\"ViR\"/\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./figures/ViT_ly4_px4.svg\" alt=\"ViT\"/\u003e\n\u003c/p\u003e\n\nAfter 10'th epoch, Realformer achieves 65.45% while Transformer achieves 64.59%\nRealFormer seems to consistently have about 1% greater accuracy, which seems reasonable (as the papaer suggested simillar result)\n\n**Model : layers = 8, hidden_dim = 128, feedforward_dim = 512, head_cnt = 4**\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./figures/ViR_ly8_px4.svg\" alt=\"ViR\"/\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./figures/ViT_ly8_px4.svg\" alt=\"ViT\"/\u003e\n\u003c/p\u003e\n\nHaving 4 more layers obviously improves in general, and still, RealFormer consistently wins in terms of accuracy (68.3% vs 66.3%). Notice that **larger the model, bigger the difference** seems to follow here too. (I wonder how much of difference it would make on ViT-Large) \n\nWhen it comes to computation time, there was almost zero difference. (I guess adding residual attention score is O(L^2) operation, compared to matrix multiplication in softmax which is O(L^2 * D))\n\n## Conclusion\nUse RealFormer. It benifits with almost zero additional resource!\n\n## To make a custom RealFormer for other tasks\nIts not a pip package, but you can use the ResEncoderBlock module in the models.py to make a Encoder Only Transformer like the following :\n\n```python\n\nimport ResEncoderBlock from models\n\ndef RealFormer(nn.Module):\n...\n  def __init__(self, ...):\n  ...\n    self.mains = nn.Sequential(*[ResEncoderBlock(emb_s = 32, head_cnt = 8, dp1 = 0.1, dp2 = 0.1) for _ in range(layer_cnt)])\n  ...\n  def forward(self, x):\n  ...\n    prev = None\n    for resencoder in self.mains:\n        x, prev = resencoder(x, prev = prev)\n  ...\n    return x\n```\nIf you're not really clear what is going on or what to do, request me to make this a pip package.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloneofsimo%2Frealformer-pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloneofsimo%2Frealformer-pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloneofsimo%2Frealformer-pytorch/lists"}