|
1 | 1 | Metadata-Version: 2.1 |
2 | 2 | Name: PyTorchLayerViz |
3 | | -Version: 1.0 |
| 3 | +Version: 1.1 |
4 | 4 | Summary: PyTorchLayerViz is a Python library that allows you to visualize the weights and feature maps of a PyTorch model. |
5 | 5 | Author: Simone Panico |
6 | 6 | Author-email: simone.panico@icloud.com |
7 | 7 | Keywords: python,pytorch,deep learning,model |
8 | 8 | Description-Content-Type: text/markdown |
| 9 | +License-File: LICENSE.md |
9 | 10 | Requires-Dist: torch |
10 | 11 | Requires-Dist: torchvision |
11 | 12 | Requires-Dist: pillow |
12 | 13 | Requires-Dist: matplotlib |
13 | 14 |
|
14 | | -PyTorchLayerViz is a Python library designed to assist developers and researchers in visualizing |
15 | | -the weights and feature maps of PyTorch models. This tool provides easy-to-use functions to help |
16 | | -understand and interpret deep learning models, making it an essential utility for anyone working |
17 | | -with PyTorch. |
| 15 | + |
| 16 | + |
| 17 | +# PyTorchLayerViz |
| 18 | + |
| 19 | +**PyTorchLayerViz** is a Python library designed to assist developers and researchers in visualizing the weights and feature maps of PyTorch models. This tool provides easy-to-use functions to help understand and interpret deep learning models, making it an essential utility for anyone working with PyTorch. |
| 20 | + |
| 21 | +## Table of Contents |
| 22 | + |
| 23 | +- [PyTorchLayerViz](#pytorchlayerviz) |
| 24 | + - [Table of Contents](#table-of-contents) |
| 25 | + - [Installation](#installation) |
| 26 | + - [Usage](#usage) |
| 27 | + - [Parameters](#parameters) |
| 28 | + - [Features](#features) |
| 29 | + - [Examples](#examples) |
| 30 | + - [Example Picture](#example-picture) |
| 31 | + - [Code](#code) |
| 32 | + - [Output](#output) |
| 33 | + - [Contributing](#contributing) |
| 34 | + - [License](#license) |
| 35 | + - [Contact](#contact) |
| 36 | + |
| 37 | +## Installation |
| 38 | + |
| 39 | +To install PyTorchLayerViz, you can use pip: |
| 40 | + |
| 41 | +```bash |
| 42 | +pip install pytorchlayerviz |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +Here is a basic example of how to use PyTorchLayerViz: |
| 48 | + |
| 49 | +```python |
| 50 | +from pytorchlayerviz import get_feature_maps |
| 51 | +import matplotlib.pyplot as plt |
| 52 | +from PIL import Image |
| 53 | +import torch |
| 54 | +from torch import nn |
| 55 | +from torchvision import datasets, transforms, models |
| 56 | +from torchvision.transforms import ToTensor |
| 57 | + |
| 58 | +# Define your model |
| 59 | +model = torch.nn.Sequential( |
| 60 | + torch.nn.Conv2d(1, 20, 5), |
| 61 | + torch.nn.ReLU(), |
| 62 | + torch.nn.Conv2d(20, 64, 5), |
| 63 | + torch.nn.ReLU() |
| 64 | +) |
| 65 | + |
| 66 | +layers_to_check = [nn.Conv2d] # Define all Layers you want to pass your picture |
| 67 | + |
| 68 | +input_image_path = 'pictures/hamburger.jpg' # Path to your example picture |
| 69 | + |
| 70 | +get_feature_maps(model = model, layers_to_check = layers_to_check, input_image_path = input_image_path) # Call function from pytorchlayerviz |
| 71 | +``` |
| 72 | + |
| 73 | +### Parameters |
| 74 | + |
| 75 | +- **model (nn.Module)** – The PyTorch model whose layers' feature maps you want to visualize. *Required*. |
| 76 | +- **layers_to_check (arr of nn.Module)** – List of layer types (e.g., `nn.Conv2d`) to check for feature maps. *Required*. |
| 77 | +- **input_image_path (str)** – Path to the input image file. *Required*. |
| 78 | +- **transform (transforms.Compose, optional)** – A function/transform that takes in an image and returns a transformed version. Default is None. *Optional*. |
| 79 | +- **sequential_order (bool, optional)** – If True, the layers are visualized in the order they are defined in the model. If false it will first go through the first layer defined in the arrDefault is True. *Optional*. |
| 80 | + |
| 81 | +If transform is none, this will be used: |
| 82 | + |
| 83 | +```python |
| 84 | + transform = transforms.Compose([ |
| 85 | + transforms.Resize((224, 224)), # Resize the image to 224x224 pixels |
| 86 | + transforms.ToTensor(), # Convert the image to a PyTorch tensor |
| 87 | + ]) |
| 88 | +``` |
| 89 | + |
| 90 | +If you want to pass your own transform, make sure you resize the image and convert it to a tensor with `transforms.ToTensor()` |
| 91 | + |
| 92 | +## Features |
| 93 | + |
| 94 | +* Visualize Weights: Easily visualize the weights of each layer in your PyTorch model. |
| 95 | +* Visualize Feature Maps: Generate and visualize feature maps for given inputs. |
| 96 | +* Customizable: Flexible options for customizing visualizations. |
| 97 | + |
| 98 | + |
| 99 | +## Examples |
| 100 | + |
| 101 | +### Example Picture |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +### Code |
| 106 | + |
| 107 | +```python |
| 108 | +pretrained_model = models.vgg16(pretrained=True) |
| 109 | +input_image_path = 'hamburger.jpg' |
| 110 | +layers_to_check= [nn.MaxPool2d] |
| 111 | + |
| 112 | +get_feature_maps(model = pretrained_model, layers_to_check = layers_to_check, input_image_path = input_image_path, sequential_order = False) |
| 113 | +``` |
| 114 | + |
| 115 | +### Output |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +## Contributing |
| 121 | + |
| 122 | +I welcome contributions to PyTorchLayerViz! If you'd like to contribute, please follow these steps: |
| 123 | + |
| 124 | +1. Fork the repository. |
| 125 | +2. Create a new branch (*git checkout -b feature-branch*). |
| 126 | +3. Make your changes. |
| 127 | +4. Commit your changes (*git commit -m 'Add new feature'*). |
| 128 | +5. Push to the branch (*git push origin feature-branch*). |
| 129 | +6. Open a pull request. |
| 130 | + |
| 131 | +## License |
| 132 | + |
| 133 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details. |
| 134 | + |
| 135 | +## Contact |
| 136 | + |
| 137 | +For any questions, suggestions, or issues, please open an issue on GitHub or contact me. |
| 138 | + |
| 139 | +* Simone Panico: simone.panico@icloud.com |
| 140 | +* Github Issues: https://github.com/simone-panico/PyTorchLayerViz/issues |
| 141 | + |
0 commit comments