Skip to main content

Torch Model Manager

Torch Model Manager is an open-source python project designed for Deep Learning developpers that aims to make the use of pytorch library easy. The version version is still under developpment. The package allows us to access, search and delete layers by index, attributes or instance.

Examples of Use

  1. Initialization
from torchvision import
from torch_model_manager import TorchModelManager

# Assume you have a PyTorch model 'model'
model = models.vgg16(pretrained=True)

model_manager = TorchModelManager(model)
  1. Get Named Layers
named_layers = model_manager.get_named_layers()

# output
>>> {
    'features': {
        '0': 'Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '1': 'ReLU(inplace=True)',
        '2': 'Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '3': 'ReLU(inplace=True)',
        '4': 'MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)',
        '5': 'Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '6': 'ReLU(inplace=True)',
        '7': 'Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '8': 'ReLU(inplace=True)',
        '9': 'MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)',
        '10': 'Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '11': 'ReLU(inplace=True)',
        '12': 'Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '13': 'ReLU(inplace=True)',
        '14': 'Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '15': 'ReLU(inplace=True)',
        '16': 'MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)',
        '17': 'Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '18': 'ReLU(inplace=True)',
        '19': 'Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '20': 'ReLU(inplace=True)',
        '21': 'Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '22': 'ReLU(inplace=True)',
        '23': 'MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)',
        '24': 'Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '25': 'ReLU(inplace=True)',
        '26': 'Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '27': 'ReLU(inplace=True)',
        '28': 'Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',
        '29': 'ReLU(inplace=True)',
        '30': 'MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)',
    },
    'avgpool': 'AdaptiveAvgPool2d(output_size=(7, 7))',
    'classifier': {
        '0': 'Linear(in_features=25088, out_features=4096, bias=True)',
        '1': 'ReLU(inplace=True)',
        '2': 'Dropout(p=0.5, inplace=False)',
        '3': 'Linear(in_features=4096, out_features=4096, bias=True)',
        '4': 'ReLU(inplace=True)',
        '5': 'Dropout(p=0.5, inplace=False)',
        '6': 'Linear(in_features=4096, out_features=1000, bias=True)'
    }
}

This method allows the user to access the overall architecture of the model in dictionnary format.

  1. Get Layer by Index
layer_index = ['classifier', 6]
layer = model_manager.get_layer_by_index(layer_index)

>>> Linear(in_features=4096, out_features=1000, bias=True)

The index is represented by a list, where each position represents a level. For instance, in the previous example, 'classifier' is the index to access the first level of the model architecture, and 6 is the index of the layer at the second level.

  1. Get Layer by Attribute
layers = model_manager.get_layer_by_attribute('out_features', 1000, '==')
>>> {('classifier', 6): Linear(in_features=4096, out_features=1000, bias=True)}

Retrieves all the layers satifying the given condition out_features = 1000.

  1. Get Layers by Conditions
# Retrieve layers that satisfy the given conditions
conditions = {
            'and': [{'==': ('kernel_size', (1, 1))}, {'==': ('stride', (1, 1))}],
            'or': [{'==': ('kernel_size', (3, 3))}]
            }
layers = model_manager.get_layer_by_attributes(conditions)
>>> {('features', 0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))}

It is almost the same as the previous one, but this time it extracts the layers that satisfy a set of conditions.

  1. Get Layer by Instance
# Search for layers in the model by their instance type
layers = model_manager.get_layer_by_instance(nn.Conv2d)
>>> {('features', 0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)), ('features', 28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))}

The get_layer_by_instance method allows you to extract layers of a specific type from the model. In the previous example, the extracted layers are convolutional layers.

  1. Delete Layer by Index The deletion process involves the following steps:

  2. Search for the layers and retrieve their indexes.

  3. Delete the layers at the corresponding indexes.

Here is an example of how to delete layers using different methods:

  • Delete a layer by index:
# Delete a layer from the model using its index
model_manager.delete_layer_by_index(['features', 0])
  1. Delete Layer by Attribute
# Delete layers from the model based on a specific attribute
model_manager.delete_layer_by_attribute('activation', 'relu', '==')
  1. Delete Layers by Conditions
# Delete layers from the model based on multiple conditions
conditions = {
    'and': [{'==': ('kernel_size', (1, 1))}, {'==': ('stride', (1, 1))}],
    'or': [{'==': ('kernel_size', (3, 3))}]
}
model_manager.delete_layer_by_attributes(conditions)
  1. Delete Layer by Instance
# Delete layers from the model by their instance type
model_manager.delete_layer_by_instance(nn.Conv2d)

Metadata

Release files for torch-model-manager 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torch-model-manager 0.2.0
File Size Uploaded
torch_model_manager-0.2.0.tar.gz 16.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torch-model-manager 0.2.0
File Interpreter ABI Platform
torch_model_manager-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 30.9 kB

Release files / torch_model_manager-0.2.0.tar.gz

Download URL torch_model_manager-0.2.0.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a37c08c0ba59f369f97b7991fb73a60ed8bf892cc62ffcd9573a6e11a84ce3a9
BLAKE2b-256 checksum
How to use checksums
d8368336c07cf05cc134b5135cb7218b6d083d68321e82b592ca5d53497f236f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Release files / torch_model_manager-0.2.0-py3-none-any.whl

Download URL torch_model_manager-0.2.0-py3-none-any.whl
Size 14.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7e728de90c25364191753a06348170f7f7207f6a5523422bd5e8cb653c84f51
BLAKE2b-256 checksum
How to use checksums
019d3ab344bcc6abe18e5d2964e2daa8edfaf1362814192e1b9fed015b11e08c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page