如何将2d numpy数组重塑为4d(没有ValueError)

时间:2016-07-11 09:54:51

标签: python numpy image-processing h5py

我有将.csv文件转换为h5(二进制)的脚本, .csv文件列包含:Image(作为numpy数组)和另外3个浮点数。

import os

import numpy as np
from pandas.io.parsers import read_csv
from sklearn.utils import shuffle
import h5py

FTRAIN = 'training.csv'
FTEST = 'test.csv'


def load(test=False, cols=None):
    """Loads data from FTEST if *test* is True, otherwise from FTRAIN.
    Pass a list of *cols* if you're only interested in a subset of the
    target columns.
    """
    fname = FTEST if test else FTRAIN
    df = read_csv(os.path.expanduser(fname))  # load pandas dataframe

    # The Image column has pixel values separated by space; convert
    # the values to numpy arrays:
    df['Image'] = df['Image'].apply(lambda im: np.fromstring(im, sep=' '))

    if cols:  # get a subset of columns
        df = df[list(cols) + ['Image']]

    print(df.count())  # prints the number of values for each column
    df = df.dropna()  # drop all rows that have missing values in them

    X = np.vstack(df['Image'].values) / 255.  # scale pixel values to [0, 1]
    X = X.astype(np.float32)

    if not test:  # only FTRAIN has any target columns
        y = df[df.columns[:-1]].values
        y = (y - 48) / 48  # scale target coordinates to [-1, 1]
        X, y = shuffle(X, y, random_state=42)  # shuffle train data
        y = y.astype(np.float32)
    else:
        y = None

    return X, y

def writeHdf5(t,data,label=None):
    with h5py.File(os.getcwd()+ '/'+t + '_data.h5', 'w') as f:
        f['data'] = data
        if label is not None:
            f['label'] = label
    with open(os.getcwd()+ '/'+t + '_data_list.txt', 'w') as f:
        f.write(os.getcwd()+ '/' +t + '_data.h5\n')


X, y = load()
X = X.reshape((X.shape[0],3,96,96))
sep = 1600
writeHdf5('train',X[0:sep],y[0:sep])
writeHdf5('val',X[sep:],y[sep:])

X,y= load(True)
X = X.reshape((X.shape[0],3,96,96))
writeHdf5('test',X,y)

当我想要将X从2d重塑为4d时:

 X = X.reshape((X.shape[0],3,256,256)

X形状:(10464,2622)(2d - 转换前)

它向我显示以下错误:

ValueError: total size of new array must be unchanged
我做错了什么?感谢。

0 个答案:

没有答案