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 Deformable Convolution operation (the commonly used torchvision.ops.deform_conv2d) proposed in https://arxiv.org/abs/1811.11168, and the Transposed Deformable Convolution proposed in https://arxiv.org/abs/2210.09446 (currently without interpolation kernel scaling). It also supports their 1D and 3D equivalences, which are not available in torchvision (thus the name).

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 (mask activation proposed in https://arxiv.org/abs/2211.05778):

    • 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) with C++17 features enabled. 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, int]] = 1,
        padding: Union[int, Tuple[int, int]] = 0,
        dilation: Union[int, Tuple[int, 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, int]] = 1,
        padding: Union[int, Tuple[int, int]] = 0,
        output_padding: Union[int, Tuple[int, int]] = 0,
        dilation: Union[int, Tuple[int, 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.4.2.tar.gz (74.0 kB view details)

Uploaded Source

Built Distributions

tvdcn-0.4.2-cp311-cp311-win_amd64.whl (10.2 MB view details)

Uploaded CPython 3.11 Windows x86-64

tvdcn-0.4.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (34.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

tvdcn-0.4.2-cp311-cp311-macosx_10_9_x86_64.whl (540.7 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

tvdcn-0.4.2-cp310-cp310-win_amd64.whl (10.2 MB view details)

Uploaded CPython 3.10 Windows x86-64

tvdcn-0.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (34.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

tvdcn-0.4.2-cp310-cp310-macosx_10_9_x86_64.whl (540.7 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

tvdcn-0.4.2-cp39-cp39-win_amd64.whl (10.2 MB view details)

Uploaded CPython 3.9 Windows x86-64

tvdcn-0.4.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (34.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

tvdcn-0.4.2-cp39-cp39-macosx_10_9_x86_64.whl (540.7 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

tvdcn-0.4.2-cp38-cp38-win_amd64.whl (10.2 MB view details)

Uploaded CPython 3.8 Windows x86-64

tvdcn-0.4.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (34.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

tvdcn-0.4.2-cp38-cp38-macosx_10_9_x86_64.whl (540.7 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for tvdcn-0.4.2.tar.gz
Algorithm Hash digest
SHA256 1ce76e8ab7eb64b164a9a86b8f844afc81cf6f799bff1cbad4cd271aa09be1ed
MD5 ef22e66bc7e147adecc54ee9b06ed66e
BLAKE2b-256 d3c1dfeb03fc2f06574005abd902ce5588e0e37bd4f23ef88877e1f2b5315b93

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.4.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 10.2 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.4.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 567e22a57a4e97b0806a690bb3693c109497c93fbd41b54a544d4b9b844a1746
MD5 905ed2280d19c760f67c0de44a568d85
BLAKE2b-256 d1161edd64a2697c98d7025c4aa455feeb7a39b3a3cbe5e93854ec0729861348

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9cf9a3306f9cce7f2ad957011d192e0524a09656311bb1b4454ddbe91e53a288
MD5 82362423e34b7be1a743b0257bf362ba
BLAKE2b-256 273e3fe0117123bb878291e020fcea7e7d1256345b4039c0201100ca0478d1e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c309189d33758a1f82c71aee54e69f8639d9d7d8994bf435dc31a5305898bd32
MD5 b5348eb9361dfadb6995885c8297bf03
BLAKE2b-256 3ac0e4858e81112590a9895d29ca803345470dd2dd6ef9f91c147bdbb86aadf5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.4.2-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 10.2 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.4.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f6328622bc8d349ac9053c13fbf40b374c3d4daf944eee812bc3f0eff28ebf4c
MD5 b54eed152bf320ac44b3312cc178a442
BLAKE2b-256 ef8aa9fe454435bd528ae46346da33e2778353b78d2c6f635250c4747c7e277e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6b62c1354ef9f541eec885a3d38ed2bdac9ed1a74b97c4e77010f10bd09db3d0
MD5 6272c2e90cb44b67a8249202a1299169
BLAKE2b-256 3f8d8fe2e37f40f66c2adbed2bc93646c2dde1d833451775147557a13232a177

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 df9df5d9b85c086d5bf6850de2f946f3deb0b5b0abca54c19eee8bec23040987
MD5 fd7e810a55fb7464c78c6517a0863b9b
BLAKE2b-256 447ac95335020d248ea3983172ea2cd6f26ef703ad4c12cf52b14f2186762385

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.4.2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 10.2 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.4.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 eeb7f474a1bc1749501e598292ee3b1e372a445015e73de887e862e5803dfba6
MD5 bdd59702c7c43f2c44e223baf29b8b27
BLAKE2b-256 65acefa2728d3711a0b6519163d32e2625d016307496e6a843b1424ea32eb0a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 332a80eb552b51e5da1143a155c354ace0b41195b44a260423bd097b8917b623
MD5 c2e21942076ad988f1acceaa3b12f210
BLAKE2b-256 1b6fbd567e759932cf58ce82fd834a33cef60bf794993036f751b60f87f9b987

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8318d3d8bcb5ae1df46ef46e9c6b9a11a17618848852aa611938a98a0242963e
MD5 22ab71e1c728bfa0c902f90c9c976712
BLAKE2b-256 88a90c0918ec7ed5d7475e992756df8cd4b71e52a7ba746b0ecd6fc2dd959517

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.4.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 10.2 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.4.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 009adf17c77c681651e35298e37ccc150f7dfbbf1f961ce4f9eccc156c63ba9c
MD5 a782119e7e1cd3029cc158ce70742245
BLAKE2b-256 1635dc3191b01539c288cfdd8dac97c05b5cc741a92fccf71d4771cb8547df1a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6935daf871c58b47a93537d8a9885d1e2b74f038dfad87d5397d51fc2c96dcb3
MD5 dcc544a4676135a3326feb1fd294e70e
BLAKE2b-256 5e9bcf6fa2e8e94a6486cfac3af53b8f9b72069b9a6ecbdb9026b8846ba14c50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c0e22063a7edd1f99ad635cee3cd49032b2a34d5c7f064238d70aa8037c75fe9
MD5 790ddbe9236cc42f8edc1b096a8a579c
BLAKE2b-256 cf599b28d883bf3c3bfa6453279fdaf1ec5f4b8b3c854f11713d2e421c16e76b

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