Last active
December 6, 2022 06:09
-
-
Save curegit/bbfd2efef553f3dd1a71dd42e56bc63f to your computer and use it in GitHub Desktop.
Chainer で Lanczos 補間による画像二倍縮小層の実装
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
import numpy as np | |
from chainer import Link | |
from chainer.functions import pad, convolution_2d | |
def lanczos(x, n): | |
return 0.0 if abs(x) > n else np.sinc(x) * np.sinc(x / n) | |
class Lanczos2xDownsampler(Link): | |
def __init__(self, n=3): | |
super().__init__() | |
ys = np.array([lanczos(i / 2 + 0.25, n) for i in range(-2 * n, 2 * n)]) | |
ys = ys / np.sum(ys) | |
k = ys.reshape(1, n * 4) * ys.reshape(n * 4, 1) | |
self.w = np.array([[k]], dtype=np.float32) | |
self.n = n | |
def __call__(self, x): | |
b, c, h, w = x.shape | |
p = self.n * 2 - 1 | |
h1 = x.reshape(b * c, 1, h, w) | |
h2 = pad(h1, ((0, 0), (0, 0), (p, p), (p, p)), mode="symmetric") | |
h3 = convolution_2d(h2, self.xp.array(self.w), stride=2) | |
return h3.reshape(b, c, h // 2, w // 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment