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
  • offset and mask can be applied in separate groups (following 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:

from tvdcn import _HAS_OPS

assert _HAS_OPS

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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.11 Windows x86-64

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

Uploaded CPython 3.11 macOS 10.9+ x86-64

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

Uploaded CPython 3.10 Windows x86-64

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

Uploaded CPython 3.10 macOS 10.9+ x86-64

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

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.9 macOS 10.9+ x86-64

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

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: tvdcn-0.2.2.tar.gz
  • Upload date:
  • Size: 34.7 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.2.tar.gz
Algorithm Hash digest
SHA256 3611c1c39f05cb5cfd6d739f617b5388d6d88d7fd770c5ed9d22705bf02863d0
MD5 a58eb24778452b186807842e1660b0e4
BLAKE2b-256 44eff2cda353310bcc6c4f06a8f98aeb7a8f886d8e91b46e5aa697572d6695e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.2-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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3debee5691ed4dbe60be2d583d253dad08b57f80b4f4e3962081f0d8dd6cf864
MD5 343d8ce04acf42703b98bbb21bf55a69
BLAKE2b-256 258dc5c86b731b8808f3a3c0732cafa9aae385766b64928a95f8ecac6569e780

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 31b62c55912c18f5c4f8656d427dddac5f9d2550e603fd171280aa4c43846cd0
MD5 9174b645a9f322596a2710c1cc06614a
BLAKE2b-256 521c78349cc9113afa4936d2ff6d2207f24abe4df58b5c8c720f975cb9ebb04a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bd022106afe498b6a6d60da7a3f14e69df6cad0d5cf60601ecf989f65c47186d
MD5 45f57e5571013fa43baa46c7e9cfe5a6
BLAKE2b-256 1c8f317fe63c17cc73c21f725250df0edd62e3a4563ce6db19246d6c4745af32

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.2-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.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 343482ba5254ea9da6bdf150e0434349ad07041a7b785e56160edf73e9b7f1b5
MD5 61ecd63e716c816197e772a6f26b7685
BLAKE2b-256 b0f36167a7542425400f1972ce8c26daf48a64dc525850ecbc0b955fdb6ed95d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 47aa3ecb0e8d8e39eea1fe0f27cec9a4f5bd9ba650b70a504daec06fa6f6e49b
MD5 1764e6a2d3aabfd27b1dca875d902600
BLAKE2b-256 79ca67b8db0f13c5aedec3e8123cf067974f7dd3ec4fb80223b5d1d79c4d8b2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cce44c2673df044eaf9c0c70d72ea37f22c68de9490c10b4f6b0ff3907f2f9b6
MD5 de874ab05fa0e3fceba36f4a67332f23
BLAKE2b-256 bea4113e0835bc9a8b0b7963f0339376ad4086f89d2ab7795a274beebb852c48

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.2-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.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 eb6a8461fc37194e6128cd691aa7dcc14aa29494e34806633981923807f0f26c
MD5 6ff7550d35d33fb3fe562d75249d514f
BLAKE2b-256 736992d26f7423a1074de2314b1ed4d64e0db1dada720b6f1ebddcb70f11778e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8242696e795f77cc29cc346db86c688ccd23558354781f6e0353593bd5f990b6
MD5 90e5904660b88dc214b0cc8b3ac14ed9
BLAKE2b-256 9a28bf3b3fef3d1ce7204fc9419397ef746e85b754ef05afcf3940d12a02830f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 99931b824305ab7a6d64b31e3414fb411ac715465a456884d02c05a7d99f5e01
MD5 cbaab04ac222a3798ccf38306e052dad
BLAKE2b-256 4b5441eb195cc73c4fefc37315136294c172abe57960ad86baa4da59da26f2b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tvdcn-0.2.2-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.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 1f587726bc536ec8d24220dbcd5dbec6f27698ed19a0a51d31043d06bf78735c
MD5 bc145fff79b32abcd521d235ceb52003
BLAKE2b-256 cf781fe971cc90eb4e644f69f7ed7cc1443986e1f9980c25de1b6ee3ea5a5f1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6af6a11c7f0a5ade1c25ca0c0659a6db95225f75057fb957ab545badb4fc108a
MD5 b17bfe1db392df07f3f2964475d9f833
BLAKE2b-256 856e37dd05d9b764a3d8f67406dbff78557f9a92a532c872b1a453ecf3b8afe9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tvdcn-0.2.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 06b5b1e2ec24a68f3a217a5ffbcb59fd47606c15943cc432c5bb17eb7f2a7dc8
MD5 191edfd6eb55263ca19f7e384f175909
BLAKE2b-256 fd4bc95c3a88331b7a25a2d30fb4f767b525570287ab9e5f1cac84620fead79d

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