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语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

Pydantic中model_validator的实现

《Pydantic中model_validator的实现》本文主要介绍了Pydantic中model_validator的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录引言基础知识创建 Pydantic 模型使用 model_validator 装饰器高级用法mo

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

关于WebSocket协议状态码解析

《关于WebSocket协议状态码解析》:本文主要介绍关于WebSocket协议状态码的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录WebSocket协议状态码解析1. 引言2. WebSocket协议状态码概述3. WebSocket协议状态码详解3

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

CSS Padding 和 Margin 区别全解析

《CSSPadding和Margin区别全解析》CSS中的padding和margin是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍padding和... 目录css Padding 和 Margin 全解析1. Padding: 内边距2. Margin: 外边距3. Padd

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@