Skip to content

Commit b1ef50d

Browse files
committed
v1.1
1 parent 1a0172b commit b1ef50d

7 files changed

Lines changed: 150 additions & 17 deletions

File tree

PyTorchLayerViz.egg-info/PKG-INFO

Lines changed: 129 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,141 @@
11
Metadata-Version: 2.1
22
Name: PyTorchLayerViz
3-
Version: 1.0
3+
Version: 1.1
44
Summary: PyTorchLayerViz is a Python library that allows you to visualize the weights and feature maps of a PyTorch model.
55
Author: Simone Panico
66
Author-email: simone.panico@icloud.com
77
Keywords: python,pytorch,deep learning,model
88
Description-Content-Type: text/markdown
9+
License-File: LICENSE.md
910
Requires-Dist: torch
1011
Requires-Dist: torchvision
1112
Requires-Dist: pillow
1213
Requires-Dist: matplotlib
1314

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+
![PyTorchLayerViz - Version](https://img.shields.io/pypi/v/PyTorchLayerViz.svg)
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+
![Example Picture](pictures/hamburger.jpg)
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+
![Hamburger result Picture](pictures/hamburger_results.png)
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+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
LICENSE.md
2+
README.md
13
setup.py
24
PyTorchLayerViz/__init__.py
35
PyTorchLayerViz/main.py
46
PyTorchLayerViz.egg-info/PKG-INFO
57
PyTorchLayerViz.egg-info/SOURCES.txt
8+
PyTorchLayerViz.egg-info/commands.txt
69
PyTorchLayerViz.egg-info/dependency_links.txt
710
PyTorchLayerViz.egg-info/requires.txt
811
PyTorchLayerViz.egg-info/top_level.txt
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
python setup.py sdist bdist_wheel
2+
pip install dist/
3+
twine upload dist/*
4+

build/lib/PyTorchLayerViz/main.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
import torch
2+
import torch.nn as nn
3+
import torchvision.transforms as transforms
4+
from PIL import Image
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
18
##########################################################
29
def extract_layers_and_weights(model, layers_to_check, sequential_order=True):
310
# Initialize dictionaries and lists to store layers and their weights
@@ -46,9 +53,9 @@ def is_layer_to_check(layer):
4653
add_layer_info(layer_type, module)
4754

4855
# Print the counts of each layer type
49-
if not sequential_order:
50-
for layer_type, count in layer_counters.items():
51-
print(f"Total {layer_type} layers: {count}")
56+
# if not sequential_order:
57+
# for layer_type, count in layer_counters.items():
58+
# print(f"Total {layer_type} layers: {count}")
5259

5360
if sequential_order:
5461
return ordered_layers, ordered_layer_names
@@ -133,6 +140,4 @@ def get_feature_maps(model, layers_to_check, input_image_path, transform = None,
133140
feature_maps, layer_names = extract_feature_maps([layer for layer_list in layers.values() for layer in layer_list], input_image)
134141

135142
processed_feature_maps = process_feature_maps(feature_maps)
136-
print("Done 3")
137143
plot_feature_maps(processed_feature_maps, layer_names)
138-
print("Done 4")
5.75 KB
Binary file not shown.

dist/pytorchlayerviz-1.1.tar.gz

5.22 KB
Binary file not shown.

setup.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = '1.0'
3+
with open("README.md", "r") as f:
4+
LONG_DESCRIPTION = f.read()
5+
6+
VERSION = '1.1'
47
DESCRIPTION = "PyTorchLayerViz is a Python library that allows you to visualize the weights and feature maps of a PyTorch model."
5-
LONG_DESCRIPTION = """\
6-
PyTorchLayerViz is a Python library designed to assist developers and researchers in visualizing
7-
the weights and feature maps of PyTorch models. This tool provides easy-to-use functions to help
8-
understand and interpret deep learning models, making it an essential utility for anyone working
9-
with PyTorch.
10-
"""
118

129
setup(
1310
name="PyTorchLayerViz",

0 commit comments

Comments
 (0)