OpenViSTAI · Agents · Blockchain · Crypto
Menu
AI Developer Tools & SDKs

Keras Surgeon

Keras Surgeon is a Python library for structural edits to trained Keras models, including pruning neurons or channels and deleting, inserting or replacing layers. It supports single-layer operations, batching multiple edits through the Surgeon class, and identifying channels to prune.

Independent OpenViST project profile, based on linked sources.

Keras Surgeon — Pruning channels from trained Keras models
OpenViST project illustration. See the linked sources for product details.
Understand the project

Practical overview

When it fits

Use it when a trained Keras model needs structural edits, such as channel pruning or layer changes, and its layer types and shape requirements fit the operation.

Who it is for

  • Developers modifying trained models built with standalone Keras or tensorflow.keras.
  • Practitioners exploring neuron or channel pruning and other structural model changes.

Documented uses

  • Delete selected neurons or channels from a layer.
  • Delete, insert or replace layers in an existing model.
  • Batch multiple structural edits into one operation.
  • Use channel-identification methods to help choose channels for pruning.
Before you choose

Requirements & limits

The public GitHub repository identifies the project as open source and specifies the MIT license. Installation is documented through pip.

  • The model input shape must be defined, and recurrent layers require a defined sequence length.
  • Unsupported layer types affected by an operation may cause errors; downstream layers can also be affected by pruning.
  • The included examples show how to use pruning operations, but the README says they do not particularly demonstrate pruning benefits.

Check current requirements in the official documentation. Source-based guidance does not mean OpenViST has run or independently tested this project.

Source-based practical guide

Install Keras-Surgeon

Install Keras-Surgeon for use with a Keras model.

Before you start

  • Python environment with pip.
  • A tensorflow.keras or standalone keras installation.

Steps

  1. Install the library with the command below.

    Documented command
    pip install kerassurgeon
  2. To install the example dependencies as well, use the command below.

    Documented command
    pip install kerassurgeon[examples]

Expected outcome

Keras-Surgeon is installed and can be used to modify trained Keras models.

Guide limitations

  • The library supports many commonly used layer types; the README notes that other layer types may be limited.
Source-based practical guide

Prune channels and insert a layer with Surgeon

Apply channel deletion and layer insertion to a model in one Surgeon operation.

Before you start

  • An existing Keras model, referenced as model.
  • The model layers referenced as layer_1 and layer_2, and a new layer referenced as new_layer_1.

Steps

  1. Create a Surgeon instance with surgeon = Surgeon(model).

  2. Queue channel deletion with surgeon.add_job('delete_channels', layer_1, channels=[2, 6, 8]).

  3. Queue insertion before layer_2 with surgeon.add_job('insert_layer', layer_2, new_layer=new_layer_1).

  4. Apply the queued changes with new_model = surgeon.operate().

Expected outcome

The example produces `new_model` with the specified channels deleted from `layer_1` and `new_layer_1` inserted before `layer_2`.

Guide limitations

  • The README says many commonly used layer types are fully supported but warns that models containing other layer types may have limitations.
  • The example assumes the model and referenced layers already exist; it does not document model loading or evaluation.
Evidence and transparency

Sources & review

Source record details
Source availability
Active
Sources checked
15 November 2026
Original website title
BenWhetton/keras-surgeon · Official homepage title · retained in its source language