Skip to main content

PyTorch bindings of the nnutils library

Project description

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: 2.7, 3.5, 3.6 or 3.7 (tested with version 2.7, 3.5, 3.6 and 3.7).
  • PyTorch >= 1.1.0 (tested with version 1.1.0).
  • C++11 compiler (tested with GCC 4.8.2, 5.5.0, 6.4.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.

From Pypi (recommended)

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-0.3.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

AVX512 related issues

Some compiling problems may arise when using CUDA and newer host compilers with AVX512 instructions. Please, install GCC 4.9 and use it as the host compiler for NVCC. 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).

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nnutils_pytorch-0.4.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distributions

File details

Details for the file nnutils_pytorch-0.4.0.tar.gz.

File metadata

  • Download URL: nnutils_pytorch-0.4.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for nnutils_pytorch-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c50e17517ac076c81e7b7046b46c6f62ffd6c29b123bfb1d889812726d099d83
MD5 eeb6279a4049d77fb013d6de593e1ff7
BLAKE2b-256 aec4b10c585188599bfb3d598705a9803573c129a95d3b95adedb53aa4862934

See more details on using hashes here.

File details

Details for the file nnutils_pytorch-0.4.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: nnutils_pytorch-0.4.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for nnutils_pytorch-0.4.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fc008020f5964e44d017d39f1ea467776bc99ee985985cc2ab9063389e9a7776
MD5 9dc1e410be7275f2d1ed8a87f64d5b14
BLAKE2b-256 b6f8887e40297871c7b1ab56a6facbf33dffd99fffda7a2ed12392f018f45a02

See more details on using hashes here.

File details

Details for the file nnutils_pytorch-0.4.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: nnutils_pytorch-0.4.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for nnutils_pytorch-0.4.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f4fc145f8640f46f7cad3ef935c933bdfe3adfd258b272166479e3fd4b2d951d
MD5 4986a1d523c142d133f6b389af35ba32
BLAKE2b-256 8dc862675775952fd089c193cfe5991561c14425b5dbfa3299e704fc50574994

See more details on using hashes here.

File details

Details for the file nnutils_pytorch-0.4.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: nnutils_pytorch-0.4.0-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for nnutils_pytorch-0.4.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5d89ff0c293d4095971d8c46cd097b74d2a43d518175ecb49943975da0442a0d
MD5 749f7573fa4e8ac5686124e9f3219078
BLAKE2b-256 7c1a6ec4641b580012a3f78122785e635e00b32a322e271435872297242d74b3

See more details on using hashes here.

File details

Details for the file nnutils_pytorch-0.4.0-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

  • Download URL: nnutils_pytorch-0.4.0-cp27-cp27mu-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 2.7mu
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for nnutils_pytorch-0.4.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ab71a48ea8f97cd6025c0cfff2822b88e0024a8c3741540f5c4ba2b721331a45
MD5 88da15e85b265798fe1621a52e60f697
BLAKE2b-256 6951f9895d35cae8f22ca293af2d00a2beddc08df72f6eafe780e7016562079f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page