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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.11 Windows x86-64

tvdcn-0.4.0-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.0-cp311-cp311-macosx_10_9_x86_64.whl (540.9 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

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

Uploaded CPython 3.10 Windows x86-64

tvdcn-0.4.0-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.0-cp310-cp310-macosx_10_9_x86_64.whl (540.9 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

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

Uploaded CPython 3.9 Windows x86-64

tvdcn-0.4.0-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.0-cp39-cp39-macosx_10_9_x86_64.whl (540.9 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

tvdcn-0.4.0-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.0-cp38-cp38-macosx_10_9_x86_64.whl (540.9 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tvdcn-0.4.0.tar.gz
  • Upload date:
  • Size: 74.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.4.0.tar.gz
Algorithm Hash digest
SHA256 624ff8ce6c02c73638515960161b3ab666c7ac17373f556857578e636ad5db96
MD5 77efb50894cf64d87685973f5fbdad22
BLAKE2b-256 aa0ce74420b218e2a54859bd142dc939e03c9c6e11a5a4f14ce281c2109d4bf7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.4.0-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.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2316231a79ff20d3cd5d96a9aa515951b34a0fcdeffa7b8b97974c3cfa60a350
MD5 da5ab5214194c4a61f8775ae63e36a55
BLAKE2b-256 79f5d37148d49a54193e1b41a1286f840e8b20b2b89ccf4171998f8eefa2aaee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 05a7c958431a64d4eff93bbe0eda1648a981b34206a5bb59f08850dd123fb80c
MD5 3a5d2b95dff526614f63f494a6b253f3
BLAKE2b-256 9251da06db4a547c3665a4bbbd8a30036581d7644800e9563bfafa4c6fea078c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 788a373d3791a9763d67145d57e79d70ae556bb3d54d9e81937f330c8752f5ba
MD5 c727a25dbd93ba991ba31392e9671550
BLAKE2b-256 bba16ea2b7c4fefe47b285960a3975f754d6937718718f12450d20d51d336398

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.4.0-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.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 583f739373e3c714897c2310f7fb0dcc0f49635f3c2e1215896afb4d54e931bb
MD5 e7f191494a5d1663b1843029c1c4b212
BLAKE2b-256 b20fe27aa8a3f4a2add4a9ddf7f85acae79b7e8cebffc1ff7c1ef59cff4c9933

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 52745c2e8358d2aa1f9465a4e2a82202bf05813a1c3b43aab64cd3ae7e72d258
MD5 0606f57e1a72d2552e975a4bb7966026
BLAKE2b-256 8d6fa250ad609c7ecaa035b7ed354820fc45b3ffdaa388e4ed2fe26cc3b248be

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9f69e5972ab7769689679450a3b398f0efedb5cada47030355abb2b3c57b9254
MD5 150010a73ae9c9ec3d3dd896232fbc5d
BLAKE2b-256 0592bce4cc93856fdeb84430a237c8b8f8951de1f13abb72af6b0175f502a491

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.4.0-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.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 fc6ad956d4d9443d51bdb539e8308ad2b1d151e61f5a62fe6fd5d7985d1b2028
MD5 94dc6ce29e7e6047b7785a49b0b9ca8b
BLAKE2b-256 60ba44c654161ac156bae4b63586f262a6a46b3b082ba0409b07213097b3b8a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 22786de8c75bf9e97bdac000feb051392dbb1c4881e0d010667211a6d23773a1
MD5 ce1bb4490d1e6b9ebf4785dcc3ace0e8
BLAKE2b-256 733cf3420fcf9569dec188f6b18edc6e85339cba5162b10389b85a618ff6373c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 255ffdd970c371b3b441e3faa2c758b212db127c7c4967bd90ce36eaad19144d
MD5 703f6f45ed1b116a6a52b58606849fbb
BLAKE2b-256 23403218eca83f18ba04b1a2e1ce220e4b03ef9d61aa3136799347aa6887c9c9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.4.0-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.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 dbb6817e2e6c2a0c4edb561c11d099d001a357f8db1afbc2c46cc7817da1e563
MD5 bf7f74f92cf860c4390578af34139a74
BLAKE2b-256 86c87555f0271a07067bd79a3a616e06008424cc03e8e6fbe731af80941fd682

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 98aa237195e3c922d46d927ffd44ca71440c666a7454a3d52860333690f5dc2f
MD5 daa9851adff2bd1ac23741d3118dbd46
BLAKE2b-256 fc4fefbdf41a7cbe1653a3dbaf2d74322b6a0f4768a87b233fc1572b906ca0c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c94bcaaf8df122b6e26b7d5452df708b1a7ab1b37042bbbc53c1a5874415b76a
MD5 de9b5834aa37fadbf1ab3c2ea3790f09
BLAKE2b-256 68c44746ee89d4790c0023bf2592c49789e16b3451839ef7125c29d59891710e

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