tensorflow#

Tensorflow models are used in out-of-distribution detectors in the dataeval.detectors.ood module.

DataEval provides both basic default models through the utility dataeval.utils.tensorflow.models.create_model() as well as constructors which allow for customization of the encoder, decoder and any other applicable layers used by the model.

Functions#

dataeval.utils.tensorflow.models.create_model(model_type: AE | AEGMM | PixelCNN | VAE | VAEGMM, input_shape: tuple[int, int, int], encoding_dim: int | None = None, n_gmm: int | None = None, gmm_latent_dim: int | None = None)#

Create a default model for the specified model type.

Parameters:
  • model_type (Union[AE, AEGMM, PixelCNN, VAE, VAEGMM]) – The model type to create.

  • input_shape (Tuple[int, int, int]) – The input shape of the data used.

  • encoding_dim (int, optional - default None) – The target encoding dimensionality.

  • n_gmm (int, optional - default None) – Number of components used in the GMM layer.

  • gmm_latent_dim (int, optional - default None) – Latent dimensionality of the GMM layer.

Models#

class dataeval.utils.tensorflow.models.AE(encoder_net: keras.Model, decoder_net: keras.Model)#

Combine encoder and decoder in AE.

Parameters:
  • encoder_net (keras.Model) – Layers for the encoder wrapped in a keras.Sequential class.

  • decoder_net (keras.Model) – Layers for the decoder wrapped in a keras.Sequential class.

class dataeval.utils.tensorflow.models.AEGMM(encoder_net: keras.Model, decoder_net: keras.Model, gmm_density_net: keras.Model, n_gmm: int, recon_features: Callable = eucl_cosim_features)#

Deep Autoencoding Gaussian Mixture Model.

Parameters:
  • encoder_net (keras.Model) – Layers for the encoder wrapped in a keras.Sequential class.

  • decoder_net (keras.Model) – Layers for the decoder wrapped in a keras.Sequential class.

  • gmm_density_net (keras.Model) – Layers for the GMM network wrapped in a keras.Sequential class.

  • n_gmm (int) – Number of components in GMM.

  • recon_features (Callable, default eucl_cosim_features) – Function to extract features from the reconstructed instance by the decoder.

class dataeval.utils.tensorflow.models.PixelCNN(image_shape: tuple, conditional_shape: tuple | None = None, num_resnet: int = 5, num_hierarchies: int = 3, num_filters: int = 160, num_logistic_mix: int = 10, receptive_field_dims: tuple = (3, 3), dropout_p: float = 0.5, resnet_activation: str = 'concat_elu', l2_weight: float = 0.0, use_weight_norm: bool = True, use_data_init: bool = True, high: int = 255, low: int = 0, dtype=tf.float32)#

Construct Pixel CNN++ distribution.

Parameters:
  • image_shape (tuple) – 3D TensorShape or tuple for the [height, width, channels] dimensions of the image.

  • conditional_shape (tuple, optional - default None) – TensorShape or tuple for the shape of the conditional input, or None if there is no conditional input.

  • num_resnet (int, default 5) – The number of layers (shown in Figure 2 of [2]) within each highest-level block of Figure 2 of [1].

  • num_hierarchies (int, default 3) – The number of highest-level blocks (separated by expansions/contractions of dimensions in Figure 2 of [1].)

  • num_filters (int, default 160) – The number of convolutional filters.

  • num_logistic_mix (int, default 10) – Number of components in the logistic mixture distribution.

  • tuple (receptive_field_dims) – Height and width in pixels of the receptive field of the convolutional layers above and to the left of a given pixel. The width (second element of the tuple) should be odd. Figure 1 (middle) of [2] shows a receptive field of (3, 5) (the row containing the current pixel is included in the height). The default of (3, 3) was used to produce the results in [1].

  • (3 (default) – Height and width in pixels of the receptive field of the convolutional layers above and to the left of a given pixel. The width (second element of the tuple) should be odd. Figure 1 (middle) of [2] shows a receptive field of (3, 5) (the row containing the current pixel is included in the height). The default of (3, 3) was used to produce the results in [1].

  • 3) – Height and width in pixels of the receptive field of the convolutional layers above and to the left of a given pixel. The width (second element of the tuple) should be odd. Figure 1 (middle) of [2] shows a receptive field of (3, 5) (the row containing the current pixel is included in the height). The default of (3, 3) was used to produce the results in [1].

  • dropout_p (float, default 0.0) – The dropout probability. Should be between 0 and 1.

  • resnet_activation (str, default "concat_elu") – The type of activation to use in the resnet blocks. May be ‘concat_elu’, ‘elu’, or ‘relu’.

  • l2_weight (float, default 0.0) – The L2 regularization weight.

  • use_weight_norm (bool, default True) – If True then use weight normalization (works only in Eager mode).

  • use_data_init (bool, default True) – If True then use data-dependent initialization (has no effect if use_weight_norm is False).

  • high (int, default 255) – The maximum value of the input data (255 for an 8-bit image).

  • low (int, default 0) – The minimum value of the input data.

  • dtype (tensorflow dtype, default tf.float32) – Data type of the Distribution.

class dataeval.utils.tensorflow.models.VAE(encoder_net: keras.Model, decoder_net: keras.Model, latent_dim: int, beta: float = 1.0)#

Combine encoder and decoder in VAE.

Parameters:
  • encoder_net (keras.Model) – Layers for the encoder wrapped in a keras.Sequential class.

  • decoder_net (keras.Model) – Layers for the decoder wrapped in a keras.Sequential class.

  • latent_dim (int) – Dimensionality of the latent space.

  • beta (float, default 1.0) – Beta parameter for KL-divergence loss term.

class dataeval.utils.tensorflow.models.VAEGMM(encoder_net: keras.Model, decoder_net: keras.Model, gmm_density_net: keras.Model, n_gmm: int, latent_dim: int, recon_features: Callable = eucl_cosim_features, beta: float = 1.0)#

Variational Autoencoding Gaussian Mixture Model.

Parameters:
  • encoder_net (keras.Model) – Layers for the encoder wrapped in a keras.Sequential class.

  • decoder_net (keras.Model) – Layers for the decoder wrapped in a keras.Sequential class.

  • gmm_density_net (keras.Model) – Layers for the GMM network wrapped in a keras.Sequential class.

  • n_gmm (int) – Number of components in GMM.

  • latent_dim (int) – Dimensionality of the latent space.

  • recon_features (Callable, default eucl_cosim_features) – Function to extract features from the reconstructed instance by the decoder.

  • beta (float, default 1.0) – Beta parameter for KL-divergence loss term.

Reconstruction Functions#

dataeval.utils.tensorflow.recon.eucl_cosim_features(x: Tensor, y: Tensor, max_eucl: float = 100.0) Tensor#

Compute features extracted from the reconstructed instance using the relative Euclidean distance and cosine similarity between 2 tensors.

Parameters:
  • x (tf.Tensor) – Tensor used in feature computation.

  • y (tf.Tensor) – Tensor used in feature computation.

  • max_eucl (float, default 1e2) – Maximum value to clip relative Euclidean distance by.

Returns:

Tensor concatenating the relative Euclidean distance and cosine similarity features.

Return type:

tf.Tensor

Loss Function Classes#

class dataeval.utils.tensorflow.loss.Elbo(cov_type: Literal['cov_full', 'cov_diag'] | float = 1.0, x: Tensor | ndarray[Any, dtype[_ScalarType_co]] | None = None)#

Compute ELBO loss.

The covariance matrix can be specified by passing the full covariance matrix, the matrix diagonal, or a scale identity multiplier. Only one of these should be specified. If none are specified, the identity matrix is used.

Parameters:
  • cov_type (Union[Literal["cov_full", "cov_diag"], float], default 1.0) – Full covariance matrix, diagonal variance matrix, or scale identity multiplier.

  • x (ArrayLike, optional - default None) – Dataset used to calculate the covariance matrix. Required for full and diagonal covariance matrix types.

class dataeval.utils.tensorflow.loss.LossGMM(w_recon: float = 1e-07, w_energy: float = 0.1, w_cov_diag: float = 0.005, elbo: Elbo | None = None)#

Loss function used for AE and VAE with GMM.

Parameters:
  • w_recon (float, default 1e-7) – Weight on elbo loss term.

  • w_energy (float, default 0.1) – Weight on sample energy loss term.

  • w_cov_diag (float, default 0.005) – Weight on covariance regularizing loss term.

  • elbo (Elbo, optional - default None) – ELBO loss function used to calculate w_recon.