Skip to content

Instantly share code, notes, and snippets.

Avatar

Vadim Markovtsev vmarkovtsev

View GitHub Profile
View add_output_pixel_clipping.py
def save_image(tensor: Union[np.ndarray, tf.Tensor], path: str):
if hasattr(tensor, "numpy"):
tensor = tensor.numpy()
tensor = np.clip(tensor, 0, 255) # <<< this is new
Image.fromarray(np.squeeze(tensor).astype(np.uint8), "RGB").save(path)
View lanczos_resize_edgetpu_fixed.py
# Remember to remove the old model!
# rm *.tflite
from typing import Optional
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)
View visualize_lanczos_kernel.py
# The kernel size is 15 instead of 5 for greater detail.
dim = 15
kernel = np.squeeze(create_lanczos_resize_kernel(dim).numpy()[:, :, 0, 0])
plt.contourf(np.linspace(-dim, dim, dim * 2 + 1), np.linspace(-dim, dim, dim * 2 + 1), kernel)
plt.colorbar()
plt.show()
View lanczos_resize_edgetpu.py
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)
View motion_blur_edgetpu_fixed.py
# Remember to remove the old model!
# rm *.tflite
import json
import urllib.request
def generate_edgetpu_model(log: logging.Logger, images_shape: Tuple[int], func: callable, name: str):
"""Convert tf.function to Edge TPU model."""
View motion_blur_edgetpu.py
# Remember to remove the old model!
# rm *.tflite
import subprocess
from tensorflow.lite.python.interpreter import load_delegate
def create_motion_blur_func_edgetpu(images_shape, dim, angle, edgetpu=True):
name = "motion_blur_%s_%d_%.2f" % ("_".join(map(str, images_shape)), dim, angle)
ctor = lambda: create_motion_blur_func(images_shape, dim, angle)
View motion_blur_tflite.py
import logging
from pathlib import Path
def create_motion_blur_func(images_shape: Tuple[int], dim: int, angle: float):
kernel = create_motion_blur_kernel(dim, angle)
# This is new: input signature definition
@tf.function(input_signature=[tf.TensorSpec(images_shape, tf.float32)])
def motion_blur_func(images):
View motion_blur_with_tf.function.py
from PIL import Image
def create_motion_blur_func(dim: int, angle: float):
kernel = create_motion_blur_kernel(dim, angle)
def motion_blur_func(images):
return tf.nn.depthwise_conv2d(images, kernel, strides=[1] * 4, padding="SAME")
return motion_blur_func
View visualize_motion_blur_kernel.py
import matplotlib.pyplot as plt
plt.rcParams["image.cmap"] = "hot"
# 25x25 kernel pointing straight to the east
dim = 25
kernel = np.squeeze(create_motion_blur_kernel(dim, (90/180) * np.pi).numpy()[:, :, 0, 0])
plt.contourf(np.linspace(-dim//2, dim//2, dim), np.linspace(-dim//2, dim//2, dim), kernel)
plt.colorbar()
plt.show()
View create_motion_blur_kernel.py
from typing import Tuple, Union
import numpy as np
from skimage.draw import line_aa
import tensorflow as tf
def create_motion_blur_kernel(dim: int, angle: float) -> tf.Tensor:
# Define a disk which contains the dim x dim square
radius = np.sqrt(0.5 * dim**2)
You can’t perform that action at this time.