I'm trying to use my own dataset that consists of two categories. I do not understand how can solve this. How can I fix this? it seems like the model gets the shape of the image as input instead of the actual image.
print X_train.shape
print y_train.shape
print X_test.shape
print y_test.shape
(55, 3, 150, 150)
(55, 1)
(14, 3, 150, 150)
(14, 1)
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
K.set_image_dim_ordering('th')
model = Sequential()
#model.add(Convolution2D(32, kernel_size=(3, 3), input_shape=(3, IMG_SIZE, IMG_SIZE)))
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(3,150,150)))
#model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('sigmoid'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])

ValueError: Error when checking target: expected activation_4 to have 2 dimensions, but got array with shape (14, 3, 150, 150)
I'm also having this issue:
keras 2.1.2
tensorflow 1.4.0
def create_model(config):
input_tensor = Input(shape=(config.target_img_size[0], config.target_img_size[1], config.num_channels), name="InputTensor")
model = Conv2D(32, (10, 10), activation='relu', use_bias=True, name="InputConv")(input_tensor)
model = Conv2D(32, (7, 7), activation='relu', name="Conv2")(model)
model = Conv2D(32, (5, 5), activation='relu', name="Conv3")(model)
model = Conv2D(32, (3, 3), activation='relu', name="Conv4")(model)
model = Dropout(0.25, name="Drop1")(model)
model = Dense(1024, activation='relu', name='Dense1')(model)
model = Dense(512, activation='relu', name='Dense2')(model)
model = Dropout(0.25, name="Drop2")(model)
predictions = Dense(1, activation='sigmoid', name="OutputBinary")(model)
out_model = Model(inputs=input_tensor, outputs=predictions)
print(out_model)
out_model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
return out_model
Then later I call fit_generator() with an x of (8, 224, 224, 3) and a y of (8,2) (used keras.utils.np_utils.to_categorical to produce y)
and I get:
File "/opt/anaconda3/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(args, *kwargs)
File "/opt/anaconda3/lib/python3.5/site-packages/keras/engine/training.py", line 2173, in fit_generator
class_weight=class_weight)
File "/opt/anaconda3/lib/python3.5/site-packages/keras/engine/training.py", line 1859, in train_on_batch
check_batch_axis=True)
File "/opt/anaconda3/lib/python3.5/site-packages/keras/engine/training.py", line 1440, in _standardize_user_data
exception_prefix='target')
File "/opt/anaconda3/lib/python3.5/site-packages/keras/engine/training.py", line 149, in _standardize_input_data
str(array.shape))
ValueError: Error when checking target: expected OutputBinary to have 4 dimensions, but got array with shape (8, 2)
For myself, I solved this. I had to introduce a Flatten() layer inbetween my Conv2D layers and my Dense layers and that fixed it.
Also had to change my truth vector from shape (num_batches, 2) to (num_batches, 1) since my last layer has just the 1 output.
Why was there a need for the layer_flatten? I also solved a similar problem like this but don't understand the reason why?
Well, it turns out that you can go from a Conv2D to an implicit "Dense2D" I think was the problem. I don't know what the actual intermediate shape was, but my (num_batch, 224, 224, 32) output of my Conv2D layer was probably resulting in a tensor of shape (num_batch, 224, 224, 1024) for the first Dense layer, instead of a shape of (num_batch, 1024) like I was expecting. The Flatten() layer allows Keras/Tensorflow to reduce the dimensionality, aka to unpack the 224 x 224 x 32 convolutional filters from 32 2D spaces down into 1 1D space, which can then be normally connected to 1024 new 1D nodes like we would expect. I think that was the problem for my case since I wasn't using Flatten() but I don't know if that's the same problem OP had, since he is already using Flatten().
Most helpful comment
For myself, I solved this. I had to introduce a Flatten() layer inbetween my Conv2D layers and my Dense layers and that fixed it.
Also had to change my truth vector from shape (num_batches, 2) to (num_batches, 1) since my last layer has just the 1 output.