Files
torzdf 837bc2d51d Faceswap 3 (#1516)
* FaceSwap 3 (#1515)
* Update extract pipeline
* Update requirements + setup for nvidia
* Remove allow-growth option
* tf.keras to keras updates
* lib.model.losses - Port + fix all loss functions for Keras3
* lib.model - port initializers, layers. normalization to Keras3
* lib.model.autoclip to Keras 3
* Update mixed precision layer storage
* model file to .keras format
* Restructure nn_blocks to initialize layers in __init__
* Tensorboard
  - Trainer: Add Torch compatible Tensorboard callbacks
  - GUI event reader remove TF dependency
* Loss logging
  - Flush TB logs on save
  - Replace TB live iterator for GUI
* Backup models on total loss drop rather than per side
* Update all models to Keras3 Compat
* Remove lib.model.session
* Update clip ViT to Keras 3
* plugins.extract.mask.unet-dfl - Fix for Keras3/Torch backend
* Port AdaBelief to Keras 3
* setup.py:
  - Add --dev flag for dev tool install
* Fix Keras 3 syntax
* Fix LR Finder for Keras 3
* Fix mixed precision switching for Keras  3
* Add more optimizers + open up config setting
* train: Remove updating FS1 weights to FS2 models
* Alignments: Remove support for legacy .json files
* tools.model:
  - Remove TF Saved Format saving
  - Fix Backup/Restore + Nan-Scan
* Fix inference model creation for Keras 3
* Preview tool: Fix for Keras3
* setup.py: Configure keras backend
* train: Migration of FS2 models to FS3
* Training: Default coverage to 100%
* Remove DirectML backend
* Update setup for MacOS
* GUI: Force line reading to UTF-8
* Remove redundant Tensorflow references
* Remove redundant code
* Legacy model loading: Fix TFLamdaOp scalar ops and DepthwiseConv2D
* Add vertical offset option for training
* Github actions: Add more python versions
* Add python version to workflow names
* Github workflow: Exclude Python 3.12 for macOS
* Implement custom training loop
* Fs3 - Add RTX5xxx and ROCm 6.1-6.4 support (#1511)
* setup.py: Add Cuda/ROCm version select options
* bump minimum python version to 3.11
* Switch from setup.cgf to pyproject.toml
* Documentation: Update all docs to use automodapi
* Allow sysinfo to run with missing packages + correctly install tk under Linux
* Bugfix: dot naming convention in clip models
* lib.config: Centralise globally rather than passing as object
- Add torch DataParallel for multi-gpu training
  - GUI: Group switches together when generating cli args
  - CLI: Remove deprecated multi-character argparse args
  - Refactor:
    - Centralise tensorboard reading/writing + unit tests
    - Create trainer plugin interfaces + add original + distributed
* Update installers
2025-12-21 02:45:11 +00:00

180 lines
7.1 KiB
Python

#!/usr/bin python3
""" Calculate Exponential Moving Average for faceswap GUI Stats. """
import logging
import numpy as np
from lib.logger import parse_class_init
from lib.utils import get_module_objects
logger = logging.getLogger(__name__)
class ExponentialMovingAverage:
""" Reshapes data before calculating exponential moving average, then iterates once over the
rows to calculate the offset without precision issues.
Parameters
----------
data : :class:`numpy.ndarray`
A 1 dimensional numpy array to obtain smoothed data for
amount : float
in the range (0.0, 1.0) The alpha parameter (smoothing amount) for the moving average.
Notes
-----
Adapted from: https://stackoverflow.com/questions/42869495
"""
def __init__(self, data: np.ndarray, amount: float) -> None:
logger.debug(parse_class_init(locals()))
assert data.ndim == 1
amount = min(max(amount, 0.001), 0.999)
self._data = np.nan_to_num(data)
self._alpha = 1. - amount
self._dtype = "float32" if data.dtype == np.float32 else "float64"
self._row_size = self._get_max_row_size()
self._out = np.empty_like(data, dtype=self._dtype)
logger.debug("Initialized %s", self.__class__.__name__)
def __call__(self) -> np.ndarray:
""" Perform the exponential moving average calculation.
Returns
-------
:class:`numpy.ndarray`
The smoothed data
"""
if self._data.size <= self._row_size:
self._ewma_vectorized(self._data, self._out) # Normal function can handle this input
else:
self._ewma_vectorized_safe() # Use the safe version
return self._out
def _get_max_row_size(self) -> int:
""" Calculate the maximum row size for the running platform for the given dtype.
Returns
-------
int
The maximum row size possible on the running platform for the given :attr:`_dtype`
Notes
-----
Might not be the optimal value for speed, which is hard to predict due to numpy
optimizations.
"""
# Use :func:`np.finfo(dtype).eps` if you are worried about accuracy and want to be safe.
epsilon = np.finfo(self._dtype).tiny
# If this produces an OverflowError, make epsilon larger:
retval = int(np.log(epsilon) / np.log(1 - self._alpha)) + 1
logger.debug("row_size: %s", retval)
return retval
def _ewma_vectorized_safe(self) -> None:
""" Perform the vectorized exponential moving average in a safe way. """
num_rows = int(self._data.size // self._row_size) # the number of rows to use
leftover = int(self._data.size % self._row_size) # the amount of data leftover
first_offset = self._data[0]
if leftover > 0:
# set temporary results to slice view of out parameter
out_main_view = np.reshape(self._out[:-leftover], (num_rows, self._row_size))
data_main_view = np.reshape(self._data[:-leftover], (num_rows, self._row_size))
else:
out_main_view = self._out.reshape(-1, self._row_size)
data_main_view = self._data.reshape(-1, self._row_size)
self._ewma_vectorized_2d(data_main_view, out_main_view) # get the scaled cumulative sums
scaling_factors = (1 - self._alpha) ** np.arange(1, self._row_size + 1)
last_scaling_factor = scaling_factors[-1]
# create offset array
offsets = np.empty(out_main_view.shape[0], dtype=self._dtype)
offsets[0] = first_offset
# iteratively calculate offset for each row
for i in range(1, out_main_view.shape[0]):
offsets[i] = offsets[i - 1] * last_scaling_factor + out_main_view[i - 1, -1]
# add the offsets to the result
out_main_view += offsets[:, np.newaxis] * scaling_factors[np.newaxis, :]
if leftover > 0:
# process trailing data in the 2nd slice of the out parameter
self._ewma_vectorized(self._data[-leftover:],
self._out[-leftover:],
offset=out_main_view[-1, -1])
def _ewma_vectorized(self,
data: np.ndarray,
out: np.ndarray,
offset: float | None = None) -> None:
""" Calculates the exponential moving average over a vector. Will fail for large inputs.
The result is processed in place into the array passed to the `out` parameter
Parameters
----------
data : :class:`numpy.ndarray`
A 1 dimensional numpy array to obtain smoothed data for
out : :class:`numpy.ndarray`
A location into which the result is stored. It must have the same shape and dtype as
the input data
offset : float, optional
The offset for the moving average, scalar. Default: the value held in data[0].
"""
if data.size < 1: # empty input, return empty array
return
offset = data[0] if offset is None else offset
# scaling_factors -> 0 as len(data) gets large. This leads to divide-by-zeros below
scaling_factors = np.power(1. - self._alpha, np.arange(data.size + 1, dtype=self._dtype),
dtype=self._dtype)
# create cumulative sum array
np.multiply(data, (self._alpha * scaling_factors[-2]) / scaling_factors[:-1],
dtype=self._dtype, out=out)
np.cumsum(out, dtype=self._dtype, out=out)
out /= scaling_factors[-2::-1] # cumulative sums / scaling
if offset != 0:
noffset = np.asarray(offset).astype(self._dtype, copy=False)
out += noffset * scaling_factors[1:]
def _ewma_vectorized_2d(self, data: np.ndarray, out: np.ndarray) -> None:
""" Calculates the exponential moving average over the last axis.
The result is processed in place into the array passed to the `out` parameter
Parameters
----------
data : :class:`numpy.ndarray`
A 1 or 2 dimensional numpy array to obtain smoothed data for.
out : :class:`numpy.ndarray`
A location into which the result is stored. It must have the same shape and dtype as
the input data
"""
if data.size < 1: # empty input, return empty array
return
# calculate the moving average
scaling_factors = np.power(1. - self._alpha, np.arange(data.shape[1] + 1,
dtype=self._dtype),
dtype=self._dtype)
# create a scaled cumulative sum array
np.multiply(data,
np.multiply(self._alpha * scaling_factors[-2],
np.ones((data.shape[0], 1), dtype=self._dtype),
dtype=self._dtype) / scaling_factors[np.newaxis, :-1],
dtype=self._dtype, out=out)
np.cumsum(out, axis=1, dtype=self._dtype, out=out)
out /= scaling_factors[np.newaxis, -2::-1]
__all__ = get_module_objects(__name__)