-CTPN scene text detection principles and implementation

Original Address:  https://zhuanlan.zhihu.com/p/34757009  recommend original

For character recognition complex scenes, the first localization position to the text, i.e., text detection. This has been a hot topic.

Detecting Text in Natural Image with Connectionist Text Proposal Networkarxiv.org

 

CTPN is a text detection algorithm proposed in ECCV 2016. CTPN combined with LSTM depth CNN network, which can effectively detect the transverse distribution of the complex character of the scene results in Figure 1, is relatively good text detection algorithms. Since CTPN come from improving Faster RCNN, the default paper reader is familiar with the principles and Faster RCNN CNN network structure.

https://pic1.zhimg.com/80/v2-658b7caad2c9a8163efe314e7cd6f284_hd.jpgFIG 1 detects SceneText

CTPN Related:

caffe Code:

tianzhi0549 - Overviewgithub.com

CTPN network structure

CTPN detect only the original text arranged laterally. CTPN Faster R-CNN structure substantially similar to, but with the addition LSTM layer. Assuming that the input   ImagesRF Royalty Free :

  • First VGG feature extraction to obtain a size of   conv5 feature map of.
  • After the conv5 doing   a sliding window, i.e. each point around the binding   regions wherein a length obtained   feature vector. Output   feature map, which feature apparently only CNN spatial characteristics to the study.
  • This feature map and then be Reshape

 

  • Then   and the maximum length of time   the data stream input bidirectional LSTM, learning sequence characteristics of each line. LSTM two-way output   , and then the recovery Reshape shape:

 

The feature includes both spatial characteristics, also contains the sequence characteristics LSTM learned.

  • Then after "FC" convolution layer becomes   feature
  • Finally, after a similar Faster R-CNN RPN network, obtain text proposals, FIG. 2-b.

https://pic3.zhimg.com/80/v2-b29f366f73ac0fba695435770e85809e_hd.jpgFIG 2 CTPN network structure

More specifically, the network structure, use netscope view CTPN of deploy.prototxt network profile.

Here to explain how the conv5 feature map  becomes :

https://pic4.zhimg.com/80/v2-4399a8ecb012241fa542e084eb7d727f_hd.jpg

In the original code is extracted caffe 9 near the points near each point with im2col, and then thus processed each row:

 

Each channel thus treated is then:

 

And im2col acceleration operation is a convolution, i.e. a matrix multiplication convolution becomes so quick calculation using Blas library.

Special note: The above is an explanation of the original Paper + Caffe code, other code to achieve the range of similarities and differences not discussed herein!

Next, the article around the following three questions:

  1. Why use two-way LSTM
  2. Text proposals how to generate FIG. 2-b in the output layer by FC
  3. How to determine the final position by the text Text proposals, i.e. text line Construction Algorithm

Why use two-way LSTM?

  • For RNN readers do not understand the principles, please refer RNN principle introduction:

完全解析RNN,Seq2Seq和Attention机制zhuanlan.zhihu.com

  • 关于LSTM长短期记忆模型,请参考

Understanding LSTM Networkscolah.github.io

  • CTPN中为何使用双向LSTM?

https://pic1.zhimg.com/80/v2-8d72777321cbf1336b79d839b6c7f9fc_hd.jpg图3 CNN卷积计算示意图

CNN学习的是感受野内的空间信息,LSTM学习的是序列特征。对于文本序列检测,显然既需要CNN抽象空间特征,也需要序列特征(毕竟文字是连续的)。

CTPN中使用双向LSTM,相比一般单向LSTM有什么优势?双向LSTM实际上就是将2个方向相反的LSTM连起来,如图r。

https://pic1.zhimg.com/80/v2-bc5266c4587af49516adb2cee4351838_hd.jpg图4 BLSTM

一般来说,双向LSTM都好于单向LSTM。还是看LSTM介绍文章中的例子:

我的手机坏了,我打算____一部新手机。

假设使用LSTM对空白部分填词。如果只看横线前面的词,“手机坏了”,那么“我”是打算“修”还是“买”还是“大哭一场”?双向LSTM能看到后面的词是“一部新手机“,那么横线上的词填“买“的概率就大得多了。显然对于文字检测,这种情况也依然适用。

如何通过"FC"卷积层输出产生图2-b中的Text proposals?

https://pic2.zhimg.com/80/v2-8496528d21dfd1c4e90df4ff57fa6221_hd.jpg图5 CTPN的RPN网络

CTPN通过CNN和BLSTM学到一组“空间 + 序列”特征后,在"FC"卷积层后接入RPN网络。这里的RPN与Faster R-CNN类似,分为两个分支:

  1. 左边分支用于bounding box regression。由于fc feature map每个点配备了10个Anchor,同时只回归中心y坐标与高度2个值,所以rpn_bboxp_red有20个channels
  2. 右边分支用于Softmax分类Anchor

具体RPN网络与Faster R-CNN完全一样,所以不再介绍,只分析不同之处。

竖直Anchor定位文字位置

由于CTPN针对的是横向排列的文字检测,所以其采用了一组(10个)等宽度的Anchors,用于定位文字位置。Anchor宽高为:

 

 

需要注意,由于CTPN采用VGG16模型提取特征,那么conv5 feature map的宽高都是输入Image的宽高的   

同时fc与conv5 width和height都相等。

如图6所示,CTPN为fc feature map每一个点都配备10个上述Anchors。

https://pic2.zhimg.com/80/v2-93e22f54fb0231b3f763f2f8129913ad_hd.jpg图6 CTPN Anchor

这样设置Anchors是为了:

  1. 保证在   方向上,Anchor覆盖原图每个点且不相互重叠。
  2. 不同文本在   方向上高度差距很大,所以设置Anchors高度为11-283,用于覆盖不同高度的文本目标。

多说一句,我看还有人不停的问Anchor大小为什么对应原图尺度,而不是conv5/fc特征尺度。这是因为Anchor是目标的候选框,经过后续分类+位置修正获得目标在原图尺度的检测框。那么这就要求Anchor必须是对应原图尺度!除此之外,如果Anchor大小对应conv5/fc尺度,那就要求Bounding box regression把很小的框回归到很大,这已经超出Regression小范围修正框的设计目的。

获得Anchor后,与Faster R-CNN类似,CTPN会做如下处理:

  1. Softmax判断Anchor中是否包含文本,即选出Softmax score大的正Anchor
  2. Bounding box regression修正包含文本的Anchor的中心y坐标高度

注意,与Faster R-CNN不同的是,这里Bounding box regression不修正Anchor中心x坐标和宽度。具体回归方式如下:

https://pic3.zhimg.com/80/v2-738d5b097b64f8012cef7b9d3c05f7b2_hd.jpg

其中,   是回归预测的坐标,   是Ground Truth,   和   是Anchor的中心y坐标和高度。Bounding box regression具体原理请参考之前文章。

Anchor经过上述Softmax和   方向bounding box regeression处理后,会获得图7所示的一组竖直条状text proposal。后续只需要将这些text proposal用文本线构造算法连接在一起即可获得文本位置。

https://pic1.zhimg.com/80/v2-447461eb54bcc3c93992ffd1c70bcfb8_hd.jpg图7 Text proposal

在论文中,作者也给出了直接使用Faster R-CNN RPN生成普通proposal与CTPN LSTM+竖直Anchor生成text proposal的对比,如图8,明显可以看到CTPN这种方法更适合文字检测。

https://pic1.zhimg.com/80/v2-82a34bf3b3591c4a21d90e8997ed1534_hd.jpg图8

文本线构造算法

在上一个步骤中,已经获得了图7所示的一串或多串text proposal,接下来就要采用文本线构造办法,把这些text proposal连接成一个文本检测框。

https://pic4.zhimg.com/80/v2-de8098e725d168a038f197ce0707faaf_hd.jpg图9

为了说明问题,假设某张图有图9所示的2个text proposal,即蓝色和红色2组Anchor,CTPN采用如下算法构造文本线:

  1. 按照水平   坐标排序Anchor
  2. 按照规则依次计算每个Anchor   的   ,组成 
  3. 通过   建立一个Connect graph,最终获得文本检测框

下面详细解释。假设每个Anchor index如绿色数字,同时每个Anchor Softmax score如黑色数字。

文本线构造算法通过如下方式建立每个Anchor      

正向寻找:

  1. 沿水平正方向,寻找和   水平距离小于50的候选Anchor
  2. 从候选Anchor中,挑出与   竖直方向   的Anchor
  3. 挑出符合条件2中Softmax score最大的 

再反向寻找:

  1. 沿水平负方向,寻找和   水平距离小于50的候选Anchor
  2. 从候选Anchor中,挑出与   竖直方向   的Anchor
  3. 挑出符合条件2中Softmax score最大的 

最后对比   和   :

  1. 如果   ,则这是一个最长连接,那么设置 
  2. 如果   ,说明这不是一个最长的连接(即该连接肯定包含在另外一个更长的连接中)。

https://pic1.zhimg.com/80/v2-822f0709d3e30df470a8e17f09a25de0_hd.jpg图10 构造文本线

举例说明,如图10,Anchor已经按照   顺序排列好,并具有图中的Softmax score(这里的score是随便给出的,只用于说明文本线构造算法):

  1. 对于   的   ,向前寻找50像素,满足   且score最大的是   ,即   ;   反向寻找,满足   且score最大的是   ,即   。由于   ,   是最长连接,那么设置 
  2. 对于   正向寻找得到   ;   反向寻找得到   ,但是   ,即   不是最长连接,包含在   中。

然后,这样就建立了一个   的Connect graph(其中   是正Anchor数量)。遍历Graph:

  1.  且   ,所以Anchor index 1->3->7组成一个文本,即蓝色文本区域。
  2.  且   ,所以Anchor index 6->10->12组成另外一个文本,即红色文本区域。

这样就通过Text proposals确定了文本检测框。

训练策略

由于作者没有给出CTPN原始训练代码,所以此处仅能根据论文分析。

 

明显可以看出,该Loss分为3个部分:

  1. Anchor Softmax loss:该Loss用于监督学习每个Anchor中是否包含文本。   表示是否是Groud truth。
  2. Anchor y coord regression loss:该Loss用于监督学习每个包含为本的Anchor的Bouding box regression y方向offset,类似于Smooth L1 loss。其中   是   中判定为有文本的Anchor,或者与Groud truth vertical IoU>0.5。
  3. Anchor x coord regression loss:该Loss用于监督学习每个包含文本的Anchor的Bouding box regression x方向offset,与y方向同理。前两个Loss存在的必要性很明确,但这个Loss有何作用作者没有解释(从训练和测试的实际效果看,作用不大)

说明一下,在Bounding box regression的训练过程中,其实只需要注意被判定成正的Anchor,不需要去关心杂乱的负Anchor。这与Faster R-CNN类似。

总结

  1. 由于加入LSTM,所以CTPN对水平文字检测效果超级好。
  2. 因为Anchor设定的原因,CTPN只能检测横向分布的文字,小幅改进加入水平Anchor即可检测竖直文字。但是由于框架限定,对不规则倾斜文字检测效果非常一般。
  3. CTPN加入了双向LSTM学习文字的序列特征,有利于文字检测。但是引入LSTM后,在训练时很容易梯度爆炸,需要小心处理。

 

发布了62 篇原创文章 · 获赞 235 · 访问量 169万+

Guess you like

Origin blog.csdn.net/javastart/article/details/104061133