Last active
November 27, 2019 22:05
-
-
Save vmarkovtsev/56d7df571e3b92f8fe83764a9762f1fc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def create_lanczos_resize_func_edgetpu(images_shape: Tuple[int], dim: int, factor: int, edgetpu=True): | |
name = "lanczos_resize_%s_%d_%d" % ("_".join(map(str, images_shape)), dim, factor) | |
ctor = lambda: create_lanczos_resize_func(images_shape, dim, factor) | |
return create_func_edgetpu(images_shape, ctor, name, edgetpu) | |
def create_lanczos_resize_func(images_shape: Tuple[int], dim: int, factor: int): | |
kernel = create_lanczos_resize_kernel(dim) | |
@tf.function(input_signature=[tf.TensorSpec(images_shape, tf.float32)]) | |
def lanczos_func(images): | |
# Note how we stride: every `factor` pixel is chosen | |
return tf.nn.depthwise_conv2d(images, kernel, strides=[1, factor, factor, 1], padding="SAME") | |
return lanczos_func | |
def create_lanczos_resize_kernel(dim: int): | |
x = np.linspace(-dim, dim, dim * 2 + 1) | |
y = np.linspace(-dim, dim, dim * 2 + 1) | |
mx, my = np.meshgrid(x, y) | |
# Our "constant" is 3 | |
kernel = np.sinc(mx / (dim / 3))*np.sinc(mx / dim) * np.sinc(my / (dim / 3)) * np.sinc(my / dim) | |
kernel /= kernel.sum() | |
return tf.tile(tf.constant(kernel.astype(np.float32))[:, :, None, None], [1, 1, 3, 1]) | |
lanczos_resize = create_lanczos_resize_func(images.shape, 5, 2) | |
save_image(lanczos_resize(images), "result_origin.jpg") | |
lanczos_resize = create_lanczos_resize_func_edgetpu(images.shape, 5, 2) | |
save_image(lanczos_resize(images), "result_edgetpu.jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment