# cjm-pytorch-utils
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
## Install
``` sh
pip install cjm_pytorch_utils
```
## How to use
### set_seed
``` python
from cjm_pytorch_utils.core import set_seed
```
``` python
seed = 1234
set_seed(seed)
```
### pil_to_tensor
``` python
from cjm_pytorch_utils.core import pil_to_tensor
from PIL import Image
from torchvision import transforms
```
``` python
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
``` python
from cjm_pytorch_utils.core import tensor_to_pil
```
``` python
tensor_img = tensor_to_pil(transforms.ToTensor()(src_img))
tensor_img
```
![](index_files/figure-commonmark/cell-8-output-1.png)
### iterate_modules
``` python
from cjm_pytorch_utils.core import iterate_modules
import torch
from torchvision import models
```
``` python
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
``` python
from cjm_pytorch_utils.core import tensor_stats_df
```
``` python
tensor_stats_df(torch.randn(1, 3, 256, 256))
```
<div>
<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.003342 |
| std | 0.99868 |
| min | -4.558271 |
| max | 4.815985 |
| shape | (1, 3, 256, 256) |
</div>
### get_torch_device
``` python
from cjm_pytorch_utils.core import get_torch_device
```
``` python
get_torch_device()
```
'cuda'
### denorm_img_tensor
``` python
from cjm_pytorch_utils.core import denorm_img_tensor
```
``` python
tensor_to_pil(img_tensor)
```
![](index_files/figure-commonmark/cell-20-output-1.png)
``` python
tensor_to_pil(denorm_img_tensor(img_tensor, [0.5], [0.5]))
```
![](index_files/figure-commonmark/cell-21-output-1.png)
Raw data
{
"_id": null,
"home_page": "https://github.com/cj-mills/cjm-pytorch-utils",
"name": "cjm-pytorch-utils",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "nbdev jupyter notebook python",
"author": "cj-mills",
"author_email": "millscj.mills2@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/b6/73/51804cccd2797d45aa0b131189456ac100af76b211b08350d72b60f62100/cjm_pytorch_utils-0.0.8.tar.gz",
"platform": null,
"description": "# cjm-pytorch-utils\n\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n## Install\n\n``` sh\npip install cjm_pytorch_utils\n```\n\n## How to use\n\n### set_seed\n\n``` python\nfrom cjm_pytorch_utils.core import set_seed\n```\n\n``` python\nseed = 1234\nset_seed(seed)\n```\n\n### pil_to_tensor\n\n``` python\nfrom cjm_pytorch_utils.core import pil_to_tensor\nfrom PIL import Image\nfrom torchvision import transforms\n```\n\n``` python\nimg_path = img_path = '../images/cat.jpg'\nsrc_img = Image.open(img_path).convert('RGB')\nprint(f\"Source Image Size: {src_img.size}\")\n\nimg_tensor = pil_to_tensor(src_img, [0.5], [0.5])\nimg_tensor.shape, img_tensor.min(), img_tensor.max()\n```\n\n Source Image Size: (768, 512)\n\n (torch.Size([1, 3, 512, 768]), tensor(-1.), tensor(1.))\n\n### tensor_to_pil\n\n``` python\nfrom cjm_pytorch_utils.core import tensor_to_pil\n```\n\n``` python\ntensor_img = tensor_to_pil(transforms.ToTensor()(src_img))\ntensor_img\n```\n\n![](index_files/figure-commonmark/cell-8-output-1.png)\n\n### iterate_modules\n\n``` python\nfrom cjm_pytorch_utils.core import iterate_modules\nimport torch\nfrom torchvision import models\n```\n\n``` python\nvgg = models.vgg16(weights=models.VGG16_Weights.IMAGENET1K_V1).features\n\nfor index, module in enumerate(iterate_modules(vgg)):\n if type(module) == torch.nn.modules.activation.ReLU:\n print(f\"{index}: {module}\")\n```\n\n 1: ReLU(inplace=True)\n 3: ReLU(inplace=True)\n 6: ReLU(inplace=True)\n 8: ReLU(inplace=True)\n 11: ReLU(inplace=True)\n 13: ReLU(inplace=True)\n 15: ReLU(inplace=True)\n 18: ReLU(inplace=True)\n 20: ReLU(inplace=True)\n 22: ReLU(inplace=True)\n 25: ReLU(inplace=True)\n 27: ReLU(inplace=True)\n 29: ReLU(inplace=True)\n\n### tensor_stats_df\n\n``` python\nfrom cjm_pytorch_utils.core import tensor_stats_df\n```\n\n``` python\ntensor_stats_df(torch.randn(1, 3, 256, 256))\n```\n\n<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n .dataframe tbody tr th {\n vertical-align: top;\n }\n .dataframe thead th {\n text-align: right;\n }\n</style>\n\n| | 0 |\n|-------|------------------|\n| mean | 0.003342 |\n| std | 0.99868 |\n| min | -4.558271 |\n| max | 4.815985 |\n| shape | (1, 3, 256, 256) |\n\n</div>\n\n### get_torch_device\n\n``` python\nfrom cjm_pytorch_utils.core import get_torch_device\n```\n\n``` python\nget_torch_device()\n```\n\n 'cuda'\n\n### denorm_img_tensor\n\n``` python\nfrom cjm_pytorch_utils.core import denorm_img_tensor\n```\n\n``` python\ntensor_to_pil(img_tensor)\n```\n\n![](index_files/figure-commonmark/cell-20-output-1.png)\n\n``` python\ntensor_to_pil(denorm_img_tensor(img_tensor, [0.5], [0.5]))\n```\n\n![](index_files/figure-commonmark/cell-21-output-1.png)\n",
"bugtrack_url": null,
"license": "Apache Software License 2.0",
"summary": "Some utility functions for working with PyTorch.",
"version": "0.0.8",
"project_urls": {
"Homepage": "https://github.com/cj-mills/cjm-pytorch-utils"
},
"split_keywords": [
"nbdev",
"jupyter",
"notebook",
"python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6a1fbfe678df47d83fa600287da3fe19de8c7e4817f3ff9bbb493301fae843c2",
"md5": "a724d8e97b89624305ef35a1fc4a6801",
"sha256": "406608c10ac5a022f16f5d2ba8dd843fdfe338809fe975987538b83bf8f0c172"
},
"downloads": -1,
"filename": "cjm_pytorch_utils-0.0.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a724d8e97b89624305ef35a1fc4a6801",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 8316,
"upload_time": "2024-09-19T05:40:27",
"upload_time_iso_8601": "2024-09-19T05:40:27.323302Z",
"url": "https://files.pythonhosted.org/packages/6a/1f/bfe678df47d83fa600287da3fe19de8c7e4817f3ff9bbb493301fae843c2/cjm_pytorch_utils-0.0.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b67351804cccd2797d45aa0b131189456ac100af76b211b08350d72b60f62100",
"md5": "4b0a3218c4a2429e9f90ee414a528868",
"sha256": "a7ddc70630509190b7d4e1c2b151533e89a10f7ca84255639dd3a5d2e4dc8f11"
},
"downloads": -1,
"filename": "cjm_pytorch_utils-0.0.8.tar.gz",
"has_sig": false,
"md5_digest": "4b0a3218c4a2429e9f90ee414a528868",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 8333,
"upload_time": "2024-09-19T05:40:28",
"upload_time_iso_8601": "2024-09-19T05:40:28.429964Z",
"url": "https://files.pythonhosted.org/packages/b6/73/51804cccd2797d45aa0b131189456ac100af76b211b08350d72b60f62100/cjm_pytorch_utils-0.0.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-19 05:40:28",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cj-mills",
"github_project": "cjm-pytorch-utils",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "cjm-pytorch-utils"
}