Stack Exchange
tensorflow / keras: Can not squeeze dim[1], expected a dimension of 1, got 3
I have a basic CRNN model that looks like this:
char_list = string.ascii_letters+string.digits 
#Model
inputs = Input(shape=(32,300,1))
 
conv_1 = Conv2D(64, (3,3), activation = 'relu', padding='same')(inputs)
pool_1 = MaxPool2D(pool_size=(2, 2), strides=2)(conv_1)
conv_2 = Conv2D(128, (3,3), activation = 'relu', padding='same')(pool_1)
pool_2 = MaxPool2D(pool_size=(2, 2), strides=2)(conv_2)
conv_3 = Conv2D(256, (3,3), activation = 'relu', padding='same')(pool_2)
conv_4 = Conv2D(256, (3,3), activation = 'relu', padding='same')(conv_3)
pool_4 = MaxPool2D(pool_size=(2, 1))(conv_4)
conv_5 = Conv2D(512, (3,3), activation = 'relu', padding='same')(pool_4)
batch_norm_5 = BatchNormalization()(conv_5)
conv_6 = Conv2D(512, (3,3), activation = 'relu', padding='same')(batch_norm_5)
batch_norm_6 = BatchNormalization()(conv_6)
pool_6 = MaxPool2D(pool_size=(2, 1))(batch_norm_6)
conv_7 = Conv2D(512, (2,2), activation = 'relu')(pool_6)

squeezed = Lambda(lambda x: K.squeeze(x, 1))(conv_7)

blstm_1 = Bidirectional(LSTM(256, return_sequences=True))(squeezed)
blstm_2 = Bidirectional(LSTM(256, return_sequences=True))(blstm_1)

outputs = Dense(len(char_list)+1, activation = 'softmax')(blstm_2)

act_model = Model(inputs, outputs)

if the input is of shape (32,300,1) it works, but if I try to change the height in the input to 64 (64,300,1) , I get an error:

ValueError: Can not squeeze dim[1], expected a dimension of 1, got 3 for '{{node lambda_2/Squeeze}} = SqueezeT=DT_FLOAT, squeeze_dims=[1]' with input shapes: [?,3,74,512].

why this error occurs? and how to solve it?

you can consider https://stackoverflow.com/a/63789979/10375049

After conv7 layer check the dimension, when you use K.squeeze(x, 1) the dimension for the input which you are providing must be (?, 1, ?, ..), the dim must be 1 at index 1. You may refer this link for squeeze doc.

If you are using softmax activation you should set its loss to categorical_crossentropy. And if using the sparse_categorical_crossentropy loss, your targets should be integer targets.
