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

相关文章

Mybatis对MySQL if 函数的不支持问题解读

《Mybatis对MySQLif函数的不支持问题解读》接手项目后,为了实现多租户功能,引入了Mybatis-plus,发现之前运行正常的SQL语句报错,原因是Mybatis不支持MySQL的if函... 目录MyBATis对mysql if 函数的不支持问题描述经过查询网上搜索资料找到原因解决方案总结Myb

C++ 右值引用(rvalue references)与移动语义(move semantics)深度解析

《C++右值引用(rvaluereferences)与移动语义(movesemantics)深度解析》文章主要介绍了C++右值引用和移动语义的设计动机、基本概念、实现方式以及在实际编程中的应用,... 目录一、右值引用(rvalue references)与移动语义(move semantics)设计动机1

MySQL 筛选条件放 ON后 vs 放 WHERE 后的区别解析

《MySQL筛选条件放ON后vs放WHERE后的区别解析》文章解释了在MySQL中,将筛选条件放在ON和WHERE中的区别,文章通过几个场景说明了ON和WHERE的区别,并总结了ON用于关... 今天我们来讲讲数据库筛选条件放 ON 后和放 WHERE 后的区别。ON 决定如何 "连接" 表,WHERE

Mybatis的mapper文件中#和$的区别示例解析

《Mybatis的mapper文件中#和$的区别示例解析》MyBatis的mapper文件中,#{}和${}是两种参数占位符,核心差异在于参数解析方式、SQL注入风险、适用场景,以下从底层原理、使用场... 目录MyBATis 中 mapper 文件里 #{} 与 ${} 的核心区别一、核心区别对比表二、底

Python容器转换与共有函数举例详解

《Python容器转换与共有函数举例详解》Python容器是Python编程语言中非常基础且重要的概念,它们提供了数据的存储和组织方式,下面:本文主要介绍Python容器转换与共有函数的相关资料,... 目录python容器转换与共有函数详解一、容器类型概览二、容器类型转换1. 基本容器转换2. 高级转换示

Agent开发核心技术解析以及现代Agent架构设计

《Agent开发核心技术解析以及现代Agent架构设计》在人工智能领域,Agent并非一个全新的概念,但在大模型时代,它被赋予了全新的生命力,简单来说,Agent是一个能够自主感知环境、理解任务、制定... 目录一、回归本源:到底什么是Agent?二、核心链路拆解:Agent的"大脑"与"四肢"1. 规划模

MySQL字符串转数值的方法全解析

《MySQL字符串转数值的方法全解析》在MySQL开发中,字符串与数值的转换是高频操作,本文从隐式转换原理、显式转换方法、典型场景案例、风险防控四个维度系统梳理,助您精准掌握这一核心技能,需要的朋友可... 目录一、隐式转换:自动但需警惕的&ld编程quo;双刃剑”二、显式转换:三大核心方法详解三、典型场景

SQL 注入攻击(SQL Injection)原理、利用方式与防御策略深度解析

《SQL注入攻击(SQLInjection)原理、利用方式与防御策略深度解析》本文将从SQL注入的基本原理、攻击方式、常见利用手法,到企业级防御方案进行全面讲解,以帮助开发者和安全人员更系统地理解... 目录一、前言二、SQL 注入攻击的基本概念三、SQL 注入常见类型分析1. 基于错误回显的注入(Erro

pandas使用apply函数给表格同时添加多列

《pandas使用apply函数给表格同时添加多列》本文介绍了利用Pandas的apply函数在DataFrame中同时添加多列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、Pandas使用apply函数给表格同时添加多列二、应用示例一、Pandas使用apply函

C++ 多态性实战之何时使用 virtual 和 override的问题解析

《C++多态性实战之何时使用virtual和override的问题解析》在面向对象编程中,多态是一个核心概念,很多开发者在遇到override编译错误时,不清楚是否需要将基类函数声明为virt... 目录C++ 多态性实战:何时使用 virtual 和 override?引言问题场景判断是否需要多态的三个关