Skip to main content

Torchvision+ Deformable Convolution Networks

GitHub Workflow Status PyPI Downloads GitHub DOI

This package contains PyTorch implementations of Deformable Convolution (the commonly used torchvision.ops.deform_conv2d) proposed in https://arxiv.org/abs/1811.11168, and Transposed Deformable Convolution proposed in https://arxiv.org/abs/2210.09446 (currently without interpolation kernel scaling). It also supports their 1D and 3D equivalents, which are not available in torchvision (thus the name).

Highlights

  • Supported operators: (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
  • Supplementary operators: tvdcn.ops.mask_softmax1d, mask_softmax2d, and mask_softmax3d (the mask activation proposed in https://arxiv.org/abs/2211.05778).
  • Offsets, masks, and bias are optional. Offset and mask groups can be configured separately from convolution groups.
  • Packed modules can generate offsets and masks, with sigmoid or softmax mask activations.
  • Supports float32, float64, float16, and bfloat16 on CPU and CUDA.
  • Autograd, autocast, and fake kernels are registered with torch.library. Operators work with torch.compile(fullgraph=True) and torch.export.

Note: tvdcn doesn't support ONNX export.

What's New in 2.0

Version 2.0 uses the PyTorch stable ABI. Operator names and schemas are unchanged from 1.x. PyPI provides one abi3 wheel per platform, compatible with Python 3.10 and later and with PyTorch 2.14 and later. The C++ API is still available through the header-inline wrappers in tvdcn/csrc/ops; see the C++ API smoke example.

Requirements

  • Python >= 3.10
  • PyTorch >= 2.14

Installation

From PyPI

Install tvdcn with pip:

pip install tvdcn

PyPI wheels are available for Linux x86_64 and Windows with CPU and CUDA kernels, and for macOS arm64 with CPU kernels. The Linux and Windows CUDA kernels are built with CUDA 13.0 for sm_75, sm_80, sm_86, sm_90, sm_100, and sm_120, plus PTX. CUDA kernels load only when PyTorch has CUDA; the same wheel works on CPU with a CPU-only PyTorch installation. For CUDA 12 or older drivers, build tvdcn from source.

From Source

Install a C++ compiler and, for CUDA kernels, a CUDA toolkit with nvcc. With PyTorch installed in the build environment, run:

pip install . --no-build-isolation

For in-place use, run:

python setup.py build_ext --inplace

Build options are documented at the top of setup.py, including TVDCN_BUILD_CUDA, TVDCN_CUDA_ARCH_LIST, and TVDCN_LOCAL_VERSION.

Usage

Operators

The package offers six deformable convolution functions, listed in Highlights. Their argument order differs slightly from torchvision.ops.deform_conv2d; see the migration guide.

import torch
from tvdcn.ops import deform_conv2d

x = torch.rand(1, 3, 8, 8)
weight = torch.rand(4, 3, 3, 3)
offset = torch.rand(1, 18, 6, 6)
mask = torch.rand(1, 9, 6, 6).sigmoid()
y = deform_conv2d(x, weight, offset, mask)
print(y.shape)

If offset=None and mask=None, the operation is identical to conventional convolution.

The transposed operators take offsets and masks with the spatial size of the input:

import torch
from tvdcn.ops import deform_conv_transpose2d

x = torch.rand(1, 4, 6, 6)
weight = torch.rand(4, 3, 3, 3)  # (in_channels, out_channels // groups, kH, kW)
offset = torch.rand(1, 18, 6, 6)
mask = torch.rand(1, 9, 6, 6).sigmoid()
y = deform_conv_transpose2d(x, weight, offset, mask, stride=2)
print(y.shape)  # torch.Size([1, 3, 13, 13])

Neural Network Layers

The nn.Module wrappers are DeformConv1d/2d/3d and DeformConvTranspose1d/2d/3d, available from tvdcn. Pass offsets and optionally masks to their forward method:

import torch
from tvdcn import DeformConv2d

conv = DeformConv2d(3, 4, kernel_size=3)
x = torch.rand(1, 3, 8, 8)
offset = torch.rand(1, 18, 6, 6)
mask = torch.rand(1, 9, 6, 6).sigmoid()

y = conv(x, offset, mask)
print(y.shape)

The PackedDeformConv1d/2d/3d and PackedDeformConvTranspose1d/2d/3d modules generate offsets (and optionally masks) with convolution layers. Configure offset_groups, mask_groups, deformable, and modulated as needed. For example, mask_activation='softmax' selects softmax mask activation; offset_activation and mask_activation also accept nn.Module instances.

Modules can be compiled or exported with PyTorch:

import torch
from tvdcn import DeformConv2d

conv = DeformConv2d(3, 4, kernel_size=3)
x = torch.rand(1, 3, 8, 8)
offset = torch.rand(1, 18, 6, 6)
mask = torch.rand(1, 9, 6, 6).sigmoid()

compiled = torch.compile(conv, fullgraph=True)
print(compiled(x, offset, mask).shape)

exported = torch.export.export(conv, (x, offset, mask))
print(exported.module()(x, offset, mask).shape)

For transposed packed modules, offsets and masks are generated with pointwise convolution.

See the examples folder for more.

Documentation

See the documentation and changelog.

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.

Metadata

Release files for tvdcn 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tvdcn 2.0.0
File Size Uploaded Provenance Actions
tvdcn-2.0.0.tar.gz 95.6 kB With provenance Details for tvdcn-2.0.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for tvdcn 2.0.0
File Interpreter ABI Platform Actions
tvdcn-2.0.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for tvdcn-2.0.0-cp310-abi3-win_amd64.whl
tvdcn-2.0.0-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.10 abi3 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for tvdcn-2.0.0-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
tvdcn-2.0.0-cp310-abi3-macosx_14_0_arm64.whl CPython 3.10 abi3 macOS 14.0+ ARM64 Details for tvdcn-2.0.0-cp310-abi3-macosx_14_0_arm64.whl

Total release size: 17.5 MB

Release files / tvdcn-2.0.0.tar.gz

Download URL tvdcn-2.0.0.tar.gz
Size 95.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7d298a010e268768b69522ed22eded777815beb0ee1543d2f86b83517f8b56a1
BLAKE2b-256 checksum
How to use checksums
b6c217f1590593780c141d23e98ccf7a96de3414f8ded59376cfc855f6a33c67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Release files / tvdcn-2.0.0-cp310-abi3-win_amd64.whl

Download URL tvdcn-2.0.0-cp310-abi3-win_amd64.whl
Size 8.3 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
46d457d4844f7f69bcffe12caa06944720d18e266c7f2a79f8e20c479fd82b06
BLAKE2b-256 checksum
How to use checksums
59adf8026338020e19cfbf7649e3ad2a1920f153206293453d6e819fa7a676ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Release files / tvdcn-2.0.0-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL tvdcn-2.0.0-cp310-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 8.7 MB
Tags CPython 3.10 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
9a1394ad728c66454f56012145ee666d3e330cd1772ed4f4c26c5f1d5b056433
BLAKE2b-256 checksum
How to use checksums
bb7dfb73b892ee4b5f516eaacbed3c87f8f915e79fd295af092fda32fe7ba934
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Release files / tvdcn-2.0.0-cp310-abi3-macosx_14_0_arm64.whl

Download URL tvdcn-2.0.0-cp310-abi3-macosx_14_0_arm64.whl
Size 336.1 kB
Tags CPython 3.10 abi3 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
9e8c3b571a79df3de2cb8a8a99f62e0becf2786ea7cc99f4cc174624044c8b1f
BLAKE2b-256 checksum
How to use checksums
6d91804060abf224c1efa45b3a6510082f9d2e49ec67f7f6b0e22aa69db3dcbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Security for tvdcn 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for tvdcn-2.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page