Skip to content

Instantly share code, notes, and snippets.

View vmarkovtsev's full-sized avatar

Vadim Markovtsev vmarkovtsev

View GitHub Profile
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
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):
# 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)
# 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()
# 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)
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)
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)
#!/bin/bash
# License: Public Domain.
echo "export PYSPARK_PYTHON=python3" | tee -a /etc/profile.d/spark_config.sh /etc/*bashrc /usr/lib/spark/conf/spark-env.sh
echo "export PYTHONHASHSEED=0" | tee -a /etc/profile.d/spark_config.sh /etc/*bashrc /usr/lib/spark/conf/spark-env.sh
echo "spark.executorEnv.PYTHONHASHSEED=0" >> /etc/spark/conf/spark-defaults.conf
# Only run on the master node
ROLE=$(/usr/share/google/get_metadata_value attributes/dataproc-role)
if [[ "${ROLE}" == 'Master' ]]; then
@vmarkovtsev
vmarkovtsev / README.md
Last active January 21, 2021 12:52
How to configure bash to notify finished commands when unfocused

Open ~/.bashrc. Append export WINDOWID=... in the header as follows:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
 *i*) ;;
# 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."""