Example #1
0
    By AnDenix, 2018-2019
    Based on the dfaker model: https://github.com/dfaker

    Acknowledgments:
    kvrooman for numerous insights and invaluable aid
    DeepHomage for lots of testing
    """
import logging

from lib.model.nn_blocks import (Conv2DOutput, Conv2DBlock, ResidualBlock,
                                 UpscaleBlock, Upscale2xBlock)
from lib.utils import FaceswapError, get_backend

from ._base import ModelBase, KerasModel

if get_backend() == "amd":
    from keras.layers import (AveragePooling2D, BatchNormalization,
                              Concatenate, Dense, Dropout, Flatten, Input,
                              Reshape, LeakyReLU, UpSampling2D)
else:
    # Ignore linting errors from Tensorflow's thoroughly broken import system
    from tensorflow.keras.layers import (  # pylint:disable=import-error,no-name-in-module
        AveragePooling2D, BatchNormalization, Concatenate, Dense, Dropout,
        Flatten, Input, Reshape, LeakyReLU, UpSampling2D)

logger = logging.getLogger(__name__)  # pylint: disable=invalid-name


class Model(ModelBase):
    """ DLight Autoencoder Model """
    def __init__(self, *args, **kwargs):
Example #2
0
#!/usr/bin/env python3
""" Sanity checks for Faceswap. """

import inspect

import pytest

import keras
from keras import backend as K

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 == "cpu"
            and lib == "tensorflow") or (_BACKEND == "amd"
                                         and lib == "plaidml")


@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 and standard
    keras for AMD. """
    assert ((_BACKEND == "cpu" and keras.__version__ == "2.4.0")
            or (_BACKEND == "amd" and keras.__version__ == "2.2.4"))
#!/usr/bin/env python3
""" Tests for Faceswap Initializers.

Adapted from Keras tests.
"""

from keras import backend as K
from keras import initializers as k_initializers
import pytest
import numpy as np

from lib.model import initializers
from lib.utils import get_backend

CONV_SHAPE = (3, 3, 256, 2048)
CONV_ID = get_backend().upper()


def _runner(init,
            shape,
            target_mean=None,
            target_std=None,
            target_max=None,
            target_min=None):
    variable = K.variable(init(shape))
    output = K.get_value(variable)
    lim = 3e-2
    if target_std is not None:
        assert abs(output.std() - target_std) < lim
    if target_mean is not None:
        assert abs(output.mean() - target_mean) < lim
Example #4
0
from itertools import product

import pytest
import numpy as np

from keras import Input, Model, backend as K
from numpy.testing import assert_allclose

from lib.model.nn_blocks import NNBlocks
from lib.utils import get_backend

_PARAMS = ["use_icnr_init", "use_convaware_init", "use_reflect_padding"]
_VALUES = list(product([True, False], repeat=len(_PARAMS)))
_IDS = [
    "{}[{}]".format("|".join([_PARAMS[idx] for idx, b in enumerate(v) if b]),
                    get_backend().upper()) for v in _VALUES
]


def block_test(layer_func, kwargs={}, input_shape=None):
    """Test routine for faceswap neural network blocks.

    Tests are simple and are to ensure that the blocks compile on both tensorflow
    and plaidml backends
    """
    # generate input data
    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: