Exemplo n.º 1
0
    def test_zca(self):
        """
        Confirm that ZCA.inv_P_ is the correct inverse of ZCA.P_.
        There's a lot else about the ZCA class that could be tested here.
        """
        preprocessor = ZCA()
        preprocessor.fit(self.X)

        identity = np.identity(self.X.shape[1], theano.config.floatX)
        # Check some basics of transformation matrix
        assert preprocessor.P_.shape == (self.X.shape[1], self.X.shape[1])
        assert_allclose(np.dot(preprocessor.P_,
                               preprocessor.inv_P_), identity, rtol=1e-4)

        preprocessor = ZCA(filter_bias=0.0)
        preprocessed_X = self.get_preprocessed_data(preprocessor)

        # Check if preprocessed data matrix is white
        assert_allclose(np.cov(preprocessed_X.transpose(),
                               bias=1), identity, rtol=1e-4)

        # Check if we obtain correct solution
        zca_transformed_X = np.array(
            [[-1.0199, -0.1832, 1.9528, -0.9603, -0.8162],
             [0.0729, 1.4142, 0.2529, 1.1861, -1.0876],
             [0.9575, -1.1173, -0.5435, -1.4372, -0.1057],
             [0.6348, 1.1258, 0.2692, -0.8893, 1.1669],
             [-0.9769, 0.8297, -1.8676, -0.6055, -0.5096],
             [-1.5700, -0.8389, -0.0931, 0.8877, 1.6089],
             [0.4993, -1.4219, -0.3443, 0.9664, -1.1022],
             [1.4022, 0.1917, 0.3736, 0.8520, 0.8456]]
        )
        assert_allclose(preprocessed_X, zca_transformed_X, rtol=1e-3)
Exemplo n.º 2
0
        def test(store_inverse):
            preprocessed_X = copy.copy(self.X)
            preprocessor = ZCA(store_inverse=store_inverse)

            dataset = DenseDesignMatrix(X=preprocessed_X,
                                        preprocessor=preprocessor,
                                        fit_preprocessor=True)

            preprocessed_X = dataset.get_design_matrix()
            assert_allclose(self.X, preprocessor.inverse(preprocessed_X))
Exemplo n.º 3
0
def test_zca_dataset():
    """
    Tests the ZCA_Dataset class.
    """
    # Preparation
    rng = np.random.RandomState([2014, 11, 4])
    start = 0
    stop = 990
    num_examples = 1000
    num_feat = 5
    num_classes = 2

    # random_dense_design_matrix has values that are centered and of
    # unit stdev, which is not useful to test the ZCA.
    # So, we replace its value by an uncentered uniform one.
    raw = random_dense_design_matrix(rng, num_examples, num_feat, num_classes)
    x = rng.uniform(low=-0.5, high=2.0, size=(num_examples, num_feat))
    x = x.astype(np.float32)
    raw.X = x

    zca = ZCA(filter_bias=0.0)
    zca.apply(raw, can_fit=True)
    zca_dataset = ZCA_Dataset(raw, zca, start, stop)

    # Testing general behaviour
    mean = zca_dataset.X.mean(axis=0)
    var = zca_dataset.X.std(axis=0)
    assert_allclose(mean, np.zeros(num_feat), atol=1e-2)
    assert_allclose(var, np.ones(num_feat), atol=1e-2)

    # Testing mapback()
    y = zca_dataset.mapback(zca_dataset.X)
    assert_allclose(x[start:stop], y)

    # Testing mapback_for_viewer()
    y = zca_dataset.mapback_for_viewer(zca_dataset.X)
    z = x/np.abs(x).max(axis=0)
    assert_allclose(z[start:stop], y, rtol=1e-2)

    # Testing adjust_for_viewer()
    y = zca_dataset.adjust_for_viewer(x.T).T
    z = x/np.abs(x).max(axis=0)
    assert_allclose(z, y)

    # Testing adjust_to_be_viewed_with()
    y = zca_dataset.adjust_to_be_viewed_with(x, 2*x, True)
    z = zca_dataset.adjust_for_viewer(x)
    assert_allclose(z/2, y)
    y = zca_dataset.adjust_to_be_viewed_with(x, 2*x, False)
    z = x/np.abs(x).max()
    assert_allclose(z/2, y)

    # Testing has_targets()
    assert zca_dataset.has_targets()
Exemplo n.º 4
0
    def test(store_inverse):
        rng = np.random.RandomState([1, 2, 3])
        X = as_floatX(rng.randn(15, 10))
        preprocessed_X = copy.copy(X)
        preprocessor = ZCA(store_inverse=store_inverse)

        dataset = DenseDesignMatrix(X=preprocessed_X,
                                    preprocessor=preprocessor,
                                    fit_preprocessor=True)

        preprocessed_X = dataset.get_design_matrix()

        assert_allclose(X, preprocessor.inverse(preprocessed_X))
Exemplo n.º 5
0
    def test(store_inverse):
        rng = np.random.RandomState([1, 2, 3])
        X = as_floatX(rng.randn(15, 10))
        preprocessed_X = copy.copy(X)
        preprocessor = ZCA(store_inverse=store_inverse)

        dataset = DenseDesignMatrix(X=preprocessed_X,
                                    preprocessor=preprocessor,
                                    fit_preprocessor=True)

        preprocessed_X = dataset.get_design_matrix()

        assert_allclose(X, preprocessor.inverse(preprocessed_X))
Exemplo n.º 6
0
def test_zca_dataset():
    """
    Test that a ZCA dataset can be constructed without crashing. No
    attempt to verify correctness of behavior.
    """

    rng = np.random.RandomState([2014, 11, 4])
    num_examples = 5
    dim = 3
    num_classes = 2
    raw = random_dense_design_matrix(rng, num_examples, dim, num_classes)
    zca = ZCA()
    zca.apply(raw, can_fit=True)
    zca_dataset = ZCA_Dataset(raw, zca, start=1, stop=4)
Exemplo n.º 7
0
    def test_zca_dtypes(self):
        """
        Confirm that ZCA.fit works regardless of dtype of
        data and config.floatX
        """

        orig_floatX = config.floatX

        try:
            for floatX in ['float32', 'float64']:
                for dtype in ['float32', 'float64']:
                    preprocessor = ZCA()
                    preprocessor.fit(self.X)
        finally:
            config.floatX = orig_floatX
Exemplo n.º 8
0
    def test_zca_dtypes(self):
        """
        Confirm that ZCA.fit works regardless of dtype of
        data and config.floatX
        """

        orig_floatX = config.floatX

        try:
            for floatX in ['float32', 'float64']:
                for dtype in ['float32', 'float64']:
                    preprocessor = ZCA()
                    preprocessor.fit(self.X)
        finally:
            config.floatX = orig_floatX
Exemplo n.º 9
0
def test_zca_dtypes():
    """
    Confirm that ZCA.fit works regardless of dtype of data and config.floatX
    """

    orig_floatX = config.floatX

    try:
        for floatX in ['float32', 'float64']:
            for dtype in ['float32', 'float64']:
                rng = np.random.RandomState([1, 2, 3])
                X = rng.randn(15, 10).astype(dtype)
                preprocessor = ZCA()
                preprocessor.fit(X)
    finally:
        config.floatX = orig_floatX
Exemplo n.º 10
0
def test_zca_dtypes():
    """
    Confirm that ZCA.fit works regardless of dtype of data and config.floatX
    """

    orig_floatX = config.floatX

    try:
        for floatX in ['float32', 'float64']:
            for dtype in ['float32', 'float64']:
                rng = np.random.RandomState([1, 2, 3])
                X = rng.randn(15, 10).astype(dtype)
                preprocessor = ZCA()
                preprocessor.fit(X)
    finally:
        config.floatX = orig_floatX
Exemplo n.º 11
0
def test_zca():
    """
    Confirm that ZCA.inv_P_ is the correct inverse of ZCA.P_.
    There's a lot else about the ZCA class that could be tested here.
    """

    rng = np.random.RandomState([1, 2, 3])
    X = as_floatX(rng.randn(15, 10))
    preprocessor = ZCA()
    preprocessor.fit(X)

    def is_identity(matrix):
        identity = np.identity(matrix.shape[0], theano.config.floatX)
        abs_difference = np.abs(identity - matrix)
        return (abs_difference < .0001).all()

    assert preprocessor.P_.shape == (X.shape[1], X.shape[1])
    assert not is_identity(preprocessor.P_)
    assert is_identity(np.dot(preprocessor.P_, preprocessor.inv_P_))
Exemplo n.º 12
0
def test_zca():
    """
    Confirm that ZCA.inv_P_ is the correct inverse of ZCA.P_.
    There's a lot else about the ZCA class that could be tested here.
    """

    rng = np.random.RandomState([1, 2, 3])
    X = as_floatX(rng.randn(15, 10))
    preprocessor = ZCA()
    preprocessor.fit(X)

    def is_identity(matrix):
        identity = np.identity(matrix.shape[0], theano.config.floatX)
        abs_difference = np.abs(identity - matrix)
        return (abs_difference < .0001).all()

    assert preprocessor.P_.shape == (X.shape[1], X.shape[1])
    assert not is_identity(preprocessor.P_)
    assert is_identity(np.dot(preprocessor.P_, preprocessor.inv_P_))
Exemplo n.º 13
0
    def test_num_components(self):
        # Keep 3 components
        preprocessor = ZCA(filter_bias=0.0, n_components=3)
        preprocessed_X = self.get_preprocessed_data(preprocessor)

        zca_truncated_X = np.array(
            [[-0.8938, -0.3084, 1.1105, 0.1587, -1.4073],
             [0.3346, 0.5193, 1.1371, 0.6545, -0.4199],
             [0.7613, -0.4823, -1.0578, -1.1997, -0.4993],
             [0.9250, 0.5012, -0.2743, 0.1735, 0.8105],
             [-0.4928, -0.6319, -1.0359, -0.7173, 0.1469],
             [-1.8060, -0.1758, -0.2943, 0.7208, 1.4359],
             [0.0079, -0.2582, 0.1368, -0.3571, -0.8147],
             [1.1636, 0.8362, 0.2777, 0.5666, 0.7480]])
        assert_allclose(zca_truncated_X, preprocessed_X, rtol=1e-3)

        # Drop 2 components: result should be similar
        preprocessor = ZCA(filter_bias=0.0, n_drop_components=2)
        preprocessed_X = self.get_preprocessed_data(preprocessor)
        assert_allclose(zca_truncated_X, preprocessed_X, rtol=1e-3)
Exemplo n.º 14
0
    def test_zca(self):
        """
        Confirm that ZCA.inv_P_ is the correct inverse of ZCA.P_.
        There's a lot else about the ZCA class that could be tested here.
        """
        preprocessor = ZCA()
        preprocessor.fit(self.X)

        identity = np.identity(self.X.shape[1], theano.config.floatX)
        # Check some basics of transformation matrix
        assert preprocessor.P_.shape == (self.X.shape[1], self.X.shape[1])
        assert_allclose(np.dot(preprocessor.P_, preprocessor.inv_P_),
                        identity,
                        rtol=1e-4)

        preprocessor = ZCA(filter_bias=0.0)
        preprocessed_X = self.get_preprocessed_data(preprocessor)

        # Check if preprocessed data matrix is white
        assert_allclose(np.cov(preprocessed_X.transpose(), bias=1),
                        identity,
                        rtol=1e-4,
                        atol=1e-4)

        # Check if we obtain correct solution
        zca_transformed_X = np.array(
            [[-1.0199, -0.1832, 1.9528, -0.9603, -0.8162],
             [0.0729, 1.4142, 0.2529, 1.1861, -1.0876],
             [0.9575, -1.1173, -0.5435, -1.4372, -0.1057],
             [0.6348, 1.1258, 0.2692, -0.8893, 1.1669],
             [-0.9769, 0.8297, -1.8676, -0.6055, -0.5096],
             [-1.5700, -0.8389, -0.0931, 0.8877, 1.6089],
             [0.4993, -1.4219, -0.3443, 0.9664, -1.1022],
             [1.4022, 0.1917, 0.3736, 0.8520, 0.8456]])
        assert_allclose(preprocessed_X, zca_transformed_X, rtol=1e-3)
Exemplo n.º 15
0
from pylearn2.datasets.preprocessing import ZCA
from pylearn2.utils import serial

from black_box_dataset import BlackBoxDataset

extra = BlackBoxDataset('extra')

zca = ZCA(filter_bias=.1)

zca.fit(extra.X)

serial.save('zca.pkl', zca)
Exemplo n.º 16
0
from pylearn2.datasets.mnist import MNIST
train = MNIST(which_set = 'train')
from pylearn2.datasets.preprocessing import ZCA
zca = ZCA()
zca.apply(train, can_fit=True)
from pylearn2.utils import serial
serial.save('mnist_zca.pkl', zca)
Exemplo n.º 17
0
layers = [l1, l2, l3, l4, output]

mdl = mlp.MLP(layers,
              input_space=in_space)

trainer = sgd.SGD(learning_rate=.17,
                  batch_size=128,
                  learning_rule=learning_rule.Momentum(.5),
                  # Remember, default dropout is .5
                  cost=Dropout(input_include_probs={'l1': .8},
                               input_scales={'l1': 1.}),
                  termination_criterion=EpochCounter(max_epochs=475),
                  monitoring_dataset={'valid': tst,
                                      'train': trn})

preprocessor = Pipeline([GlobalContrastNormalization(scale=55.), ZCA()])
trn.apply_preprocessor(preprocessor=preprocessor, can_fit=True)
tst.apply_preprocessor(preprocessor=preprocessor, can_fit=False)
serial.save('kaggle_cifar10_preprocessor.pkl', preprocessor)

watcher = best_params.MonitorBasedSaveBest(
    channel_name='valid_y_misclass',
    save_path='kaggle_cifar10_maxout_zca.pkl')

velocity = learning_rule.MomentumAdjustor(final_momentum=.65,
                                          start=1,
                                          saturate=250)

decay = sgd.LinearDecayOverEpoch(start=1,
                                 saturate=500,
                                 decay_factor=.01)
Exemplo n.º 18
0
from galatea.datasets.norb_tiny import NORB_Tiny

train = NORB_Tiny('train')

from pylearn2.datasets.preprocessing import Pipeline, GlobalContrastNormalization, ZCA

pipeline = Pipeline()

pipeline.items = [GlobalContrastNormalization(), ZCA()]

train.apply_preprocessor(pipeline, can_fit=True)

from pylearn2.utils.serial import save

save('norb_tiny_preprocessed_train.pkl', train)
save('norb_tiny_preprocessor.pkl', pipeline)
Exemplo n.º 19
0
if __name__ == "__main__":

    #Load dataset
    train_x, test_x, train_y, test_y = unpack_facedataset(
    )  # 7:3 ratio for train:test

    # preprocess images
    # GCN and ZCA object!!
    # Normalize and then ZCA whitening
    # Normalized data only used on inversion, not in training
    try:
        zca = load("faces/zca.data")
    except Exception as e:
        print("Failed to load preprocessed data from disk, computing zca")
        train_x_normalized = global_contrast_normalize(train_x * 255,
                                                       scale=55.)
        zca = ZCA()
        zca.fit(train_x_normalized)
        save("faces/zca.data", zca)

    x = tf.compat.v1.placeholder(tf.float32, shape=[None, 112 * 92])
    y_ = tf.compat.v1.placeholder(tf.float32, shape=[None, 40])
    model = Model(x, y_)
    session = tf.compat.v1.InteractiveSession()
    session.run(tf.compat.v1.global_variables_initializer())
    #print(f"test {test_y.shape} t: {type(test_y)} ; train {train_y.shape} t: {type(train_y)}")
    model.train(train_x, train_y, session, test_x, test_y, 250)

    perform_inversion(zca, test_x[0::3], model, session)
    # perform_inversion(train_x, test_x[0::3], model, session)
Exemplo n.º 20
0
parser = argparse.ArgumentParser()
parser.add_argument('--folder')
parser.add_argument('-o')

args = parser.parse_args()
model_path = '/data/lisa/exp/wuzhen/conv2d/' + args.folder + '/convolutional_network_best.pkl'

import os
#X = np.load(os.environ['PYLEARN2_DATA_PATH'] + '/faceEmo/test_X.npy')
#y = np.load(os.environ['PYLEARN2_DATA_PATH'] + '/faceEmo/test_y.npy')
#print 'X.shape before', X.shape
X = np.load('test_input.npy')
X = X.astype('float32')
test_set = DenseDesignMatrix(X)

preproc = ZCA()
preproc.fit(test_set.X)
preproc.apply(test_set)
X = test_set.X

X = X.reshape(X.shape[0], 48, 48, 1).astype('float32')

f = open(model_path, 'rb')
mlp = cPickle.load(f)

X_theano = mlp.get_input_space().make_batch_theano()
#X_theano = T.tensor4()
y_theano = mlp.fprop(X_theano)

func = function(inputs=[X_theano], outputs=y_theano)
Exemplo n.º 21
0
from pylearn2.datasets.mnist import MNIST

train = MNIST(which_set='train')
from pylearn2.datasets.preprocessing import ZCA

zca = ZCA()
zca.apply(train, can_fit=True)
from pylearn2.utils import serial

serial.save('mnist_zca.pkl', zca)