trivial-torch-tools


Nametrivial-torch-tools JSON
Version 0.6.4 PyPI version JSON
download
home_pagehttps://github.com/jeff-hykin/trivial-torch-tools.git
SummaryDecorators for reducing pytorch boilerplate
upload_time2023-07-18 14:48:05
maintainer
docs_urlNone
authorJeff Hykin
requires_python>=3.6
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # What is this?

Functions and decorators I found myself rewriting for every pytorch project

# How do I use this?

`pip install trivial-torch-tools`

```python
from trivial_torch_tools import Sequential, init
import torch.nn as nn

class Model(nn.Module):
    @init.to_device()
    # ^ does self.to() and defaults to GPU if available (uses default_device variable)
    @init.save_and_load_methods(model_attributes=["layers"], basic_attributes=["input_shape"])
    # ^ creates self.save(path=self.path) and self.load(path=self.path)
    def __init__(self):
        self.input_shape = (81,81,3)
        layers = Sequential(input_shape=self.input_shape)
        # ^ dynamically compute the output shape/size of layers (the nn.Linear below)
        layers.add_module('conv1'   , nn.Conv2d(input_shape[0], 32, kernel_size=8, stride=4, padding=0))
        layers.add_module('relu1'   , nn.ReLU())
        layers.add_module('flatten' , nn.Flatten(start_dim=1, end_dim=-1))
        layers.add_module('linear1' , nn.Linear(in_features=layers.output_size, out_features=10)) 
        layers.add_module('sigmoid1', nn.Sigmoid())
        self.layers = layers
        
        # layers.output_size
        # layers.output_shape
        # layers.layer_shapes
   
# available tools
from trivial_torch_tools import *

core.default_device # defaults to cuda if available
core.to_tensor(nested_lists_of_arrays_tuples_and_more) # aggresively converts objects to tensors

# decorators for def __init__()
@model.init.to_device(device=default_device)
@model.init.save_and_load_methods(basic_attributes=[], model_attributes=[], path_attribute="path")
@model.init.forward_sequential_method
# decorators for def forward(): # or whatever 
@model.convert_each_arg.to_tensor() # Use to_tensor(which_args=[0]) to only convert first arg
@model.convert_each_arg.to_device() # Use to_device(which_args=[0]) to only convert first arg
@model.convert_each_arg.to_batched_tensor(number_of_dimensions=4) # 4 works for color images
@model.convert_each_arg.torch_tensor_from_opencv_format()

image.tensor_from_path(path)
image.pil_image_from_tensor(tensor)
image.torch_tensor_from_opencv_format(tensor_or_array)
image.opencv_tensor_from_torch_format(tensor)
image.opencv_array_from_pil_image(image_obj)

OneHotifier.tensor_from_argmax(tensor)             # [0.1,99,0,0,] => [0,1,0,0,]
OneHotifier.index_from_one_hot(tensor)             # [0,1,0,0,] => 2
OneHotifier.index_tensor_from_onehot_batch(tensor) # [[0,1,0,0,]] => [2]

import torch
converter = OneHotifier(possible_values=[ "thing0", ('thing', 1), {"thing":2} ])
converter.to_one_hot({"thing":2}) # >>> tensor([0,0,1])
converter.from_one_hot(torch.tensor([0,0,1])) # >>> {"thing":2}
```



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jeff-hykin/trivial-torch-tools.git",
    "name": "trivial-torch-tools",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "Jeff Hykin",
    "author_email": "jeff.hykin@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/c4/f3/7912dac88d8696b41635e5da495494801488db0e8877c1340265bf9f9658/trivial_torch_tools-0.6.4.tar.gz",
    "platform": null,
    "description": "# What is this?\n\nFunctions and decorators I found myself rewriting for every pytorch project\n\n# How do I use this?\n\n`pip install trivial-torch-tools`\n\n```python\nfrom trivial_torch_tools import Sequential, init\nimport torch.nn as nn\n\nclass Model(nn.Module):\n    @init.to_device()\n    # ^ does self.to() and defaults to GPU if available (uses default_device variable)\n    @init.save_and_load_methods(model_attributes=[\"layers\"], basic_attributes=[\"input_shape\"])\n    # ^ creates self.save(path=self.path) and self.load(path=self.path)\n    def __init__(self):\n        self.input_shape = (81,81,3)\n        layers = Sequential(input_shape=self.input_shape)\n        # ^ dynamically compute the output shape/size of layers (the nn.Linear below)\n        layers.add_module('conv1'   , nn.Conv2d(input_shape[0], 32, kernel_size=8, stride=4, padding=0))\n        layers.add_module('relu1'   , nn.ReLU())\n        layers.add_module('flatten' , nn.Flatten(start_dim=1, end_dim=-1))\n        layers.add_module('linear1' , nn.Linear(in_features=layers.output_size, out_features=10)) \n        layers.add_module('sigmoid1', nn.Sigmoid())\n        self.layers = layers\n        \n        # layers.output_size\n        # layers.output_shape\n        # layers.layer_shapes\n   \n# available tools\nfrom trivial_torch_tools import *\n\ncore.default_device # defaults to cuda if available\ncore.to_tensor(nested_lists_of_arrays_tuples_and_more) # aggresively converts objects to tensors\n\n# decorators for def __init__()\n@model.init.to_device(device=default_device)\n@model.init.save_and_load_methods(basic_attributes=[], model_attributes=[], path_attribute=\"path\")\n@model.init.forward_sequential_method\n# decorators for def forward(): # or whatever \n@model.convert_each_arg.to_tensor() # Use to_tensor(which_args=[0]) to only convert first arg\n@model.convert_each_arg.to_device() # Use to_device(which_args=[0]) to only convert first arg\n@model.convert_each_arg.to_batched_tensor(number_of_dimensions=4) # 4 works for color images\n@model.convert_each_arg.torch_tensor_from_opencv_format()\n\nimage.tensor_from_path(path)\nimage.pil_image_from_tensor(tensor)\nimage.torch_tensor_from_opencv_format(tensor_or_array)\nimage.opencv_tensor_from_torch_format(tensor)\nimage.opencv_array_from_pil_image(image_obj)\n\nOneHotifier.tensor_from_argmax(tensor)             # [0.1,99,0,0,] => [0,1,0,0,]\nOneHotifier.index_from_one_hot(tensor)             # [0,1,0,0,] => 2\nOneHotifier.index_tensor_from_onehot_batch(tensor) # [[0,1,0,0,]] => [2]\n\nimport torch\nconverter = OneHotifier(possible_values=[ \"thing0\", ('thing', 1), {\"thing\":2} ])\nconverter.to_one_hot({\"thing\":2}) # >>> tensor([0,0,1])\nconverter.from_one_hot(torch.tensor([0,0,1])) # >>> {\"thing\":2}\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Decorators for reducing pytorch boilerplate",
    "version": "0.6.4",
    "project_urls": {
        "Homepage": "https://github.com/jeff-hykin/trivial-torch-tools.git"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a6b910ad214463a26ec95e9af3728963124ac8a9f05aabd8182eac11bb38afa3",
                "md5": "c2c834f5205d3a3744984296c8436212",
                "sha256": "a9186fa7c42f719ee6e6ad1bbcdd795c31ba83db74a51e987db9739b80280604"
            },
            "downloads": -1,
            "filename": "trivial_torch_tools-0.6.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c2c834f5205d3a3744984296c8436212",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 2249751,
            "upload_time": "2023-07-18T14:48:00",
            "upload_time_iso_8601": "2023-07-18T14:48:00.380310Z",
            "url": "https://files.pythonhosted.org/packages/a6/b9/10ad214463a26ec95e9af3728963124ac8a9f05aabd8182eac11bb38afa3/trivial_torch_tools-0.6.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c4f37912dac88d8696b41635e5da495494801488db0e8877c1340265bf9f9658",
                "md5": "7f7a266049d622b064dad8d973bb2334",
                "sha256": "29f31099eed4ea041f47dfd40568eef2cfa099a0e4e634cb142a81ff9944b635"
            },
            "downloads": -1,
            "filename": "trivial_torch_tools-0.6.4.tar.gz",
            "has_sig": false,
            "md5_digest": "7f7a266049d622b064dad8d973bb2334",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 2113237,
            "upload_time": "2023-07-18T14:48:05",
            "upload_time_iso_8601": "2023-07-18T14:48:05.570546Z",
            "url": "https://files.pythonhosted.org/packages/c4/f3/7912dac88d8696b41635e5da495494801488db0e8877c1340265bf9f9658/trivial_torch_tools-0.6.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-18 14:48:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jeff-hykin",
    "github_project": "trivial-torch-tools",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "trivial-torch-tools"
}
        
Elapsed time: 0.19425s