Skip to main content

nnutils-pytorch

Build Status

PyTorch bindings of different neural network-related utilities implemented for CPUs and GPUs (CUDA).

So far, most of the utils are related to my need of working with images of different sizes grouped into batches with padding.

Included functions

Adaptive pooling

Adaptive pooling layers included in several packages like Torch or PyTorch assume that all images in the batch have the same size. This implementation takes into account the size of each individual image within the batch (before padding) to apply the adaptive pooling.

Currently implemented: Average and maximum adaptive pooling.

import torch
from nnutils_pytorch import adaptive_avgpool_2d, adaptive_maxpool_2d

# Two random images, with three channels, 10 pixels height, 12 pixels width
x = torch.rand(2, 3, 10, 12)
# Matrix (N x 2) containing the height and width of each image.
xs = torch.tensor([[10, 6], [6, 12], dtype=torch.int64)

# Pool images to a fixed size, taking into account the original size of each
# image before padding.
#
# Output tensor has shape (2, 3, 3, 5)
y1 = adaptive_avgpool_2d(batch_input=x, output_sizes=(3, 5), batch_sizes=xs)

# Pool a single dimension of the images, taking into account the original
# size of each image before padding. The None dimension is not pooled.
#
# Output tensor has shape (2, 3, 5, 12)
y2 = adaptive_maxpool_2d(x, (5, None), xs)

Important: The implementation assumes that the images are aligned to the top-left corner.

Masking images by size

If you are grouping images of different sizes into batches padded with zeros, you may need to mask the output/input tensors after/before some layers. This layer is very handy (and efficient) in these cases.

import torch
from nnutils_pytorch import mask_image_from_size

# Two random images, with three channels, 10 pixels height, 12 pixels width
x = torch.rand(2, 3, 10, 12)
# Matrix (N x 2) containing the height and width of each image.
xs = torch.tensor([[10, 6], [6, 12], dtype=torch.int64)

# Note: mask_image_from_size is differentiable w.r.t. x
y = mask_image_from_size(x, xs, mask_value=0)  # mask_value is optional.

Important: The implementation assumes that the images are aligned to the top-left corner.

Requirements

  • Python: 3.6, 3.7 or 3.8 (tested with version 3.6, 3.7 and 3.8).
  • PyTorch >= 1.6.0 (tested with version 1.6.0).
  • C++14 compiler (tested with GCC 7.5.0).
  • For GPU support: CUDA Toolkit.

Installation

The installation process should be pretty straightforward assuming that you have correctly installed the required libraries and tools.

The setup process compiles the package from source, and will compile with CUDA support if this is available for PyTorch.

pip install nnutils-pytorch

You may find the package already compiled for different Python, CUDA and CPU configurations in: http://www.jpuigcerver.net/projects/nnutils-pytorch/whl/

For instance, if you want to install the CPU-only version for Python 3.7:

pip install http://www.jpuigcerver.net/projects/nnutils-pytorch/whl/cpu/nnutils_pytorch-1.6.0-cp37-cp37m-linux_x86_64.whl

From GitHub

git clone https://github.com/jpuigcerver/nnutils.git
cd nnutils/pytorch
python setup.py build
python setup.py install

Some compiling problems may arise when using CUDA and newer host compilers with AVX512 instructions. Please, install GCC 7.5 or above and use it as the host compiler for NVCC 10.2. You can simply set the CC and CXX environment variables before the build/install commands:

CC=gcc-4.9 CXX=g++-4.9 pip install nnutils-pytorch

or (if you are using the GitHub source code):

CC=gcc-4.9 CXX=g++-4.9 python setup.py build

Testing

You can test the library once installed using unittest. In particular, run the following commands:

python -m unittest nnutils_pytorch.adaptive_avgpool_2d_test
python -m unittest nnutils_pytorch.adaptive_maxgpool_2d_test
python -m unittest nnutils_pytorch.mask_image_from_size_test

All tests should pass (CUDA tests are only executed if supported).

Metadata

Release files for nnutils-pytorch-cuda 1.13.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for nnutils-pytorch-cuda 1.13.1
File
nnutils_pytorch_cuda-1.13.1-cp311-cp311-manylinux1_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.5+ x86-64 Details
nnutils_pytorch_cuda-1.13.1-cp310-cp310-manylinux1_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.5+ x86-64 Details
nnutils_pytorch_cuda-1.13.1-cp39-cp39-manylinux1_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.5+ x86-64 Details
nnutils_pytorch_cuda-1.13.1-cp38-cp38-manylinux1_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.5+ x86-64 Details

Total release size: 26.3 MB

Release files / nnutils_pytorch_cuda-1.13.1-cp311-cp311-manylinux1_x86_64.whl

Download URL nnutils_pytorch_cuda-1.13.1-cp311-cp311-manylinux1_x86_64.whl
Size 6.6 MB
Tags CPython 3.11 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
e24126e33bae7453242b7630e4096fd47087d8e6ee572667c7a0d44c3a79e75b
BLAKE2b-256 checksum
How to use checksums
64125b8fd2895ba7c58586e7e9578de0bc10d681b6514e1374f0dd3f2b703c55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release files / nnutils_pytorch_cuda-1.13.1-cp310-cp310-manylinux1_x86_64.whl

Download URL nnutils_pytorch_cuda-1.13.1-cp310-cp310-manylinux1_x86_64.whl
Size 6.6 MB
Tags CPython 3.10 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
fbd7c023a9cba34ab3366753c90dc3e0cd6dead3081eb974e8458d6365f05fc9
BLAKE2b-256 checksum
How to use checksums
63114486d3cb93d21b9bb1c1803669e8d89acd0b095ca387e165fc58f2c739a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release files / nnutils_pytorch_cuda-1.13.1-cp39-cp39-manylinux1_x86_64.whl

Download URL nnutils_pytorch_cuda-1.13.1-cp39-cp39-manylinux1_x86_64.whl
Size 6.6 MB
Tags CPython 3.9 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
1c03acaa72bc603b0bb122d22a39b4271e8f1e2292f84a838cb5de1f4215d5cb
BLAKE2b-256 checksum
How to use checksums
ed517a12b775f5181541bd43886521d8343b4aeac564b82b1643bd4776b3e9ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release files / nnutils_pytorch_cuda-1.13.1-cp38-cp38-manylinux1_x86_64.whl

Download URL nnutils_pytorch_cuda-1.13.1-cp38-cp38-manylinux1_x86_64.whl
Size 6.6 MB
Tags CPython 3.8 Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
13b1e4bfc2acd42c5befaf0a1a3b3b3ddb6431e3d4d6f73366a0dfc26edecf22
BLAKE2b-256 checksum
How to use checksums
78e31d5c6bd6f0f7d45fa3f0c4e952dfe461f6023a1f0b28c2abc4ef39b67070
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Security for nnutils-pytorch-cuda 1.13.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page