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)
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)
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
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
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_))
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) batch_size = mlp.batch_size
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)
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)