Files
faceswap/tests/lib/model/nn_blocks_test.py
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

86 lines
3.1 KiB
Python

#!/usr/bin/env python3
""" Tests for Faceswap Custom Layers.
Adapted from Keras tests.
"""
from itertools import product
import pytest
import numpy as np
from numpy.testing import assert_allclose
from keras import device, Input, Model, backend as K
from lib.model import nn_blocks
from lib.utils import get_backend
from plugins.train import train_config as cfg
# pylint:disable=unused-import
from tests.lib.config.helpers import patch_config # noqa:[F401]
def block_test(layer_func, # pylint:disable=dangerous-default-value,too-many-locals
kwargs={},
input_shape=None):
"""Test routine for faceswap neural network blocks. """
# generate input data
# pylint:disable=duplicate-code
assert input_shape
input_dtype = K.floatx()
input_data_shape = list(input_shape)
for i, var_e in enumerate(input_data_shape):
if var_e is None:
input_data_shape[i] = np.random.randint(1, 4)
input_data = 10 * np.random.random(input_data_shape)
input_data = input_data.astype(input_dtype)
expected_output_dtype = input_dtype
# test in functional API
inp = Input(shape=input_shape[1:], dtype=input_dtype)
outp = layer_func(inp, **kwargs)
assert outp.dtype == expected_output_dtype
# check with the functional API
model = Model(inp, outp)
actual_output = model.predict(input_data, verbose=0) # type:ignore
# test serialization, weight setting at model level
model_config = model.get_config()
recovered_model = model.__class__.from_config(model_config)
if model.weights:
weights = model.get_weights()
recovered_model.set_weights(weights)
_output = recovered_model.predict(input_data, verbose=0) # type:ignore
assert_allclose(_output, actual_output, rtol=1e-3)
# for further checks in the caller function
return actual_output
_PARAMS = ["use_icnr_init", "use_convaware_init", "use_reflect_padding"]
_VALUES = list(product([True, False], repeat=len(_PARAMS)))
_IDS = [f"{'|'.join([_PARAMS[idx] for idx, b in enumerate(v) if b])}[{get_backend().upper()}]"
for v in _VALUES]
@pytest.mark.parametrize(_PARAMS, _VALUES, ids=_IDS)
def test_blocks(use_icnr_init,
use_convaware_init,
use_reflect_padding,
patch_config): # pylint:disable=redefined-outer-name # noqa:[F811]
""" Test for all blocks contained within the NNBlocks Class """
config = {"icnr_init": use_icnr_init,
"conv_aware_init": use_convaware_init,
"reflect_padding": use_reflect_padding}
patch_config(cfg, config)
with device("cpu"):
block_test(nn_blocks.Conv2DOutput(64, 3), input_shape=(2, 8, 8, 32))
block_test(nn_blocks.Conv2DBlock(64), input_shape=(2, 8, 8, 32))
block_test(nn_blocks.SeparableConv2DBlock(64), input_shape=(2, 8, 8, 32))
block_test(nn_blocks.UpscaleBlock(64), input_shape=(2, 4, 4, 128))
block_test(nn_blocks.Upscale2xBlock(64, fast=True), input_shape=(2, 4, 4, 128))
block_test(nn_blocks.Upscale2xBlock(64, fast=False), input_shape=(2, 4, 4, 128))
block_test(nn_blocks.ResidualBlock(64), input_shape=(2, 4, 4, 64))