CTPN源码解析3.1-model()函数解析

2024-03-03 18:32
文章标签 源码 函数 解析 model 3.1 ctpn

本文主要是介绍CTPN源码解析3.1-model()函数解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文本检测算法一:CTPN

CTPN源码解析1-数据预处理split_label.py

CTPN源码解析2-代码整体结构和框架

CTPN源码解析3.1-model()函数解析

CTPN源码解析3.2-loss()函数解析

CTPN源码解析4-损失函数

CTPN源码解析5-文本线构造算法构造文本行

CTPN训练自己的数据集

由于解析的这个CTPN代码是被banjin-xjyeragonruan大神重新封装过的,所以代码整体结构非常的清晰,简洁!不像上次解析FasterRCNN的代码那样跳来跳去,没跳几步脑子就被跳乱了[捂脸],向大神致敬!PS:里面肯定会有理解和注释错误的,欢迎批评指正!

解析源码地址:https://github.com/eragonruan/text-detection-ctpn

知乎:从代码实现的角度理解CTPN:https://zhuanlan.zhihu.com/p/49588885

知乎:理解文本检测网络CTPN:https://zhuanlan.zhihu.com/p/77883736

知乎:场景文字检测—CTPN原理与实现:https://zhuanlan.zhihu.com/p/34757009

 

model()函数流程

model()函数代码

'''
0)传入图像,图像每个通道数减去相应的值,再将3个通道合并成一个图像
1)通过vgg16获得特征图conv5_3,shape(?,?,?,512)
2)滑动窗口获得特征向量rpn_conv,shape(?,?,?,512)
3)将得到的特征向量rpn_conv输入Bilstm中,得到lstm_output,shape(?,?,?,512)的输出
4)将lstm_output分别送入全连接层,得到 bbox_pred(预测框坐标)shape(?,?,?,40),cls_pred(分类概率值) shape(?,?,?,20)。
5)shape转换,返回相应的值
'''
def model(image):image = mean_image_subtraction(image) #图像每个通道数减去相应的值,再将3个通道合并成一个图像with slim.arg_scope(vgg.vgg_arg_scope()):conv5_3 = vgg.vgg_16(image)  #nets/vgg.py,VGG16作为基础网络,提取特征图  shape(N,H,W,512)rpn_conv = slim.conv2d(conv5_3, 512, 3) #在conv5_3上做3x3滑窗,又卷积一次  shape(N,H,W,512)# B×H×W×C大小的feature map经过BLSTM得到[B*H,W,512]大小的lstm_outputlstm_output = Bilstm(rpn_conv, 512, 128, 512, scope_name='BiLSTM')  # shape(?,?,?,512)# 本代码做了调整:1.[B*H,W,512]大小的lstm_output没有接卷积层(FC代表卷积)# 2.[B*H,W,512]大小的lstm_output直接预测的四个回归量bbox_pred = lstm_fc(lstm_output, 512, 10 * 4, scope_name="bbox_pred") #网络预测回归输出  # shape(?,?,?,40)cls_pred = lstm_fc(lstm_output, 512, 10 * 2, scope_name="cls_pred")   #网络预测分类输出  # shape(?,?,?,20)# transpose: (1, H, W, A x d) -> (1, H, WxA, d)cls_pred_shape = tf.shape(cls_pred) # 将矩阵的维度输出为一个维度矩阵 shape(?,?,?,20)-> shape(4,?)cls_pred_reshape = tf.reshape(cls_pred, [cls_pred_shape[0], cls_pred_shape[1], -1, 2]) # shape(?,?,?,20)-># shape(?,?,?,2)cls_pred_reshape_shape = tf.shape(cls_pred_reshape) # 将矩阵的维度输出为一个维度矩阵 shape(?,?,?,2)-> shape(4,?)cls_prob = tf.reshape(tf.nn.softmax(tf.reshape(cls_pred_reshape, [-1, cls_pred_reshape_shape[3]])),[-1, cls_pred_reshape_shape[1], cls_pred_reshape_shape[2], cls_pred_reshape_shape[3]],name="cls_prob")  # shape(?,?,?,?)return bbox_pred, cls_pred, cls_prob

下面按model()函数的处理步骤分别解析源码

0)传入图像,图像每个通道数减去相应的值,再将3个通道合并成一个图像

这一步在model()函数中的执行语句是:

image = mean_image_subtraction(image) #图像每个通道数减去相应的值,再将3个通道合并成一个图像
'''
图像每个通道数减去相应的值,再将3个通道合并成一个图像
'''
def mean_image_subtraction(images, means=[123.68, 116.78, 103.94]):num_channels = images.get_shape().as_list()[-1]  #获取图像通道数if len(means) != num_channels:raise ValueError('len(means) must match the number of channels')channels = tf.split(axis=3, num_or_size_splits=num_channels, value=images)for i in range(num_channels):channels[i] -= means[i]  #图像每个通道数减去相应的值return tf.concat(axis=3, values=channels)  #再将3个通道合并成一个图像

1)通过vgg16获得特征图conv5_3,shape(?,?,?,512)

这一步在model()函数中的执行语句是:

rpn_conv = slim.conv2d(conv5_3, 512, 3) #在conv5_3上做3x3滑窗,又卷积一次  shape(N,H,W,512)

我就不贴vgg16卷积的代码了。

2)滑动窗口获得特征向量rpn_conv,shape(?,?,?,512)

这一步在model()函数中的执行语句是:

 rpn_conv = slim.conv2d(conv5_3, 512, 3) #在conv5_3上做3x3滑窗,又卷积一次  shape(N,H,W,512)

原意是结合该点周边9个点的信息,但在tensorflow中就用卷积代替了。

3)将得到的特征向量rpn_conv输入Bilstm中,得到lstm_output,shape(?,?,?,512)的输出

这一步在model()函数中的执行语句是:

# B×H×W×C大小的feature map经过BLSTM得到[B*H,W,512]大小的lstm_outputlstm_output = Bilstm(rpn_conv, 512, 128, 512, scope_name='BiLSTM')  # shape(?,?,?,512)

双向lstm获取横向(宽度方向)序列特征

'''
#BLSTM 双向LSTM
net,  特征图
input_channel,  输入的通道数 
hidden_unit_num, 隐藏层单元数目
output_channel,  输出的通道数
scope_name       #名称
'''
def Bilstm(net, input_channel, hidden_unit_num, output_channel, scope_name):# width--->time step  width方向作为序列方向with tf.variable_scope(scope_name) as scope:shape = tf.shape(net) #获取特征图的维度信息N, H, W, C = shape[0], shape[1], shape[2], shape[3]net = tf.reshape(net, [N * H, W, C])   # 改变数据格式  # shape(N * H, W, C)net.set_shape([None, None, input_channel])    # shape(?,?,input_channel)lstm_fw_cell = tf.contrib.rnn.LSTMCell(hidden_unit_num, state_is_tuple=True) #前向lstmlstm_bw_cell = tf.contrib.rnn.LSTMCell(hidden_unit_num, state_is_tuple=True) #反向lstmlstm_out, last_state = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, net, dtype=tf.float32)lstm_out = tf.concat(lstm_out, axis=-1) # axis=1 代表在第1个维度拼接lstm_out = tf.reshape(lstm_out, [N * H * W, 2 * hidden_unit_num])# 这种初始化方法比常规高斯分布初始化、截断高斯分布初始化及 Xavier 初始化的泛化/缩放性能更好init_weights = tf.contrib.layers.variance_scaling_initializer(factor=0.01, mode='FAN_AVG', uniform=False)init_biases = tf.constant_initializer(0.0)weights = make_var('weights', [2 * hidden_unit_num, output_channel], init_weights)  # 初始化权重biases = make_var('biases', [output_channel], init_biases)  # 初始化偏移outputs = tf.matmul(lstm_out, weights) + biasesoutputs = tf.reshape(outputs, [N, H, W, output_channel]) #还原成原来的形状return outputs

4)将lstm_output分别送入全连接层,得到 bbox_pred(预测框坐标)shape(?,?,?,40),cls_pred(分类概率值) shape(?,?,?,20)。

这一步在model()函数中的执行语句是:

    # 本代码做了调整:1.[B*H,W,512]大小的lstm_output没有接卷积层(FC代表卷积)# 2.[B*H,W,512]大小的lstm_output直接预测的四个回归量bbox_pred = lstm_fc(lstm_output, 512, 10 * 4, scope_name="bbox_pred") #网络预测回归输出  # shape(?,?,?,40)cls_pred = lstm_fc(lstm_output, 512, 10 * 2, scope_name="cls_pred")   #网络预测分类输出  # shape(?,?,?,20)
'''
全连接层,改变输出通道数
'''
def lstm_fc(net, input_channel, output_channel, scope_name):with tf.variable_scope(scope_name) as scope:shape = tf.shape(net)N, H, W, C = shape[0], shape[1], shape[2], shape[3]net = tf.reshape(net, [N * H * W, C])init_weights = tf.contrib.layers.variance_scaling_initializer(factor=0.01, mode='FAN_AVG', uniform=False)init_biases = tf.constant_initializer(0.0)weights = make_var('weights', [input_channel, output_channel], init_weights) #全连接层512-》output_channelbiases = make_var('biases', [output_channel], init_biases)output = tf.matmul(net, weights) + biasesoutput = tf.reshape(output, [N, H, W, output_channel])return output

5)shape转换,返回相应的值

这一步在model()函数中的执行语句是:

    # transpose: (1, H, W, A x d) -> (1, H, WxA, d)cls_pred_shape = tf.shape(cls_pred) # 将矩阵的维度输出为一个维度矩阵 shape(?,?,?,20)-> shape(4,?)cls_pred_reshape = tf.reshape(cls_pred, [cls_pred_shape[0], cls_pred_shape[1], -1, 2]) # shape(?,?,?,20)-># shape(?,?,?,2)cls_pred_reshape_shape = tf.shape(cls_pred_reshape) # 将矩阵的维度输出为一个维度矩阵 shape(?,?,?,2)-> shape(4,?)cls_prob = tf.reshape(tf.nn.softmax(tf.reshape(cls_pred_reshape, [-1, cls_pred_reshape_shape[3]])),[-1, cls_pred_reshape_shape[1], cls_pred_reshape_shape[2], cls_pred_reshape_shape[3]],name="cls_prob")  # shape(?,?,?,?)return bbox_pred, cls_pred, cls_prob

然后整个model()操作就结束了。

这篇关于CTPN源码解析3.1-model()函数解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/770516

相关文章

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客