Skip to content

Commit

Permalink
Add linear methods, e.g. bicubic
Browse files Browse the repository at this point in the history
2. Update citations
  • Loading branch information
WolframRhodium committed Dec 21, 2018
1 parent 249d189 commit 03eb63b
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CARN/progressive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Converted from **official** [nmhkahn/NTIRE2018](https://github.com/nmhkahn/NTIRE
---

### Reference
**[1] Ahn, N., Kang, B., & Sohn, K. A. (2018). Image Super-resolution via Progressive Cascading Residual Network. In proceedings of the IEEE Conference on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://openaccess.thecvf.com/content_cvpr_2018_workshops/papers/w13/Ahn_Image_Super-Resolution_via_CVPR_2018_paper.pdf) [[Code]](https://github.com/nmhkahn/NTIRE2018)**
**[1] Ahn, N., Kang, B., & Sohn, K. A. (2018). [Image Super-resolution via Progressive Cascading Residual Network](https://ieeexplore.ieee.org/document/8575275). In the IEEE/CVF Conference on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://openaccess.thecvf.com/content_cvpr_2018_workshops/papers/w13/Ahn_Image_Super-Resolution_via_CVPR_2018_paper.pdf) [[Code]](https://github.com/nmhkahn/NTIRE2018)**

[2] Timofte, R., Gu, S. H., Wu, J. Q., Van Gool, L., Zhang, L., Yang, M. H., ... & Fu, L. Z. (2018). NTIRE 2018 Challenge on Single Image Super-Resolution: Methods and Results. In proceedings of the IEEE Converence on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://people.ee.ethz.ch/~timofter/publications/NTIRE2018_SR_report_CVPRW-2018.pdf) [[Project]](http://www.vision.ee.ethz.ch/ntire18/#challenge)
[2] Timofte, R., Gu, S. H., Wu, J. Q., Van Gool, L., Zhang, L., Yang, M. H., ... & Fu, L. Z. (2018). [NTIRE 2018 Challenge on Single Image Super-Resolution: Methods and Results](https://ieeexplore.ieee.org/document/8575282/). In the IEEE/CVF Converence on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://people.ee.ethz.ch/~timofter/publications/NTIRE2018_SR_report_CVPRW-2018.pdf) [[Project]](http://www.vision.ee.ethz.ch/ntire18/#challenge)
4 changes: 2 additions & 2 deletions DBPN/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Converted from **official** [alterzero/DBPN-caffe](https://github.com/alterzero/
---

### Reference
**[1] Haris, M., Shakhnarovich, G., & Ukita, N. (2018). Deep Back-Projection Networks For Super-Resolution. In proceedings of the IEEE Conference on Computer Vision and Pattern Recognition. [[PDF]](https://www.toyota-ti.ac.jp/Lab/Denshi/iim/members/muhammad.haris/projects/DBPN_cvpr2018_final.pdf) [[Code-Caffe]](https://github.com/alterzero/DBPN-caffe) [[Code-PyTorch]](https://github.com/alterzero/DBPN-Pytorch) [[Supplementary Material]](https://www.toyota-ti.ac.jp/Lab/Denshi/iim/members/muhammad.haris/projects/DBPN_cvpr2018_supp_material.pdf) [[Project]](https://www.toyota-ti.ac.jp/Lab/Denshi/iim/members/muhammad.haris/projects/DBPN.html)**
**[1] Haris, M., Shakhnarovich, G., & Ukita, N. (2018). [Deep Back-Projection Networks For Super-Resolution](https://ieeexplore.ieee.org/document/8578277/). In the IEEE/CVF Conference on Computer Vision and Pattern Recognition. [[PDF]](https://www.toyota-ti.ac.jp/Lab/Denshi/iim/members/muhammad.haris/projects/DBPN_cvpr2018_final.pdf) [[Code-Caffe]](https://github.com/alterzero/DBPN-caffe) [[Code-PyTorch]](https://github.com/alterzero/DBPN-Pytorch) [[Supplementary Material]](https://www.toyota-ti.ac.jp/Lab/Denshi/iim/members/muhammad.haris/projects/DBPN_cvpr2018_supp_material.pdf) [[Project]](https://www.toyota-ti.ac.jp/Lab/Denshi/iim/members/muhammad.haris/projects/DBPN.html)**

[2] Timofte, R., Gu, S. H., Wu, J. Q., Van Gool, L., Zhang, L., Yang, M. H., ... & Fu, L. Z. (2018). NTIRE 2018 Challenge on Single Image Super-Resolution: Methods and Results. In proceedings of the IEEE Converence on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://people.ee.ethz.ch/~timofter/publications/NTIRE2018_SR_report_CVPRW-2018.pdf) [[Project]](http://www.vision.ee.ethz.ch/ntire18/#challenge)
[2] Timofte, R., Gu, S. H., Wu, J. Q., Van Gool, L., Zhang, L., Yang, M. H., ... & Fu, L. Z. (2018). [NTIRE 2018 Challenge on Single Image Super-Resolution: Methods and Results](https://ieeexplore.ieee.org/document/8575282/). In the IEEE/CVF Converence on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://people.ee.ethz.ch/~timofter/publications/NTIRE2018_SR_report_CVPRW-2018.pdf) [[Project]](http://www.vision.ee.ethz.ch/ntire18/#challenge)
2 changes: 1 addition & 1 deletion IDN/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Converted from **official** [Zheng222/IDN-Caffe](https://github.com/Zheng222/IDN
---

### Reference
[1] Hui, Z., Wang, X. & Gao, X. (2018). Fast and Accurate Single Image Super-Resolution via Information Distillation Network. In proceedings of the IEEE Conference on Computer Vision and Pattern Recognition. [[PDF]](http://openaccess.thecvf.com/content_cvpr_2018/papers/Hui_Fast_and_Accurate_CVPR_2018_paper.pdf) [[Code]](https://github.com/Zheng222/IDN-Caffe) [[Poster]](https://github.com/Zheng222/IDN-Caffe/blob/2bcc23e4f66684cb7e1fbb3ceb4a957f830b83e6/files/cvpr18_poster.pdf)
[1] Hui, Z., Wang, X. & Gao, X. (2018). [Fast and Accurate Single Image Super-Resolution via Information Distillation Network](https://ieeexplore.ieee.org/document/8578180/). In the IEEE/CVF Conference on Computer Vision and Pattern Recognition. [[PDF]](http://openaccess.thecvf.com/content_cvpr_2018/papers/Hui_Fast_and_Accurate_CVPR_2018_paper.pdf) [[Code]](https://github.com/Zheng222/IDN-Caffe) [[Poster]](https://github.com/Zheng222/IDN-Caffe/blob/2bcc23e4f66684cb7e1fbb3ceb4a957f830b83e6/files/cvpr18_poster.pdf)
4 changes: 2 additions & 2 deletions ProSR/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Converted from **official** [fperazzi/proSR](https://github.com/fperazzi/proSR/t
---

### Reference
**[1] Wang, Y., Perazzi, F., McWilliams, B., Sorkine-Hornung, A., Sorkine-Hornung, O., & Schroers, C. (2018). A Fully Progressive Approach to Single-Image Super-Resolution. In proceedings of the IEEE Conference on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://igl.ethz.ch/projects/prosr/prosr-cvprw-2018-wang-et-al.pdf) [[Code]](https://github.com/fperazzi/proSR) [[Supplementary Material]](http://igl.ethz.ch/projects/prosr/prosr-supplemental.pdf) [[Video]](http://igl.ethz.ch/projects/prosr/prosr-video.mp4) [[Project]](http://igl.ethz.ch/projects/prosr/)**
**[1] Wang, Y., Perazzi, F., McWilliams, B., Sorkine-Hornung, A., Sorkine-Hornung, O., & Schroers, C. (2018). [A Fully Progressive Approach to Single-Image Super-Resolution](https://ieeexplore.ieee.org/document/8575284/). In the IEEE/CVF Conference on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://igl.ethz.ch/projects/prosr/prosr-cvprw-2018-wang-et-al.pdf) [[Code]](https://github.com/fperazzi/proSR) [[Supplementary Material]](http://igl.ethz.ch/projects/prosr/prosr-supplemental.pdf) [[Video]](http://igl.ethz.ch/projects/prosr/prosr-video.mp4) [[Project]](http://igl.ethz.ch/projects/prosr/)**

[2] Timofte, R., Gu, S. H., Wu, J. Q., Van Gool, L., Zhang, L., Yang, M. H., ... & Fu, L. Z. (2018). NTIRE 2018 Challenge on Single Image Super-Resolution: Methods and Results. In proceedings of the IEEE Converence on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://people.ee.ethz.ch/~timofter/publications/NTIRE2018_SR_report_CVPRW-2018.pdf) [[Project]](http://www.vision.ee.ethz.ch/ntire18/#challenge)
[2] Timofte, R., Gu, S. H., Wu, J. Q., Van Gool, L., Zhang, L., Yang, M. H., ... & Fu, L. Z. (2018). [NTIRE 2018 Challenge on Single Image Super-Resolution: Methods and Results](https://ieeexplore.ieee.org/document/8575282/). In the IEEE/CVF Converence on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://people.ee.ethz.ch/~timofter/publications/NTIRE2018_SR_report_CVPRW-2018.pdf) [[Project]](http://www.vision.ee.ethz.ch/ntire18/#challenge)
2 changes: 1 addition & 1 deletion RDN/Residual-Dense-Network-Caffe@xueshengke/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Converted from [xueshengke/Residual-Dense-Network-Caffe](https://github.com/xues
---

### Reference
[1] Zhang, Y., Tian, Y., Kong, Y., Zhong, B. & Fu, Y. (2018). Residual Dense Network for Image Super-Resolution. In the IEEE Conference on Computer Vision and Pattern Recognition. [[PDF]](http://openaccess.thecvf.com/content_cvpr_2018/papers/Zhang_Residual_Dense_Network_CVPR_2018_paper.pdf) [[Code]](https://github.com/yulunzhang/RDN)
[1] Zhang, Y., Tian, Y., Kong, Y., Zhong, B. & Fu, Y. (2018). [Residual Dense Network for Image Super-Resolution](https://ieeexplore.ieee.org/document/8578360/). In the IEEE/CVF Conference on Computer Vision and Pattern Recognition. [[PDF]](http://openaccess.thecvf.com/content_cvpr_2018/papers/Zhang_Residual_Dense_Network_CVPR_2018_paper.pdf) [[Code]](https://github.com/yulunzhang/RDN)
2 changes: 1 addition & 1 deletion SESR/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Converted from **official** [opteroncx/SESR](https://github.com/opteroncx/SESR/t
---

### Reference
[1] Cheng, X., Li, X., Yang, J. & Tai, Y. (2018). SESR: Single Image Super Resolution with Recursive Squeeze and Excitation Networks. In proceedings of International Conference on Pattern Recognition. [[PDF]](https://arxiv.org/pdf/1801.10319) [[Code]](https://github.com/opteroncx/SESR)
[1] Cheng, X., Li, X., Yang, J. & Tai, Y. (2018). [SESR: Single Image Super Resolution with Recursive Squeeze and Excitation Networks](https://ieeexplore.ieee.org/document/8546130/). International Conference on Pattern Recognition. [[PDF]](https://arxiv.org/pdf/1801.10319) [[Code]](https://github.com/opteroncx/SESR)
2 changes: 1 addition & 1 deletion TSCN/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Converted from **official** [Zheng222/TSCN](https://github.com/Zheng222/TSCN/com
---

### Reference
[1] Hui, Z., Wang, X., & Gao, X. (2018). Two-Stage Convolutional Network for Image Super-Resolution. In proceedings of International Conference on Pattern Recognition. [[PDF]](https://github.com/Zheng222/TSCN/blob/20818c148a6f02a0d5e7c75553528f52dbebcf6a/files/TSCN.pdf) [[Code]](https://github.com/Zheng222/TSCN) [[Poster]](https://github.com/Zheng222/TSCN/raw/master/files/icpr18_poster.pdf)
[1] Hui, Z., Wang, X., & Gao, X. (2018). [Two-Stage Convolutional Network for Image Super-Resolution](https://ieeexplore.ieee.org/document/8545648/). International Conference on Pattern Recognition. [[PDF]](https://github.com/Zheng222/TSCN/blob/20818c148a6f02a0d5e7c75553528f52dbebcf6a/files/TSCN.pdf) [[Code]](https://github.com/Zheng222/TSCN) [[Poster]](https://github.com/Zheng222/TSCN/raw/master/files/icpr18_poster.pdf)
2 changes: 1 addition & 1 deletion WDSR/wdsr@krasserm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Converted from [krasserm/wdsr](https://github.com/krasserm/wdsr/tree/4701c31b8fb
### Reference
**[1] Yu, J. H., Fan, Y. C., Yang, J. C., Xu, N., Wang, Z. W., Wang, X. C. & Huang, T. (2018). Wide Activation for Efficient and Accurate Image Super-Resolution. arXiv preprint arXiv:1808.08718. [[PDF]](https://arxiv.org/pdf/1808.08718) [[Code]](https://github.com/JiahuiYu/wdsr_ntire2018)**

[2] Timofte, R., Gu, S. H., Wu, J. Q., Van Gool, L., Zhang, L., Yang, M. H., ... & Fu, L. Z. (2018). NTIRE 2018 Challenge on Single Image Super-Resolution: Methods and Results. In proceedings of the IEEE Converence on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://people.ee.ethz.ch/~timofter/publications/NTIRE2018_SR_report_CVPRW-2018.pdf) [[Project]](http://www.vision.ee.ethz.ch/ntire18/#challenge)
[2] Timofte, R., Gu, S. H., Wu, J. Q., Van Gool, L., Zhang, L., Yang, M. H., ... & Fu, L. Z. (2018). [NTIRE 2018 Challenge on Single Image Super-Resolution: Methods and Results](https://ieeexplore.ieee.org/document/8575282/). In the IEEE/CVF Converence on Computer Vision and Pattern Recognition Workshops. [[PDF]](http://people.ee.ethz.ch/~timofter/publications/NTIRE2018_SR_report_CVPRW-2018.pdf) [[Project]](http://www.vision.ee.ethz.ch/ntire18/#challenge)
Binary file added linear_method/bicubic_2x-0000.params
Binary file not shown.
83 changes: 83 additions & 0 deletions linear_method/bicubic_2x-symbol.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"nodes": [
{
"op": "null",
"name": "data",
"inputs": []
},
{
"op": "Pad",
"name": "pad0",
"attrs": {
"mode": "edge",
"pad_width": "(0, 0, 0, 0, 2, 2, 0, 0)"
},
"inputs": [[0, 0, 0]]
},
{
"op": "null",
"name": "bicubic_2x_h_weight",
"attrs": {
"kernel": "(8, 1)",
"no_bias": "True",
"num_filter": "1",
"num_group": "1",
"pad": "(7, 0)",
"stride": "(2, 1)"
},
"inputs": []
},
{
"op": "Deconvolution",
"name": "bicubic_2x_h",
"attrs": {
"kernel": "(8, 1)",
"no_bias": "True",
"num_filter": "1",
"num_group": "1",
"pad": "(7, 0)",
"stride": "(2, 1)"
},
"inputs": [[1, 0, 0], [2, 0, 0]]
},
{
"op": "Pad",
"name": "pad1",
"attrs": {
"mode": "edge",
"pad_width": "(0, 0, 0, 0, 0, 0, 2, 2)"
},
"inputs": [[3, 0, 0]]
},
{
"op": "null",
"name": "bicubic_2x_w_weight",
"attrs": {
"kernel": "(1, 8)",
"no_bias": "True",
"num_filter": "1",
"num_group": "1",
"pad": "(0, 7)",
"stride": "(1, 2)"
},
"inputs": []
},
{
"op": "Deconvolution",
"name": "bicubic_2x_w",
"attrs": {
"kernel": "(1, 8)",
"no_bias": "True",
"num_filter": "1",
"num_group": "1",
"pad": "(0, 7)",
"stride": "(1, 2)"
},
"inputs": [[4, 0, 0], [5, 0, 0]]
}
],
"arg_nodes": [0, 2, 5],
"node_row_ptr": [0, 1, 2, 3, 4, 5, 6, 7],
"heads": [[6, 0, 0]],
"attrs": {"mxnet_version": ["int", 10300]}
}
71 changes: 71 additions & 0 deletions linear_method/example_produce_resizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import mxnet as mx
from resize_weight import resize_weight

# a script that produces a (center-aligned) linear interpolation resizer in the form of CNN in MXNet

# params
scale = 2
kernel = 'bicubic'
channels = 1
downsample = False

if not downsample:
name = '{}_{}x'.format(kernel, scale)

weight_1d = resize_weight(scale=scale, kernel=kernel)
width = len(weight_1d)
taps = (width + 1) // (2 * scale)

# tries to fix the boundary condition via external padding, but the value here might not be accurate
pad = scale // 2 + (2 * taps - 1) * scale

# Defining the network
# Since the interpolations are separable, we implemented them via spatial separable convolutions
# It's straightforward to modify the code to obtain a single convolution implementation
data = mx.symbol.Variable('data') # assumes NCHW data format
upsample_h = mx.symbol.pad(data, mode='edge', pad_width=(0, 0, 0, 0, taps, taps, 0, 0))
upsample_h = mx.symbol.Deconvolution(upsample_h, kernel=(width, 1), stride=(scale, 1), pad=(pad, 0),
num_filter=channels, num_group=channels, no_bias=True, name='{}_h'.format(name))
upsample_w = mx.symbol.pad(upsample_h, mode='edge', pad_width=(0, 0, 0, 0, 0, 0, taps, taps))
upsample_w = mx.symbol.Deconvolution(upsample_w, kernel=(1, width), stride=(1, scale), pad=(0, pad),
num_filter=channels, num_group=channels, no_bias=True, name='{}_w'.format(name))

# Loading weights
net = mx.gluon.SymbolBlock(outputs=upsample_w, inputs=data)
net_params = net.collect_params()
net_params['{}_h_weight'.format(name)]._load_init(mx.nd.array(weight_1d.reshape((1, 1, width, 1))), ctx=mx.cpu())
net_params['{}_w_weight'.format(name)]._load_init(mx.nd.array(weight_1d.reshape((1, 1, 1, width))), ctx=mx.cpu())
net.hybridize()
test_data = mx.nd.ones((1, channels, 96, 96))
net.forward(test_data)

# Output
net.export(name)

else:
name = '{}_{}x_downsample'.format(kernel, scale)

weight_1d = resize_weight(scale=scale, kernel=kernel, downsample=True)
width = len(weight_1d)
taps = (width + 1) // (2 * scale)
pad = scale // 2 + (2 * taps - 1) * scale

data = mx.symbol.Variable('data')
upsample_h = mx.symbol.pad(data, mode='edge', pad_width=(0, 0, 0, 0, 2 * taps - 1, 2 * taps, 0, 0))
upsample_h = mx.symbol.Deconvolution(upsample_h, kernel=(width, 1), stride=(scale, 1), pad=(pad, 0),
num_filter=channels, num_group=channels, no_bias=True, name='{}_h'.format(name))
upsample_w = mx.symbol.pad(upsample_h, mode='edge', pad_width=(0, 0, 0, 0, 0, 0, 2 * taps - 1, 2 * taps - 1))
upsample_w = mx.symbol.Deconvolution(upsample_w, kernel=(1, width), stride=(1, scale), pad=(0, pad),
num_filter=channels, num_group=channels, no_bias=True, name='{}_w'.format(name))

net = mx.gluon.SymbolBlock(outputs=upsample_w, inputs=data)
net_params = net.collect_params()
net_params['{}_h_weight'.format(name)]._load_init(mx.nd.array(weight_1d.reshape((1, 1, width, 1))), ctx=mx.cpu())
net_params['{}_w_weight'.format(name)]._load_init(mx.nd.array(weight_1d.reshape((1, 1, 1, width))), ctx=mx.cpu())

net.hybridize()

test_data = mx.nd.ones((1, channels, 96, 96))
net.forward(test_data)

net.export(name)
114 changes: 114 additions & 0 deletions linear_method/resize_weight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import numpy as np
import functools

_vectorize_fp64 = functools.partial(np.vectorize, otypes=[np.float64])

@_vectorize_fp64
def box(x):
"""matlab.images.internal.resize.box"""

return -0.5 <= x < 0.5

@_vectorize_fp64
def triangle(x):
"""matlab.images.internal.resize.triangle"""

return (x + 1) * (-1 <= x < 0) + (1 - x) * (0 <= x <= 1)

@_vectorize_fp64
def cubic(x):
"""matlab.images.internal.resize.cubic"""

absx = abs(x)
absx2 = absx ** 2
absx3 = absx ** 3

return ((1.5 * absx3 - 2.5 * absx2 + 1) * (absx <= 1) +
(-0.5 * absx3 + 2.5 * absx2 - 4 * absx + 2) * (1 < absx <= 2))

@_vectorize_fp64
def lanczos2(x):
"""matlab.images.internal.resize.lanczos2"""

eps = np.finfo(np.float64).eps

return (np.sin(np.pi * x) * np.sin(np.pi * x / 2) + eps) / ((np.pi ** 2 * x ** 2 / 2) + eps) * (abs(x) < 2)

@_vectorize_fp64
def lanczos3(x):
"""matlab.images.internal.resize.lanczos2"""

eps = np.finfo(np.float64).eps

return (np.sin(np.pi * x) * np.sin(np.pi * x / 3) + eps) / ((np.pi ** 2 * x ** 2 / 3) + eps) * (abs(x) < 3)


def resize_weight(scale=2, kernel=None, downsample=False):
"""A function that outputs weights for a specified resizer
This function assumes center-aligned interpolation.
Args:
scale: (int) Resize factor. The type of value for downscale is also an integer.
Default is 2.
kernel: (str) Interpolation method.
Available methods: box, bilinear(triangle), bicubic(cubic), lanczos2, lanczos3.
Default is 'bicubic'.
downsample: (bool) Whether the weights are used for downsample instead of upsample.
Default is False.
Ref:
[1] P. Getreuer. (2011). Linear Methods for Image Interpolation. Image Processing On Line, 1, pp. 238–259.
"""

if not isinstance(scale, int) or scale <= 1:
raise ValueError('\'scale\' must be an integer greater than 1!')

if kernel is None:
kernel = 'cubic'
else:
kernel = kernel.lower()

if kernel == 'bicubic':
kernel = 'cubic'

if kernel == 'bilinear':
kernel = 'triangle'

if kernel in ['box', 'triangle']:
taps = 1
elif kernel in ['cubic', 'lanczos2']:
taps = 2
elif kernel in ['lanczos3']:
taps = 3
else:
raise ValueError('Unknown resize kernel {}!'.format(kernel))

func = eval(f'{kernel}')

if scale % 2 == 0: # scale is even
kernel_width = 2 * scale * taps
weight_1d = np.empty(kernel_width, dtype=np.float64)
weight_1d[kernel_width//2:] = func([(2 * i + 1) / (2 * scale) for i in range(kernel_width // 2)])
weight_1d[:kernel_width//2] = weight_1d[:kernel_width//2-1:-1] # mirror

else: # scale is odd
kernel_width = 2 * scale * taps - 1
weight_1d = np.empty(kernel_width, dtype=np.float64)
weight_1d[kernel_width//2:] = func([(i / scale) for i in range(kernel_width // 2 + 1)])
weight_1d[:kernel_width//2] = weight_1d[:kernel_width//2:-1] # mirror

# kernel normalization:
for i in range(scale):
weight_1d[i::scale] /= weight_1d[i::scale].sum()

if downsample:
weight_1d /= scale

if kernel in ['box']:
weight_1d = weight_1d[scale//2:-scale//2]

return weight_1d
Loading

0 comments on commit 03eb63b

Please sign in to comment.