-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fda5bf
commit edf2cc4
Showing
10 changed files
with
257 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# ------------------------------------------------------------------------ # | ||
# Copyright 2022 SPTK Working Group # | ||
# # | ||
# Licensed under the Apache License, Version 2.0 (the "License"); # | ||
# you may not use this file except in compliance with the License. # | ||
# You may obtain a copy of the License at # | ||
# # | ||
# http://www.apache.org/licenses/LICENSE-2.0 # | ||
# # | ||
# Unless required by applicable law or agreed to in writing, software # | ||
# distributed under the License is distributed on an "AS IS" BASIS, # | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # | ||
# See the License for the specific language governing permissions and # | ||
# limitations under the License. # | ||
# ------------------------------------------------------------------------ # | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
from ..misc.utils import to | ||
|
||
|
||
class Histogram(nn.Module): | ||
"""See `this page <https://sp-nitech.github.io/sptk/latest/main/histogram.html>`_ | ||
for details. | ||
Parameters | ||
---------- | ||
n_bin : int >= 1 | ||
Number of bins, :math:`K`. | ||
lower_bound : float < U | ||
Lower bound of the histogram, :math:`L`. | ||
upper_bound : float > L | ||
Upper bound of the histogram, :math:`U`. | ||
norm : bool | ||
If True, normalize the histogram. | ||
softness : float > 0 | ||
A smoothing parameter. The smaller value makes the output closer to the true | ||
histogram, but the gradient vanishes. | ||
References | ||
---------- | ||
.. [1] M. Avi-Aharon et al., "DeepHist: Differentiable joint and color histogram | ||
layers for image-to-image translation," *arXiv preprint arXiv:2005.03995*, | ||
2020. | ||
""" | ||
|
||
def __init__( | ||
self, n_bin=10, lower_bound=0, upper_bound=1, norm=False, softness=1e-3 | ||
): | ||
super(Histogram, self).__init__() | ||
|
||
assert 1 <= n_bin | ||
assert lower_bound < upper_bound | ||
assert 0 < softness | ||
|
||
self.norm = norm | ||
self.softness = softness | ||
|
||
centers = self._precompute(n_bin, lower_bound, upper_bound) | ||
self.register_buffer("centers", centers) | ||
|
||
def forward(self, x): | ||
"""Compute histogram. | ||
Parameters | ||
---------- | ||
x : Tensor [shape=(..., T)] | ||
Input data. | ||
Returns | ||
------- | ||
out : Tensor [shape=(..., K)] | ||
Histogram. | ||
Examples | ||
-------- | ||
>>> x = diffsptk.ramp(9) | ||
>>> histogram = diffsptk.Histogram(n_bin=4, lower_bound=-0.1, upper_bound=9.1) | ||
>>> h = histogram(x) | ||
>>> h | ||
tensor([3., 2., 2., 3.]) | ||
""" | ||
return self._forward(x, self.norm, self.softness, self.centers) | ||
|
||
@staticmethod | ||
def _forward(x, norm, softness, centers): | ||
y = x.unsqueeze(-2) - centers.unsqueeze(-1) # (..., K, T) | ||
g = 0.5 * (centers[1] - centers[0]) | ||
h = torch.sigmoid((y + g) / softness) - torch.sigmoid((y - g) / softness) | ||
h = h.sum(-1) | ||
if norm: | ||
h /= h.sum(-1, keepdim=True) | ||
return h | ||
|
||
@staticmethod | ||
def _func(x, n_bin, lower_bound, upper_bound, norm, softness): | ||
centers = Histogram._precompute( | ||
n_bin, lower_bound, upper_bound, dtype=x.dtype, device=x.device | ||
) | ||
return Histogram._forward(x, norm, softness, centers) | ||
|
||
@staticmethod | ||
def _precompute(n_bin, lower_bound, upper_bound, dtype=None, device=None): | ||
width = (upper_bound - lower_bound) / n_bin | ||
bias = lower_bound + 0.5 * width | ||
centers = torch.arange(n_bin, dtype=torch.double, device=device) * width + bias | ||
return to(centers, dtype=dtype) |
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 |
---|---|---|
|
@@ -7,3 +7,5 @@ entropy | |
:members: | ||
|
||
.. autofunction:: diffsptk.functional.entropy | ||
|
||
.. seealso:: :ref:`histogram` |
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,11 @@ | ||
.. _histogram: | ||
|
||
histogram | ||
--------- | ||
|
||
.. autoclass:: diffsptk.Histogram | ||
:members: | ||
|
||
.. autofunction:: diffsptk.functional.histogram | ||
|
||
.. seealso:: :ref:`entropy` |
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,60 @@ | ||
# ------------------------------------------------------------------------ # | ||
# Copyright 2022 SPTK Working Group # | ||
# # | ||
# Licensed under the Apache License, Version 2.0 (the "License"); # | ||
# you may not use this file except in compliance with the License. # | ||
# You may obtain a copy of the License at # | ||
# # | ||
# http://www.apache.org/licenses/LICENSE-2.0 # | ||
# # | ||
# Unless required by applicable law or agreed to in writing, software # | ||
# distributed under the License is distributed on an "AS IS" BASIS, # | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # | ||
# See the License for the specific language governing permissions and # | ||
# limitations under the License. # | ||
# ------------------------------------------------------------------------ # | ||
|
||
import pytest | ||
import torch | ||
|
||
import diffsptk | ||
import tests.utils as U | ||
|
||
|
||
@pytest.mark.parametrize("device", ["cpu", "cuda"]) | ||
@pytest.mark.parametrize("module", [False, True]) | ||
@pytest.mark.parametrize("norm", [False, True]) | ||
def test_compatibility( | ||
device, module, norm, K=4, lower_bound=-1, upper_bound=1, L=50, B=2 | ||
): | ||
histogram = U.choice( | ||
module, | ||
diffsptk.Histogram, | ||
diffsptk.functional.histogram, | ||
{}, | ||
{ | ||
"n_bin": K, | ||
"lower_bound": lower_bound, | ||
"upper_bound": upper_bound, | ||
"norm": norm, | ||
"softness": 1e-4, | ||
}, | ||
) | ||
|
||
opt = "-n" if norm else "" | ||
U.check_compatibility( | ||
device, | ||
histogram, | ||
[], | ||
[f"nrand -l {B*L}"], | ||
f"histogram -t {L} -b {K} -l {lower_bound} -u {upper_bound} {opt}", | ||
[], | ||
dx=L, | ||
dy=K, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("device", ["cpu", "cuda"]) | ||
def test_differentiability(device, K=4, L=50): | ||
histogram = diffsptk.Histogram(K, lower_bound=-1, upper_bound=1, softness=1e-1) | ||
U.check_differentiability(device, histogram, [L]) |
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