Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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. 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). I have only two classes, and only 10 training images total (I can get more if that is thought to be the problem).

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. 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:

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). Is this the place I have a problem since I have an error of Incompatible shapes: [x,2] vs. [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

Tensorflow version: 1.13.1

Python version: Python 3.7.3

Here is the code for comment on to_categorical shape:

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...

like image 212
Jacob Bunzel Avatar asked Apr 08 '19 13:04

Jacob Bunzel


1 Answers

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.

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.

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.

And thanks!

like image 149
danyfang Avatar answered Oct 17 '22 02:10

danyfang