コード例 #1
0
ファイル: data_loaders.py プロジェクト: deep0learning/LMADA
def load_usps(invert=False, zero_centre=False, val=False, scale28=False):
    #
    #
    # Load USPS
    #
    #

    print('Loading USPS...')

    if val:
        d_usps = usps.USPS()
    else:
        d_usps = usps.USPS(n_val=None)

    d_usps.train_X = d_usps.train_X[:]
    d_usps.val_X = d_usps.val_X[:]
    d_usps.test_X = d_usps.test_X[:]
    d_usps.train_y = d_usps.train_y[:]
    d_usps.val_y = d_usps.val_y[:]
    d_usps.test_y = d_usps.test_y[:]

    if scale28:

        def _resize_tensor(X):
            X_prime = np.zeros((X.shape[0], 1, 28, 28), dtype=np.float32)
            for i in range(X.shape[0]):
                X_prime[i, 0, :, :] = resize(X[i, 0, :, :], (28, 28),
                                             mode='constant')
            return X_prime

        # Scale 16x16 to 28x28
        d_usps.train_X = _resize_tensor(d_usps.train_X)
        d_usps.val_X = _resize_tensor(d_usps.val_X)
        d_usps.test_X = _resize_tensor(d_usps.test_X)

    if invert:
        # Invert
        d_usps.train_X = 1.0 - d_usps.train_X
        d_usps.val_X = 1.0 - d_usps.val_X
        d_usps.test_X = 1.0 - d_usps.test_X

    if zero_centre:
        d_usps.train_X = d_usps.train_X * 2.0 - 1.0
        d_usps.test_X = d_usps.test_X * 2.0 - 1.0

    print(
        'USPS: train: X.shape={}, y.shape={}, val: X.shape={}, y.shape={}, test: X.shape={}, y.shape={}'
        .format(d_usps.train_X.shape, d_usps.train_y.shape, d_usps.val_X.shape,
                d_usps.val_y.shape, d_usps.test_X.shape, d_usps.test_y.shape))

    print('USPS: train: X.min={}, X.max={}'.format(d_usps.train_X.min(),
                                                   d_usps.train_X.max()))

    d_usps.n_classes = 10

    return d_usps
コード例 #2
0
def test_val_0():
    from batchup.datasets import usps

    ds = usps.USPS(n_val=0)

    assert ds.train_X.shape == (7291, 1, 16, 16)
    assert ds.train_X.dtype == np.float32

    assert ds.train_y.shape == (7291, )
    assert ds.train_y.dtype == np.int32

    assert ds.val_X.shape == (0, 1, 16, 16)
    assert ds.val_X.dtype == np.float32

    assert ds.val_y.shape == (0, )
    assert ds.val_y.dtype == np.int32

    assert ds.test_X.shape == (2007, 1, 16, 16)
    assert ds.test_X.dtype == np.float32

    assert ds.test_y.shape == (2007, )
    assert ds.test_y.dtype == np.int32