Metadata-Version: 2.1
Name: dc1d
Version: 0.0.2
Summary: A PyTorch implementation of 1D deformable convolution
Project-URL: Homepage, https://github.com/jwr1995/DeformConv1d
Project-URL: Bug Tracker, https://github.com/jwr1995/DeformConv1d/issues
Author-email: William Ravenscroft <jwravenscroft1@sheffield.ac.uk>
License: MIT License
        
        Copyright (c) 2022 William Ravenscroft
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.8
Description-Content-Type: text/markdown

# dc1d (DeformConv1d)
An 1D implementation of a deformable convolutional layer implemented in pure Python in PyTorch. The code style is designed to imitate similar classes in PyTorch such as ```torch.nn.Conv1D``` and ```torchvision.ops.DeformConv2D```.

# Requirements
You must install PyTorch. Follow the details on the website to install properly: https://pytorch.org/get-started/locally/

This package was most thoroughly tested on PyTorch 1.12.1 running CUDA 11.6 on Windows with Python version 3.8.

# Installation
```
pip install dc1d
```

# Usage
## Example of how to use the deformable convolutional layer ```DeformConv1d()``` with timing information.
```DeformConv1d``` is the deformable convolution layer designed to imitate ```torch.nn.Conv1d```.
Note: ```DeformConv1d``` does not compute the offset values used in its ```forward(...)``` call. These most be computed outside the layer.

```
import time

# Import layer
from dc1d.nn import DeformConv1d

# Hyperparameters
batch_size = 16
in_channels = 512
out_channels = 512
kernel_size = 16
stride = 1
padding = "same"
dilation = 3
groups = 1
bias = True
length = 128

# Construct layer
model = DeformConv1d(
    in_channels = in_channels,
    out_channels = out_channels,
    kernel_size = kernel_size,
    stride = stride,
    padding = "same",
    dilation = dilation,
    groups = groups,
    bias = True,
    device="cuda"
)

# Generate input sequence
x = torch.rand(batch_size, in_channels, length,requires_grad=True).cuda()
print(x.shape)

# Generate offsets by first computing the desired output length
output_length = x.shape[-1]-dilation*(kernel_size-1)
offsets = nn.Parameter(torch.ones(batch_size, 1, output_length, kernel_size, requires_grad=True, device="cuda"))

# Process the input sequence and time it
start = time.time()
y = model(x, offsets)
end = time.time()

# Print output shape and time taken
print(y.shape)
print("Deformable runtime =",end-start)
```
---
For more detailed examples, the ```nn``` and ```ops``` modules have example usage scripts appended to the bottom of the file inside their ```if __name__ == "__main__":``` clauses. For example one could run 
```
python dc1d/nn.py
```
to compare the runtime of our ```DeformConv1d``` layer against ```torch.nn.Conv1d```.

A class called ```PackedConv1d``` also exists in ```dc1d.nn``` which computes the offsets using a depthwise separable convolutionoperation as detailed in our paper below 

# Papers
Please cite the following if you use this package4
```
@article{ravenscroft2022dtcn,
  title={Deformable Temporal Convolutional Networks for Monaural Noisy Reverberant Speech Separation},
  author={Ravenscroft, William and Goetze, Stefan and Hain, Thomas},
  year={2022}
}
```
