简体   繁体   English

(使用Keras)在Tensorflow中'InvalidArgumentError:不兼容的形状:[10,2] vs. [10]'是什么原因?

[英]What is the cause of 'InvalidArgumentError: Incompatible shapes: [10,2] vs. [10]' in tensorflow (with Keras)?

I am trying to use a CNN for object detection using Tensorflow with Keras. 我正在尝试使用Tensorflow和Keras将CNN用于对象检测。 I am fairly new to this, so I was using a tutorial as a guide but with my own set and a few other things. 我对此还很陌生,因此我使用了一个教程作为指南,但同时拥有自己的设置和其他一些东西。 The error I get is Tensorflow's incompatible shapes with [x,2] vs. [x], where x is any number of training images I have and 2 is the number of classes. 我得到的错误是Tensorflow与[x,2]与[x]不兼容的形状,其中x是我拥有的任意数量的训练图像,而2是类的数量。 I was using a small number of images just for testing, but I am pretty sure that is not the problem? 我仅使用少量图像进行测试,但是我可以确定这不是问题吗?

I have tried different multiples of training images with no luck, and I have looked at model.summary() to see if the model is laid out exactly how I want it. 我尝试了不同倍数的训练图像,但是没有运气,我查看了model.summary()来查看模型是否按照我想要的方式进行了布局。 Also, I have printed the shapes of my training images and their labels, and they look correct. 另外,我已经打印了训练图像的形状及其标签,它们看起来正确。

The images are of size 28 x 28 pixels, with a flat size of 784 and a full shape of (28,28,1), 1 being the number of channels (greyscale). 图像尺寸为28 x 28像素,平面尺寸为784,完整形状为(28,28,1),其中1为通道数(灰度)。 I have only two classes, and only 10 training images total (I can get more if that is thought to be the problem). 我只有两节课,总共只有10张训练图像(如果认为是问题的话,我可以得到更多)。

model = Sequential()

model.add(InputLayer(input_shape=(img_size_flat,)))

model.add(Reshape(img_shape_full))

model.add(Conv2D(kernel_size=5, strides=1, filters=16, padding='same',
                 activation='relu', name='layer_conv1'))
model.add(MaxPooling2D(pool_size=2, strides=2))

model.add(Conv2D(kernel_size=5, strides=1, filters=36, padding='same',
                 activation='relu', name='layer_conv2'))
model.add(MaxPooling2D(pool_size=2, strides=2))

model.add(Flatten())

model.add(Dense(128, activation='relu'))

model.add(Dense(num_classes, activation='softmax'))

from tensorflow.python.keras.optimizers import Adam
optimizer = Adam(lr=1e-3)

model.compile(optimizer=optimizer,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

from tensorflow.python.keras.utils import to_categorical
model.fit(x=data.train,
    y=to_categorical(data.train_labels),
    batch_size=128, epochs=1)

I used to_categorical() on the labels only because they were somehow being converted to ints. 我仅在标签上使用to_categorical(),因为它们以某种方式被转换为整数。 I checked that they retained their correct values and such. 我检查了它们是否保留了正确的值等等。

I printed the model summary to check the layout: 我打印了模型摘要以检查布局:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
reshape (Reshape)            (None, 28, 28, 1)         0         
_________________________________________________________________
layer_conv1 (Conv2D)         (None, 28, 28, 16)        416       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 16)        0         
_________________________________________________________________
layer_conv2 (Conv2D)         (None, 14, 14, 36)        14436     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 36)          0         
_________________________________________________________________
flatten (Flatten)            (None, 1764)              0         
_________________________________________________________________
dense (Dense)                (None, 128)               225920    
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 258       
=================================================================
Total params: 241,030
Trainable params: 241,030
Non-trainable params: 0
_________________________________________________________________
None

I printed the size of the numpy data: 我打印了numpy数据的大小:

print(data.train.shape)
print(data.train_labels.shape)

which prints 哪个打印

(10, 784) #This is the shape of the images
(10, 2) #This is the shape of the labels

Error: 错误:

2019-04-08 10:46:40.239226: I tensorflow/stream_executor/dso_loader.cc:152] successfully opened CUDA library cublas64_100.dll locally
Traceback (most recent call last):
  File "C:/Users/bunja/Dev/testCellDet/project/venv/main.py", line 182, in <module>
    batch_size=128, epochs=1)
  File "C:\Users\bunja\Miniconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 880, in fit
    validation_steps=validation_steps)
  File "C:\Users\bunja\Miniconda3\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py", line 329, in model_iteration
    batch_outs = f(ins_batch)
  File "C:\Users\bunja\Miniconda3\lib\site-packages\tensorflow\python\keras\backend.py", line 3076, in __call__
    run_metadata=self.run_metadata)
  File "C:\Users\bunja\Miniconda3\lib\site-packages\tensorflow\python\client\session.py", line 1439, in __call__
    run_metadata_ptr)
  File "C:\Users\bunja\Miniconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [10,2] vs. [10]
     [[{{node metrics/acc/Equal}}]]
     [[{{node loss/mul}}]]

As can be seen, the summary shows dense_1 as having an output shape of (None, 2). 可以看出,该摘要显示了density_1为具有(None,2)的输出形状。 Is this the place I have a problem since I have an error of Incompatible shapes: [x,2] vs. [x]? 这是我有问题的地方吗,因为我遇到形状不兼容的错误:[x,2]与[x]? I have checked over the tutorial I originally used to learn this stuff and found no major differences. 我已经检查了我最初用来学习这些内容的教程,发现没有重大区别。 I am still new to this, so it may be something little, and I may be missing some info so please ask if you have any questions. 我对此还很陌生,所以可能有点少,并且我可能缺少一些信息,所以请问您是否有任何问题。 Thank you!!!!! 谢谢!!!!!

Extra info: 额外信息:

GPU: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335 GPU:GeForce GTX 1080 Major:6 minor:1 memoryClockRate(GHz):1.7335

Tensorflow version: 1.13.1 Tensorflow版本:1.13.1

Python version: Python 3.7.3 Python版本:Python 3.7.3

Here is the code for comment on to_categorical shape: 以下是对to_categorical形状进行注释的代码:

print(data.train_labels.shape)
print()
print(to_categorical(data.train_labels).shape)

Output: 输出:

(10, 2)

(10, 2, 2)

I have a feeling this could be the source of my error? 我觉得这可能是我的错误的根源? But I am not sure how to fix it... 但是我不确定如何解决...

to_categorical is usually used when you have the label in the list format and you need to perform one-hot encoding in order to transform it to the correct shape to feed it to models during training. 当标签具有列表格式并且需要执行one-hot编码以便将其转换为正确的形状以在训练期间将其输入模型时,通常使用to_categorical

But in your case, your label is already of the same shape as you defined in your model, so the one-hot encoding is not necessary. 但是在您的情况下,标签已经具有与模型中定义的形状相同的形状,因此不需要one-hot编码。

You can view the None as batch_size and this will give you a clearer picture of how the data are transformed from the input to the output. 您可以将None作为batch_size查看,这将使您更清楚地了解数据如何从输入转换为输出。

And thanks! 谢谢!

I just tried it myself. 我只是自己尝试过。 It worked very well for me. 对我来说效果很好。 Maybe it's your train_data. 也许是您的train_data。 Can you show us that? 你能告诉我们吗?

If I understand you correctly, you use input like this: 如果我对您的理解正确,则可以使用以下输入:

train_x = np.random.rand(5,784)
train_y = np.array([[1,0],[0,1],[0,1],[1,0],[0,1]])

If I train the model with these values, I will not get any mistakes. 如果我使用这些值训练模型,则不会出错。 And also the predictions work. 预测也起作用。

print(model.predict(np.random.rand(1,784)))

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

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