繁体   English   中英

Keras输入中的尺寸数不正确

[英]Incorrect number of dimensions in Keras input

我试图沿着我在想什么,遵循的是对于几乎,但从未完全工作keras第五或第六简单的入门教程。 剥离所有内容后,我似乎发现输入格式存在问题。 我读入一组图像,然后提取两种类型,手语图像和手语零图像。 然后,我设置一个由1和0组成的数组以对应于实际的图像,然后确定大小和类型。

import numpy as np 
from subprocess import check_output

print(check_output(["ls", "../data/keras/"]).decode("utf8"))
## load dataset of images of sign language numbers
x = np.load('../data/keras/npy_dataset/X.npy')
# Get the zeros and ones, construct a list of known values (Y)
X = np.concatenate((x[204:409], x[822:1027] ), axis=0) # from 0 to 204 is zero sign and from 205 to 410 is one sign 
Y = np.concatenate((np.zeros(205), np.ones(205)), axis=0).reshape(X.shape[0],1)

# test shape and type
print("X shape: " , X.shape)
print("X class: " , type(X))
print("Y shape: " , Y.shape)
print("Y type: " , type(Y))

这给了我:

X shape:  (410, 64, 64)
X class:  <class 'numpy.ndarray'>
Y shape:  (410, 1)
Y type:  <class 'numpy.ndarray'>

一切都很好。 然后,我使用Tensorflow作为后端从Keras加载相关位,并尝试构建分类器。

# get the relevant keras bits. 
from keras.models import Sequential
from keras.layers import Convolution2D 
# construct a classifier

classifier = Sequential() # initialize neural network
classifier.add(Convolution2D(32, (3, 3), input_shape=(410, 64, 64), activation="relu", data_format="channels_last"))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

classifier.fit(X, Y, batch_size=32, epochs=10, verbose=1)

结果是:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (410, 64, 64)

我认为这是一个SO问题 ,建议我需要更改输入形状以添加第4个维度-尽管它也表示需要更改输出形状,但我找不到任何地方可以指定一个输出形状,所以我假设这意味着我应该将输入形状更改为input_shape =(1、64、64、1)。 但是,如果更改输入形状,则会立即得到:

ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5

这个 github问题暗示的是因为我不再需要指定样本数量。 因此,我剩下的情况是使用一种输入形状并得到一个错误,或者对其进行更改并得到另一个错误。 阅读让我觉得我可能需要重塑我的数据包含关于X声道的信息,但如果我加入

X = X.reshape(X.shape[0], 64, 64, 1)
print(X.shape)

然后我得到

ValueError: Error when checking target: expected conv2d_1 to have 4 dimensions, but got array with shape (410, 1)

如果我将重塑更改为其他任何形式,即

X = X.reshape(X.shape[0], 64, 64, 2)

然后,我收到一条消息,提示它无法重整数据,因此,如果确实存在问题,那么我显然在做错什么。

我已经阅读了建议的Conv2d文档 ,该文档对我来说完全没有任何意义。 还有其他人可以吗?

首先,我使用了以下数据集(与您的情况类似):

import numpy as np
import keras

X = np.random.randint(256, size=(410, 64, 64))
Y = np.random.randint(10, size=(410, 1))
x_train = X[:, :, :, np.newaxis]
y_train = keras.utils.to_categorical(Y, num_classes=10)

然后按如下所示修改您的代码以使其工作:

from keras.models import Sequential
from keras.layers import Convolution2D, Flatten, Dense 

classifier = Sequential() # initialize neural network
classifier.add(Convolution2D(32, (3, 3), input_shape=(64, 64, 1), activation="relu", data_format="channels_last"))
classifier.add(Flatten())
classifier.add(Dense(10, activation='softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
classifier.fit(x_train, y_train, batch_size=32, epochs=10, verbose=1)
  1. X的形状从410 x 64 x 64更改为410 x 64 x 64 x 1 (带有通道1)。

  2. input_shape是样本数据的形状,即64 x 64 x 1

  3. 使用keras.utils.to_categorical()num_classes=10单热编码keras.utils.to_categorical()更改了Y的形状。

  4. 在编译之前,由于需要categorical_crossentropy因此应用了Flatten()Dense()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM