{"id":16359316,"url":"https://github.com/gaborvecsei/ridurre-network-filter-pruning-keras","last_synced_at":"2025-03-21T00:31:40.023Z","repository":{"id":84266160,"uuid":"169144287","full_name":"gaborvecsei/Ridurre-Network-Filter-Pruning-Keras","owner":"gaborvecsei","description":"Keras model convolutional filter pruning package","archived":false,"fork":false,"pushed_at":"2019-02-16T15:44:00.000Z","size":195,"stargazers_count":44,"open_issues_count":7,"forks_count":14,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-01T01:22:53.771Z","etag":null,"topics":["deep-learning","filter-pruning","keras","keras-layer","machine-learning","network","pruning","python","python3","tensorflow"],"latest_commit_sha":null,"homepage":"https://gaborvecsei.com","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/gaborvecsei.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":"2019-02-04T20:27:13.000Z","updated_at":"2023-03-31T02:06:12.000Z","dependencies_parsed_at":"2023-05-24T04:45:44.007Z","dependency_job_id":null,"html_url":"https://github.com/gaborvecsei/Ridurre-Network-Filter-Pruning-Keras","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaborvecsei%2FRidurre-Network-Filter-Pruning-Keras","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaborvecsei%2FRidurre-Network-Filter-Pruning-Keras/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaborvecsei%2FRidurre-Network-Filter-Pruning-Keras/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaborvecsei%2FRidurre-Network-Filter-Pruning-Keras/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gaborvecsei","download_url":"https://codeload.github.com/gaborvecsei/Ridurre-Network-Filter-Pruning-Keras/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244094267,"owners_count":20397020,"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","filter-pruning","keras","keras-layer","machine-learning","network","pruning","python","python3","tensorflow"],"created_at":"2024-10-11T02:08:05.244Z","updated_at":"2025-03-21T00:31:40.017Z","avatar_url":"https://github.com/gaborvecsei.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ridurre - Filter Pruning in Deep Convolutional Networks\n\nPruning is the process when we try to shrink a network by removing the not so significant/redundant filters.\n\nThis package is a mini-framework which you can easily use on your existing models and also you can define your own\npruning methods without any struggle.\n\n\u003cimg src=\"art/pruning_framework.png\" alt=\"pruning framework diagram\"\u003e\n\n\n## Install\n\n`pip install ridurre`\n\n- Install packages inside `requirements.txt`\n    - `pip install -r requirements.txt`\n\n## Example Results\n\nThese results were achieved with the [example](example/model_pruning_example.py) provided:\n\n- *Cifar10* dataset\n- *ResNetV1*\n- KMeans filter pruning\n    - Clustering factor: `0.9` (which can be considered as an aggressive pruning)\n\n\u003cimg src=\"art/training.png\" width=\"600\" alt=\"training with pruning\"\u003e\n\n\u003cimg src=\"art/pruning.png\" width=\"600\" alt=\"pruning\"\u003e\n\n## Usage\n\n### Define you own pruning method\n\nYou can make your own pruning method by creating a new class which has the parent `BasePruning`. There is only 1 thing\nyou should take care and that the implementation of the `run_pruning_for_conv2d_layer` function.\n\nFor an example just take a look at the [`RandomFilterPruning`](/ridurre/random_pruning.py) code.\n\n### Use an already existing method\n\nCheck out the [`example/model_pruning_example.py`](/example/model_pruning_example.py) for a simple but\nextensive tutorial\n\n#### Callbacks\n\nYou will need to define 2 callbacks for the pruning:\n\n- **Model compile function**\n    - 1 argument:\n        - `model` which is a `keras.models.Model`\n    - This should define how to compile the model\n    - Example:\n        ```python\n        def compile_model(my_model):\n            my_model.compile(optimizer=optimizers.Adam(lr=0.001),\n                             loss=losses.categorical_crossentropy,\n                             metrics=[\"accuracy\"])\n        ```\n- **Finetune function**\n    - 3 arguments:\n        - `model` which is a `keras.models.Model`\n        - `initial_epoch` which is an `int`: This defines the initial epoch state for the model fitting.\n        For example it is 12 if we trained the model for 12 epochs before this function was called\n        - `finetune_epochs` which is an `int`: Defines how much should we train after a pruning.\n    - This should define how to finetune out model\n    - Example:\n        ```python\n        def finetune_model(my_model, initial_epoch, finetune_epochs):\n                my_model.fit(x_train,\n                             y_train,\n                             32,\n                             epochs=finetune_epochs,\n                             validation_data=(x_test, y_test),\n                             callbacks=callbacks,\n                             initial_epoch=initial_epoch,\n                             verbose=1)\n        ```\n\n#### Pruning\n\nYou will need to select which pruning method you would like to use. In this example I will use the KMeans pruning\n\n```python\nimport ridurre\n\n# Create the model\nmodel = build_model(...)\n\n# Define compile callback\ndef compile_my_model(model):\n    model.compile(...)\n\n# Compile with your callback (of course you can use different compilation for this train and the pruning)\ncompile_my_model(model)\n\n# Train if you would like to start from a better position\nmodel.fit(...)\n\n# Define finetuning callback\ndef finetune_my_model(model, initial_epoch, finetune_epochs):\n    model.fit(..., epochs=finetune_epochs, initial_epoch=initial_epoch)\n\n# We can start pruning\npruning = ridurre.KMeansFilterPruning(0.9,\n                                             compile_my_model,\n                                             finetune_my_model,\n                                             6,\n                                             maximum_pruning_percent=0.4,\n                                             maximum_prune_iterations=12)\nmodel, _ = pruning.run_pruning(model)\n```\n\nAt the end of the pruning step, you will have a trained and pruned model which you can use.\nI can recommend to train your model after the pruning for just a little longer as an extra step towards accuracy.\n\n## Future work\n\n- Look for problematic cases, where there is a merging (like `add`) and warn the user that the different inputs to that\noperations should be pruned in the same manner\n    - A good example for a case like this is *ResNet*\n- Define \"pruneable\" set of layers\n    - With regex or layer indices\n    - This needs to find `add`, `multiply`, `average`, etc... operations (layers) which needs same filter number\n    from the different inputs\n- Different pruning factors for channels with different number of filters\n- More pruning solutions\n- Do not depend on kerassurgeon as I only use the channel delete function\n\n## Papers\n\n*[1]* [Filter Level Pruning Based on Similar Feature Extraction for\nConvolutional Neural Networks](https://www.jstage.jst.go.jp/article/transinf/E101.D/4/E101.D_2017EDL8248/_pdf)\n\n*[2]* [Demystifying Neural Network Filter Pruning](https://openreview.net/pdf?id=rJffBWBtoX)\n\n## About\n\nGábor Vecsei\n\n- [Website](https://gaborvecsei.com)\n- [Personal Blog](https://gaborvecsei.wordpress.com/)\n- [LinkedIn](https://www.linkedin.com/in/gaborvecsei)\n- [Twitter](https://twitter.com/GAwesomeBE)\n- [Github](https://github.com/gaborvecsei)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgaborvecsei%2Fridurre-network-filter-pruning-keras","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgaborvecsei%2Fridurre-network-filter-pruning-keras","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgaborvecsei%2Fridurre-network-filter-pruning-keras/lists"}