Some utility functions for working with PyTorch.
Project description
cjm-pytorch-utils
Install
pip install cjm_pytorch_utils
How to use
pil_to_tensor
from cjm_pytorch_utils.core import pil_to_tensor
from PIL import Image
from torchvision import transforms
img_path = img_path = '../images/cat.jpg'
src_img = Image.open(img_path).convert('RGB')
print(f"Source Image Size: {src_img.size}")
img_tensor = pil_to_tensor(src_img, [0.5], [0.5])
img_tensor.shape, img_tensor.min(), img_tensor.max()
Source Image Size: (768, 512)
(torch.Size([1, 3, 512, 768]), tensor(-1.), tensor(1.))
tensor_to_pil
from cjm_pytorch_utils.core import tensor_to_pil
tensor_img = tensor_to_pil(transforms.ToTensor()(src_img))
tensor_img
iterate_modules
from cjm_pytorch_utils.core import iterate_modules
import torch
from torchvision import models
vgg = models.vgg16(weights=models.VGG16_Weights.IMAGENET1K_V1).features
for index, module in enumerate(iterate_modules(vgg)):
if type(module) == torch.nn.modules.activation.ReLU:
print(f"{index}: {module}")
1: ReLU(inplace=True)
3: ReLU(inplace=True)
6: ReLU(inplace=True)
8: ReLU(inplace=True)
11: ReLU(inplace=True)
13: ReLU(inplace=True)
15: ReLU(inplace=True)
18: ReLU(inplace=True)
20: ReLU(inplace=True)
22: ReLU(inplace=True)
25: ReLU(inplace=True)
27: ReLU(inplace=True)
29: ReLU(inplace=True)
tensor_stats_df
from cjm_pytorch_utils.core import tensor_stats_df
tensor_stats_df(torch.randn(1, 3, 256, 256))
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
0 | |
---|---|
mean | 0.000952 |
std | 0.998587 |
min | -4.616786 |
max | 5.122179 |
shape | (1, 3, 256, 256) |
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for cjm_pytorch_utils-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f692e877eb9fef5cf13c1497b5358d29f60486ee1e75c5ebf040c5bf9652c1b |
|
MD5 | 076401d5fb016740bf765c45d462ed73 |
|
BLAKE2b-256 | 67380a1c8326688887fd0bdfdd023e19d177fd704d3826d3afa2a2156d0cc5dd |