tensorflow.keras during fit - Invalid argument Input to reshape is a tensor with 983040 values, but the requested shape has 1966080

Viewed 682

How do I fix this error that I get, when I fit tensorflow.keras.Model, like:

history_model_2 = model.fit(train_data.next_batch(),
                            validation_data=validation_data.next_batch(),
                            epochs=32)

this is the error I get:

InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  Input to reshape is a tensor with 983040 values, but the requested shape has 1966080
     [[node model_2/reshape/Reshape (defined at <ipython-input-82-15c7d8d22e71>:10) ]]
     [[model_2/ctc/Cast_3/_90]]
  (1) Invalid argument:  Input to reshape is a tensor with 983040 values, but the requested shape has 1966080
     [[node model_2/reshape/Reshape (defined at <ipython-input-82-15c7d8d22e71>:10) ]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_33412]

Function call stack:
train_function -> train_function

{{ in my model.fit(), train_data.next_batch() is a generator that generates data for both x and y arguments(I have used this since model.fit_generator is being deprecated and this generator and almost complete code is partially inspired from this example from keras ocr examples on GitHub, also from which I have used ctc loss function shown below.) }}

Here is my complete model:

from tensorflow.keras import layers
from tensorflow.keras import Model
from tensorflow.keras import backend as tf_keras_backend

def ctc_lambda_func(args):
    y_pred, labels, input_length, label_length = args
    # the 2 is critical here, since the first couple outputs of the RNN tend to be garbage:
    y_pred = y_pred[:, 2:, :]
    return tf_keras_backend.ctc_batch_cost(labels, y_pred, input_length, label_length)

# Make Network
input_data = layers.Input(name='the_input', shape=(128, 64, 1), dtype='float32')  # (None, 128, 64, 1)

# Convolution layer (VGG)
inner = layers.Conv2D(64, (3, 3), padding='same', name='conv1', kernel_initializer='he_normal', activation='relu')(input_data)  # (None, 128, 64, 64)
inner = layers.BatchNormalization()(inner)
inner = layers.Activation('relu')(inner)
inner = layers.MaxPooling2D(pool_size=(2, 2), name='max1')(inner)  # (None,64, 32, 64)

inner = layers.Conv2D(128, (3, 3), padding='same', name='conv2', kernel_initializer='he_normal', activation='relu')(inner)  # (None, 64, 32, 128)
inner = layers.BatchNormalization()(inner)
inner = layers.Activation('relu')(inner)
inner = layers.MaxPooling2D(pool_size=(2, 2), name='max2')(inner)  # (None, 32, 16, 128)

inner = layers.Conv2D(256, (3, 3), padding='same', name='conv3', kernel_initializer='he_normal', activation='relu')(inner)  # (None, 32, 16, 256)
inner = layers.BatchNormalization()(inner)
inner = layers.Activation('relu')(inner)
inner = layers.Conv2D(256, (3, 3), padding='same', name='conv4', kernel_initializer='he_normal', activation='relu')(inner)  # (None, 32, 16, 256)
inner = layers.BatchNormalization()(inner)
inner = layers.Activation('relu')(inner)
inner = layers.MaxPooling2D(pool_size=(1, 2), name='max3')(inner)  # (None, 32, 8, 256)

inner = layers.Conv2D(512, (3, 3), padding='same', name='conv5', kernel_initializer='he_normal', activation='relu')(inner)  # (None, 32, 8, 512)
inner = layers.BatchNormalization()(inner)
inner = layers.Activation('relu')(inner)
inner = layers.Conv2D(512, (3, 3), padding='same', name='conv6', activation='relu')(inner)  # (None, 32, 8, 512)
inner = layers.BatchNormalization()(inner)
inner = layers.Activation('relu')(inner)
inner = layers.MaxPooling2D(pool_size=(1, 2), name='max4')(inner)  # (None, 32, 4, 512)

inner = layers.Conv2D(512, (2, 2), padding='same', kernel_initializer='he_normal', name='con7', activation='relu')(inner)  # (None, 32, 4, 512)
before_reshape = layers.BatchNormalization()(inner)
inner = layers.Activation('relu')(inner)

# CNN to RNN
reshape_op = layers.Reshape(target_shape=((32, 2048)), name='reshape')(before_reshape)  # (None, 32, 2048)
dense_after_reshape = layers.Dense(64, activation='relu', kernel_initializer='he_normal', name='dense1')(reshape_op)  # (None, 32, 64)

# RNN layer
gru_1 = layers.GRU(256, return_sequences=True, kernel_initializer='he_normal', name='gru1')(dense_after_reshape)  # (None, 32, 512)
gru_1b = layers.GRU(256, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru1_b')(dense_after_reshape)
reversed_gru_1b = layers.Lambda(lambda inputTensor: tf_keras_backend.reverse(inputTensor, axes=1)) (gru_1b)

gru1_merged = layers.add([gru_1, reversed_gru_1b])  # (None, 32, 512)
gru1_merged = layers.BatchNormalization()(gru1_merged)

gru_2 = layers.GRU(256, return_sequences=True, kernel_initializer='he_normal', name='gru2')(gru1_merged)
gru_2b = layers.GRU(256, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru2_b')(gru1_merged)
reversed_gru_2b= layers.Lambda(lambda inputTensor: tf_keras_backend.reverse(inputTensor, axes=1)) (gru_2b)

gru2_merged = layers.concatenate([gru_2, reversed_gru_2b])  # (None, 32, 1024)
gru2_merged = layers.BatchNormalization()(gru2_merged)

# transforms RNN output to character activations:
y_pred = layers.Dense(num_classes, kernel_initializer='he_normal',name='dense2', activation='softmax')(gru2_merged) #(None, 32, 80)
y_pred = layers.Activation('softmax', name='softmax')(inner)

labels = layers.Input(name='the_labels', shape=[16], dtype='float32')
input_length = layers.Input(name='input_length', shape=[1], dtype='int64')
label_length = layers.Input(name='label_length', shape=[1], dtype='int64')

# loss function
loss_out = layers.Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')(
    [y_pred, labels, input_length, label_length]
)

model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)

compiling it:

model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer = 'adam')

I have also tried to make sure dimensions are correct by debugging in multiple ways, but to no avail.

How do I fix this? Or what have I done incorrectly that led to this error?

EDIT 1: Here is the model summary:
And my batch size is 64. model summary as image

1 Answers

There was a mistake in the generator I had prepared for pre-processing images. It yielded images of 64,64 instead of 128,64. My bad to not have checked it.

Related