Metadata-Version: 2.1
Name: s2d2s
Version: 1
Summary: Depth-to-Space and Space-to-Depth PyTorch implementation compatible with onnx
Author: lnstadrum
License: MIT License
        
        Copyright (c) 2023 lnstadrum
        
        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.
        
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Description-Content-Type: text/markdown
License-File: LICENSE

# Overview

`PixelShuffle`/`PixelUnshuffle` in PyTorch and `depth_to_space`/`space_to_depth` in TensorFlow are very similar, but they are not numerically identical unless the upsampled image contains a single channel. This can be easyly verified by hand: while the tensor dimensions match between the PyTorch and TensorFlow worlds, the output channels do not follow the same order.

However, in some deployment setups, there might be performance benefits to using the space-to-depth/depth-to-space variant. For example, at the moment of writing, [Android NN API](https://developer.android.com/ndk/guides/neuralnetworks) only supports depth-to-space and space-to-depth.

This repository provides a unit-tested space-to-depth/depth-to-space implementation for PyTorch, supporting conversion to [SpaceToDepth](http://www.xavierdupre.fr/app/mlprodict/helpsphinx/onnxops/onnx__SpaceToDepth.html)/[DepthToSpace](http://www.xavierdupre.fr/app/mlprodict/helpsphinx/onnxops/onnx__DepthToSpace.html) ONNX ops when exporting the model as an ONNX graph for further deployment.

The provided implementation follows channels-first PyTorch standard, allowing for arbitrary number of outer dimensions, i.e. it supports tensors of `[..., C, H, W]` layouts.

# Installation

```
python3 -m pip install s2d2s
```

# Usage

Both functional implementations and `nn.Module`s are available and can be used as follows:

```
from s2d2s import space_to_depth, depth_to_space

y = space_to_depth(x, 2)
```

```
from s2d2s import SpaceToDepth, DepthToSpace

module = SpaceToDepth(2)
y = module(x)
```
