def load_tf_vae(filepath: str, state_dict: Dict) -> tf.keras.Model: """ Load VAE. Parameters ---------- filepath Save directory. state_dict Dictionary containing the latent dimension and beta parameters. Returns ------- Loaded VAE. """ model_dir = os.path.join(filepath, 'model') if not [f for f in os.listdir(model_dir) if not f.startswith('.')]: logger.warning( 'No encoder, decoder or vae found in {}.'.format(model_dir)) return None encoder_net = tf.keras.models.load_model( os.path.join(model_dir, 'encoder_net.h5')) decoder_net = tf.keras.models.load_model( os.path.join(model_dir, 'decoder_net.h5')) vae = VAE(encoder_net, decoder_net, state_dict['latent_dim'], beta=state_dict['beta']) vae.load_weights(os.path.join(model_dir, 'vae.ckpt')) return vae
def __init__(self, threshold: float = None, score_type: str = 'mse', # TODO: reconstruction proba; make sure to infer correct distribution vae: tf.keras.Model = None, encoder_net: tf.keras.Sequential = None, decoder_net: tf.keras.Sequential = None, latent_dim: int = None, samples: int = 10, beta: float = 1., data_type: str = None ) -> None: """ VAE-based outlier detector. Parameters ---------- threshold Threshold used for outlier score to determine outliers. score_type Metric used for outlier scores. Either 'mse' (mean squared error) or 'proba' (reconstruction probabilities) supported. vae A trained tf.keras model if available. encoder_net Layers for the encoder wrapped in a tf.keras.Sequential class if no 'vae' is specified. decoder_net Layers for the decoder wrapped in a tf.keras.Sequential class if no 'vae' is specified. latent_dim Dimensionality of the latent space. samples Number of samples sampled to evaluate each instance. beta Beta parameter for KL-divergence loss term. data_type Optionally specify the data type (tabular, image or time-series). Added to metadata. """ super().__init__() if threshold is None: logger.warning('No threshold level set. Need to infer threshold using `infer_threshold`.') self.threshold = threshold self.score_type = score_type self.samples = samples # check if model can be loaded, otherwise initialize VAE model if isinstance(vae, tf.keras.Model): self.vae = vae elif isinstance(encoder_net, tf.keras.Sequential) and isinstance(decoder_net, tf.keras.Sequential): self.vae = VAE(encoder_net, decoder_net, latent_dim, beta=beta) # define VAE model else: raise TypeError('No valid format detected for `vae` (tf.keras.Model) ' 'or `encoder_net` and `decoder_net` (tf.keras.Sequential).') # set metadata self.meta['detector_type'] = 'offline' self.meta['data_type'] = data_type
latent_dim = 50 encoder_net = tf.keras.Sequential([ InputLayer(input_shape=(input_dim, )), Dense(128, activation=tf.nn.relu), Dense(latent_dim, activation=None) ]) decoder_net = tf.keras.Sequential([ InputLayer(input_shape=(latent_dim, )), Dense(128, activation=tf.nn.relu), Dense(input_dim, activation=tf.nn.sigmoid) ]) ae = AE(encoder_net, decoder_net) vae = VAE(encoder_net, decoder_net, latent_dim) tests = [ae, vae] @pytest.fixture def tf_v_ae_mnist(request): # load and preprocess MNIST data (X_train, _), (X_test, _) = tf.keras.datasets.mnist.load_data() X = X_train.reshape(60000, input_dim)[:1000] # only train on 1000 instances X = X.astype(np.float32) X /= 255 # init model, predict with untrained model, train and predict with trained model model = request.param X_recon_untrained = model(X).numpy()
def __init__(self, threshold: float = None, vae: tf.keras.Model = None, model: tf.keras.Model = None, encoder_net: tf.keras.Sequential = None, decoder_net: tf.keras.Sequential = None, latent_dim: int = None, samples: int = 10, beta: float = 0., data_type: str = None) -> None: """ VAE-based adversarial detector. Parameters ---------- threshold Threshold used for adversarial score to determine adversarial instances. vae A trained tf.keras model if available. model A trained tf.keras classification model. encoder_net Layers for the encoder wrapped in a tf.keras.Sequential class if no 'vae' is specified. decoder_net Layers for the decoder wrapped in a tf.keras.Sequential class if no 'vae' is specified. latent_dim Dimensionality of the latent space. samples Number of samples sampled to evaluate each instance. beta Beta parameter for KL-divergence loss term. data_type Optionally specifiy the data type (tabular, image or time-series). Added to metadata. """ super().__init__() if threshold is None: logger.warning( 'No threshold level set. Need to infer threshold using `infer_threshold`.' ) self.threshold = threshold self.samples = samples self.model = model for layer in self.model.layers: # freeze model layers layer.trainable = False # check if model can be loaded, otherwise initialize VAE model if isinstance(vae, tf.keras.Model): self.vae = vae elif isinstance(encoder_net, tf.keras.Sequential) and isinstance( decoder_net, tf.keras.Sequential): self.vae = VAE(encoder_net, decoder_net, latent_dim, beta=beta) # define VAE model else: raise TypeError( 'No valid format detected for `vae` (tf.keras.Model) ' 'or `encoder_net` and `decoder_net` (tf.keras.Sequential).') # set metadata self.meta['detector_type'] = 'offline' self.meta['data_type'] = data_type