How to solve the shapes of model are incompatible? - python

This is my train & test split shape:
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
----------------------
(120000, 72)
(12000, 72)
(120000, 6)
(12000, 6)
I reshape the data for CNN:
X_train = X_train.reshape(len(X_train), X_train.shape[1], 1)
X_test = X_test.reshape(len(X_test), X_test.shape[1], 1)
X_train.shape, X_test.shape
-------------------------------------------------------------------
((120000, 72, 1), (12000, 72, 1))
Making the deep learning function:
def model():
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=6, activation='relu',
padding='same', input_shape=(72, 1)))
model.add(BatchNormalization())
# adding a pooling layer
model.add(MaxPooling1D(pool_size=(3), strides=2, padding='same'))
model.add(Conv1D(filters=64, kernel_size=6, activation='relu',
padding='same', input_shape=(72, 1)))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=(3), strides=2, padding='same'))
model.add(Conv1D(filters=64, kernel_size=6, activation='relu',
padding='same', input_shape=(72, 1)))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=(3), strides=2, padding='same'))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
When i fit it shows error: Shapes (32, 6) and (32, 3) are incompatible
model = model()
model.summary()
logger = CSVLogger('logs.csv', append=True)
his = model.fit(X_train, y_train, epochs=30, batch_size=32,
validation_data=(X_test, y_test), callbacks=[logger])
---------------------------------------------------------------
ValueError: Shapes (32, 6) and (32, 3) are incompatible
What problem and how can i solve it?

Your targets are 6 dimensional, but your prediction is 3 dimensional, thus the mismatch. You should have Dense(6) as the last layer of your model, not Dense(3)

Related

Error during fitting of data on model. Target output needs to match

When trying to do:
model_training.fit(X_train, Y_train, validation_data=(X_test, Y_test), epochs=100)
I get this error:
A target array with shape (25000, 2) was passed for an output of shape (None, 3, 2) while using as loss binary_crossentropy. This loss expects targets to have the same shape as the output.
Here is my model setup.
any ideas appreciated!
model_training = Sequential()
# input_layer = keras.Input(shape=(300,1))
model_training.add(InputLayer(input_shape=(300,1)))
model_training.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='tanh'))
model_training.add(Dropout(0.2))
model_training.add(MaxPooling1D(pool_size=3))
model_training.add(Dropout(0.2))
model_training.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='tanh'))
model_training.add(Dropout(0.2))
model_training.add(MaxPooling1D(pool_size=3))
model_training.add(Dropout(0.2))
model_training.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='tanh'))
model_training.add(Dropout(0.2))
model_training.add(MaxPooling1D(pool_size=3))
model_training.add(Dropout(0.2))
model_training.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='tanh'))
model_training.add(Dropout(0.2))
model_training.add(MaxPooling1D(pool_size=3))
model_training.add(Dropout(0.2))
# model_training.add(LSTM(300, dropout=0.2, recurrent_dropout=0.2))
#model.add(Dropout(0.2))
model_training.add(Dense(2))
model_training.add(Activation('sigmoid'))
model_training.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model_training.summary())
X_train shape: (25000, 300)
Y_train shape: (25000, 2)
model_training.add(Flatten())
This fixed it for me.
Needed to flatten the data to match the output.

ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 256, 256]

Everything is okay until I convert my image to grayscale. So the rgb's shape is (256, 256, 3) but grayscale has (256, 256). When I feed it, I get that error.
network = Sequential()
network.add(Convolution2D(32, kernel_size=(3, 3),strides=1,activation='relu',input_shape=(256, 256)))
network.add(MaxPooling2D((2, 2)))
# network.add(Convolution2D(32, kernel_size=(3, 3), strides=1, activation='relu'))
# network.add(MaxPooling2D((2, 2)))
network.add(Convolution2D(64, kernel_size=(3, 3), strides=1, activation='relu'))
network.add(MaxPooling2D((2, 2)))
# network.add(Convolution2D(64, kernel_size=(3, 3), strides=1, activation='relu'))
# network.add(MaxPooling2D((2, 2)))
network.add(Convolution2D(128, kernel_size=(3, 3), strides=1, activation='relu'))
network.add(MaxPooling2D((2, 2)))
# network.add(Convolution2D(128, kernel_size=(3, 3), strides=1, activation='relu'))
# network.add(MaxPooling2D((2, 2)))
network.add(Flatten())
network.add(Dense(256, activation = 'relu'))
network.add(Dense(2, activation = 'softmax'))
checkpoint_path = os.path.join("/---------/grayscale", "weights.best.hdf5")
checkpoint = ModelCheckpoint(checkpoint_path, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10)
callbacks_list = [checkpoint, es]
network.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
You have to feed images of shape 256x256x1 in your network.
To convert your initial x_train into your new X_train:
X_train=np.reshape(x_train,(x_train.shape[0], x_train.shape[1],x_train.shape[2],1))
and finally change your input_shape from input_shape=(256,256) to input_shape=(256,256,1)

Got error: strides should be of length 1, 3 or 5 but was 2 for 2D CNN - LSTM

I tried to use CNN LSTM to classify but I got this error message: strides should be of length 1, 3 or 5 but was 2 for 2D CNN - LSTM. Here is my code:
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1],X_train.shape[2],X_train.shape[3] ))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1], X_test.shape[2],X_test.shape[3]))
print("build model...")
model = Sequential()
model.add(TimeDistributed(Conv2D(64, (3,3), padding='same', activation='relu',data_format="channels_last"),
input_shape = X_train.shape))
model.add(TimeDistributed( Conv2D(64, (3,3), padding='same', activation='relu')))
model.add(TimeDistributed(MaxPooling2D((2))))
model.add(TimeDistributed(Flatten()))
model.add(Dropout(0.5))
model.add(LSTM(256, return_sequences=False, dropout=0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=10, epochs=70, validation_data=(X_test, y_test))
Please help me fix it. Thanks!

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

I have train & test image data for which Shapes are given below.
X_test.shape , y_test.shape , X_train.shape , y_train.shape
((277, 128, 128, 3), (277, 1), (1157, 128, 128, 3), (1157, 1))
I am training a model
def baseline_model():
filters = 100
model = Sequential()
model.add(Conv2D(filters, (3, 3), input_shape=(128, 128, 3), padding='same', activation='relu'))
#model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Conv2D(filters, (3, 3), activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Flatten())
model.add(Conv2D(filters, (3, 3), activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(filters, (3, 3), activation='relu', padding='same'))
model.add(Activation('linear'))
model.add(BatchNormalization())
model.add(Dense(512, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
# Compile model
lrate = 0.01
epochs = 10
decay = lrate/epochs
sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
model.compile(loss='sparse_categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
print(model.summary())
return model
But I am getting an error Given below
Error when checking target: expected dense_35 to have 4 dimensions,
but got array with shape (1157, 1)
Please tell me what mistake I am making and how to fix this. I have attached snapshot of model summary
One thing you have probably forgotten to do is adding a Flatten layer right before the first Dense layer:
model.add(BatchNormalization())
model.add(Flatten()) # flatten the output of previous layer before feeding it to Dense layer
model.add(Dense(512, activation='relu'))
You need it because Dense layer does not flatten its input; rather, it is applied on the last dimension.
Although dense_35 needs to feed with 4 dimension data, according to the error, the network feed with 2 dimension data which is the label vector.

ValueError: Error when checking target: expected dense_9 to have shape (7,) but got array with shape (1,)

Please help. I'm getting this error on running the training code on colab
want to do multi label classification (7 distinct output labels)
ValueError: Error when checking target: expected dense_9 to have shape (7,) but got array with shape (1,)
My code is as follows:
with open("data/fer2013/fer2013.csv") as f:
x_train = np.array(x_train, 'float32')
y_train = np.array(y_train, 'float32')
x_test = np.array(x_test, 'float32')
y_test = np.array(y_test, 'float32')
x_train /= 255 #normalize inputs between [0, 1]
x_test /= 255
x_train = x_train.reshape(x_train.shape[0], 48, 48, 1)
x_train = x_train.astype('float32')
x_test = x_test.reshape(x_test.shape[0], 48, 48, 1)
x_test = x_test.astype('float32')
model.add(BatchNormalization(input_shape=(48,48,1)))
#1st convolution layer
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2, 2)))
#2nd convolution layer
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2, 2)))
#3rd convolution layer
model.add(Conv2D(256, (2, 2), activation='relu'))
model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2, 2)))
#4th convolution layer
model.add(Conv2D(512, (2, 2), activation='relu'))
model.add(Conv2D(512, (2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(1,1), strides=(2, 2)))
model.add(Flatten())
#fully connected neural networks
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))
## Start the training
#model.fit(x_train, y_train, epochs=epochs, validation_split=0.0, shuffle=True) #train for all trainset
model.fit_generator(train_generator, steps_per_epoch=batch_size, epochs=epochs, validation_data=(x_test,y_test)) #train for randomly selected one
score = model.evaluate(x_test, y_test, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100))
##########################------------------------############################
# serialize model to JSON
model_json = model.to_json()
with open("model/model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model/weights.h5")
print("Saved model to disk")
############################---------------------###############################
want to do multi label classification (7 distinct output labels)
I'm expecting a json file model.json
but i'm getting an error
ValueError: Error when checking target: expected dense_9 to have shape (7,) but got array with shape (1,)

Categories

Resources