在做ocr的时候,使用keras框架实现crnn,最后一步解码使用ctc_decode.
keras ctc decode
在测试的时候,随着batch的增加,越运行速度越慢,从开始的2-3s到后来的50+s,感觉非常奇怪,找了所有的原因,打印出每一步的时间,发现问题现在K.get_value()上。
搜索,发现也有人遇到相同的问题。
https://blog.csdn.net/mingshili/article/details/81941677?utm_source=blogxgwz5
是因为这两步每次运行都会给默认图增加一些节点,len( tf.get_default_graph().as_graph_def().node) 可以打印默认图节点数量来观察。
看来keras没有明显的定义图和会话这些东西,但是存在隐患。
没看懂上面博客介绍的解决方法,想到几个解决方法。
1.把增加的节点删除。
想直接改变tf.get_default_graph().as_graph_def().node, 失败。
2.为这个ctc解码过程单独创造一个图。
创造了之后和cnn网络的冲突。
3.tensorflow解决
要固定传入ctc decode的参数,使用placeholder,每次传入list,如果直接把list传入tf的函数,就会出现每次新增很多节点的问题。但是使用placeholder就得keras tensorflow混用,还好这两部分相对独立。ctc decode的输入输出都是list,中间过程使用tensor就行。
代码如下
class Ctc_Decode:
# 用tf定义一个专门ctc解码的图和会话,就不会一直增加节点了,速度快了很多
def __init__(self ,batch_size, timestep, nclass):
self.batch_size = batch_size
self.timestep = timestep
self.nclass = nclass
self.graph_ctc = tf.Graph()
with self.graph_ctc.as_default():
self.y_pred_tensor = tf.placeholder(shape=[self.batch_size, self.timestep, self.nclass], dtype=tf.float32, name="y_pred_tensor")
self._y_pred_tensor = tf.transpose(self.y_pred_tensor, perm=[1, 0, 2]) # 要把timestep 放在第一维
self.input_length_tensor = tf.placeholder(shape=[self.batch_size, 1], dtype=tf.int32, name="input_length_tensor")
self._input_length_tensor = tf.squeeze(self.input_length_tensor, axis=1) # 传进来的是 [batch_size,1] 所以要去掉一维
self.ctc_decode, _ = tf.nn.ctc_greedy_decoder(self._y_pred_tensor, self._input_length_tensor)
self.decoded_sequences = tf.sparse_tensor_to_dense(self.ctc_decode[0])
self.ctc_sess = tf.Session(graph=self.graph_ctc)
def ctc_decode_tf(self, args):
y_pred, input_length = args
decoded_sequences = self.ctc_sess.run(self.decoded_sequences,
feed_dict={self.y_pred_tensor: y_pred, self.input_length_tensor: input_length})
return decoded_sequences
把图和会话都写进类里,这样调用就行了
ctc_class = Ctc_Decode(batch_size=batch_size,timestep=img_w//8, nclass=nclass)
predict_y = ctc_class.ctc_decode_tf([predict_y, batch_x['input_length']]) # ctc解码
参考:
https://www.zhihu.com/question/58577743/answer/280929057