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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.11 Windows x86-64

tvdcn-0.3.2-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.2-cp311-cp311-macosx_10_9_x86_64.whl (477.4 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

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

Uploaded CPython 3.10 Windows x86-64

tvdcn-0.3.2-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.2-cp310-cp310-macosx_10_9_x86_64.whl (477.4 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

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

Uploaded CPython 3.9 Windows x86-64

tvdcn-0.3.2-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.2-cp39-cp39-macosx_10_9_x86_64.whl (477.4 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

tvdcn-0.3.2-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.2-cp38-cp38-macosx_10_9_x86_64.whl (477.4 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tvdcn-0.3.2.tar.gz
  • Upload date:
  • Size: 51.4 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.2.tar.gz
Algorithm Hash digest
SHA256 fec51a59c5243829df4328288491a3f3585d12a0219473f6d06625056b6c35f6
MD5 fa025a5844ef9ef921cee0f455cff02c
BLAKE2b-256 bdc3217ba816df077fd57badda1be45f9f9eef744a51c96fe3a955d631f4b009

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.2-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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1f51aa0ea19aecd201afe4dc0347c5ae79f8071c8010b9adcb6fb1dabed50a9a
MD5 4a6f83f26f3239f8e05ec6589d799ffa
BLAKE2b-256 a6b95367e9c055a0b01cc0edce6c3e614c2326085532db5319801acb687ce027

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 11ca94844bcaba72b8a9e0ffa205b96dcd01be3d71416cb29f3e498a14f38f51
MD5 ffd6554ce020dc99eb08964b5e59d5b1
BLAKE2b-256 3e21d720956361589e448d018d47dd251e4b1e0da2a27ce2d4ecc2f57671ae51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 97386abff25a189b945ac05a956a469a491eb5bd76f7ae575b5963d08bfd4018
MD5 f10cc9fb16395c3a4d0929cc52cdaca6
BLAKE2b-256 ac81819000ec2fe20a9fc49191fb0e4ce3f4c8ad08c628dc5c510111ba5c70f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.2-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.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 dfc49a115ff736a7b52fefa38759541fdfc414923dbc02b592490fc82cdd9b6a
MD5 1f8a3d443ad2f91b57dfd87b56902c7c
BLAKE2b-256 0fa7ca3fdd13b81ba6780cc7002984f7724aa1c29909342c2877291051b26b7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 64f968f4cbf26419cf32c53e8d8e6cedc68bbd4137a2ff067d3180db5a011753
MD5 d212a98ae9176dd55c21fdd32cff9c33
BLAKE2b-256 c7ddc76dbe44be036ec3b747d57e984b8c837dc4587b60e22a1245cbac6a34b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a64048ed029cf557e987640b0f126fd2e21cd87be51b1db5965b78269317bb52
MD5 d45951c7e54e44fb3b704266ede9390d
BLAKE2b-256 12e57fe623df6ff85a5f15b2dd8180f765a7e909284eeb02f15c6be5aa78b1a0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.2-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.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c12574e2b43ec597aeb1ae7de2c5beef1c378e000e7df46e2f364f4b2d0e7fd8
MD5 a727a8918fa6bce6fd3938e9304866a6
BLAKE2b-256 baee04ca8ad8c334800cc398ecbc2999ed89ce301e7d2f39aedae370648e4f65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5bb4b57a6b0a945ab66d09af896622b979b03f2ef77fbcedc3335f1e406e9426
MD5 37ecbf7508564350db44432aaeb41ccf
BLAKE2b-256 02533dc6456c645b3dbac083a0d1d7d141c80ae9c49e5692a509c3a86d2807a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4dda683de1cdd6994a86adcabe9a15a9654b7479384033a655951a50a9e6d18f
MD5 e726af4cb318835d0b0c5f3d610bbeb9
BLAKE2b-256 0ba2d8c4f9a5c807c77b0860a3b7a13010304f995953cded08b2a3ad571ae9ae

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.3.2-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.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 bc39cc8c294066c26186de8444ab401ef6341c777e8ecb9c12f1e5c2bd300d86
MD5 f39c3c06eb2cc9bfd886cdb870d72f12
BLAKE2b-256 982188cdd63163357a86555c018156d1e701b9f32a370c3918445572acf47328

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3a8d299bacfcfec3be38ae945d8297785834ef9d0848b495d9638b8a8c23df0d
MD5 530dcefb5ea9bac1933d95560f54e1da
BLAKE2b-256 00990f7cb36157ac42eba85efba2543b9a8e33527c90e1fdf1712900fe970b58

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.3.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 db933d7119780c841d370175a0ca216d960796946f7cd68398ab50361cdd7eb1
MD5 cca4dc13c7092e0c608101a139b51067
BLAKE2b-256 c521c3ad884a5e721b40b8806fb90ab7008e2731d6f15e34390b1e9f25ccc699

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