mirror of
https://github.com/iperov/DeepFaceLive
synced 2025-08-20 21:43:22 -07:00
add xlib.avecl
This commit is contained in:
parent
932edfe875
commit
0058474da7
56 changed files with 5569 additions and 0 deletions
39
xlib/avecl/_internal/op/gaussian_blur.py
Normal file
39
xlib/avecl/_internal/op/gaussian_blur.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
import numpy as np
|
||||
|
||||
from ..Tensor import Tensor
|
||||
from .depthwise_conv2D import depthwise_conv2D
|
||||
|
||||
|
||||
def gaussian_blur (input_t : Tensor, sigma, dtype=None) -> Tensor:
|
||||
"""
|
||||
arguments
|
||||
|
||||
input_t Tensor(...,H,W)
|
||||
|
||||
sigma float
|
||||
|
||||
|
||||
"""
|
||||
if sigma <= 0.0:
|
||||
return input_t.copy() #TODO
|
||||
|
||||
device = input_t.get_device()
|
||||
|
||||
key = (gaussian_blur, sigma)
|
||||
kernel_t = device.get_cached_data(key)
|
||||
if kernel_t is None:
|
||||
kernel_t = Tensor.from_value( _make_gaussian_kernel(sigma, np.float32), device=device )
|
||||
device.set_cached_data(key, kernel_t)
|
||||
|
||||
output_t = depthwise_conv2D(input_t, kernel_t, dtype=dtype)
|
||||
return output_t
|
||||
|
||||
def _make_gaussian_kernel(sigma : float, dtype):
|
||||
kernel_size = max(3, int(2 * 2 * sigma))
|
||||
if kernel_size % 2 == 0:
|
||||
kernel_size += 1
|
||||
mean = np.floor(0.5 * kernel_size)
|
||||
kernel_1d = np.array([ np.exp(-(float(x) - float(mean)) ** 2 / (2 * sigma ** 2)) for x in range(kernel_size)])
|
||||
np_kernel = np.outer(kernel_1d, kernel_1d)
|
||||
kernel = np_kernel / np.sum(np_kernel)
|
||||
return kernel.astype(dtype)
|
Loading…
Add table
Add a link
Reference in a new issue