cjm-pytorch-utils


Namecjm-pytorch-utils JSON
Version 0.0.6 PyPI version JSON
download
home_pagehttps://github.com/cj-mills/cjm-pytorch-utils
SummarySome utility functions for working with PyTorch.
upload_time2023-09-20 18:51:34
maintainer
docs_urlNone
authorcj-mills
requires_python>=3.9
licenseApache Software License 2.0
keywords nbdev jupyter notebook python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .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": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "nbdev jupyter notebook python",
    "author": "cj-mills",
    "author_email": "millscj.mills2@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/64/43/926adc765aca3667c163c10ddb7d409e18b1c3249d48bf2c8924ce168af3/cjm-pytorch-utils-0.0.6.tar.gz",
    "platform": null,
    "description": "# cjm-pytorch-utils\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&#10;    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n&#10;    .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.6",
    "project_urls": {
        "Homepage": "https://github.com/cj-mills/cjm-pytorch-utils"
    },
    "split_keywords": [
        "nbdev",
        "jupyter",
        "notebook",
        "python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "703d6f3752a4121fdfd9bde6e9748e8417e7ab74d2d4bdf4e2f4b30c85aad9b0",
                "md5": "3c69f6e4ec0027a60a9f836bad54503d",
                "sha256": "1df28642e970b45613eddd387e1971b6871c21d9f1dcca38f59834bbe0fcbb61"
            },
            "downloads": -1,
            "filename": "cjm_pytorch_utils-0.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3c69f6e4ec0027a60a9f836bad54503d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 7345,
            "upload_time": "2023-09-20T18:51:32",
            "upload_time_iso_8601": "2023-09-20T18:51:32.519294Z",
            "url": "https://files.pythonhosted.org/packages/70/3d/6f3752a4121fdfd9bde6e9748e8417e7ab74d2d4bdf4e2f4b30c85aad9b0/cjm_pytorch_utils-0.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6443926adc765aca3667c163c10ddb7d409e18b1c3249d48bf2c8924ce168af3",
                "md5": "cbab1162872978c6b45deeb1ea7f1223",
                "sha256": "8e3de441f8497407cb277297b77e4f308e335cd832b26b60a36dfab65670dbf0"
            },
            "downloads": -1,
            "filename": "cjm-pytorch-utils-0.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "cbab1162872978c6b45deeb1ea7f1223",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 7037,
            "upload_time": "2023-09-20T18:51:34",
            "upload_time_iso_8601": "2023-09-20T18:51:34.125153Z",
            "url": "https://files.pythonhosted.org/packages/64/43/926adc765aca3667c163c10ddb7d409e18b1c3249d48bf2c8924ce168af3/cjm-pytorch-utils-0.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-20 18:51:34",
    "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"
}
        
Elapsed time: 0.11871s