Python – Keras value error: Dimensions must be equal

Keras value error: Dimensions must be equal… here is a solution to the problem.

Keras value error: Dimensions must be equal

The goal of the model is to classify video inputs based on the words associated with them. Each input has dimensions of 45 frames, 1 gray channel, 100-pixel rows, and 150-pixel columns (45, 1, 100, 150), while each corresponding output is a single-hot encoded representation of one of 3 possible words (e.g. “is”=> [0, 0, 1]

).

During model compilation, the following error occurred:

ValueError: Dimensions must be equal, but are 1 and 3 for 'Conv2D_94' (op: 'Conv2D') with 
input shapes: [?,100,150,1], [3,3,3,32].

This is the script used to train the model:

video = Input(shape=(self.frames_per_sequence,
                     1,
                     self.rows,
                     self.columns))
cnn = InceptionV3(weights="imagenet",
                  include_top=False)
cnn.trainable = False
encoded_frames = TimeDistributed(cnn)(video)
encoded_vid = LSTM(256)(encoded_frames)
hidden_layer = Dense(output_dim=1024, activation="relu")(encoded_vid)
outputs = Dense(output_dim=class_count, activation="softmax")(hidden_layer)
osr = Model(, outputs)
optimizer = Nadam(lr=0.002,
                  beta_1=0.9,
                  beta_2=0.999,
                  epsilon=1e-08,
                  schedule_decay=0.004)
osr.compile(loss="categorical_crossentropy",
            optimizer=optimizer,
            metrics=["categorical_accuracy"]) 

Solution

According to Convolution2D in Keras, below should be the shape of the input and filter.

shape of input = [batch, in_height, in_width, in_channels]
shape of filter = [filter_height, filter_width, in_channels, out_channels]

So, you get the meaning of the error –

ValueError: Dimensions must be equal, but are 1 and 3 for 'Conv2D_94' (op: 'Conv2D') with 
input shapes: [?,100,150,1], [3,3,3,32].

[?,100,150,1] means that the in_channels value is 1 and [3,3,3,32] means that the in_channels value is 3. That’s why you get the error – Dimensions must be equal, but are 1 and 3.

So you can change the shape of the filter to [3, 3, 1, 32].

Related Problems and Solutions