-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Added stack upsampling * test: Added unittest for functional form * feat: Added upsample modules * feat: Added module version * test: Extended unittest * docs: Added module to documentation * docs: Updated documentation * style: Fixed lint
- Loading branch information
Showing
13 changed files
with
146 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
from .dropblock import * | ||
from .attention import * | ||
from .lambda_layer import * | ||
from .upsample import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright (C) 2019-2021, François-Guillaume Fernandez. | ||
|
||
# This program is licensed under the Apache License version 2. | ||
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details. | ||
|
||
from torch import Tensor | ||
import torch.nn as nn | ||
from .. import functional as F | ||
|
||
__all__ = ['StackUpsample2d'] | ||
|
||
|
||
class StackUpsample2d(nn.Module): | ||
"""Implements a loss-less upsampling operation described in `"Real-Time Single Image and Video Super-Resolution | ||
Using an Efficient Sub-Pixel Convolutional Neural Network" <https://arxiv.org/pdf/1609.05158.pdf>`_ | ||
by unstacking the channel axis into adjacent information. | ||
.. image:: https://docs.fast.ai/images/pixelshuffle.png | ||
:align: center | ||
Args: | ||
scale_factor (int): spatial scaling factor | ||
""" | ||
|
||
def __init__(self, scale_factor: int) -> None: | ||
super().__init__() | ||
self.scale_factor = scale_factor | ||
|
||
def forward(self, x: Tensor) -> Tensor: | ||
|
||
return F.stack_upsample2d(x, self.scale_factor) | ||
|
||
def extra_repr(self) -> str: | ||
return f"scale_factor={self.scale_factor}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright (C) 2019-2021, François-Guillaume Fernandez. | ||
|
||
# This program is licensed under the Apache License version 2. | ||
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details. | ||
|
||
import pytest | ||
import torch | ||
from holocron.nn.modules import upsample | ||
from holocron.nn import functional as F | ||
|
||
|
||
def test_stackupsample2d(): | ||
|
||
num_batches = 2 | ||
num_chan = 4 | ||
x = torch.arange(num_batches * num_chan * 4 ** 2).view(num_batches, num_chan, 4, 4) | ||
|
||
# Test functional API | ||
with pytest.raises(AssertionError): | ||
F.stack_upsample2d(x, 3) | ||
|
||
# Check that it's the inverse of concat_downsample2d | ||
x = torch.rand((num_batches, num_chan, 32, 32)) | ||
down = F.concat_downsample2d(x, scale_factor=2) | ||
up = F.stack_upsample2d(down, scale_factor=2) | ||
assert torch.equal(up, x) | ||
|
||
# module interface | ||
mod = upsample.StackUpsample2d(scale_factor=2) | ||
assert torch.equal(mod(down), up) | ||
assert repr(mod) == "StackUpsample2d(scale_factor=2)" |