睿智的目标检测18——Keras搭建Faster-RCNN目标检测平台

清疚 2023-07-07 11:18 177阅读 0赞

睿智的目标检测18——Keras搭建FasterRCNN目标检测平台

  • 学习前言
  • 什么是FasterRCNN目标检测算法
  • 源码下载
  • Faster-RCNN实现思路
    • 一、预测部分
      • 1、主干网络介绍
      • 2、获得Proposal建议框
      • 3、Proposal建议框的解码
      • 4、对Proposal建议框加以利用(RoiPoolingConv)
      • 5、在原图上进行绘制
      • 6、整体的执行流程
    • 二、训练部分
      • 1、建议框网络的训练
      • 2、Roi网络的训练
  • 训练自己的Faster-RCNN模型
    • 一、数据集的准备
    • 二、数据集的处理
    • 三、开始网络训练
    • 四、训练结果预测

学习前言

最近对实例分割感兴趣了,不过实例分割MaskRCNN是基于FasterRCNN的,之前学了非常多的One-Stage的目标检测算法,对FasterRCNN并不感兴趣,这次我们来学学FasterRCNN。
在这里插入图片描述

什么是FasterRCNN目标检测算法

在这里插入图片描述
Faster-RCNN是一个非常有效的目标检测算法,虽然是一个比较早的论文, 但它至今仍是许多目标检测算法的基础。

Faster-RCNN作为一种two-stage的算法,与one-stage的算法相比,two-stage的算法更加复杂且速度较慢,但是检测精度会更高。

事实上也确实是这样,Faster-RCNN的检测效果非常不错,但是检测速度与训练速度有待提高。

源码下载

https://github.com/bubbliiiing/faster-rcnn-keras
喜欢的可以点个star噢。

Faster-RCNN实现思路

一、预测部分

1、主干网络介绍

在这里插入图片描述
Faster-RCNN可以采用多种的主干特征提取网络,常用的有VGG,Resnet,Xception等等,本文采用的是Resnet网络,关于Resnet的介绍大家可以看我的另外一篇博客https://blog.csdn.net/weixin_44791964/article/details/102790260。

Faster-RCNN对输入进来的图片尺寸没有固定,但是一般会把输入进来的图片短边固定成600,如输入一张1200x1800的图片,会把图片不失真的resize到600x900上。

ResNet50有两个基本的块,分别名为Conv BlockIdentity Block。其中:

  • Conv Block输入和输出的维度是不一样的,所以不能连续串联,它的作用是改变网络的维度;
  • Identity Block输入维度和输出维度相同,可以串联,用于加深网络的。

上述提到的Conv Block的结构如下,由于残差边上存在卷积,Conv Block输入和输出的维度是不一样的
在这里插入图片描述
Identity Block的结构如下,由于残差边上不存在卷积,Identity Block输入维度和输出维度相同
在这里插入图片描述

Faster-RCNN的主干特征提取网络部分只用到了Resnet50当中高宽压缩四次的特征层,此时我们可以获得一个共享特征层,假设我们输入进来的图片是600x600x3,我们最后会获得一个38x38x3的特征层。

原本Resnet50第五次高宽压缩的部分ROI中使用

对Faster-RCNN的主干特征提取网络部分而言,以输入的图片为600x600为例shape变化如下:
在这里插入图片描述
最后一层的输出就是公用特征层。

实现代码:

  1. class BatchNormalization(Layer):
  2. def __init__(self, epsilon=1e-3, axis=-1,
  3. weights=None, beta_init='zero', gamma_init='one',
  4. gamma_regularizer=None, beta_regularizer=None, **kwargs):
  5. self.supports_masking = True
  6. self.beta_init = initializers.get(beta_init)
  7. self.gamma_init = initializers.get(gamma_init)
  8. self.epsilon = epsilon
  9. self.axis = axis
  10. self.gamma_regularizer = regularizers.get(gamma_regularizer)
  11. self.beta_regularizer = regularizers.get(beta_regularizer)
  12. self.initial_weights = weights
  13. super(BatchNormalization, self).__init__(**kwargs)
  14. def build(self, input_shape):
  15. self.input_spec = [InputSpec(shape=input_shape)]
  16. shape = (input_shape[self.axis],)
  17. self.gamma = self.add_weight(shape,
  18. initializer=self.gamma_init,
  19. regularizer=self.gamma_regularizer,
  20. name='{}_gamma'.format(self.name),
  21. trainable=False)
  22. self.beta = self.add_weight(shape,
  23. initializer=self.beta_init,
  24. regularizer=self.beta_regularizer,
  25. name='{}_beta'.format(self.name),
  26. trainable=False)
  27. self.running_mean = self.add_weight(shape, initializer='zero',
  28. name='{}_running_mean'.format(self.name),
  29. trainable=False)
  30. self.running_std = self.add_weight(shape, initializer='one',
  31. name='{}_running_std'.format(self.name),
  32. trainable=False)
  33. if self.initial_weights is not None:
  34. self.set_weights(self.initial_weights)
  35. del self.initial_weights
  36. self.built = True
  37. def call(self, x, mask=None):
  38. assert self.built, 'Layer must be built before being called'
  39. input_shape = K.int_shape(x)
  40. reduction_axes = list(range(len(input_shape)))
  41. del reduction_axes[self.axis]
  42. broadcast_shape = [1] * len(input_shape)
  43. broadcast_shape[self.axis] = input_shape[self.axis]
  44. if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
  45. x_normed = K.batch_normalization(
  46. x, self.running_mean, self.running_std,
  47. self.beta, self.gamma,
  48. epsilon=self.epsilon)
  49. else:
  50. broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
  51. broadcast_running_std = K.reshape(self.running_std, broadcast_shape)
  52. broadcast_beta = K.reshape(self.beta, broadcast_shape)
  53. broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
  54. x_normed = K.batch_normalization(
  55. x, broadcast_running_mean, broadcast_running_std,
  56. broadcast_beta, broadcast_gamma,
  57. epsilon=self.epsilon)
  58. return x_normed
  59. def get_config(self):
  60. config = {
  61. 'epsilon': self.epsilon,
  62. 'axis': self.axis,
  63. 'gamma_regularizer': self.gamma_regularizer.get_config() if self.gamma_regularizer else None,
  64. 'beta_regularizer': self.beta_regularizer.get_config() if self.beta_regularizer else None}
  65. base_config = super(BatchNormalization, self).get_config()
  66. return dict(list(base_config.items()) + list(config.items()))
  67. def identity_block(input_tensor, kernel_size, filters, stage, block):
  68. filters1, filters2, filters3 = filters
  69. conv_name_base = 'res' + str(stage) + block + '_branch'
  70. bn_name_base = 'bn' + str(stage) + block + '_branch'
  71. x = Conv2D(filters1, (1, 1), name=conv_name_base + '2a')(input_tensor)
  72. x = BatchNormalization(name=bn_name_base + '2a')(x)
  73. x = Activation('relu')(x)
  74. x = Conv2D(filters2, kernel_size,padding='same', name=conv_name_base + '2b')(x)
  75. x = BatchNormalization(name=bn_name_base + '2b')(x)
  76. x = Activation('relu')(x)
  77. x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)
  78. x = BatchNormalization(name=bn_name_base + '2c')(x)
  79. x = layers.add([x, input_tensor])
  80. x = Activation('relu')(x)
  81. return x
  82. def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):
  83. filters1, filters2, filters3 = filters
  84. conv_name_base = 'res' + str(stage) + block + '_branch'
  85. bn_name_base = 'bn' + str(stage) + block + '_branch'
  86. x = Conv2D(filters1, (1, 1), strides=strides,
  87. name=conv_name_base + '2a')(input_tensor)
  88. x = BatchNormalization(name=bn_name_base + '2a')(x)
  89. x = Activation('relu')(x)
  90. x = Conv2D(filters2, kernel_size, padding='same',
  91. name=conv_name_base + '2b')(x)
  92. x = BatchNormalization(name=bn_name_base + '2b')(x)
  93. x = Activation('relu')(x)
  94. x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)
  95. x = BatchNormalization(name=bn_name_base + '2c')(x)
  96. shortcut = Conv2D(filters3, (1, 1), strides=strides,
  97. name=conv_name_base + '1')(input_tensor)
  98. shortcut = BatchNormalization(name=bn_name_base + '1')(shortcut)
  99. x = layers.add([x, shortcut])
  100. x = Activation('relu')(x)
  101. return x
  102. def ResNet50(inputs):
  103. #-----------------------------------#
  104. # 假设输入进来的图片是600,600,3
  105. #-----------------------------------#
  106. img_input = inputs
  107. # 600,600,3 -> 300,300,64
  108. x = ZeroPadding2D((3, 3))(img_input)
  109. x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
  110. x = BatchNormalization(name='bn_conv1')(x)
  111. x = Activation('relu')(x)
  112. # 300,300,64 -> 150,150,64
  113. x = MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x)
  114. # 150,150,64 -> 150,150,256
  115. x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
  116. x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
  117. x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
  118. # 150,150,256 -> 75,75,512
  119. x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
  120. x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
  121. x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
  122. x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
  123. # 75,75,512 -> 38,38,1024
  124. x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
  125. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
  126. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
  127. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
  128. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
  129. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
  130. # 最终获得一个38,38,1024的共享特征层
  131. return x

2、获得Proposal建议框

在这里插入图片描述
上一步获得的公用特征层在图像中就是Feature Map

假设我们输入进来的图片是600x600x3,我们最后会获得一个38x38x3的特征层。

其有两个应用:

  • 一个是利用一次3x3的卷积进行特征整合,再进行一个9(分解为9 x 1)通道的1x1卷积,还有一个36(分解为9 x 4)通道的1x1卷积。这两个卷积的结果分别用于判断先验框内部是否包含物体以及这个先验框的调整参数
  • 一个是和ROIPooling结合使用。

这一部分主要是解析第一个应用。

在Faster-RCNN中,共享特征层上的每一个像素点的先验框的数量是9,上述提到的两个1x1卷积的结果对应了下述两个内容:

  • 9 x 4的卷积 用于预测 共享特征层上 每一个网格点上 每一个先验框的变化情况。(为什么说是变化情况呢,这是因为Faster-RCNN需要结合先验框获得建议框,9 x 4卷积的预测结果就是先验框的变化情况。)
  • 9 x 1的卷积 用于预测 共享特征层上 每一个网格点上 每一个先验框内部是否包含了物体。

当我们输入的图片的shape是600x600x3的时候,共享特征层的shape就是38x38x1024,相当于把输入进来的图像分割成38x38的网格,然后每个网格点存在9个先验框,这些先验框有不同的大小,在图像上密密麻麻。

上述提到的两个卷积:

  • 9 x 4的卷积的结果会对这些先验框进行调整,获得建议框。
  • 9 x 1的卷积的结果会判断先验框内是否包含物体。

到此位置还只是粗略的一个框的获取,也就是一个建议框。然后我们会在建议框里面继续找东西。

实现代码为:

  1. #----------------------------------------------------#
  2. # 创建建议框网络
  3. # 该网络结果会对先验框进行调整获得建议框
  4. #----------------------------------------------------#
  5. def get_rpn(base_layers, num_anchors):
  6. #----------------------------------------------------#
  7. # 利用一个512通道的3x3卷积进行特征整合
  8. #----------------------------------------------------#
  9. x = Conv2D(512, (3, 3), padding='same', activation='relu', kernel_initializer=random_normal(stddev=0.02), name='rpn_conv1')(base_layers)
  10. #----------------------------------------------------#
  11. # 利用一个1x1卷积调整通道数,获得预测结果
  12. #----------------------------------------------------#
  13. x_class = Conv2D(num_anchors, (1, 1), activation = 'sigmoid', kernel_initializer=random_normal(stddev=0.02), name='rpn_out_class')(x)
  14. x_regr = Conv2D(num_anchors * 4, (1, 1), activation = 'linear', kernel_initializer=random_normal(stddev=0.02), name='rpn_out_regress')(x)
  15. x_class = Reshape((-1, 1),name="classification")(x_class)
  16. x_regr = Reshape((-1, 4),name="regression")(x_regr)
  17. return [x_class, x_regr]

3、Proposal建议框的解码

通过第二步我们获得了38x38x9个先验框对应的预测结果。预测结果包含两部分。
9 x 4的卷积 用于预测 共享特征层上 每一个网格点上 每一个先验框的变化情况。
9 x 1的卷积 用于预测 共享特征层上 每一个网格点上 每一个先验框内部是否包含了物体。

该共享特征层相当于将整个图像分成38x38个网格;然后从每个网格中心建立9个先验框一共38x38x9个,12996个先验框。

当输入图像shape不同时,先验框的数量也会发生改变。这和共享特征层的shape相关。
在这里插入图片描述
先验框虽然可以代表一定的框的位置信息与框的大小信息,但是其是有限的,无法表示任意情况,因此还需要调整。

9 x 4中的9表示了这个网格点所包含的先验框数量,其中的4表示了框的中心与长宽的调整情况。

实现代码如下:

  1. def decode_boxes(self, mbox_loc, anchors, variances):
  2. # 获得先验框的宽与高
  3. anchor_width = anchors[:, 2] - anchors[:, 0]
  4. anchor_height = anchors[:, 3] - anchors[:, 1]
  5. # 获得先验框的中心点
  6. anchor_center_x = 0.5 * (anchors[:, 2] + anchors[:, 0])
  7. anchor_center_y = 0.5 * (anchors[:, 3] + anchors[:, 1])
  8. # 建议框距离先验框中心的xy轴偏移情况
  9. detections_center_x = mbox_loc[:, 0] * anchor_width * variances[0]
  10. detections_center_x += anchor_center_x
  11. detections_center_y = mbox_loc[:, 1] * anchor_height * variances[1]
  12. detections_center_y += anchor_center_y
  13. # 建议框的宽与高的求取
  14. detections_width = np.exp(mbox_loc[:, 2] * variances[2])
  15. detections_width *= anchor_width
  16. detections_height = np.exp(mbox_loc[:, 3] * variances[3])
  17. detections_height *= anchor_height
  18. # 获取建议框的左上角与右下角
  19. detections_xmin = detections_center_x - 0.5 * detections_width
  20. detections_ymin = detections_center_y - 0.5 * detections_height
  21. detections_xmax = detections_center_x + 0.5 * detections_width
  22. detections_ymax = detections_center_y + 0.5 * detections_height
  23. # 建议框的左上角与右下角进行堆叠
  24. detections = np.concatenate((detections_xmin[:, None],
  25. detections_ymin[:, None],
  26. detections_xmax[:, None],
  27. detections_ymax[:, None]), axis=-1)
  28. # 防止超出0与1
  29. detections = np.minimum(np.maximum(detections, 0.0), 1.0)
  30. return detections
  31. def detection_out_rpn(self, predictions, anchors, variances = [0.25, 0.25, 0.25, 0.25]):
  32. #---------------------------------------------------#
  33. # 获得种类的置信度
  34. #---------------------------------------------------#
  35. mbox_conf = predictions[0]
  36. #---------------------------------------------------#
  37. # mbox_loc是回归预测结果
  38. #---------------------------------------------------#
  39. mbox_loc = predictions[1]
  40. results = []
  41. #----------------------------------------------------------------------------------------------------------------------#
  42. # 对每一张图片进行处理,由于在predict.py的时候,我们只输入一张图片,所以for i in range(len(mbox_loc))只进行一次
  43. #----------------------------------------------------------------------------------------------------------------------#
  44. for i in range(len(mbox_loc)):
  45. #--------------------------------#
  46. # 利用回归结果对先验框进行解码
  47. #--------------------------------#
  48. detections = self.decode_boxes(mbox_loc[i], anchors, variances)
  49. #--------------------------------#
  50. # 取出先验框内包含物体的概率
  51. #--------------------------------#
  52. c_confs = mbox_conf[i, :, 0]
  53. c_confs_argsort = np.argsort(c_confs)[::-1][:self.rpn_pre_boxes]
  54. #------------------------------------#
  55. # 原始的预测框较多,先选一些高分框
  56. #------------------------------------#
  57. confs_to_process = c_confs[c_confs_argsort]
  58. boxes_to_process = detections[c_confs_argsort, :]
  59. #--------------------------------#
  60. # 进行iou的非极大抑制
  61. #--------------------------------#
  62. idx = self.sess.run(self.nms_out_rpn, feed_dict={
  63. self.boxes: boxes_to_process, self.scores: confs_to_process})
  64. #--------------------------------#
  65. # 取出在非极大抑制中效果较好的内容
  66. #--------------------------------#
  67. good_boxes = boxes_to_process[idx]
  68. results.append(good_boxes)
  69. return np.array(results)
  70. return np.array(results)

4、对Proposal建议框加以利用(RoiPoolingConv)

在这里插入图片描述
让我们对建议框有一个整体的理解:
事实上建议框就是对图片哪一个区域有物体存在进行初步筛选。

通过主干特征提取网络,我们可以获得一个公用特征层,当输入图片为600x600x3的时候,它的shape是38x38x1024,然后建议框会对这个公用特征层进行截取。

其实公用特征层里面的38x38对应着图片里的38x38个区域,38x38中的每一个点相当于这个区域内部所有特征的浓缩。

建议框会对这38x38个区域进行截取,也就是认为这些区域里存在目标,然后将截取的结果进行resize,resize到14x14x1024的大小。这个过程称为ROIPooling,本质上是对截取到的各区域分割为14x14的大小后分区域池化在tensorflow中的实现形式为resize。在这里我们称上述截取到的特征层为局部特征层。

然后我们再对每个局部特征层再进行Resnet原有的第五次压缩。压缩完后进行一个平均池化,再进行一个Flatten,此时,对于每一个局部特征层,我们可以获得一个长度为2048的特征向量。我们最后对这个特征向量分别进行一个num_classes的全连接和(num_classes-1)x4全连接。其中:

  • num_classes的全连接用于对建议框进行分类;
  • (num_classes-1)x4的全连接用于对建议框进行调整(之所以-1是不调整被认定为背景的框。)

通过这些操作,我们可以获得所有建议框的调整情况,和这个建议框调整后框内物体的类别。

事实上,我们可以将建议框,看作是ROIPooling层的先验框

对Proposal建议框加以利用的过程与shape变化如图所示:
在这里插入图片描述
建议框调整后的结果就是最终的预测结果了,可以在图上进行绘画了。

  1. class RoiPoolingConv(Layer):
  2. def __init__(self, pool_size, **kwargs):
  3. self.pool_size = pool_size
  4. super(RoiPoolingConv, self).__init__(**kwargs)
  5. def build(self, input_shape):
  6. self.nb_channels = input_shape[0][3]
  7. def compute_output_shape(self, input_shape):
  8. input_shape2 = input_shape[1]
  9. return None, input_shape2[1], self.pool_size, self.pool_size, self.nb_channels
  10. def call(self, x, mask=None):
  11. assert(len(x) == 2)
  12. #--------------------------------#
  13. # 共享特征层
  14. # batch_size, 38, 38, 1024
  15. #--------------------------------#
  16. feature_map = x[0]
  17. #--------------------------------#
  18. # 建议框
  19. # batch_size, num_rois, 4
  20. #--------------------------------#
  21. rois = x[1]
  22. #---------------------------------#
  23. # 建议框数量,batch_size大小
  24. #---------------------------------#
  25. num_rois = tf.shape(rois)[1]
  26. batch_size = tf.shape(rois)[0]
  27. #---------------------------------#
  28. # 生成建议框序号信息
  29. # 用于在进行crop_and_resize时
  30. # 帮助建议框找到对应的共享特征层
  31. #---------------------------------#
  32. box_index = tf.expand_dims(tf.range(0, batch_size), 1)
  33. box_index = tf.tile(box_index, (1, num_rois))
  34. box_index = tf.reshape(box_index, [-1])
  35. rs = tf.image.crop_and_resize(feature_map, tf.reshape(rois, [-1, 4]), box_index, (self.pool_size, self.pool_size))
  36. #---------------------------------------------------------------------------------#
  37. # 最终的输出为
  38. # (batch_size, num_rois, 14, 14, 1024)
  39. #---------------------------------------------------------------------------------#
  40. final_output = K.reshape(rs, (batch_size, num_rois, self.pool_size, self.pool_size, self.nb_channels))
  41. return final_output
  42. def identity_block_td(input_tensor, kernel_size, filters, stage, block):
  43. nb_filter1, nb_filter2, nb_filter3 = filters
  44. conv_name_base = 'res' + str(stage) + block + '_branch'
  45. bn_name_base = 'bn' + str(stage) + block + '_branch'
  46. x = TimeDistributed(Conv2D(nb_filter1, (1, 1), kernel_initializer='normal'), name=conv_name_base + '2a')(input_tensor)
  47. x = TimeDistributed(BatchNormalization(), name=bn_name_base + '2a')(x)
  48. x = Activation('relu')(x)
  49. x = TimeDistributed(Conv2D(nb_filter2, (kernel_size, kernel_size), kernel_initializer='normal',padding='same'), name=conv_name_base + '2b')(x)
  50. x = TimeDistributed(BatchNormalization(), name=bn_name_base + '2b')(x)
  51. x = Activation('relu')(x)
  52. x = TimeDistributed(Conv2D(nb_filter3, (1, 1), kernel_initializer='normal'), name=conv_name_base + '2c')(x)
  53. x = TimeDistributed(BatchNormalization(), name=bn_name_base + '2c')(x)
  54. x = Add()([x, input_tensor])
  55. x = Activation('relu')(x)
  56. return x
  57. def conv_block_td(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):
  58. nb_filter1, nb_filter2, nb_filter3 = filters
  59. conv_name_base = 'res' + str(stage) + block + '_branch'
  60. bn_name_base = 'bn' + str(stage) + block + '_branch'
  61. x = TimeDistributed(Conv2D(nb_filter1, (1, 1), strides=strides, kernel_initializer='normal'), name=conv_name_base + '2a')(input_tensor)
  62. x = TimeDistributed(BatchNormalization(), name=bn_name_base + '2a')(x)
  63. x = Activation('relu')(x)
  64. x = TimeDistributed(Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same', kernel_initializer='normal'), name=conv_name_base + '2b')(x)
  65. x = TimeDistributed(BatchNormalization(), name=bn_name_base + '2b')(x)
  66. x = Activation('relu')(x)
  67. x = TimeDistributed(Conv2D(nb_filter3, (1, 1), kernel_initializer='normal'), name=conv_name_base + '2c')(x)
  68. x = TimeDistributed(BatchNormalization(), name=bn_name_base + '2c')(x)
  69. shortcut = TimeDistributed(Conv2D(nb_filter3, (1, 1), strides=strides, kernel_initializer='normal'), name=conv_name_base + '1')(input_tensor)
  70. shortcut = TimeDistributed(BatchNormalization(), name=bn_name_base + '1')(shortcut)
  71. x = Add()([x, shortcut])
  72. x = Activation('relu')(x)
  73. return x
  74. def resnet50_classifier_layers(x):
  75. # batch_size, num_rois, 14, 14, 1024 -> batch_size, num_rois, 7, 7, 2048
  76. x = conv_block_td(x, 3, [512, 512, 2048], stage=5, block='a', strides=(2, 2))
  77. # batch_size, num_rois, 7, 7, 2048 -> batch_size, num_rois, 7, 7, 2048
  78. x = identity_block_td(x, 3, [512, 512, 2048], stage=5, block='b')
  79. # batch_size, num_rois, 7, 7, 2048 -> batch_size, num_rois, 7, 7, 2048
  80. x = identity_block_td(x, 3, [512, 512, 2048], stage=5, block='c')
  81. # batch_size, num_rois, 7, 7, 2048 -> batch_size, num_rois, 1, 1, 2048
  82. x = TimeDistributed(AveragePooling2D((7, 7)), name='avg_pool')(x)
  83. return x
  84. #----------------------------------------------------#
  85. # 将共享特征层和建议框传入classifier网络
  86. # 该网络结果会对建议框进行调整获得预测框
  87. #----------------------------------------------------#
  88. def get_resnet50_classifier(base_layers, input_rois, roi_size, num_classes=21):
  89. # batch_size, 38, 38, 1024 -> batch_size, num_rois, 14, 14, 1024
  90. out_roi_pool = RoiPoolingConv(roi_size)([base_layers, input_rois])
  91. # batch_size, num_rois, 14, 14, 1024 -> num_rois, 1, 1, 2048
  92. out = resnet50_classifier_layers(out_roi_pool)
  93. # batch_size, num_rois, 1, 1, 2048 -> batch_size, num_rois, 2048
  94. out = TimeDistributed(Flatten())(out)
  95. # batch_size, num_rois, 2048 -> batch_size, num_rois, num_classes
  96. out_class = TimeDistributed(Dense(num_classes, activation='softmax', kernel_initializer=random_normal(stddev=0.02)), name='dense_class_{}'.format(num_classes))(out)
  97. # batch_size, num_rois, 2048 -> batch_size, num_rois, 4 * (num_classes-1)
  98. out_regr = TimeDistributed(Dense(4 * (num_classes - 1), activation='linear', kernel_initializer=random_normal(stddev=0.02)), name='dense_regress_{}'.format(num_classes))(out)
  99. return [out_class, out_regr]

5、在原图上进行绘制

在第四步的结尾,我们对建议框进行再一次进行解码后,我们可以获得预测框在原图上的位置,而且这些预测框都是经过筛选的。这些筛选后的框可以直接绘制在图片上,就可以获得结果了。

6、整体的执行流程

在这里插入图片描述
几个小tip:
1、共包含了两次解码过程。
2、先进行粗略的筛选再细调。
3、第一次获得的建议框解码后的结果是对共享特征层featuremap进行截取。

二、训练部分

Faster-RCNN的训练过程和它的预测过程一样,分为两部分,首先要训练获得建议框网络,然后再训练后面利用ROI获得预测结果的网络。

1、建议框网络的训练

在预测过程中我们知道,如果想要对先验框调整获得建议框,我们需要对共享特征层利用一次3x3的卷积进行特征整合,然后进行两个卷积操作。这两个卷积分别为:

  • 一个36(分解为9 x 4)通道的1x1卷积
  • 一个9(分解为9 x 1)通道的1x1卷积

两个1x1卷积的结果实际上也就是:

9 x 4的卷积 用于预测 公用特征层上 每一个网格点上 每一个先验框的变化情况。(为什么说是变化情况呢,这是因为Faster-RCNN的预测结果需要结合先验框获得预测框,预测结果就是先验框的变化情况。)

9 x 1的卷积 用于预测 公用特征层上 每一个网格点上 每一个预测框内部是否包含了物体。

也就是说,我们直接利用Faster-RCNN建议框网络预测到的结果,并不是建议框在图片上的真实位置,需要解码才能得到真实位置。

而在训练的时候,我们需要计算loss函数,这个loss函数是相对于Faster-RCNN建议框网络的预测结果的。计算loss函数我们需要进行两个操作,分别是:

  • 我们需要把图片输入到当前的Faster-RCNN建议框的网络中,得到建议框网络的预测结果;
  • 同时还需要对真实框进行编码,这个编码是把真实框的位置信息格式转化为Faster-RCNN建议框预测结果的格式信息

也就是,我们需要找到 每一张用于训练的图片每一个真实框对应的先验框,并求出如果想要得到这样一个真实框,我们的建议框预测结果应该是怎么样的。

从建议框预测结果获得真实框的过程被称作解码,而从真实框获得建议框预测结果的过程就是编码的过程。

因此我们只需要将解码过程逆过来就是编码过程了。

在进行编码前,我们还需要找到每一个真实框对应的先验框,代表这个真实框由某个先验框进行预测。

我们首先需要将每一个的真实框和所有的先验框进行一个iou计算,这一步做的工作是计算每一个真实框和所有的先验框的重合程度。

在获得每一个真实框和所有的先验框的重合程度之后,选出和每一个真实框重合程度大于一定门限的先验框。代表这个真实框由这些先验框负责预测。

由于一个先验框只能负责一个真实框的预测,所以如果某个先验框和多个真实框的重合度较大,那么这个先验框只负责与其iou最大的真实框的预测。

在这一步后,我们可以找到每一个先验框所负责预测的真实框,然后再进行上述提到的编码行为。

实现代码如下:

  1. def iou(self, box):
  2. #---------------------------------------------#
  3. # 计算出每个真实框与所有的先验框的iou
  4. # 判断真实框与先验框的重合情况
  5. #---------------------------------------------#
  6. inter_upleft = np.maximum(self.anchors[:, :2], box[:2])
  7. inter_botright = np.minimum(self.anchors[:, 2:4], box[2:])
  8. inter_wh = inter_botright - inter_upleft
  9. inter_wh = np.maximum(inter_wh, 0)
  10. inter = inter_wh[:, 0] * inter_wh[:, 1]
  11. #---------------------------------------------#
  12. # 真实框的面积
  13. #---------------------------------------------#
  14. area_true = (box[2] - box[0]) * (box[3] - box[1])
  15. #---------------------------------------------#
  16. # 先验框的面积
  17. #---------------------------------------------#
  18. area_gt = (self.anchors[:, 2] - self.anchors[:, 0])*(self.anchors[:, 3] - self.anchors[:, 1])
  19. #---------------------------------------------#
  20. # 计算iou
  21. #---------------------------------------------#
  22. union = area_true + area_gt - inter
  23. iou = inter / union
  24. return iou
  25. def encode_ignore_box(self, box, return_iou=True, variances = [0.25, 0.25, 0.25, 0.25]):
  26. #---------------------------------------------#
  27. # 计算当前真实框和先验框的重合情况
  28. #---------------------------------------------#
  29. iou = self.iou(box)
  30. ignored_box = np.zeros((self.num_anchors, 1))
  31. #---------------------------------------------------#
  32. # 找到处于忽略门限值范围内的先验框
  33. #---------------------------------------------------#
  34. assign_mask_ignore = (iou > self.ignore_threshold) & (iou < self.overlap_threshold)
  35. ignored_box[:, 0][assign_mask_ignore] = iou[assign_mask_ignore]
  36. encoded_box = np.zeros((self.num_anchors, 4 + return_iou))
  37. #---------------------------------------------------#
  38. # 找到每一个真实框,重合程度较高的先验框
  39. #---------------------------------------------------#
  40. assign_mask = iou > self.overlap_threshold
  41. #---------------------------------------------#
  42. # 如果没有一个先验框重合度大于self.overlap_threshold
  43. # 则选择重合度最大的为正样本
  44. #---------------------------------------------#
  45. if not assign_mask.any():
  46. assign_mask[iou.argmax()] = True
  47. #---------------------------------------------#
  48. # 利用iou进行赋值
  49. #---------------------------------------------#
  50. if return_iou:
  51. encoded_box[:, -1][assign_mask] = iou[assign_mask]
  52. #---------------------------------------------#
  53. # 找到对应的先验框
  54. #---------------------------------------------#
  55. assigned_anchors = self.anchors[assign_mask]
  56. #---------------------------------------------#
  57. # 逆向编码,将真实框转化为FRCNN预测结果的格式
  58. # 先计算真实框的中心与长宽
  59. #---------------------------------------------#
  60. box_center = 0.5 * (box[:2] + box[2:])
  61. box_wh = box[2:] - box[:2]
  62. #---------------------------------------------#
  63. # 再计算重合度较高的先验框的中心与长宽
  64. #---------------------------------------------#
  65. assigned_anchors_center = 0.5 * (assigned_anchors[:, :2] + assigned_anchors[:, 2:4])
  66. assigned_anchors_wh = assigned_anchors[:, 2:4] - assigned_anchors[:, :2]
  67. # 逆向求取FasterRCNN应该有的预测结果
  68. encoded_box[:, :2][assign_mask] = box_center - assigned_anchors_center
  69. encoded_box[:, :2][assign_mask] /= assigned_anchors_wh
  70. encoded_box[:, :2][assign_mask] /= np.array(variances)[:2]
  71. encoded_box[:, 2:4][assign_mask] = np.log(box_wh / assigned_anchors_wh)
  72. encoded_box[:, 2:4][assign_mask] /= np.array(variances)[2:4]
  73. return encoded_box.ravel(), ignored_box.ravel()
  74. def assign_boxes(self, boxes):
  75. #---------------------------------------------------#
  76. # assignment分为2个部分
  77. # :4 的内容为网络应该有的回归预测结果
  78. # 4 的内容为先验框是否包含物体,默认为背景
  79. #---------------------------------------------------#
  80. assignment = np.zeros((self.num_anchors, 4 + 1))
  81. assignment[:, 4] = 0.0
  82. if len(boxes) == 0:
  83. return assignment
  84. #---------------------------------------------------#
  85. # 对每一个真实框都进行iou计算
  86. #---------------------------------------------------#
  87. apply_along_axis_boxes = np.apply_along_axis(self.encode_ignore_box, 1, boxes[:, :4])
  88. encoded_boxes = np.array([apply_along_axis_boxes[i, 0] for i in range(len(apply_along_axis_boxes))])
  89. ingored_boxes = np.array([apply_along_axis_boxes[i, 1] for i in range(len(apply_along_axis_boxes))])
  90. #---------------------------------------------------#
  91. # 在reshape后,获得的ingored_boxes的shape为:
  92. # [num_true_box, num_anchors, 1] 其中1为iou
  93. #---------------------------------------------------#
  94. ingored_boxes = ingored_boxes.reshape(-1, self.num_anchors, 1)
  95. ignore_iou = ingored_boxes[:, :, 0].max(axis=0)
  96. ignore_iou_mask = ignore_iou > 0
  97. assignment[:, 4][ignore_iou_mask] = -1
  98. #---------------------------------------------------#
  99. # 在reshape后,获得的encoded_boxes的shape为:
  100. # [num_true_box, num_anchors, 4+1]
  101. # 4是编码后的结果,1为iou
  102. #---------------------------------------------------#
  103. encoded_boxes = encoded_boxes.reshape(-1, self.num_anchors, 5)
  104. #---------------------------------------------------#
  105. # [num_anchors]求取每一个先验框重合度最大的真实框
  106. #---------------------------------------------------#
  107. best_iou = encoded_boxes[:, :, -1].max(axis=0)
  108. best_iou_idx = encoded_boxes[:, :, -1].argmax(axis=0)
  109. best_iou_mask = best_iou > 0
  110. best_iou_idx = best_iou_idx[best_iou_mask]
  111. #---------------------------------------------------#
  112. # 计算一共有多少先验框满足需求
  113. #---------------------------------------------------#
  114. assign_num = len(best_iou_idx)
  115. # 将编码后的真实框取出
  116. encoded_boxes = encoded_boxes[:, best_iou_mask, :]
  117. assignment[:, :4][best_iou_mask] = encoded_boxes[best_iou_idx,np.arange(assign_num), :4]
  118. #----------------------------------------------------------#
  119. # 4代表为背景的概率,设定为0,因为这些先验框有对应的物体
  120. #----------------------------------------------------------#
  121. assignment[:, 4][best_iou_mask] = 1
  122. # 通过assign_boxes我们就获得了,输入进来的这张图片,应该有的预测结果是什么样子的
  123. return assignment

focal会忽略一些重合度相对较高但是不是非常高的先验框,一般将重合度在0.3-0.7之间的先验框进行忽略。

2、Roi网络的训练

通过上一步已经可以对建议框网络进行训练了,建议框网络会提供一些位置的建议,在ROI网络部分,其会将建议框根据进行一定的截取,并获得对应的预测结果,事实上就是将上一步建议框当作了ROI网络的先验框。

因此,我们需要计算所有建议框和真实框的重合程度,并进行筛选,如果某个真实框和建议框的重合程度大于0.5则认为该建议框为正样本,如果重合程度小于0.5则认为该建议框为负样本

因此我们可以对真实框进行编码,这个编码是相对于建议框的,也就是,当我们存在这些建议框的时候,我们的ROI预测网络需要有什么样的预测结果才能将这些建议框调整成真实框。

每次训练我们都放入128个建议框进行训练,同时要注意正负样本的平衡。
实现代码如下:

  1. class ProposalTargetCreator(object):
  2. def __init__(self, num_classes, n_sample=128, pos_ratio=0.5, pos_iou_thresh=0.5,
  3. neg_iou_thresh_high=0.5, neg_iou_thresh_low=0, variance=[0.125, 0.125, 0.25, 0.25]):
  4. self.n_sample = n_sample
  5. self.pos_ratio = pos_ratio
  6. self.pos_roi_per_image = np.round(self.n_sample * self.pos_ratio)
  7. self.pos_iou_thresh = pos_iou_thresh
  8. self.neg_iou_thresh_high = neg_iou_thresh_high
  9. self.neg_iou_thresh_low = neg_iou_thresh_low
  10. self.num_classes = num_classes
  11. self.variance = variance
  12. def bbox_iou(self, bbox_a, bbox_b):
  13. if bbox_a.shape[1] != 4 or bbox_b.shape[1] != 4:
  14. print(bbox_a, bbox_b)
  15. raise IndexError
  16. tl = np.maximum(bbox_a[:, None, :2], bbox_b[:, :2])
  17. br = np.minimum(bbox_a[:, None, 2:], bbox_b[:, 2:])
  18. area_i = np.prod(br - tl, axis=2) * (tl < br).all(axis=2)
  19. area_a = np.prod(bbox_a[:, 2:] - bbox_a[:, :2], axis=1)
  20. area_b = np.prod(bbox_b[:, 2:] - bbox_b[:, :2], axis=1)
  21. return area_i / (area_a[:, None] + area_b - area_i)
  22. def bbox2loc(self, src_bbox, dst_bbox):
  23. width = src_bbox[:, 2] - src_bbox[:, 0]
  24. height = src_bbox[:, 3] - src_bbox[:, 1]
  25. ctr_x = src_bbox[:, 0] + 0.5 * width
  26. ctr_y = src_bbox[:, 1] + 0.5 * height
  27. base_width = dst_bbox[:, 2] - dst_bbox[:, 0]
  28. base_height = dst_bbox[:, 3] - dst_bbox[:, 1]
  29. base_ctr_x = dst_bbox[:, 0] + 0.5 * base_width
  30. base_ctr_y = dst_bbox[:, 1] + 0.5 * base_height
  31. eps = np.finfo(height.dtype).eps
  32. width = np.maximum(width, eps)
  33. height = np.maximum(height, eps)
  34. dx = (base_ctr_x - ctr_x) / width
  35. dy = (base_ctr_y - ctr_y) / height
  36. dw = np.log(base_width / width)
  37. dh = np.log(base_height / height)
  38. loc = np.vstack((dx, dy, dw, dh)).transpose()
  39. return loc
  40. def calc_iou(self, R, all_boxes):
  41. bboxes = all_boxes[:, :4]
  42. label = all_boxes[:, 4]
  43. R = np.concatenate([R, bboxes], axis=0)
  44. # ----------------------------------------------------- #
  45. # 计算建议框和真实框的重合程度
  46. # ----------------------------------------------------- #
  47. if len(bboxes)==0:
  48. max_iou = np.zeros(len(R))
  49. gt_assignment = np.zeros(len(R), np.int32)
  50. gt_roi_label = np.zeros(len(R))
  51. else:
  52. iou = self.bbox_iou(R, bboxes)
  53. #---------------------------------------------------------#
  54. # 获得每一个建议框最对应的真实框的iou [num_roi, ]
  55. #---------------------------------------------------------#
  56. max_iou = iou.max(axis=1)
  57. #---------------------------------------------------------#
  58. # 获得每一个建议框最对应的真实框 [num_roi, ]
  59. #---------------------------------------------------------#
  60. gt_assignment = iou.argmax(axis=1)
  61. #---------------------------------------------------------#
  62. # 真实框的标签
  63. #---------------------------------------------------------#
  64. gt_roi_label = label[gt_assignment]
  65. #----------------------------------------------------------------#
  66. # 满足建议框和真实框重合程度大于neg_iou_thresh_high的作为负样本
  67. # 将正样本的数量限制在self.pos_roi_per_image以内
  68. #----------------------------------------------------------------#
  69. pos_index = np.where(max_iou >= self.pos_iou_thresh)[0]
  70. pos_roi_per_this_image = int(min(self.n_sample//2, pos_index.size))
  71. if pos_index.size > 0:
  72. pos_index = np.random.choice(pos_index, size=pos_roi_per_this_image, replace=False)
  73. #-----------------------------------------------------------------------------------------------------#
  74. # 满足建议框和真实框重合程度小于neg_iou_thresh_high大于neg_iou_thresh_low作为负样本
  75. # 将正样本的数量和负样本的数量的总和固定成self.n_sample
  76. #-----------------------------------------------------------------------------------------------------#
  77. neg_index = np.where((max_iou < self.neg_iou_thresh_high) & (max_iou >= self.neg_iou_thresh_low))[0]
  78. neg_roi_per_this_image = self.n_sample - pos_roi_per_this_image
  79. if neg_roi_per_this_image > neg_index.size:
  80. neg_index = np.random.choice(neg_index, size=neg_roi_per_this_image, replace=True)
  81. else:
  82. neg_index = np.random.choice(neg_index, size=neg_roi_per_this_image, replace=False)
  83. #---------------------------------------------------------#
  84. # sample_roi [n_sample, ]
  85. # gt_roi_loc [n_sample, 4]
  86. # gt_roi_label [n_sample, ]
  87. #---------------------------------------------------------#
  88. keep_index = np.append(pos_index, neg_index)
  89. sample_roi = R[keep_index]
  90. if len(bboxes) != 0:
  91. gt_roi_loc = self.bbox2loc(sample_roi, bboxes[gt_assignment[keep_index]])
  92. gt_roi_loc = gt_roi_loc / np.array(self.variance)
  93. else:
  94. gt_roi_loc = np.zeros_like(sample_roi)
  95. gt_roi_label = gt_roi_label[keep_index]
  96. gt_roi_label[pos_roi_per_this_image:] = self.num_classes - 1
  97. #---------------------------------------------------------#
  98. # X [n_sample, 4]
  99. # Y1 [n_sample, num_classes]
  100. # Y2 [n_sample, (num_clssees-1) * 8]
  101. #---------------------------------------------------------#
  102. X = np.zeros_like(sample_roi)
  103. X[:, [0, 1, 2, 3]] = sample_roi[:, [1, 0, 3, 2]]
  104. Y1 = np.eye(self.num_classes)[np.array(gt_roi_label, np.int32)]
  105. y_class_regr_label = np.zeros([np.shape(gt_roi_loc)[0], self.num_classes-1, 4])
  106. y_class_regr_coords = np.zeros([np.shape(gt_roi_loc)[0], self.num_classes-1, 4])
  107. y_class_regr_label[np.arange(np.shape(gt_roi_loc)[0])[:pos_roi_per_this_image], np.array(gt_roi_label[:pos_roi_per_this_image], np.int32)] = 1
  108. y_class_regr_coords[np.arange(np.shape(gt_roi_loc)[0])[:pos_roi_per_this_image], np.array(gt_roi_label[:pos_roi_per_this_image], np.int32)] = \
  109. gt_roi_loc[:pos_roi_per_this_image]
  110. y_class_regr_label = np.reshape(y_class_regr_label, [np.shape(gt_roi_loc)[0], -1])
  111. y_class_regr_coords = np.reshape(y_class_regr_coords, [np.shape(gt_roi_loc)[0], -1])
  112. Y2 = np.concatenate([np.array(y_class_regr_label), np.array(y_class_regr_coords)], axis = 1)
  113. return X, Y1, Y2

训练自己的Faster-RCNN模型

首先前往Github下载对应的仓库,下载完后利用解压软件解压,之后用编程软件打开文件夹。
注意打开的根目录必须正确,否则相对目录不正确的情况下,代码将无法运行。

一定要注意打开后的根目录是文件存放的目录。
在这里插入图片描述

一、数据集的准备

本文使用VOC格式进行训练,训练前需要自己制作好数据集,如果没有自己的数据集,可以通过Github连接下载VOC12+07的数据集尝试下。
训练前将标签文件放在VOCdevkit文件夹下的VOC2007文件夹下的Annotation中。
在这里插入图片描述
训练前将图片文件放在VOCdevkit文件夹下的VOC2007文件夹下的JPEGImages中。
在这里插入图片描述
此时数据集的摆放已经结束。

二、数据集的处理

在完成数据集的摆放之后,我们需要对数据集进行下一步的处理,目的是获得训练用的2007_train.txt以及2007_val.txt,需要用到根目录下的voc_annotation.py。

voc_annotation.py里面有一些参数需要设置。
分别是annotation_mode、classes_path、trainval_percent、train_percent、VOCdevkit_path,第一次训练可以仅修改classes_path

  1. '''
  2. annotation_mode用于指定该文件运行时计算的内容
  3. annotation_mode为0代表整个标签处理过程,包括获得VOCdevkit/VOC2007/ImageSets里面的txt以及训练用的2007_train.txt、2007_val.txt
  4. annotation_mode为1代表获得VOCdevkit/VOC2007/ImageSets里面的txt
  5. annotation_mode为2代表获得训练用的2007_train.txt、2007_val.txt
  6. '''
  7. annotation_mode = 0
  8. '''
  9. 必须要修改,用于生成2007_train.txt、2007_val.txt的目标信息
  10. 与训练和预测所用的classes_path一致即可
  11. 如果生成的2007_train.txt里面没有目标信息
  12. 那么就是因为classes没有设定正确
  13. 仅在annotation_mode为0和2的时候有效
  14. '''
  15. classes_path = 'model_data/voc_classes.txt'
  16. '''
  17. trainval_percent用于指定(训练集+验证集)与测试集的比例,默认情况下 (训练集+验证集):测试集 = 9:1
  18. train_percent用于指定(训练集+验证集)中训练集与验证集的比例,默认情况下 训练集:验证集 = 9:1
  19. 仅在annotation_mode为0和1的时候有效
  20. '''
  21. trainval_percent = 0.9
  22. train_percent = 0.9
  23. '''
  24. 指向VOC数据集所在的文件夹
  25. 默认指向根目录下的VOC数据集
  26. '''
  27. VOCdevkit_path = 'VOCdevkit'

classes_path用于指向检测类别所对应的txt,以voc数据集为例,我们用的txt为:
在这里插入图片描述
训练自己的数据集时,可以自己建立一个cls_classes.txt,里面写自己所需要区分的类别。

三、开始网络训练

通过voc_annotation.py我们已经生成了2007_train.txt以及2007_val.txt,此时我们可以开始训练了。
训练的参数较多,大家可以在下载库后仔细看注释,其中最重要的部分依然是train.py里的classes_path。

classes_path用于指向检测类别所对应的txt,这个txt和voc_annotation.py里面的txt一样!训练自己的数据集必须要修改!
在这里插入图片描述
修改完classes_path后就可以运行train.py开始训练了,在训练多个epoch后,权值会生成在logs文件夹中。
其它参数的作用如下:

  1. #--------------------------------------------------------#
  2. # 训练前一定要修改classes_path,使其对应自己的数据集
  3. #--------------------------------------------------------#
  4. classes_path = 'model_data/voc_classes.txt'
  5. #----------------------------------------------------------------------------------------------------------------------------#
  6. # 权值文件请看README,百度网盘下载。数据的预训练权重对不同数据集是通用的,因为特征是通用的。
  7. # 预训练权重对于99%的情况都必须要用,不用的话权值太过随机,特征提取效果不明显,网络训练的结果也不会好。
  8. # 训练自己的数据集时提示维度不匹配正常,预测的东西都不一样了自然维度不匹配
  9. #
  10. # 如果想要断点续练就将model_path设置成logs文件夹下已经训练的权值文件。
  11. # 当model_path = ''的时候不加载整个模型的权值。
  12. #
  13. # 此处使用的是整个模型的权重,因此是在train.py进行加载的。
  14. # 如果想要让模型从主干的预训练权值开始训练,则设置model_path为主干网络的权值,此时仅加载主干。
  15. # 如果想要让模型从0开始训练,则设置model_path = '',Freeze_Train = Fasle,此时从0开始训练,且没有冻结主干的过程。
  16. # 一般来讲,从0开始训练效果会很差,因为权值太过随机,特征提取效果不明显。
  17. #----------------------------------------------------------------------------------------------------------------------------#
  18. model_path = 'model_data/voc_weights_resnet.h5'
  19. #------------------------------------------------------#
  20. # 输入的shape大小
  21. #------------------------------------------------------#
  22. input_shape = [600, 600]
  23. #---------------------------------------------#
  24. # vgg或者resnet50
  25. #---------------------------------------------#
  26. backbone = "resnet50"
  27. #------------------------------------------------------------------------#
  28. # anchors_size用于设定先验框的大小,每个特征点均存在9个先验框。
  29. # anchors_size每个数对应3个先验框。
  30. # 当anchors_size = [8, 16, 32]的时候,生成的先验框宽高约为:
  31. # [128, 128] ; [256, 256]; [512, 512]; [128, 256];
  32. # [256, 512]; [512, 1024]; [256, 128] ; [512, 256];
  33. # [1024, 512]; 详情查看anchors.py
  34. # 如果想要检测小物体,可以减小anchors_size靠前的数。
  35. # 比如设置anchors_size = [64, 256, 512]
  36. #------------------------------------------------------------------------#
  37. anchors_size = [128, 256, 512]
  38. #----------------------------------------------------#
  39. # 训练分为两个阶段,分别是冻结阶段和解冻阶段。
  40. # 显存不足与数据集大小无关,提示显存不足请调小batch_size。
  41. #----------------------------------------------------#
  42. #----------------------------------------------------#
  43. # 冻结阶段训练参数
  44. # 此时模型的主干被冻结了,特征提取网络不发生改变
  45. # 占用的显存较小,仅对网络进行微调
  46. #----------------------------------------------------#
  47. Init_Epoch = 0
  48. Freeze_Epoch = 50
  49. Freeze_batch_size = 4
  50. Freeze_lr = 1e-4
  51. #----------------------------------------------------#
  52. # 解冻阶段训练参数
  53. # 此时模型的主干不被冻结了,特征提取网络会发生改变
  54. # 占用的显存较大,网络所有的参数都会发生改变
  55. #----------------------------------------------------#
  56. UnFreeze_Epoch = 100
  57. Unfreeze_batch_size = 2
  58. Unfreeze_lr = 1e-5
  59. #------------------------------------------------------#
  60. # 是否进行冻结训练,默认先冻结主干训练后解冻训练。
  61. #------------------------------------------------------#
  62. Freeze_Train = True
  63. #----------------------------------------------------#
  64. # 获得图片路径和标签
  65. #----------------------------------------------------#
  66. train_annotation_path = '2007_train.txt'
  67. val_annotation_path = '2007_val.txt'

四、训练结果预测

训练结果预测需要用到两个文件,分别是yolo.py和predict.py。
我们首先需要去yolo.py里面修改model_path以及classes_path,这两个参数必须要修改。

model_path指向训练好的权值文件,在logs文件夹里。
classes_path指向检测类别所对应的txt。

在这里插入图片描述
完成修改后就可以运行predict.py进行检测了。运行后输入图片路径即可检测。

发表评论

表情:
评论列表 (有 0 条评论,177人围观)

还没有评论,来说两句吧...

相关阅读