Skip to content

Instantly share code, notes, and snippets.

@vmarkovtsev
Last active November 27, 2019 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmarkovtsev/56d7df571e3b92f8fe83764a9762f1fc to your computer and use it in GitHub Desktop.
Save vmarkovtsev/56d7df571e3b92f8fe83764a9762f1fc to your computer and use it in GitHub Desktop.
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