Keras在kctc_decode中使用了Lambda层错误。

时间:2023-02-08 13:57:10

It appears that Keras has done alot of the heavy lifting for you when it comes to the CTC function. However I am finding it tricky to build a decode function which I don't want to run as part of my neural network. I have a custom function that is executed on epoch end which I then iterate through all my test data and evaluate the metrics, I am currently doing this by hand but want to make use of the k.ctc_decode function (both greedy and beam) however I am finding it hard to access and incorporate into my custom function.

当谈到CTC功能时,Keras似乎已经为你做了很多繁重的工作。然而,我发现要构建一个我不想作为我的神经网络的一部分运行的decode函数是很困难的。我有一个自定义函数,它在epoch结束时执行,然后遍历所有的测试数据并评估指标,我现在是手工操作,但是想要使用k。ctc_decode函数(贪婪和beam),但我发现很难访问并将其并入我的自定义函数中。

I have a model:

我有一个模型:

 # Define CTC loss
    def ctc_lambda_func(args):
        y_pred, labels, input_length, label_length = args
        return K.ctc_batch_cost(labels, y_pred, input_length, label_length)

def ctc_decode(args):
     y_pred, input_length =args
     seq_len = tf.squeeze(input_length,axis=1)

     return K.ctc_decode(y_pred=y_pred, input_length=seq_len, greedy=True, beam_width=100, top_paths=1)

input_data = Input(name='the_input', shape=(None,mfcc_features))  
x = TimeDistributed(Dense(fc_size, name='fc1', activation='relu'))(input_data) 
y_pred = TimeDistributed(Dense(num_classes, name="y_pred", activation="softmax"))(x)

labels = Input(name='the_labels', shape=[None,], dtype='int32')
input_length = Input(name='input_length', shape=[1], dtype='int32')
label_length = Input(name='label_length', shape=[1], dtype='int32')

loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred,labels,input_length,label_length])

dec = Lambda(ctc_decode, output_shape=[None,], name='decoder')([y_pred,input_length])

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



iterate = K.function([input_data, K.learning_phase()], [y_pred])
decode = K.function([y_pred, input_length], [dec])

Current error is:

当前的错误是:

dec = Lambda(ctc_decode, name='decoder')([y_pred,input_length]) File "/home/rob/py27/local/lib/python2.7/site-packages/keras/engine/topology.py", line 604, in call output_shape = self.compute_output_shape(input_shape) File "/home/rob/py27/local/lib/python2.7/site-packages/keras/layers/core.py", line 631, in compute_output_shape return K.int_shape(x) File "/home/rob/py27/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 451, in int_shape shape = x.get_shape() AttributeError: 'tuple' object has no attribute 'get_shape'

(ctc_decode, name='decoder')([y_pred,input_length])文件"/home/rob/py27/local/lib/python2.7/site-package /keras/引擎/拓扑。py“,第604行,调用output_shape = self。compute_output_shape(input_shape)文件”/home/rob/py27/local/lib/python2.7/site-package /keras/layers/core。py“,第631行,在compute_output_shape返回K.int_shape(x)文件”/home/rob/py27/local/lib/python2.7/site-package /keras/后端/ tensorflow_后端。在int_shape = x.get_shape() AttributeError中,“tuple”对象没有属性“get_shape”

Any ideas how I can do this?

有什么办法吗?

1 个解决方案

#1


0  

One tricky part is that K.ctc_decode returns tuple of single list of tensors, not a single tensor, so you can't create a layer straightforwardly. Instead try creating a decoder with K.function:

一个棘手的部分是K。ctc_decode返回单个张量列表的元组,而不是单个张量,所以不能直接创建一个层。相反,试着用k函数来创建一个解码器。

top_k_decoded, _ = K.ctc_decode(y_pred, input_lengths)
decoder = K.function([input_data, input_lengths], [top_k_decoded[0]])

Later you can call your decoder:

稍后您可以调用您的解码器:

decoded_sequences = decoder([test_input_data, test_input_lengths])

You may need some reshaping, as K.ctc_decoder requires lengths to have shape like (samples), while the lengths tensor was of shape (samples, 1).

你可能需要一些重塑,如K。ctc_decoder要求长度为(样本),而长度张量为形状(样本,1)。

#1


0  

One tricky part is that K.ctc_decode returns tuple of single list of tensors, not a single tensor, so you can't create a layer straightforwardly. Instead try creating a decoder with K.function:

一个棘手的部分是K。ctc_decode返回单个张量列表的元组,而不是单个张量,所以不能直接创建一个层。相反,试着用k函数来创建一个解码器。

top_k_decoded, _ = K.ctc_decode(y_pred, input_lengths)
decoder = K.function([input_data, input_lengths], [top_k_decoded[0]])

Later you can call your decoder:

稍后您可以调用您的解码器:

decoded_sequences = decoder([test_input_data, test_input_lengths])

You may need some reshaping, as K.ctc_decoder requires lengths to have shape like (samples), while the lengths tensor was of shape (samples, 1).

你可能需要一些重塑,如K。ctc_decoder要求长度为(样本),而长度张量为形状(样本,1)。