torch-model-manager


Nametorch-model-manager JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/Billal-MOKHTARI/torch-model-manager
SummaryA package for managing PyTorch models
upload_time2024-05-02 07:22:57
maintainerNone
docs_urlNone
authorBillal MOKHTARI
requires_pythonNone
licenseNone
keywords pytorch deep learning machine learning high level programming
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](https://img.shields.io/badge/version-0.0.5-gray?labelColor=blue&style=flat) is still under developpment. The package allows us to access, search and delete layers by index, attributes or instance.

### Examples of Use
1. **Initialization**
```python
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)
```

2. **Get Named Layers**
```python
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.


3. **Get Layer by Index**
```python
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.

4. **Get Layer by Attribute**
```python
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`.

5. **Get Layers by Conditions**
```python
# 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.

6. **Get Layer by Instance**
```python
# 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.

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

1. Search for the layers and retrieve their indexes.
2. Delete the layers at the corresponding indexes.

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

- Delete a layer by index:

```python
# Delete a layer from the model using its index
model_manager.delete_layer_by_index(['features', 0])
```

8. **Delete Layer by Attribute**
```python
# Delete layers from the model based on a specific attribute
model_manager.delete_layer_by_attribute('activation', 'relu', '==')
```
9. **Delete Layers by Conditions**
```python
# 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)
```
10. **Delete Layer by Instance**

```python
# Delete layers from the model by their instance type
model_manager.delete_layer_by_instance(nn.Conv2d)
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Billal-MOKHTARI/torch-model-manager",
    "name": "torch-model-manager",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "PyTorch, Deep Learning, Machine Learning, High Level Programming",
    "author": "Billal MOKHTARI",
    "author_email": "mokhtaribillal1@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/d8/36/8336c07cf05cc134b5135cb7218b6d083d68321e82b592ca5d53497f236f/torch_model_manager-0.2.0.tar.gz",
    "platform": null,
    "description": "# Torch Model Manager\n\n\n**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](https://img.shields.io/badge/version-0.0.5-gray?labelColor=blue&style=flat) is still under developpment. The package allows us to access, search and delete layers by index, attributes or instance.\n\n### Examples of Use\n1. **Initialization**\n```python\nfrom torchvision import\nfrom torch_model_manager import TorchModelManager\n\n# Assume you have a PyTorch model 'model'\nmodel = models.vgg16(pretrained=True)\n\nmodel_manager = TorchModelManager(model)\n```\n\n2. **Get Named Layers**\n```python\nnamed_layers = model_manager.get_named_layers()\n\n# output\n>>> {\n    'features': {\n        '0': 'Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '1': 'ReLU(inplace=True)',\n        '2': 'Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '3': 'ReLU(inplace=True)',\n        '4': 'MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)',\n        '5': 'Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '6': 'ReLU(inplace=True)',\n        '7': 'Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '8': 'ReLU(inplace=True)',\n        '9': 'MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)',\n        '10': 'Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '11': 'ReLU(inplace=True)',\n        '12': 'Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '13': 'ReLU(inplace=True)',\n        '14': 'Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '15': 'ReLU(inplace=True)',\n        '16': 'MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)',\n        '17': 'Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '18': 'ReLU(inplace=True)',\n        '19': 'Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '20': 'ReLU(inplace=True)',\n        '21': 'Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '22': 'ReLU(inplace=True)',\n        '23': 'MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)',\n        '24': 'Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '25': 'ReLU(inplace=True)',\n        '26': 'Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '27': 'ReLU(inplace=True)',\n        '28': 'Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))',\n        '29': 'ReLU(inplace=True)',\n        '30': 'MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)',\n    },\n    'avgpool': 'AdaptiveAvgPool2d(output_size=(7, 7))',\n    'classifier': {\n        '0': 'Linear(in_features=25088, out_features=4096, bias=True)',\n        '1': 'ReLU(inplace=True)',\n        '2': 'Dropout(p=0.5, inplace=False)',\n        '3': 'Linear(in_features=4096, out_features=4096, bias=True)',\n        '4': 'ReLU(inplace=True)',\n        '5': 'Dropout(p=0.5, inplace=False)',\n        '6': 'Linear(in_features=4096, out_features=1000, bias=True)'\n    }\n}\n\n```\n\nThis method allows the user to access the overall architecture of the model in dictionnary format.\n\n\n3. **Get Layer by Index**\n```python\nlayer_index = ['classifier', 6]\nlayer = model_manager.get_layer_by_index(layer_index)\n\n>>> Linear(in_features=4096, out_features=1000, bias=True)\n```\n\nThe 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.\n\n4. **Get Layer by Attribute**\n```python\nlayers = model_manager.get_layer_by_attribute('out_features', 1000, '==')\n>>> {('classifier', 6): Linear(in_features=4096, out_features=1000, bias=True)}\n```\nRetrieves all the layers satifying the given condition `out_features = 1000`.\n\n5. **Get Layers by Conditions**\n```python\n# Retrieve layers that satisfy the given conditions\nconditions = {\n            'and': [{'==': ('kernel_size', (1, 1))}, {'==': ('stride', (1, 1))}],\n            'or': [{'==': ('kernel_size', (3, 3))}]\n            }\nlayers = model_manager.get_layer_by_attributes(conditions)\n>>> {('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))}\n```\n\nIt is almost the same as the previous one, but this time it extracts the layers that satisfy a set of conditions.\n\n6. **Get Layer by Instance**\n```python\n# Search for layers in the model by their instance type\nlayers = model_manager.get_layer_by_instance(nn.Conv2d)\n>>> {('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))}\n```\n\nThe `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.\n\n7. **Delete Layer by Index**\nThe deletion process involves the following steps:\n\n1. Search for the layers and retrieve their indexes.\n2. Delete the layers at the corresponding indexes.\n\nHere is an example of how to delete layers using different methods:\n\n- Delete a layer by index:\n\n```python\n# Delete a layer from the model using its index\nmodel_manager.delete_layer_by_index(['features', 0])\n```\n\n8. **Delete Layer by Attribute**\n```python\n# Delete layers from the model based on a specific attribute\nmodel_manager.delete_layer_by_attribute('activation', 'relu', '==')\n```\n9. **Delete Layers by Conditions**\n```python\n# Delete layers from the model based on multiple conditions\nconditions = {\n    'and': [{'==': ('kernel_size', (1, 1))}, {'==': ('stride', (1, 1))}],\n    'or': [{'==': ('kernel_size', (3, 3))}]\n}\nmodel_manager.delete_layer_by_attributes(conditions)\n```\n10. **Delete Layer by Instance**\n\n```python\n# Delete layers from the model by their instance type\nmodel_manager.delete_layer_by_instance(nn.Conv2d)\n```\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A package for managing PyTorch models",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/Billal-MOKHTARI/torch-model-manager"
    },
    "split_keywords": [
        "pytorch",
        " deep learning",
        " machine learning",
        " high level programming"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "019d3ab344bcc6abe18e5d2964e2daa8edfaf1362814192e1b9fed015b11e08c",
                "md5": "70a1c0ab876a6a5d65cbf1f8eb9e8e27",
                "sha256": "f7e728de90c25364191753a06348170f7f7207f6a5523422bd5e8cb653c84f51"
            },
            "downloads": -1,
            "filename": "torch_model_manager-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "70a1c0ab876a6a5d65cbf1f8eb9e8e27",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 14628,
            "upload_time": "2024-05-02T07:22:55",
            "upload_time_iso_8601": "2024-05-02T07:22:55.473329Z",
            "url": "https://files.pythonhosted.org/packages/01/9d/3ab344bcc6abe18e5d2964e2daa8edfaf1362814192e1b9fed015b11e08c/torch_model_manager-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d8368336c07cf05cc134b5135cb7218b6d083d68321e82b592ca5d53497f236f",
                "md5": "471d9143efc9c92682b03393a4881ac5",
                "sha256": "a37c08c0ba59f369f97b7991fb73a60ed8bf892cc62ffcd9573a6e11a84ce3a9"
            },
            "downloads": -1,
            "filename": "torch_model_manager-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "471d9143efc9c92682b03393a4881ac5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 16224,
            "upload_time": "2024-05-02T07:22:57",
            "upload_time_iso_8601": "2024-05-02T07:22:57.537940Z",
            "url": "https://files.pythonhosted.org/packages/d8/36/8336c07cf05cc134b5135cb7218b6d083d68321e82b592ca5d53497f236f/torch_model_manager-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-02 07:22:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Billal-MOKHTARI",
    "github_project": "torch-model-manager",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "torch-model-manager"
}
        
Elapsed time: 0.26166s