问题描述
我正在研究手形字符识别模型。我创建了CNN + BiLSTM + CTC损失模型。但是运行model.fit()
时出错。请帮助我解决此错误。
我的模特
# input with shape of height=32 and width=128
inputs = Input(shape=(32,128,1))
# convolution layer with kernel size (3,3)
conv_1 = Conv2D(64,(3,3),activation = 'relu',padding='same')(inputs)
# poolig layer with kernel size (2,2)
pool_1 = MaxPooling2D(pool_size=(2,2),strides=2)(conv_1)
conv_2 = Conv2D(128,padding='same')(pool_1)
pool_2 = MaxPooling2D(pool_size=(2,strides=2)(conv_2)
conv_3 = Conv2D(256,padding='same')(pool_2)
conv_4 = Conv2D(256,padding='same')(conv_3)
# poolig layer with kernel size (2,1)
pool_4 = MaxPooling2D(pool_size=(2,1))(conv_4)
conv_5 = Conv2D(512,padding='same')(pool_4)
# Batch normalization layer
batch_norm_5 = BatchNormalization()(conv_5)
conv_6 = Conv2D(512,padding='same')(batch_norm_5)
batch_norm_6 = BatchNormalization()(conv_6)
pool_6 = MaxPooling2D(pool_size=(2,1))(batch_norm_6)
conv_7 = Conv2D(512,(2,activation = 'relu')(pool_6)
squeezed = Lambda(lambda x: K.squeeze(x,1))(conv_7)
# bidirectional LSTM layers with units=128
blstm_1 = Bidirectional(LSTM(128,return_sequences=True,dropout = 0.2))(squeezed)
blstm_2 = Bidirectional(LSTM(128,dropout = 0.2))(blstm_1)
outputs = Dense(len(char_dict)+1,activation = 'softmax')(blstm_2)
act_model = Model(inputs,outputs)
定义一个以以前模型的输出为输入的CTC损失模型
labels = Input(name='the_labels',shape=[max_length],dtype='float32')
input_length = Input(name='input_length',shape=[1],dtype='int64')
label_length = Input(name='label_length',dtype='int64')
def ctc_lambda_func(args):
y_pred,labels,input_length,label_length = args
return K.ctc_batch_cost(labels,y_pred,label_length)
loss_out = Lambda(ctc_lambda_func,output_shape=(1,),name='ctc')([outputs,label_length])
model = Model(inputs=[inputs,label_length],outputs=loss_out)
model.compile(loss={'ctc': lambda y_true,y_pred: y_pred},optimizer = 'adam')
model.fit(x=[input_array,output_array,train_input_length,train_label_length],y=np.zeros(input_array.shape[0]),batch_size=256,epochs = 100,validation_data = ([test_input_array,test_output_array,valid_input_length,valid_label_length],[np.zeros(test_input_array.shape[0])]),verbose = 1,callbacks = callbacks_list)
我得到的错误是
ValueError: Shape (None,17) must have rank 1
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)