Ejemplo n.º 1
0
def load_mnist(version, preprocess, categorical_labels=True, custom_name=''):

    num_classes = 10
    image_size = 28

    model_params = dict()
    model_params['num_classes'] = num_classes

    custom_name = '' if str(custom_name) == '' else str(custom_name) + '_'
    model_params['name'] = 'MLP_MNIST_' + str(custom_name) + 'v' + str(version)

    (x_train, y_train), (x_test, y_test) = mnist.load_data()

    if preprocess == 'line_profile':
        model_params['input_dim'] = image_size * 2

        lg_28 = laguerre_gauss_filter(image_size, 0.9)
        ft_lg_28 = np.fft.fft2(lg_28)

        x_pr_train, y_pr_train = ft_pipeline(ft_lg_28, x_train)
        x_pr_test, y_pr_test = ft_pipeline(ft_lg_28, x_test)

        x_train = np.abs(np.concatenate((x_pr_train, y_pr_train), axis=1))
        x_test = np.abs(np.concatenate((x_pr_test, y_pr_test), axis=1))
    elif preprocess == 'flattened':
        x_train = x_train.reshape(x_train.shape[0], -1)
        x_test = x_test.reshape(x_test.shape[0], -1)

        model_params['input_dim'] = image_size**2
    else:
        img_rows, img_cols = image_size, image_size
        x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
        x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
        input_shape = (img_rows, img_cols, 1)

        model_params['input_shape'] = input_shape
        model_params['name'] = 'CNN_MNIST_' + str(custom_name) + 'v' + str(
            version)

    if categorical_labels:
        y_train = keras.utils.to_categorical(y_train, num_classes)
        y_test = keras.utils.to_categorical(y_test, num_classes)

    splits = [('train', x_train, y_train), ('validation', x_test, y_test)]
    data = dict()
    for spl in splits:
        split, features, labels = spl
        model_params[split + '_imbalance'] = np.unique(labels,
                                                       axis=0,
                                                       return_counts=True)
        data[split] = (features, labels)

    return model_params, data
Ejemplo n.º 2
0
    def get_line_profiles(x, size, omega=0.9):

        lg_filter = laguerre_gauss_filter(size, omega)
        ft_lg_filter = np.fft.fft2(lg_filter)

        x_profile, y_profile = ft_pipeline(ft_lg_filter, x)

        # ft_pipeline_no_shift(ft_lg_filter, x)
        # ft_pipeline_no_transform(ft_lg_filter, x)

        return np.abs(np.concatenate((x_profile, y_profile), axis=1))
Ejemplo n.º 3
0
    def run(self, data_path, video=False):

        data = find_files(data_path, sorted_by_idx=False)

        if video:
            media = load_videos(data, data_path)
        else:
            media = load_images(data, data_path)

        if video:
            print(len(media), len(media[0]))

        size = self.model_specs['size']
        omega = self.model_specs['omega']

        cropper = CropImage(size)

        for instance in media:

            instance = rgb2grayscale(instance)

            plt.imshow(instance, cmap='gray')
            plt.title('Input')
            plt.show()

            original_crops = cropper.crop(instance)
            lg_filter = laguerre_gauss_filter(size, omega)
            ft_lg_filter = np.fft.fft2(lg_filter)
            x_profile, y_profile = ft_pipeline(ft_lg_filter, original_crops)
            ft_crops = np.abs(np.concatenate((x_profile, y_profile), axis=1))

            if not self.is_joblib:
                predictions = self.model.predict_on_batch(ft_crops)
            else:
                predictions = self.model.predict(ft_crops)

            print(predictions)

            pos_preds = []

            for i in range(len(predictions)):

                if not self.is_joblib:
                    pred = np.argmax(predictions[i])
                else:
                    pred = predictions[i]

                if pred == 0:
                    continue

                # img = original_crops[i]

                # plt.imshow(img, cmap='gray')
                # plt.title('Prediction: ' + str(pred))
                # plt.show()
                # plt.pause()
                # plt.close()

                pos_preds.append(original_crops[i])

            print(len(pos_preds))
            n_row, n_col = 2, 3
            _, axs = plt.subplots(n_row, n_col, figsize=(12, 12))
            axs = axs.flatten()
            plt.suptitle('Positive Predictions kNN')
            for img, ax in zip(pos_preds, axs):
                ax.imshow(img, cmap='gray')
            plt.show()
Ejemplo n.º 4
0
from tensorflow.keras.layers import Dense, Dropout

import numpy as np

# ------------ LOAD DATA -----------------------

(x_train, y_train), (x_test, y_test) = mnist.load_data()

# ------------ PRE-PROCESS DATA -----------------------

image_size = 28

lg_28 = laguerre_gauss_filter(image_size, 0.9)
ft_lg_28 = np.fft.fft2(lg_28)

x_pr_train, y_pr_train = ft_pipeline(ft_lg_28, x_train)
x_pr_test, y_pr_test = ft_pipeline(ft_lg_28, x_test)

x_train = np.concatenate((x_pr_train, y_pr_train), axis=1)
x_test = np.concatenate((x_pr_test, y_pr_test), axis=1)

num_classes = 10

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

# ------------ MODEL -----------------------

model = Sequential()
model.add(Dense(256, activation='relu', input_dim=56, name='my1'))
Ejemplo n.º 5
0
data_augmentation = True
num_predictions = 20
save_dir = os.path.join(os.getcwd(), 'saved_models')
model_name = 'keras_cifar10_trained_model.h5'

# ------------ LOAD DATA -----------------------

# The data, split between train and test sets:
(x_train_org, y_train_org), (x_test_org, y_test_org) = cifar10.load_data()

# ------------ PRE-PROCESS DATA -----------------------

lg_filter_32 = laguerre_gauss_filter(32, 0.9)
ft_lg_32 = np.fft.fft2(lg_filter_32)

x_pr_train, y_pr_train = ft_pipeline(ft_lg_32, x_train_org)
x_pr_test, y_pr_test = ft_pipeline(ft_lg_32, x_test_org)

x_train = np.abs(np.concatenate((x_pr_train, y_pr_train), axis=1))
x_test = np.abs(np.concatenate((x_pr_test, y_pr_test), axis=1))

print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train_org, num_classes)
y_test = keras.utils.to_categorical(y_test_org, num_classes)

# ------------ MODEL -----------------------
Ejemplo n.º 6
0
def get_line_profiles(x):

    x_profile, y_profile = ft_pipeline(ft_lg_32, x)
    return np.abs(np.concatenate((x_profile, y_profile), axis=1))