简体   繁体   English

Keras输入中的尺寸数不正确

[英]Incorrect number of dimensions in Keras input

I'm attempting to follow along on what I'm thinking is the 5th or 6th simple introductory tutorial for keras that almost but never quite works. 我试图沿着我在想什么,遵循的是对于几乎,但从未完全工作keras第五或第六简单的入门教程。 Stripping everything out, I appear to come down to a problem with the format of my input. 剥离所有内容后,我似乎发现输入格式存在问题。 I read in an array of images, and extract two types, images of sign language ones and images of sign language zeros. 我读入一组图像,然后提取两种类型,手语图像和手语零图像。 I then set up an array of ones and zeros to correspond to what the images actually are, then make sure of sizes and types. 然后,我设置一个由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))

This gives me: 这给了我:

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

which is all good. 一切都很好。 I then load the relevant bits from Keras, using Tensorflow as the backend and try to construct a classifier. 然后,我使用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)

This results in: 结果是:

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

This SO question , I think, suggests that my input shape needs to be altered to have a 4th dimension added to it - though it also says it's the output shape that needs to altered, I haven't been able to find anywhere to specify an output shape, so I'm assuming it is meant that I should alter the input shape to input_shape=(1, 64, 64, 1). 我认为这是一个SO问题 ,建议我需要更改输入形状以添加第4个维度-尽管它也表示需要更改输出形状,但我找不到任何地方可以指定一个输出形状,所以我假设这意味着我应该将输入形状更改为input_shape =(1、64、64、1)。 If I change my input shape however, then I immeadiately get this: 但是,如果更改输入形状,则会立即得到:

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

Which this github issue suggests is because I no longer need to specify the number of samples. 这个 github问题暗示的是因为我不再需要指定样本数量。 So I'm left with the situation of using one input shape and getting one error, or changing it and getting another error. 因此,我剩下的情况是使用一种输入形状并得到一个错误,或者对其进行更改并得到另一个错误。 Reading this and this made me think I might need to reshape my data to include information about the channels in X, but if I add in 阅读让我觉得我可能需要重塑我的数据包含关于X声道的信息,但如果我加入

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

Then I get 然后我得到

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

If I change the reshape to anything else, ie 如果我将重塑更改为其他任何形式,即

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

Then I get a message saying it's unable to reshape the data, so I'm obviously doing something wrong with that, if that is, indeed, the problem. 然后,我收到一条消息,提示它无法重整数据,因此,如果确实存在问题,那么我显然在做错什么。

I have read the suggested Conv2d docs which shed exactly zero light on the matter for me. 我已经阅读了建议的Conv2d文档 ,该文档对我来说完全没有任何意义。 Anyone else able to? 还有其他人可以吗?

At first I used the following data sets (similar to your case): 首先,我使用了以下数据集(与您的情况类似):

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)

And then modified your code as follows to work: 然后按如下所示修改您的代码以使其工作:

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. Changed the shape of X from 410 x 64 x 64 to 410 x 64 x 64 x 1 (with channel 1). X的形状从410 x 64 x 64更改为410 x 64 x 64 x 1 (带有通道1)。

  2. input_shape be the shape of a sample data, that is, 64 x 64 x 1 . input_shape是样本数据的形状,即64 x 64 x 1

  3. Changed the shape of Y using keras.utils.to_categorical() (one-hot encoding with num_classes=10 ). 使用keras.utils.to_categorical()num_classes=10单热编码keras.utils.to_categorical()更改了Y的形状。

  4. Before compiling, Flatten() and Dense() were applied because you want categorical_crossentropy . 在编译之前,由于需要categorical_crossentropy因此应用了Flatten()Dense()

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

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