Back to Repositories

Validating Deep Learning Backend Configuration in Faceswap

This test suite performs essential sanity checks for the Faceswap deep learning project, validating backend configurations and Keras implementations. It ensures proper setup of TensorFlow and DirectML backends while verifying Keras version compatibility.

Test Coverage Overview

The test suite provides comprehensive validation of core backend functionality and Keras configuration. Key areas tested include:

  • Backend type verification (CPU/DirectML)
  • Keras backend object type validation
  • TensorFlow Keras version compatibility checks
  • Variable initialization and module inspection

Implementation Analysis

The testing approach utilizes pytest’s parametrization feature to dynamically generate tests based on the backend configuration. The implementation leverages inspect module for runtime verification of backend objects and employs strict version checking for Keras compatibility.

Tests are structured using pytest fixtures and markers for organized test execution and reporting.

Technical Details

Testing Stack:
  • pytest framework for test organization and execution
  • TensorFlow Keras backend (versions 2.7.0 through 2.10.0)
  • Python inspect module for runtime verification
  • Custom backend detection utility (get_backend)

Best Practices Demonstrated

The test suite exemplifies several testing best practices including:

  • Parameterized testing for multiple scenarios
  • Clear test function naming and documentation
  • Explicit version compatibility checks
  • Modular test organization with separate backend and Keras validation
  • Proper use of assertions for validation

deepfakes/faceswap

tests/startup_test.py

            
#!/usr/bin/env python3
""" Sanity checks for Faceswap. """

import inspect
import pytest

# Ignore linting errors from Tensorflow's thoroughly broken import system
from tensorflow import keras
from tensorflow.keras import backend as K  # pylint:disable=import-error

from lib.utils import get_backend

_BACKEND = get_backend()


@pytest.mark.parametrize('dummy', [None], ids=[get_backend().upper()])
def test_backend(dummy):  # pylint:disable=unused-argument
    """ Sanity check to ensure that Keras backend is returning the correct object type. """
    test_var = K.variable((1, 1, 4, 4))
    lib = inspect.getmodule(test_var).__name__.split(".")[0]
    assert _BACKEND in ("cpu", "directml") and lib == "tensorflow"


@pytest.mark.parametrize('dummy', [None], ids=[get_backend().upper()])
def test_keras(dummy):  # pylint:disable=unused-argument
    """ Sanity check to ensure that tensorflow keras is being used for CPU """
    assert (_BACKEND in ("cpu", "directml")
            and keras.__version__ in ("2.7.0", "2.8.0", "2.9.0", "2.10.0"))