{"id":13443247,"url":"https://github.com/BenWhetton/keras-surgeon","last_synced_at":"2025-03-20T16:30:45.578Z","repository":{"id":42480107,"uuid":"101084892","full_name":"BenWhetton/keras-surgeon","owner":"BenWhetton","description":"Pruning and other network surgery for trained Keras models.","archived":false,"fork":false,"pushed_at":"2023-12-05T17:46:35.000Z","size":135,"stargazers_count":403,"open_issues_count":41,"forks_count":107,"subscribers_count":17,"default_branch":"master","last_synced_at":"2024-10-28T06:57:47.508Z","etag":null,"topics":["deep-learning","keras","network-surgery","pruning"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BenWhetton.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-08-22T16:36:40.000Z","updated_at":"2024-10-20T08:59:18.000Z","dependencies_parsed_at":"2024-10-28T04:53:23.514Z","dependency_job_id":"b5ba674e-bd36-457a-848a-d572b17da670","html_url":"https://github.com/BenWhetton/keras-surgeon","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenWhetton%2Fkeras-surgeon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenWhetton%2Fkeras-surgeon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenWhetton%2Fkeras-surgeon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenWhetton%2Fkeras-surgeon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BenWhetton","download_url":"https://codeload.github.com/BenWhetton/keras-surgeon/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244649715,"owners_count":20487475,"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":["deep-learning","keras","network-surgery","pruning"],"created_at":"2024-07-31T03:01:58.052Z","updated_at":"2025-03-20T16:30:45.231Z","avatar_url":"https://github.com/BenWhetton.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Keras-surgeon\n\nA library for performing network surgery on trained Keras models. Useful for deep neural network pruning.\n\nKeras-surgeon provides simple methods for modifying trained \n[Keras][] models. The following functionality is currently implemented:\n* delete neurons/channels from layers\n* delete layers\n* insert layers\n* replace layers\n\nKeras-surgeon is compatible with any model architecture. Any number of \nlayers can be modified in a single traversal of the network.\n\nThese kinds of modifications are sometimes known as network surgery which \ninspired the name of this package.\n\n## Background\n\nThis project was motivated by my interest in deep learning and desire to \nexperiment with some of the pruning methods I have read about in the research \nliterature.\n\nI created this package because I could not find an easy way to prune\nneurons from Keras models. I hope it will be useful to others. \n\n## Install\nKeras-Surgeon is installed from [PyPI] using pip.\n```\npip install kerassurgeon\n```\nIf you'd like to install the examples' dependencies:\n```\npip install kerassurgeon[examples]\n```\n\nIt is compatible with `tensorflow.keras` and standalone `keras`.\n\n## Usage\nThe `operations` module contains simple methods to perform network surgery on a \nsingle layer within a model.\\\nExample usage:\n```python\nfrom kerassurgeon.operations import delete_layer, insert_layer, delete_channels\n# delete layer_1 from a model\nmodel = delete_layer(model, layer_1)\n# insert new_layer_1 before layer_2 in a model\nmodel = insert_layer(model, layer_2, new_layer_3)\n# delete channels 0, 4 and 67 from layer_2 in model\nmodel = delete_channels(model, layer_2, [0,4,67])\n```\n\nThe `Surgeon` class enables many modifications to be performed in a single operation.\\\nExample usage:\n```python\n# delete channels 2, 6 and 8 from layer_1 and insert new_layer_1 before \n# layer_2 in a model\nfrom kerassurgeon import Surgeon\nsurgeon = Surgeon(model)\nsurgeon.add_job('delete_channels', layer_1, channels=[2, 6, 8])\nsurgeon.add_job('insert_layer', layer_2, new_layer=new_layer_1)\nnew_model = surgeon.operate()\n```\n\nThe `identify` module contains methods to identify which channels to prune.\n\n## Examples \nExamples are in `kerassurgeon.examples`.\\\nBoth examples identify which neurons to prune using the method described in \n[Hu et al. (2016)][]: those which have the highest Average Percentage of Zeros (APoZ).\\\nNeither example is particularly good at demonstrating the benefits of pruning \nbut they show how Keras-surgeon can be used.\\\nI would welcome any good examples from other users.\n\n### Pruning Lenet trained on MNIST\n`lenet_minst` is a very simple example showing the effects of deleting channels from a \nsimple Lenet style network trained on MNIST. It demonstrates using the simple \nmethods from `kerasurgeon.operations`.\n\n### Inception V3 fine-tuned on flowers data-set\nThis example shows how to delete channels from many layers simultaneously using \nthe `Surgeon` Class.\\\nIt is in two parts:  \n`inception_flowers_tune` shows how to fine-tune the Inception V3 model on a small flowers \ndata set (based on a combination of [Tensorflow tutorial] and [Keras blog post]).\\\n`inception_flowers_prune` demonstrates deleting channels from many layers \nsimultaneously using the `Surgeon` Class.\n\n## Limitations\nMany commonly used layer types are fully supported. Models containing other\nlayer types may cause errors depending on if the unsupported layers are affected\nby the operation. Some layers downstream of pruned layers are also affected.\n\nRecurrent layers’ sequence length must be defined.\\\nThe model’s input shape must be defined.\n\n## License\n\n[MIT](LICENSE) © Ben Whetton\n\n\n[Hu et al. (2016)]: http://arxiv.org/abs/1607.03250\n[Keras]: https://github.com/fchollet/keras\n[Tensorflow tutorial]: https://www.tensorflow.org/tutorials/image_retraining#training_on_flowers\n[Keras blog post]: https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html\n[PyPI]: https://pypi.org/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBenWhetton%2Fkeras-surgeon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBenWhetton%2Fkeras-surgeon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBenWhetton%2Fkeras-surgeon/lists"}