def load_tf_aegmm(filepath: str, state_dict: Dict) -> tf.keras.Model: """ Load AEGMM. Parameters ---------- filepath Save directory. state_dict Dictionary containing the `n_gmm` and `recon_features` parameters. Returns ------- Loaded AEGMM. """ 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, gmm density net or aegmm 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')) gmm_density_net = tf.keras.models.load_model( os.path.join(model_dir, 'gmm_density_net.h5')) aegmm = AEGMM(encoder_net, decoder_net, gmm_density_net, state_dict['n_gmm'], state_dict['recon_features']) aegmm.load_weights(os.path.join(model_dir, 'aegmm.ckpt')) return aegmm
def __init__(self, threshold: float = None, aegmm: tf.keras.Model = None, encoder_net: tf.keras.Sequential = None, decoder_net: tf.keras.Sequential = None, gmm_density_net: tf.keras.Sequential = None, n_gmm: int = None, recon_features: Callable = eucl_cosim_features, data_type: str = None) -> None: """ AEGMM-based outlier detector. Parameters ---------- threshold Threshold used for outlier score to determine outliers. aegmm A trained tf.keras model if available. encoder_net Layers for the encoder wrapped in a tf.keras.Sequential class if no 'aegmm' is specified. decoder_net Layers for the decoder wrapped in a tf.keras.Sequential class if no 'aegmm' is specified. gmm_density_net Layers for the GMM network wrapped in a tf.keras.Sequential class. n_gmm Number of components in GMM. recon_features Function to extract features from the reconstructed instance by the decoder. 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 # check if model can be loaded, otherwise initialize AEGMM model if isinstance(aegmm, tf.keras.Model): self.aegmm = aegmm elif (isinstance(encoder_net, tf.keras.Sequential) and isinstance(decoder_net, tf.keras.Sequential) and isinstance(gmm_density_net, tf.keras.Sequential)): self.aegmm = AEGMM(encoder_net, decoder_net, gmm_density_net, n_gmm, recon_features) else: raise TypeError( 'No valid format detected for `aegmm` (tf.keras.Model) ' 'or `encoder_net`, `decoder_net` and `gmm_density_net` (tf.keras.Sequential).' ) # set metadata self.meta['detector_type'] = 'offline' self.meta['data_type'] = data_type self.phi, self.mu, self.cov, self.L, self.log_det_cov = None, None, None, None, None
assert np.sum((X - X_recon_untrained)**2) > np.sum((X - X_recon)**2) @pytest.mark.parametrize('tf_v_ae_mnist', tests, indirect=True) def test_ae_vae(tf_v_ae_mnist): pass n_gmm = 1 gmm_density_net = tf.keras.Sequential([ InputLayer(input_shape=(latent_dim + 2, )), Dense(10, activation=tf.nn.relu), Dense(n_gmm, activation=tf.nn.softmax) ]) aegmm = AEGMM(encoder_net, decoder_net, gmm_density_net, n_gmm) vaegmm = VAEGMM(encoder_net, decoder_net, gmm_density_net, n_gmm, latent_dim) tests = [(aegmm, loss_aegmm), (vaegmm, loss_vaegmm)] n_tests = len(tests) @pytest.fixture def tf_v_aegmm_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