-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
280 additions
and
12 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
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
Binary file not shown.
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,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]} | ||
} |
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,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) |
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 @@ | ||
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 |
Oops, something went wrong.