Skip to main content

Torchvision+ Deformable Convolutional Networks

Project description

Torchvision+ Deformable Convolution Networks

GitHub Workflow Status PyPI GitHub

This package contains the PyTorch implementations of the 2D Deformable Convolution operation (the commonly used torchvision.ops.deform_conv2d) proposed in https://arxiv.org/abs/1811.11168, as well as its 1D and 3D equivalences, which are not available in torchvision (thus the name).

And beyond that, the package also provides the transposed versions of them, which interestingly noone has ever proposed to use. The main idea is, while offset in deformable convolution guides the convolution kernel where to get the inputs to compute the output; in transposed deformable convolution, it guides the convolution kernel where to write the outputs.

Highlights

  • Supported operations: (All are implemented in C++/Cuda)

    • tvdcn.ops.deform_conv1d
    • tvdcn.ops.deform_conv2d
    • tvdcn.ops.deform_conv3d
    • tvdcn.ops.deform_conv_transpose1d
    • tvdcn.ops.deform_conv_transpose2d
    • tvdcn.ops.deform_conv_transpose3d
  • And the following supplementary operations (for activating mask):

    • tvdcn.ops.mask_softmax1d
    • tvdcn.ops.mask_softmax2d
    • tvdcn.ops.mask_softmax3d
  • Both offset and mask can be turned off, and can be applied in separate groups.

  • All the nn.Module wrappers for these operations are implemented, everything is @torch.jit.script-able! Please check Usage.

Note: We don't care much about onnx exportation, but if you do, you can check this repo: https://github.com/masamitsu-murase/deform_conv2d_onnx_exporter.

Requirements

  • torch>=1.9.0

Installation

From PyPI:

tvdcn provides some prebuilt wheels on PyPI. Run this command to install:

pip install tvdcn

The Linux and Windows wheels are built with Cuda 11.8. If you cannot find a wheel for your Arch/Python/Cuda, or there is any problem with library linking when importing, please proceed to instructions to build from source, all steps are super easy.

Linux/Windows MacOS
Python version: 3.8-3.11 3.8-3.11
PyTorch version: torch==2.0.1 torch==2.0.1
Cuda version: 11.8 -
GPU CCs: 3.7,5.0,6.0,6.1,7.0,7.5,8.0,8.6,8.9,9.0+PTX -

From Source:

For installing from source, you need a C++ compiler (gcc/msvc) and a Cuda compiler (nvcc). Clone this repo and execute the following command:

pip install .

Or just compile the binary for inplace usage:

python setup.py build_ext --inplace

A binary (.so file for Unix and .pyd file for Windows) should be compiled inside the tvdcn folder. To check if installation is successful, try:

import tvdcn

print('Library loaded successfully:', tvdcn.has_ops())
print('Compiled with Cuda:', tvdcn.with_cuda())

Note: We use soft Cuda version compatibility checking between the built binary and the installed PyTorch, which means only major version matching is required. However, we suggest building the binaries with the same Cuda version with installed PyTorch's Cuda version to prevent any possible conflict.

Usage

Functions:

Functionally, the package offers 6 functions (listed in Highlights) much similar to torchvision.ops.deform_conv2d. However, the order of parameters is slightly different, so be cautious (check this comparison). Specifically, the signatures of deform_conv2d and deform_conv_transpose2d look like this:

def deform_conv2d(
        input: Tensor,
        weight: Tensor,
        offset: Optional[Tensor] = None,
        mask: Optional[Tensor] = None,
        bias: Optional[Tensor] = None,
        stride: Union[int, Tuple[int, ...]] = 1,
        padding: Union[int, Tuple[int, ...]] = 0,
        dilation: Union[int, Tuple[int, ...]] = 1,
        groups: int = 1) -> Tensor:
    ...


def deform_conv_transpose2d(
        input: Tensor,
        weight: Tensor,
        offset: Optional[Tensor] = None,
        mask: Optional[Tensor] = None,
        bias: Optional[Tensor] = None,
        stride: Union[int, Tuple[int, ...]] = 1,
        padding: Union[int, Tuple[int, ...]] = 0,
        output_padding: Union[int, Tuple[int, ...]] = 0,
        dilation: Union[int, Tuple[int, ...]] = 1,
        groups: int = 1) -> Tensor:
    ...

If offset=None and mask=None, the executed operations are identical to conventional convolution.

Neural Network Layers:

The nn.Module wrappers are:

  • tvdcn.ops.DeformConv1d
  • tvdcn.ops.DeformConv2d
  • tvdcn.ops.DeformConv3d
  • tvdcn.ops.DeformConvTranspose1d
  • tvdcn.ops.DeformConvTranspose2d
  • tvdcn.ops.DeformConvTranspose3d

They are subclasses of the torch.nn.modules._ConvNd, but you have to specify offset and optionally mask as extra inputs for the forward function. For example:

import torch

from tvdcn import DeformConv2d

input = torch.rand(2, 3, 64, 64)
offset = torch.rand(2, 2 * 3 * 3, 62, 62)
# if mask is None, perform the original deform_conv without modulation (v2)
mask = torch.rand(2, 1 * 3 * 3, 62, 62)

conv = DeformConv2d(3, 16, kernel_size=(3, 3))

output = conv(input, offset, mask)
print(output.shape)

Additionally, following many other implementations out there, we also implemented the packed wrappers:

  • tvdcn.ops.PackedDeformConv1d
  • tvdcn.ops.PackedDeformConv2d
  • tvdcn.ops.PackedDeformConv3d
  • tvdcn.ops.PackedDeformConvTranspose1d
  • tvdcn.ops.PackedDeformConvTranspose2d
  • tvdcn.ops.PackedDeformConvTranspose3d

These are easy-to-use classes that contain ordinary convolution layers with appropriate hyperparameters to generate offset (and mask if initialized with modulated=True); but that means less customization. The only tunable hyperparameters that effect these supplementary conv layers are offset_groups and mask_groups, which have been decoupled from and behave somewhat similar to groups.

To use the softmax activation for mask proposed in Deformable Convolution v3, set mask_activation='softmax'. offset_activation and mask_activation also accept any nn.Module.

import torch

from tvdcn import PackedDeformConv1d

input = torch.rand(2, 3, 128)

conv = PackedDeformConv1d(3, 16,
                          kernel_size=5,
                          modulated=True,
                          mask_activation='softmax')
# jit scripting
scripted_conv = torch.jit.script(conv)
print(scripted_conv)

output = scripted_conv(input)
print(output.shape)

Note: For transposed packed modules, we are generating offset and mask with pointwise convolution as we haven't found a better way to do it.

Check the examples folder, maybe you can find something helpful.

Acknowledgements

This for fun project is directly modified and extended from torchvision.ops.deform_conv2d.

License

The code is released under the MIT license. See LICENSE.txt for details.

Project details


Download files

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

Source Distribution

tvdcn-0.2.5.tar.gz (35.9 kB view details)

Uploaded Source

Built Distributions

tvdcn-0.2.5-cp311-cp311-win_amd64.whl (10.0 MB view details)

Uploaded CPython 3.11 Windows x86-64

tvdcn-0.2.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.4 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.5-cp311-cp311-macosx_10_9_x86_64.whl (423.8 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

tvdcn-0.2.5-cp310-cp310-win_amd64.whl (10.0 MB view details)

Uploaded CPython 3.10 Windows x86-64

tvdcn-0.2.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.4 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.5-cp310-cp310-macosx_10_9_x86_64.whl (423.7 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

tvdcn-0.2.5-cp39-cp39-win_amd64.whl (10.0 MB view details)

Uploaded CPython 3.9 Windows x86-64

tvdcn-0.2.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.4 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.5-cp39-cp39-macosx_10_9_x86_64.whl (423.7 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

tvdcn-0.2.5-cp38-cp38-win_amd64.whl (10.0 MB view details)

Uploaded CPython 3.8 Windows x86-64

tvdcn-0.2.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.4 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.5-cp38-cp38-macosx_10_9_x86_64.whl (423.7 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

Details for the file tvdcn-0.2.5.tar.gz.

File metadata

  • Download URL: tvdcn-0.2.5.tar.gz
  • Upload date:
  • Size: 35.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for tvdcn-0.2.5.tar.gz
Algorithm Hash digest
SHA256 fc1c3d05b2d9185b46bd320bbb8942e2558c56deb273ca7bd0b0f932a831d76c
MD5 2bef2266ec3ffefe4d7322d2cf9e52ec
BLAKE2b-256 3f9a950eaca1b4951e1b0b5dfeccb056e04b10eb51b866df0e649888550009b8

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: tvdcn-0.2.5-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 10.0 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for tvdcn-0.2.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 bbb1a1ca1563c659d44f75b6f522eefe5ab88756c87e1a6f0db73931e19e7c49
MD5 202afb9f560b755f6ba519ae3a9db3fa
BLAKE2b-256 f6eda9545f01bedf7c0a2f0351173abd14e22a0b934a042ff1cae8df41a967ee

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tvdcn-0.2.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d864b6d01335cd287ef3ec41f6753065b3143eee1763aeefa2eff514c568dc2e
MD5 2cb58d20adbcf9618142cbac80095dc5
BLAKE2b-256 03bbb86abc1f50739b99bc3e78fb02b1277828d6613ac4fa087fe4de14b99821

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tvdcn-0.2.5-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3d9125982439c98a07bfb2e05c3b2a3d0bff7e7910a3db4de9a0ae46bbadc36d
MD5 5efaab66b50c50589e6ec8dfb3b86a4c
BLAKE2b-256 69ca0abb12c79f31113c90c50763361df2093709259fb160655cbfa8313f6485

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: tvdcn-0.2.5-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 10.0 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for tvdcn-0.2.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 9198e58e35f7e201748ea44c87ea8e7854dea403f1af14225c9a61e4fa0802b4
MD5 fcb6bd90e9ddc7cf37d5ba24b2d92eab
BLAKE2b-256 18919b8d66b2622be87b94d53e1f47c0dc8fff8554065694a14301ac136782cf

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tvdcn-0.2.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fc3f86ba7cf680dc8257a9a3eec0a72ad282bfed16c57fe5611423786c124048
MD5 85343dd23ab4013fa1d9f6819fc60d79
BLAKE2b-256 37030ee6307c101f18284121753acebe4496b2ec92600edb67c95c9a5cf7fb47

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tvdcn-0.2.5-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 75f4a4abaab36c66b767bb39c1781fcfdb418367083173edad875657454e243c
MD5 63f1176ea20275ca23a4ca2e8967dca7
BLAKE2b-256 1f7fb74a610459f2a9f182366d45ad90d92be7034faa8be80b3288e0ba17878d

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: tvdcn-0.2.5-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 10.0 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for tvdcn-0.2.5-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 dde3c77cf7506093bdbeda1bcb1bb8e5494809acfcffdedbe87fa4c2c2fa3c90
MD5 6a2fe8639d5e85c2b5b50c65b0f44275
BLAKE2b-256 5c089f4f5da51d09512853f0a269e602b999a071fa99521fbb2c118b77bd2926

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tvdcn-0.2.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 13c7b88a979d0c0a5def2cce638e3f56439c4c44e15aaf7179be3be61dadc200
MD5 29cd034cb1b339fd1f1f6b1585f1b345
BLAKE2b-256 2859b6f117c1c6978191c3edfaa3679ea508430d3459d1c4827da094e9217625

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tvdcn-0.2.5-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ee01d957fb36a655d85abbd55551ddef5947b8a763018104f720899556275473
MD5 0152a0bfe524de1bb68eaf57a3a81c5b
BLAKE2b-256 ce56592b359f092b7175dc7b9aa5a6fe5ffce14b73536e576dad925bad053fc7

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: tvdcn-0.2.5-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 10.0 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for tvdcn-0.2.5-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 6b2170f36d0cf9c996bd6792ece0c200be1b50b8c9e30d035634334fe32cb005
MD5 b548065c2c0d82015ffc8211e4f36ed8
BLAKE2b-256 3d1f9c40b88abcd6f7b185af7cf347efa3a1e84d6e332d820525754949ec6b22

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tvdcn-0.2.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 190304958fe50da0adefaa321a5a0801e40052c9e50cbd8ba8bb7c460367a854
MD5 efc60d600520a188bfd6033cd205da7f
BLAKE2b-256 c77309c24482a879909585cb8cbad1c2b1316e3f6156d1f3fa5f7343ac3d113e

See more details on using hashes here.

File details

Details for the file tvdcn-0.2.5-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for tvdcn-0.2.5-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a15815a85d6df6b0ecb7a12cdb1ebfe24c6989f7835c02a39feba323449230f3
MD5 f929e7b0ef53c562129f0912a7860d61
BLAKE2b-256 83f8249da32cdc1da1986b888c1600e981fd9ee4e969c8addb93d00d5c474906

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