Files
faceswap/tests/simple_tests.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

214 lines
6.4 KiB
Python

"""
Contains some simple tests.
The purpose of this tests is to detect crashes and hangs
but NOT to guarantee the corectness of the operations.
For this we want another set of testcases using pytest.
Due to my lazy coding, DON'T USE PATHES WITH BLANKS !
"""
import sys
from subprocess import check_call, CalledProcessError
import os
from os.path import join as pathjoin, abspath, dirname
_fail_count = 0
_test_count = 0
_COLORS = {
"FAIL": "\033[1;31m",
"OK": "\033[1;32m",
"STATUS": "\033[1;37m",
"BOLD": "\033[1m",
"ENDC": "\033[0m"
}
def print_colored(text, color="OK", bold=False):
""" Print colored text
This might not work on windows,
although travis runs windows stuff in git bash, so it might ?
"""
color = _COLORS.get(color, color)
fmt = '' if not bold else _COLORS['BOLD']
print(f"{color}{fmt}{text}{_COLORS['ENDC']}")
def print_ok(text):
""" Print ok in colored text """
print_colored(text, "OK", True)
def print_fail(text):
""" Print fail in colored text """
print_colored(text, "FAIL", True)
def print_status(text):
""" Print status in colored text """
print_colored(text, "STATUS", True)
def run_test(name, cmd):
""" run a test """
global _fail_count, _test_count # pylint:disable=global-statement
print_status(f"[?] running {name}")
print(f"Cmd: {' '.join(cmd)}")
_test_count += 1
try:
check_call(cmd)
print_ok("[+] Test success")
return True
except CalledProcessError as err:
print_fail(f"[-] Test failed with {err}")
_fail_count += 1
return False
def extract_args(detector, aligner, in_path, out_path, args=None):
""" Extraction command """
py_exe = sys.executable
_extract_args = (f"{py_exe} faceswap.py extract -i {in_path} -o {out_path} -D {detector} "
f"-A {aligner}")
if args:
_extract_args += f" {args}"
return _extract_args.split()
def train_args(model, model_path, faces, iterations=1, batchsize=2, extra_args=""):
""" Train command """
py_exe = sys.executable
args = (f"{py_exe} faceswap.py train -A {faces} -B {faces} -m {model_path} -t {model} "
f"-b {batchsize} -i {iterations} {extra_args}")
return args.split()
def convert_args(in_path, out_path, model_path, writer, args=None):
""" Convert command """
py_exe = sys.executable
conv_args = (f"{py_exe} faceswap.py convert -i {in_path} -o {out_path} -m {model_path} "
f"-w {writer}")
if args:
conv_args += f" {args}"
return conv_args.split() # Don't use pathes with spaces ;)
def sort_args(in_path, out_path, sortby="face", groupby="hist"):
""" Sort command """
py_exe = sys.executable
_sort_args = f"{py_exe} tools.py sort -i {in_path} -o {out_path} -s {sortby} -g {groupby} -k"
return _sort_args.split()
def set_train_config(value):
""" Update the mixed_precision and autoclip values to given value
Parameters
----------
value: bool
The value to set the config parameters to.
"""
old_val, new_val = ("False", "True") if value else ("True", "False")
base_path = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
train_ini = os.path.join(base_path, "config", "train.ini")
try:
cmd = ["sed", "-i", f"s/autoclip = {old_val}/autoclip = {new_val}/", train_ini]
check_call(cmd)
cmd = ["sed",
"-i",
f"s/mixed_precision = {old_val}/mixed_precision = {new_val}/",
train_ini]
check_call(cmd)
print_ok(f"Set autoclip and mixed_precision to `{new_val}`")
except CalledProcessError as err:
print_fail(f"[-] Test failed with {err}")
def main():
""" Main testing script """
base_dir = pathjoin(dirname(abspath(__file__)), "data")
vid_base = pathjoin(base_dir, "vid")
img_base = pathjoin(base_dir, "imgs")
py_exe = sys.executable
was_trained = False
vid_path = pathjoin(vid_base, "test.mp4")
vid_extract = run_test(
"Extraction video with cv2-dnn detector and cv2-dnn aligner.",
extract_args("Cv2-Dnn", "Cv2-Dnn", vid_path, pathjoin(vid_base, "faces"))
)
run_test(
"Extraction images with cv2-dnn detector and cv2-dnn aligner.",
extract_args("Cv2-Dnn", "Cv2-Dnn", img_base, pathjoin(img_base, "faces"))
)
if vid_extract:
run_test(
"Generate configs and test help output",
(
py_exe, "faceswap.py", "-h"
)
)
run_test(
"Sort faces.",
sort_args(
pathjoin(vid_base, "faces"), pathjoin(vid_base, "faces_sorted"),
sortby="face"
)
)
run_test(
"Rename sorted faces.",
(
py_exe, "tools.py", "alignments", "-j", "rename",
"-a", pathjoin(vid_base, "test_alignments.fsa"),
"-c", pathjoin(vid_base, "faces_sorted"),
)
)
set_train_config(True)
run_test(
"Train lightweight model for 1 iteration with WTL, AutoClip, MixedPrecion",
train_args("lightweight",
pathjoin(vid_base, "model"),
pathjoin(vid_base, "faces"),
iterations=1,
batchsize=1,
extra_args="-M"))
set_train_config(False)
was_trained = run_test(
"Train lightweight model for 1 iterations WITHOUT WTL, AutoClip, MixedPrecion",
train_args("lightweight",
pathjoin(vid_base, "model"),
pathjoin(vid_base, "faces"),
iterations=1,
batchsize=1))
if was_trained:
run_test(
"Convert video.",
convert_args(
vid_path, pathjoin(vid_base, "conv"),
pathjoin(vid_base, "model"), "ffmpeg"
)
)
run_test(
"Convert images.",
convert_args(
img_base, pathjoin(img_base, "conv"),
pathjoin(vid_base, "model"), "opencv"
)
)
if _fail_count == 0:
print_ok(f"[+] Failed {_fail_count}/{_test_count} tests.")
sys.exit(0)
else:
print_fail(f"[-] Failed {_fail_count}/{_test_count} tests.")
sys.exit(1)
if __name__ == '__main__':
main()