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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.11 Windows x86-64

tvdcn-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

tvdcn-0.3.1-cp311-cp311-macosx_10_9_x86_64.whl (477.2 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

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

Uploaded CPython 3.10 Windows x86-64

tvdcn-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

tvdcn-0.3.1-cp310-cp310-macosx_10_9_x86_64.whl (477.2 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

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

Uploaded CPython 3.9 Windows x86-64

tvdcn-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

tvdcn-0.3.1-cp39-cp39-macosx_10_9_x86_64.whl (477.1 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

tvdcn-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.9 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

tvdcn-0.3.1-cp38-cp38-macosx_10_9_x86_64.whl (477.1 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tvdcn-0.3.1.tar.gz
  • Upload date:
  • Size: 51.2 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.1.tar.gz
Algorithm Hash digest
SHA256 b2707ea0eccba326e157afec8f001c1692ea83895b2c4e44c25db431f4514ef4
MD5 67fcc9167db4758b97636c852d50196c
BLAKE2b-256 25467a84611a33731817445ed1e09e320328ddb9d0fc8699eda50b9248407ea2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.1-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.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5afd7480d7991ae7dbc8623f0fce71ca8eebf0b2ce0c567a46c521ad666ca65d
MD5 90add46fccca13c5a1dc6fd6fb46eab8
BLAKE2b-256 30fdb3ae1b840b3d2d717af83def8dcd304a0fd4832053ff9b41a75028f080b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 095734862e94c169ae79b47f2ed889d766bbd468241f4552e2e9b66822762c93
MD5 faa677aec5d7f28fe9248bc4bbda2e46
BLAKE2b-256 d46c0118dab4d542435057c667a81ffabd11d5a8ab2063039f9c3087def41f03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ac4a98a73c9243cec053867a6a8398b1c250c833a20a7f1f1504e84b07275048
MD5 2a6a62909fca12e1387adc72673556b4
BLAKE2b-256 4106a434097eab4f85e6e2a4e30acf1130023f1ed1825ab61c2984f59567bbb4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.1-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.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 85b8fbc0eab23718fb29e5afd72dfe649b4caf000b0f59226864a374a4c0eec6
MD5 3cccb9dc7a7c3c5d43b86fe18db5b7cb
BLAKE2b-256 5d62867985480d4a7dff8221398df08e903b8b18756e34949b3b82ef4578a59f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0700dcdf115a01af3e069187fa1c4a43d4bf5769490fb88feb32e723fe85032c
MD5 bd17f645cfd39a43153bc43e7545a4c6
BLAKE2b-256 1b44093ba6edb6284a20250606d69886248e8a9906acda22667575a0787a870a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bd3fd6e8ad90c1b203592bd70a490a2cd41535d06bba958b0a0378e99adac157
MD5 e1f77006ba0ff4b71861e07e788ecc76
BLAKE2b-256 e9a12aef16f7d40845289024ba4416ea8ac311ecd8ab4ddb4b508ae96cea2733

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.1-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.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 84b7829e9e51c4040d4c92d70d1e6b9dc9380bbd1ee4befe17535b302d76f8b1
MD5 cfb41630d80f349372150c4a0a69887d
BLAKE2b-256 02b4e6addced3f480e47f5749be3fec5112cff193bb19bff54c82f1ac77557c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7af19bbad69d98c7e7afa1701c1cda6080f6e7e5a2c78a6436d5c5c4f6859a88
MD5 2c57f98d22a9afdcb4ef6c274f7d6f7e
BLAKE2b-256 0a5fa9b97f1c1356273019b210113251bd92f5ed6dd936b62e9361687801eb6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 fd367cf4948dd729906916bae8668d85841ed59840375d0236c533e0543f60c7
MD5 990482bb7cf34dc53d50c45cc42d6942
BLAKE2b-256 dfb2966da11a32a67fc7d40286fbe3aaec1c300c20e91d40877530533bd9f763

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.1-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.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 d1301a54d61e3d67f03a07e646d63c5f05672da33b83287abb07a75d01a2a44e
MD5 c1334ed786b9793d383eb554fd175961
BLAKE2b-256 ab7606009025b0537507c46a899c2db59f5f1bb94b8749e533b7d67d8371164c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 432c9d4cd5fc8b625d1eb52df0337a4e8d121de346a2075cecfd6d57001ef7c1
MD5 e77fd9d5cfbebc530a0b8e93788d0536
BLAKE2b-256 3e13471071bdeb219b31f8d5c1255c52f05592596b10795c1a76f2d0b2e05de6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2b8b8cfbdb65c04ce5126b55a5d8ba7d36263fabc0ca43f14fd3c85f1ecb8530
MD5 e6cb3c38661f539edab3501f49cb31d7
BLAKE2b-256 8002c976f62f895c03e7d65a78a78ae4a3696b1db165a2ea4956b00cb0c1fdfd

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