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 turned off, and can be applied in separate groups (Deformable Convolution v3).

  • 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.8

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, which is compatible with torch==2.0.0. 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.0 torch==2.0.0
Cuda version: 11.8 -
GPU CCs: 6.1,7.5,8.6,8.9+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.

import torch

from tvdcn import PackedDeformConv1d

input = torch.rand(2, 3, 128)

conv = PackedDeformConv1d(3, 16, kernel_size=5, modulated=True)
# 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.3.tar.gz (35.3 kB view details)

Uploaded Source

Built Distributions

tvdcn-0.2.3-cp311-cp311-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.11 Windows x86-64

tvdcn-0.2.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.3-cp311-cp311-macosx_10_9_x86_64.whl (422.6 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

tvdcn-0.2.3-cp310-cp310-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.10 Windows x86-64

tvdcn-0.2.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.3-cp310-cp310-macosx_10_9_x86_64.whl (422.6 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

tvdcn-0.2.3-cp39-cp39-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.9 Windows x86-64

tvdcn-0.2.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.3-cp39-cp39-macosx_10_9_x86_64.whl (422.6 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

tvdcn-0.2.3-cp38-cp38-win_amd64.whl (4.5 MB view details)

Uploaded CPython 3.8 Windows x86-64

tvdcn-0.2.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.9 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

tvdcn-0.2.3-cp38-cp38-macosx_10_9_x86_64.whl (422.6 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tvdcn-0.2.3.tar.gz
  • Upload date:
  • Size: 35.3 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.3.tar.gz
Algorithm Hash digest
SHA256 b6cfa7169f41e3fdc8b4974b73da3bd5ee82f2d81d308bdc52a7978936b59980
MD5 f2167e4d86b534cddc805e476b67dbec
BLAKE2b-256 fcfca7f6360e843acb3950ed7e7647266e6f40300fe2bbb1fe877b08de52eb18

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 4.5 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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 dd210c47ce94f13adb398f3c9016a5aa24149d0a966a8075ca3566d9914e9ef1
MD5 3cfb211dba0a5592fe34dc50b887f4e7
BLAKE2b-256 fd635e92eb839317c13c88ece452ccd54605c62293befe2a8740f9b6c9cb9f12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 57f6c2181342af718d076cb695cbc5ad501a7fd44c705372a0146cd7e68f0257
MD5 710b8bff3c61ebf2558f7fd72990004e
BLAKE2b-256 5a0d4d832033c41e3be3c56c82ab9aeccd70717dac42bdacf6f0a7cd2542176b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.3-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cd0bd24e648d4f1539044f48cdac6249f2ee109f850f2d0aa5e2ed9f1d0efda9
MD5 470bebc5f5a3f6770c5c63c99a6ce8b3
BLAKE2b-256 03eab854ad61e5b05983a75789ca2c8b6865a35c20d4aba51d7a79b97a21b601

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 4.5 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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 001145f16685b6772901b43b4d8397fd199bd6ce19afd9e7e02d776073eda402
MD5 6611cd5432f28fca129dfbdf27b9c2c1
BLAKE2b-256 5949bbc1598932ea10ef7b80ebcd4b3a873dc1fb87983633e6ecf5d77a4ee1a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ea9041bc78ccaf85e7f7294012c3097b439bce1b336b08bf8497bd34cffae81e
MD5 e7283521cd36ac72bc9fc2acda63c4bd
BLAKE2b-256 46f9437d9773ed3993346e5e6f56079f2059e4319b612100399f0aadb6cb0ab3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 606f2d1bfd883e55a2a173bf18f01d2af1bae6f190660abf31e15654b7cd83e1
MD5 e6d576ea79167abad13195586de76293
BLAKE2b-256 7ccb21e3f8853e195a32dbab5492b3269f5f424a54a46027929b74bc95720164

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.3-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 4.5 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.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 d363952c9f81808a1f31b2b3f0293dc8bf00e1d15461d97653b497572eddcea2
MD5 20e2ebcc3942d5594a73c7cbe3c4a879
BLAKE2b-256 6d650a1273b6184a18d6268f8c3631263e4c2b14db892b08734f478b91a0033b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e608fd2dc2ae6f5f1f7c9f7a09e3c4086c0e485e51cd19566121d031498f86b7
MD5 7f503f934a069fad234bfcda4312b459
BLAKE2b-256 6a39ad83736fc6a3f6d548cbdb3c685b346ee79083466d80430b6610d60a59e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b5656624c6de963a8a101f0952cd7a6e4e43068328622ff0edb318cf071c5a9c
MD5 2015e29ebff522101333cbe770ffca3f
BLAKE2b-256 9aa759b53de0fce70e8d878ff4c9e8f24343fc9c6ddcf46247cce208d022d331

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.3-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 4.5 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.3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 c07a83055cdd5bcbf16ec96c1af156c28ac07fdbdf8ebd4b1d35a5aab6e9b009
MD5 fe4952a3cdb3a0334ffc72b9516d1b94
BLAKE2b-256 97e17476de337465b84fc3049b41515da1dbadc86a60d2b9f937f948ff656c85

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7306179179d1195750a1b68c31f9319a0ecf9e8461fd3a9217e04ed4d4c3760b
MD5 2f1c0797c4b4e9ee9291bcf27e4eafe0
BLAKE2b-256 ad9019c4cc4e24de97dbccafefe6ba5a71cce20256012b805ab1cf39a01a3ea0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4f1a724f4e9a1c65bf775a7336e9ea94b286dfd0291a3c642f05c901988a2d29
MD5 9b9cb2bf270407e1492a48f8545f09ef
BLAKE2b-256 154266c4b5917d37095df1ad03a62f9fe4eb3bb251091f7bec860886427fd5f3

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