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: 6.1,7.5,8.6,8.9+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.4.tar.gz (35.8 kB view details)

Uploaded Source

Built Distributions

tvdcn-0.2.4-cp311-cp311-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.11 Windows x86-64

tvdcn-0.2.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.4-cp311-cp311-macosx_10_9_x86_64.whl (423.7 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

tvdcn-0.2.4-cp310-cp310-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.10 Windows x86-64

tvdcn-0.2.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.4-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.4-cp39-cp39-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.9 Windows x86-64

tvdcn-0.2.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.4-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.4-cp38-cp38-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.8 Windows x86-64

tvdcn-0.2.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.9 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.4-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.4.tar.gz.

File metadata

  • Download URL: tvdcn-0.2.4.tar.gz
  • Upload date:
  • Size: 35.8 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.4.tar.gz
Algorithm Hash digest
SHA256 8316f7eaa761d98a83e98980b7b47c073e715f32e3e213afc6d120005b87278f
MD5 41737a951c47358c39d09dec1940bf82
BLAKE2b-256 e88695957570a6436d498d664f5a085088608f4c27ee655dde1bbdd82ade2d49

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 4.5 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.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 050e4f51e157d6504d57c4ff798a980730f3930ed1d622ce680299df44c3c8d8
MD5 111c19b38d1baa1f50d7db6492c2728d
BLAKE2b-256 69c7bb923404fa98dfe268c7e7528cca963303f70032ce7d4afd8be110f94cc6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2d3a4ab3d7848a07a266cb4c2bfd67eca42a2bb4e8e3981972b7478ebee7c131
MD5 4b04966878c9d24540b18b19e3baa225
BLAKE2b-256 adb18db0fb863fff2334808c02bc7c9da014f683caa58bd2d7c4d8ac5b7626fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.4-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 93f8162b1f5013dc76b9ac256b21b8ca33242641e25e7ed456198cd9a8eb5f32
MD5 92cdf817679ade52cd5e27c1fc1356de
BLAKE2b-256 808cec907815b1f238ed5a0b655c9389c61ad3b99a15af8e42626cb485b33de2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.4-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 4.5 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.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b9a6e00fc7de4403f5ad63586bbc88293d23d607ba78982cef0506d4fcbddd06
MD5 3d784e77083d64a131a672f92e4be59f
BLAKE2b-256 2b72e5f91242cc2fec64838d91e72ec6502ba025d601d166dd59ab503a02f843

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5bf79040e8a8a8cf7055dd67e1d2fa22131eef51f17db38c27132d124137c41c
MD5 b06407efa056aa67344847e2f1859635
BLAKE2b-256 62e1a5beb60570056bc678458f4c3e23668a45d4232cdfa7565d59214d61e768

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 541f2fdb034c6ee0654f5046e8fe2b0a16c43a3beb11e895cb77ea2c932df1b7
MD5 37e063ac6afb386ae298c4af318d5ac8
BLAKE2b-256 88d0ce81361773351a5625299dabeb948e0e1315bed228d1a718cd048158223a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.4-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 4.5 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.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 f401878fdc3886d8688f5264a912408f5a0bc831f53422b0df62b6fab7a03c4e
MD5 996b3ac366ee4054563606af8972b52c
BLAKE2b-256 6160ddd50ef1b0e7584a67c5ee5e83aaddcc095289f12234c6b2c84566171961

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f2a6eaf7d6f88209658e45c24c0ff46ba8baedcb3368c46d029324f19fd72e96
MD5 4039ac6fcbb0dd6814832384ae8ad0e8
BLAKE2b-256 d82ab0029f8e386b3bb1864cb3e5ef76ce5325d46ccbcbaed7741f54d9bb1e11

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 87f3bf0f63bcfbbb5aa476d050f393b85f7d4821401e4db16cb0da124b7b3522
MD5 c1d4968befeede162cc905315ec6c058
BLAKE2b-256 751207ab372d14218fde7a4d3adc2da6337b19a594dd1611fb1abd21d1677ccc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.4-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 4.5 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.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 70a127d0a10e1729510069277444536a59a49d286689481f154458fd225df93a
MD5 3e3b45bbde90d97b1bd62a0194275c2e
BLAKE2b-256 881f746c955ee4e0575e28429b5197823eb28b6d37eda24e4fbdfb17abdabc78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 be7707bcfedfb0c3a86cf563ee5fc7232c8c84a46d320ccca716666a8590a46f
MD5 8d172b5a6dd56d98bec2c970acb46fae
BLAKE2b-256 3f12446e447266349cdc3150a36d9c9319e963d8a0b02acda1d7f85c265de42b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 933df897cb087c6fbda92f6076f2592b3b2a93da27c774484278f5b6c6bf7001
MD5 1becc98ca0321d419763ac291936f30d
BLAKE2b-256 ed9f0bcd879c0c72dad7697356601195269a058ceee6d31e11a3fdfa3bc148b0

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