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.3.0.tar.gz (51.1 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.11 Windows x86-64

tvdcn-0.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

tvdcn-0.3.0-cp311-cp311-macosx_10_9_x86_64.whl (476.0 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

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

Uploaded CPython 3.10 Windows x86-64

tvdcn-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

tvdcn-0.3.0-cp310-cp310-macosx_10_9_x86_64.whl (476.0 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

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

Uploaded CPython 3.9 Windows x86-64

tvdcn-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

tvdcn-0.3.0-cp39-cp39-macosx_10_9_x86_64.whl (476.0 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

tvdcn-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

tvdcn-0.3.0-cp38-cp38-macosx_10_9_x86_64.whl (476.0 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for tvdcn-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3127d8a478b1621515435c0b9ecd8f442ff7b271cbf3be048301f82860262a5f
MD5 a2b7dd8bbdea555f97d5b1699d302bb7
BLAKE2b-256 c6fec05708ce8c3346a4618e606c28fd716bc3053a2ce7572bd48f31b8fe87bf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.0-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.17

File hashes

Hashes for tvdcn-0.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9a7802a4d4f95daab109696d03129f0c1e7487d904991cc5f35450a9571f2172
MD5 0b5abc33d6509270894801e464c92016
BLAKE2b-256 5b36b4ccf9c4461096298eabfb853b8e3aa75977b3c79caed5229497c4b8681c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 31a1fbc460faf6e4df57d89e300c41f7dfe4c769a4a9a05071593618ae8c20af
MD5 6b3398ed9aa9865102702c6898759f39
BLAKE2b-256 1c7b32f7d30241930ad171f0fcf0b677bc4df3328438cdedbfcc40c25c4d5342

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 14279175bb7ac019b0ca08816ec8a66164a12f27767ceb99bf596e2eee4d3098
MD5 56800a0ec2677091913ed88c36478563
BLAKE2b-256 780485654b566e7b2634233a124a0cd797cce3d242125c4671db89b700520cd9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.0-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.17

File hashes

Hashes for tvdcn-0.3.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 838b570084217755085568495aa613d0906971fc0faf22cb84d9a615dfb714a4
MD5 5da4a7073ec9d21f2d9ae3e52884cfaa
BLAKE2b-256 11297e9a4e09156601d5cefcfa24aa9887dcde9b99e0e44cee0e8e2c4cd5504f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 01ad65d8d22de7a242e93ae781f22b1874a6332603b2a715a42173ebe6065e01
MD5 c70e27889c25bc36e17d13f7770d57f2
BLAKE2b-256 c38b72b39e956d5ea30485326346932931eec4d72306b741686af0545994d658

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a35d1be853e03eec3261af3a6bb746fa2e995b14d6a9c26c9c6068cd6653f78f
MD5 203dbb75cdea4a92cca42eaf724dc83d
BLAKE2b-256 bbae8b06b76ae849274c691347aa360fc0159466be0ab64d6e272d41c35510b8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.0-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.17

File hashes

Hashes for tvdcn-0.3.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 1dd09d71ae3ef7ad1dfa915c6899319204bd49133999cea67302b057d96dd3ab
MD5 5da3a80ed358e880e67d24e586b5b208
BLAKE2b-256 13167622128c369f8fa50dcaf326135adea37211b5d4b70fcc98e5ff90622e7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ee134f8931001c2fb2362369ed501f9c32043a4f87ef51b8091d53fd413532b7
MD5 deebf8d4c49c741bf23f1d7114700137
BLAKE2b-256 46372758bf40ce40321d413c461189bb41c04debd1cd20d58a00b46795b6a1eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 fb59a333a20cca2bcda2832222751e2dd9aa4ca24dc0373921667adb20166a24
MD5 2e29de821a73ce54658c5225cc89eb71
BLAKE2b-256 102f422812d58a0ae09493fa5079706410ef00290bb54c9964c5450fb94a8ff0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.0-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.17

File hashes

Hashes for tvdcn-0.3.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 4ee57cb3853ab0591edc2cbc1177fd9cf2db26baef68453bb996f6a5deafc138
MD5 b08731275e7dbaab8571bd27200144fe
BLAKE2b-256 c49a97327308030edcd1a366ec67db9328d42fca6b9189ef625e501ca6f98fb0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 acaa138334a7c65a464e2b8134c840c1af857a5998e67fd2880293bd044a4868
MD5 606bec7f5dcbf506c3654ee686a0ff3a
BLAKE2b-256 be2ec07008787f999d1dd8e24129c10728394d60efb82b629c6173a7aec342a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 80f6e56d1639c7ab316868246e6f34eb04370cf80a84ad265958f1b6011df582
MD5 247cae69efd5eed57b7c6715caa907b2
BLAKE2b-256 5dbc7fa9a6655388b3d5449b4aed33c09308b056a3eaa5fcb330ff0804d30e94

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