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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.11 Windows x86-64

tvdcn-0.2.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.4 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.6-cp311-cp311-macosx_10_9_x86_64.whl (423.9 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

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

Uploaded CPython 3.10 Windows x86-64

tvdcn-0.2.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.4 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.6-cp310-cp310-macosx_10_9_x86_64.whl (423.9 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

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

Uploaded CPython 3.9 Windows x86-64

tvdcn-0.2.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.4 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.6-cp39-cp39-macosx_10_9_x86_64.whl (423.9 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

tvdcn-0.2.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.4 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.6-cp38-cp38-macosx_10_9_x86_64.whl (423.9 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tvdcn-0.2.6.tar.gz
  • Upload date:
  • Size: 35.9 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.6.tar.gz
Algorithm Hash digest
SHA256 43b0b07550e04d75231bd20c600230fcddb4830c523027acc517cef3ad9fbc11
MD5 f98dca5d6a83e40356d2846dd47b118d
BLAKE2b-256 68be5e17b8d9a03db11b220f1042e95189b86d1dee5e54e97b93db63ab486c2f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.6-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.16

File hashes

Hashes for tvdcn-0.2.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9c9b692edfeb2ea4654275e0d6860a274bd0bcd0b0bc79d9c6f13f42997f6ed7
MD5 e340742c998d3307b6616fb383a3bb4c
BLAKE2b-256 b57185f1ad790170a0a36903200ee985f7b73533b9d5b47ad5317c6ed183dea7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e5b52a72ffca4fcc78ad77a96195ec8f80b3f6fd8c61214c889e1ef22503da52
MD5 301b123ee0ca76b5ce1333fda458d1cc
BLAKE2b-256 0abca5d7b04d5631b5a9fb30936d24e84fb219bb43669725c51f217a4d1a9ab6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.6-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0e9dae8cd3ff83b4dd5d1294acc4d1f58bf4fbe5544600ed2876f76825150081
MD5 0377a3be3cdbcb80b95cf9560142497a
BLAKE2b-256 2c1ee3f0817c6838b0374e61cb4afbc72c2d7204954633d67ef78c05681e5f9a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.6-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.16

File hashes

Hashes for tvdcn-0.2.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8c49d72d1c6ff95654985ec3fec1332f61cd921f6efda52039fc13b77c07dec8
MD5 ed7907bfd7238a0c93656fafb4cfdc6a
BLAKE2b-256 ec075f76f140abb320d607354385be662f7b01a0cd62a19fc690a6461dcd2a16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a267546e1cf8474b2abdbf78961bc8574d0ddcbaf91b3244f2e69b121c47b7f3
MD5 a34a07ab005404bcb01761f52dd0fbc8
BLAKE2b-256 88f8eb98b6902268b52b76010265dd6f49053c9f9d2b2a3df23e8a415c15b14f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.6-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e58af299b05a58c7e50cc3db782dad5dc458294b99693095c233adce42b2b11d
MD5 785ce2120d056d84d129422badd4a8ee
BLAKE2b-256 d10a34c014cd8f37bb7b4e3a60fdab263c1c223d5b8a3ec0cb218f91a60b28a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.6-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.16

File hashes

Hashes for tvdcn-0.2.6-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 ddc104aaabc420a012dd4ace65a4bf5b9211de310b54f531da87c10c5e525acd
MD5 f6b5c4c5e80a96c3ccc571fc2dc5353f
BLAKE2b-256 a05c34f19ccc202d6c57d881313d5a9b8e3c5222cf01bdd14b6337ea250e2d53

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d4ccd29a40e1e1c5de52370fcc31fa4883104acfcad846ecec654fdc4889abce
MD5 6d358b1aa2506802c9ead638cd0da968
BLAKE2b-256 7685092445a51bea1afe36aaeec0ee0ff0582e9a2ad8f8d015bc5bf67c4f8295

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.6-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 04acaa3d4c4340595373feaabf4c77fc9d82e54b58bd902ebd257e5a78e51441
MD5 16f14d8a17dce40d0e36da0b8583ea65
BLAKE2b-256 4fe8488373b20a017237bac8f8d5db1afaa9ebc6feef7d11425167ba8c38c61f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.6-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.16

File hashes

Hashes for tvdcn-0.2.6-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 4a71dc2d54f470c4e6a6b8738271f28ed864dcacebcce6662c8fb449ddb067ed
MD5 ebd7926070a76c637e810cbc2f7755f3
BLAKE2b-256 b772f0c4ea5a9920168ac792ac5bb854de322acc894c95e097b222240c6a5b22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 da7cc0acefd582c11d25a6330f7742ee77d20d5ccb5af81462e619395aec98a5
MD5 66f6ddaf23926cc8d133919c7e1474ee
BLAKE2b-256 b00772001ce93ac08d931c23a9c9b4f7aa04fa29816a962b04914f432376ae60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.6-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 af7dc2941f3c0400d2022f02386067d46ea650af480e944538987bd8fd3a9051
MD5 33d4f37b40cfdf835d25cc60a2cf195f
BLAKE2b-256 f35c23555c8eef3b9a497eebe1bbccd833eb1be4190822c4e6a13328f9ede128

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