Esempio n. 1
0
    def anom_det(self, data, k):
        """
        Calculate the reconstruction loss when evaluating test-data, and plot the most anomalous images.
        """

        data_amount = data.shape[0]
        flata = np.reshape(data, (data_amount, -1))
        recon = np.reshape(self.auto_encoder.predict(data), (data_amount, -1))

        anomality = np.zeros((data_amount, ))

        # quick safeguard against logarithm bs
        e = 1E-8
        recon[recon < e] = e
        recon[recon > 1. - e] = 1. - e

        for data_idx in range(
                data_amount
        ):  # Basically cross-entropy but the keras thing was buggy.
            anomality[data_idx] = np.sum(
                np.log(recon[data_idx, :]) * flata[data_idx, :]) + np.sum(
                    np.log(1. - recon[data_idx, :]) *
                    (1. - flata[data_idx, :]))

        # keep only most anomalous data
        data = data[np.argsort(anomality)[:k]]

        # show and push
        format_imgs(images=data, show=True)
Esempio n. 2
0
    def reconstruct(self, data, plot=False, file_name: str = None):
        rec = self.auto_encoder.predict(data)
        if plot:
            format_imgs(np.concatenate([data, rec], axis=0),
                        vert=2,
                        show=True,
                        file_name=file_name)

        return rec
Esempio n. 3
0
    def generate_random_images(self,
                               amount,
                               plot: bool,
                               file_name: str = None):
        data = self.decoder.predict(np.random.randn(amount, self.encoding_dim))

        if plot:
            format_imgs(images=data[:amount], show=True, file_name=file_name)

        return data
Esempio n. 4
0
    def generate_random_images(self,
                               batch_size: np.int = 144,
                               show_images=False):
        """
        Throw random noise on the generator, show the images.
        """
        # throw random noise on the generator and get our generated images from the prediction.
        generated_images = self.generator.predict(np.random.uniform(
            -1, 1, (batch_size, self.encoding_dim)),
                                                  verbose=0)

        # Show the images
        format_imgs(generated_images, show=show_images)
        return generated_images
Esempio n. 5
0
    def anom_det(self, data, k):
        data_amount = data.shape[0]
        anomality = np.zeros((data_amount,))

        samples = self.decoder.predict(np.random.randn(1000, self.encoding_dim))
        # quick safeguard against logarithm bs
        e = 1e-8
        samples[samples < e] = e
        samples[samples > 1. - e] = 1. - e

        flata = np.reshape(data, (data_amount, -1))
        samples = np.reshape(samples, (samples.shape[0], -1))

        for idx in range(data_amount):  # Reconstruction probability and reconstruction error
            anomality[idx] = np.sum(np.mean(np.log(samples), axis=0) * flata[idx, :]) + np.sum(np.mean(np.log(1 - samples), axis=0) * 1 - flata[idx, :])

        data = data[np.argsort(anomality)[:k]]

        format_imgs(images=data, show=True, file_name='vae-anomalies.png')

        return