Exemplo n.º 1
0
def convert_orig_to_np():
    from pylearn2.datasets.filetensor import read
    import gzip
    import cPickle
    # Load data
    path_orig = os.environ['ML_DATA_PATH']+'/norb_orig/'
    prefix_train = path_orig+'smallnorb-5x46789x9x18x6x2x96x96-training-'
    train_cat = read(gzip.open(prefix_train+'cat.mat.gz'))
    train_dat = read(gzip.open(prefix_train+'dat.mat.gz'))
    train_info = read(gzip.open(prefix_train+'info.mat.gz'))
    prefix_test = path_orig+'smallnorb-5x01235x9x18x6x2x96x96-testing-'
    test_cat = read(gzip.open(prefix_test+'cat.mat.gz'))
    test_dat = read(gzip.open(prefix_test+'dat.mat.gz'))
    test_info = read(gzip.open(prefix_test+'info.mat.gz'))
    
    # Save originals matrices to file
    files = (('train_cat', train_cat), ('train_dat_96', train_dat), ('train_info', train_info), ('test_cat', test_cat), ('test_dat_96', test_dat), ('test_info', test_info))
    for fname, tensor in files:
        print 'Saving to ', fname, '...'
        with gzip.open(path+fname+'.pkl.gz','wb') as f:
            cPickle.dump(tensor, f)

    # Save downscaled version too
    w = 48
    files = (('test_dat', test_dat),)
    for fname, tensor in files:
        print 'Generating downscaled version ' + fname + '...'
        left = reshape_images(tensor[:,0,:,:], (w,w))
        right = reshape_images(tensor[:,1,:,:], (w,w)) 
        result = np.zeros((tensor.shape[0], 2, w, w), dtype=np.uint8)
        result[:,0,:,:] = left
        result[:,1,:,:] = right
        f = gzip.open(path+fname+'_'+str(w)+'.pkl.gz', 'wb')
        cPickle.dump(result, f)
        f.close()
Exemplo n.º 2
0
def load_filetensor(fname):
    f = None
    try:
        if not os.path.exists(fname):
            fname = fname+'.gz'
            f = gzip.open(fname)
        elif fname.endswith('.gz'):
            f = gzip.open(fname)
        else:
            f = open(fname)
        d = ft.read(f)
    finally:
        if f:
            f.close()

    return d
Exemplo n.º 3
0
def convert_orig_to_np():
    from pylearn2.datasets.filetensor import read
    import gzip
    import cPickle
    # Load data
    path_orig = './data/small_norb/mat/'
    prefix_train = path_orig+'smallnorb-5x46789x9x18x6x2x96x96-training-'
    train_cat = read(gzip.open(prefix_train+'cat.mat.gz'))
    train_dat = read(gzip.open(prefix_train+'dat.mat.gz'))
    train_info = read(gzip.open(prefix_train+'info.mat.gz'))
    prefix_test = path_orig+'smallnorb-5x01235x9x18x6x2x96x96-testing-'
    test_cat = read(gzip.open(prefix_test+'cat.mat.gz'))
    test_dat = read(gzip.open(prefix_test+'dat.mat.gz'))
    test_info = read(gzip.open(prefix_test+'info.mat.gz'))
    
    # Save originals matrices to file
    files = (('train_cat', train_cat), ('train_dat_96', train_dat), ('train_info', train_info), ('test_cat', test_cat), ('test_dat_96', test_dat), ('test_info', test_info))
    for fname, tensor in files:
        print 'Saving to ', fname, '...'
        with gzip.open(path+fname+'.pkl.gz','wb') as f:
            cPickle.dump(tensor, f)
    
    # Save downscaled version too
    w = 48
    files = (('train_dat', train_dat),('test_dat', test_dat))
    for fname, tensor in files:
        print 'Generating downscaled version ' + fname + '...'
        left = reshape_images(tensor[:,0,:,:], (w,w))
        right = reshape_images(tensor[:,1,:,:], (w,w)) 
        result = np.zeros((tensor.shape[0], 2, w,w), dtype=np.uint8)
        result[:,0,:,:] = left
        result[:,1,:,:] = right
        f = gzip.open(path+fname+'_'+str(w)+'.pkl.gz', 'wb')
        cPickle.dump(result, f)
        f.close()

    w = 32
    files = (('train_dat', train_dat),('test_dat', test_dat))
    for fname, tensor in files:
        print 'Generating downscaled version ' + fname + '...'
        left = reshape_images(tensor[:,0,:,:], (w,w))
        right = reshape_images(tensor[:,1,:,:], (w,w)) 
        result = np.zeros((tensor.shape[0], 2, w, w), dtype=np.uint8)
        result[:,0,:,:] = left
        result[:,1,:,:] = right
        f = gzip.open(path+fname+'_'+str(w)+'.pkl.gz', 'wb')
        cPickle.dump(result, f)
        f.close()