ModelBase

class plugins.train.model._base.model.ModelBase(model_dir: str, arguments: argparse.Namespace, predict: bool = False)

Bases: object

Base class that all model plugins should inherit from.

Parameters:
  • model_dir (str) – The full path to the model save location

  • arguments (argparse.Namespace) – The arguments that were passed to the train or convert process as generated from Faceswap’s command line arguments

  • predict (bool) – True if the model is being loaded for inference, False if the model is being loaded for training. Default: False

Attributes Summary

coverage_ratio

The ratio of the training image to crop out and train on as defined in user configuration options.

freeze_layers

Override to set plugin specific layers that can be frozen.

input_shapes

A flattened list corresponding to all of the inputs to the model.

io

Input/Output operations for the model

iterations

The total number of iterations that the model has trained.

load_layers

Override to set plugin specific layers that can be loaded.

model

The compiled model for this plugin.

model_name

The name of the keras model.

name

The name of this model based on the plugin name.

output_shapes

A flattened list corresponding to all of the outputs of the model.

state

The state settings for the current plugin.

Methods Summary

add_history(loss)

Add the current iteration's loss history to _io.history.

build()

Build the model and assign to model.

build_model(inputs)

Override for Model Specific autoencoder builds.

Attributes Documentation

coverage_ratio

The ratio of the training image to crop out and train on as defined in user configuration options.

NB: The coverage ratio is a raw float, but will be applied to integer pixel images.

To ensure consistent rounding and guaranteed even image size, the calculation for coverage should always be: \((original_size * coverage_ratio // 2) * 2\)

freeze_layers

Override to set plugin specific layers that can be frozen. Defaults to [“encoder”]

input_shapes

A flattened list corresponding to all of the inputs to the model.

io

Input/Output operations for the model

iterations

The total number of iterations that the model has trained.

load_layers

Override to set plugin specific layers that can be loaded. Defaults to [“encoder”]

model

The compiled model for this plugin.

model_name

The name of the keras model. Generally this will be the same as name but some plugins will override this when they contain multiple architectures

name

The name of this model based on the plugin name.

output_shapes

A flattened list corresponding to all of the outputs of the model.

state

The state settings for the current plugin.

Methods Documentation

add_history(loss: np.ndarray) None

Add the current iteration’s loss history to _io.history.

Called from the trainer after each iteration, for tracking loss drop over time between save iterations.

Parameters:

loss (np.ndarray) – The loss values for the A and B side for the current iteration. This should be the collated loss values for each side.

Return type:

None

build() None

Build the model and assign to model.

Within the defined strategy scope, either builds the model from scratch or loads an existing model if one exists.

If running inference, then the model is built only for the required side to perform the swap function, otherwise the model is then compiled with the optimizer and chosen loss function(s).

Finally, a model summary is outputted to the logger at verbose level.

Return type:

None

build_model(inputs: list[Input]) Model

Override for Model Specific autoencoder builds.

Parameters:

inputs (list[Input]) – A list of keras.layers.Input tensors. This will be a list of 2 tensors (one for each side) each of shapes input_shape.

Returns:

  • See Keras documentation for the correct structure, but note that parameter name

  • is a required rather than an optional argument in Faceswap. You should assign this to

  • the attribute self.name that is automatically generated from the plugin’s filename.

Return type:

Model

add_history(loss: np.ndarray) None

Add the current iteration’s loss history to _io.history.

Called from the trainer after each iteration, for tracking loss drop over time between save iterations.

Parameters:

loss (np.ndarray) – The loss values for the A and B side for the current iteration. This should be the collated loss values for each side.

Return type:

None

build() None

Build the model and assign to model.

Within the defined strategy scope, either builds the model from scratch or loads an existing model if one exists.

If running inference, then the model is built only for the required side to perform the swap function, otherwise the model is then compiled with the optimizer and chosen loss function(s).

Finally, a model summary is outputted to the logger at verbose level.

Return type:

None

build_model(inputs: list[Input]) Model

Override for Model Specific autoencoder builds.

Parameters:

inputs (list[Input]) – A list of keras.layers.Input tensors. This will be a list of 2 tensors (one for each side) each of shapes input_shape.

Returns:

  • See Keras documentation for the correct structure, but note that parameter name

  • is a required rather than an optional argument in Faceswap. You should assign this to

  • the attribute self.name that is automatically generated from the plugin’s filename.

Return type:

Model

property coverage_ratio: float

The ratio of the training image to crop out and train on as defined in user configuration options.

NB: The coverage ratio is a raw float, but will be applied to integer pixel images.

To ensure consistent rounding and guaranteed even image size, the calculation for coverage should always be: \((original_size * coverage_ratio // 2) * 2\)

property freeze_layers: list[str]

Override to set plugin specific layers that can be frozen. Defaults to [“encoder”]

input_shape: tuple[int, ...]

A tuple of ints defining the shape of the faces that the model takes as input. This should be overridden by model plugins in their __init__() function. If the input size is the same for both sides of the model, then this can be a single 3 dimensional tuple. If the inputs have different sizes for “A” and “B” this should be a list of 2 3 dimensional shape tuples, 1 for each side respectively.

property input_shapes: list[tuple[None, int, int, int]]

A flattened list corresponding to all of the inputs to the model.

property io: IO

Input/Output operations for the model

property iterations: int

The total number of iterations that the model has trained.

property load_layers: list[str]

Override to set plugin specific layers that can be loaded. Defaults to [“encoder”]

property model: Model

The compiled model for this plugin.

property model_name: str

The name of the keras model. Generally this will be the same as name but some plugins will override this when they contain multiple architectures

property name: str

The name of this model based on the plugin name.

property output_shapes: list[tuple[None, int, int, int]]

A flattened list corresponding to all of the outputs of the model.

property state: State

The state settings for the current plugin.