ValueError:要传递给模型的Numpy数组列表不是模型期望的大小

时间:2020-07-06 10:04:11

标签: python keras

我已经在Internet上下载了一段代码并想自己运行它,但是我遇到了这个错误并且无法解决它。我很伤心,请帮助我!真诚的!

这是一些代码部分

def splitTrainTestSet(X, y, testRatio, randomState=345):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=testRatio, random_state=randomState,
                                                        stratify=y)
    return X_train, X_test, y_train, y_test


def applyPCA(X, numComponents=75):
    newX = np.reshape(X, (-1, X.shape[2]))
    pca = PCA(n_components=numComponents, whiten=True)
    newX = pca.fit_transform(newX)
    newX = np.reshape(newX, (X.shape[0],X.shape[1], numComponents))
    return newX, pca


def padWithZeros(X, margin=2):
    newX = np.zeros((X.shape[0] + 2 * margin, X.shape[1] + 2* margin, X.shape[2]))
    x_offset = margin
    y_offset = margin
    newX[x_offset:X.shape[0] + x_offset, y_offset:X.shape[1] + y_offset, :] = X
    return newX


def createImageCubes(X, y, windowSize=5, removeZeroLabels = True):
    margin = int((windowSize - 1) / 2)
    zeroPaddedX = padWithZeros(X, margin=margin)
    # split patches
    patchesData = np.zeros((X.shape[0] * X.shape[1], windowSize, windowSize, X.shape[2]))
    patchesLabels = np.zeros((X.shape[0] * X.shape[1]))
    patchIndex = 0
    for r in range(margin, zeroPaddedX.shape[0] - margin):
        for c in range(margin, zeroPaddedX.shape[1] - margin):
            patch = zeroPaddedX[r - margin:r + margin + 1, c - margin:c + margin + 1]   
            patchesData[patchIndex, :, :, :] = patch
            patchesLabels[patchIndex] = y[r-margin, c-margin]
            patchIndex = patchIndex + 1
    if removeZeroLabels:
        patchesData = patchesData[patchesLabels>0,:,:,:]
        patchesLabels = patchesLabels[patchesLabels>0]
        patchesLabels -= 1
    return patchesData, patchesLabels

这是代码的另一部分

X, y = loadData(dataset)

K = X.shape[2]
K = 32 if dataset == 'IP' else 15
X, pca = applyPCA(X, numComponents=K)
#principalComponents = pca.fit_transform(X)
X, y = createImageCubes(X, y, windowSize=windowSize)
Xtrain, Xtest, ytrain, ytest = splitTrainTestSet(X, y, test_ratio)

input_shape = (27, 27, 32, 1)
output_channels = 16

Xtrain = Xtrain.reshape(-1, windowSize, windowSize, K, 1)
Xtest = Xtest.reshape(-1, windowSize, windowSize, K, 1)
print("X_train shape :", Xtrain.shape)
print("Xtest shape : ", Xtest.shape)
ytrain = np_utils.to_categorical(ytrain)
ytest = np_utils.to_categorical(ytest)
print("ytrain shape : ", ytrain.shape)
print("ytest shape : ", ytest.shape)
model = build_model(input_shape, output_channels)
model.summary()
from keras.callbacks import ModelCheckpoint
# checkpoint
filepath = "dice_coefficient.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='dice_coefficient', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
history = model.fit([np.array(Xtrain)],np.array(ytrain),batch_size=256, epochs=450, callbacks=callbacks_list) 

错误在这里

history = model.fit([np.array(Xtrain)],np.array(ytrain),batch_size=256, epochs=450, callbacks=callbacks_list)

错误信息:

Error when checking model target: the list of Numpy arrays that you are passsing to your model is not the size the model expected. Expected to see 2array(s),but instead got the following list of 1 arrays: [array([[0., 0., 0., ..., 0., 0., 0.]                                               ,
       [0., 1., 0., ..., 0., 0., 0.],
       [0., 0., 1., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 1., 0., 0.],
       [0., 0., 1., ..., 0., 0., 0....

我真的很想哭,真伤心,一天的工作我解决不了。

0 个答案:

没有答案