睿智的目标检测33——Keras搭建Efficientdet目标检测平台

电玩女神 2023-02-12 06:29 143阅读 0赞

睿智的目标检测33——Keras搭建Efficientdet目标检测平台

  • 学习前言
  • 什么是Efficientdet目标检测算法
  • 源码下载
  • Efficientdet实现思路
    • 一、预测部分
      • 1、主干网络介绍
      • 2、BiFPN加强特征提取
      • 3、从特征获取预测结果
      • 4、预测结果的解码
      • 5、在原图上进行绘制
    • 二、训练部分
      • 1、真实框的处理
      • 2、利用处理完的真实框与对应图片的预测结果计算loss
        • a、控制正负样本的权重
        • b、控制容易分类和难分类样本的权重
        • c、两种权重控制方法合并
  • 训练自己的Efficientdet模型
    • 一、数据集的准备
    • 二、数据集的处理
    • 三、开始网络训练
    • 四、训练结果预测

学习前言

一起来看看Efficientdet的keras实现吧,顺便训练一下自己的数据。
在这里插入图片描述

什么是Efficientdet目标检测算法

最近,谷歌大脑 Mingxing Tan、Ruoming Pang 和 Quoc V. Le 提出新架构 EfficientDet,结合 EfficientNet(同样来自该团队)和新提出的 BiFPN,实现新的 SOTA 结果。
在这里插入图片描述

源码下载

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

Efficientdet实现思路

一、预测部分

1、主干网络介绍

在这里插入图片描述
Efficientdet采用Efficientnet作为主干特征提取网络。EfficientNet-B0对应Efficientdet-D0;EfficientNet-B1对应Efficientdet-D1;以此类推。

EfficientNet模型具有很独特的特点,这个特点是参考其它优秀神经网络设计出来的。经典的神经网络特点如下:
1、利用残差神经网络增大神经网络的深度,通过更深的神经网络实现特征提取。
2、改变每一层提取的特征层数,实现更多层的特征提取,得到更多的特征,提升宽度。
3、通过增大输入图片的分辨率也可以使得网络可以学习与表达的东西更加丰富,有利于提高精确度

EfficientNet就是将这三个特点结合起来,通过一起缩放baseline模型MobileNet中就通过缩放α实现缩放模型,不同的α有不同的模型精度,α=1时为baseline模型;ResNet其实也是有一个baseline模型,在baseline的基础上通过改变图片的深度实现不同的模型实现),同时调整深度宽度输入图片的分辨率完成一个优秀的网络设计。

在EfficientNet模型中,其使用一组固定的缩放系数统一缩放网络深度、宽度和分辨率。
假设想使用 2N倍的计算资源,我们可以简单的对网络深度扩大αN倍、宽度扩大βN 、图像尺寸扩大γN倍,这里的α,β,γ都是由原来的小模型上做微小的网格搜索决定的常量系数。
如图为EfficientNet的设计思路,从三个方面同时拓充网络的特性。
在这里插入图片描述
本博客以Efficientnet-B0和Efficientdet-D0为例,进行Efficientdet的解析。

Efficientnet-B0由1个Stem+16个大Blocks堆叠构成,16个大Blocks可以分为1、2、2、3、3、4、1个Block。Block的通用结构如下,其总体的设计思路是Inverted residuals结构和残差结构,在3x3或者5x5网络结构前利用1x1卷积升维,在3x3或者5x5网络结构后增加了一个关于通道的注意力机制,最后利用1x1卷积降维后增加一个大残差边。
在这里插入图片描述
整体结构如下:
在这里插入图片描述
最终获得三个有效特征层传入到BIFPN当中进行下一步的操作。

  1. import collections
  2. import math
  3. import string
  4. from keras import backend, layers
  5. MOMENTUM = 0.99
  6. EPSILON = 1e-3
  7. #-------------------------------------------------#
  8. # 一共七个大结构块,每个大结构块都有特定的参数
  9. #-------------------------------------------------#
  10. BlockArgs = collections.namedtuple('BlockArgs', [
  11. 'kernel_size', 'num_repeat', 'input_filters', 'output_filters',
  12. 'expand_ratio', 'id_skip', 'strides', 'se_ratio'
  13. ])
  14. BlockArgs.__new__.__defaults__ = (None,) * len(BlockArgs._fields)
  15. DEFAULT_BLOCKS_ARGS = [
  16. BlockArgs(kernel_size=3, num_repeat=1, input_filters=32, output_filters=16,
  17. expand_ratio=1, id_skip=True, strides=[1, 1], se_ratio=0.25),
  18. BlockArgs(kernel_size=3, num_repeat=2, input_filters=16, output_filters=24,
  19. expand_ratio=6, id_skip=True, strides=[2, 2], se_ratio=0.25),
  20. BlockArgs(kernel_size=5, num_repeat=2, input_filters=24, output_filters=40,
  21. expand_ratio=6, id_skip=True, strides=[2, 2], se_ratio=0.25),
  22. BlockArgs(kernel_size=3, num_repeat=3, input_filters=40, output_filters=80,
  23. expand_ratio=6, id_skip=True, strides=[2, 2], se_ratio=0.25),
  24. BlockArgs(kernel_size=5, num_repeat=3, input_filters=80, output_filters=112,
  25. expand_ratio=6, id_skip=True, strides=[1, 1], se_ratio=0.25),
  26. BlockArgs(kernel_size=5, num_repeat=4, input_filters=112, output_filters=192,
  27. expand_ratio=6, id_skip=True, strides=[2, 2], se_ratio=0.25),
  28. BlockArgs(kernel_size=3, num_repeat=1, input_filters=192, output_filters=320,
  29. expand_ratio=6, id_skip=True, strides=[1, 1], se_ratio=0.25)
  30. ]
  31. #-------------------------------------------------#
  32. # Kernel的初始化器
  33. #-------------------------------------------------#
  34. CONV_KERNEL_INITIALIZER = {
  35. 'class_name': 'VarianceScaling',
  36. 'config': {
  37. 'scale': 2.0,
  38. 'mode': 'fan_out',
  39. 'distribution': 'normal'
  40. }
  41. }
  42. #-------------------------------------------------#
  43. # Swish激活函数
  44. #-------------------------------------------------#
  45. def get_swish():
  46. def swish(x):
  47. return x * backend.sigmoid(x)
  48. return swish
  49. #-------------------------------------------------#
  50. # Dropout层
  51. #-------------------------------------------------#
  52. def get_dropout():
  53. class FixedDropout(layers.Dropout):
  54. def _get_noise_shape(self, inputs):
  55. if self.noise_shape is None:
  56. return self.noise_shape
  57. symbolic_shape = backend.shape(inputs)
  58. noise_shape = [symbolic_shape[axis] if shape is None else shape
  59. for axis, shape in enumerate(self.noise_shape)]
  60. return tuple(noise_shape)
  61. return FixedDropout
  62. #-------------------------------------------------#
  63. # 该函数的目的是保证filter的大小可以被8整除
  64. #-------------------------------------------------#
  65. def round_filters(filters, width_coefficient, depth_divisor):
  66. filters *= width_coefficient
  67. new_filters = int(filters + depth_divisor / 2) // depth_divisor * depth_divisor
  68. new_filters = max(depth_divisor, new_filters)
  69. if new_filters < 0.9 * filters:
  70. new_filters += depth_divisor
  71. return int(new_filters)
  72. #-------------------------------------------------#
  73. # 计算模块的重复次数
  74. #-------------------------------------------------#
  75. def round_repeats(repeats, depth_coefficient):
  76. return int(math.ceil(depth_coefficient * repeats))
  77. def mb_conv_block(inputs, block_args, activation, drop_rate=None, prefix=''):
  78. Dropout = get_dropout()
  79. #-------------------------------------------------#
  80. # 利用Inverted residuals
  81. # part1 利用1x1卷积进行通道数上升
  82. #-------------------------------------------------#
  83. filters = block_args.input_filters * block_args.expand_ratio
  84. if block_args.expand_ratio != 1:
  85. x = layers.Conv2D(filters, 1,
  86. padding='same',
  87. use_bias=False,
  88. kernel_initializer=CONV_KERNEL_INITIALIZER,
  89. name=prefix + 'expand_conv')(inputs)
  90. x = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON, name=prefix + 'expand_bn')(x)
  91. x = layers.Activation(activation, name=prefix + 'expand_activation')(x)
  92. else:
  93. x = inputs
  94. #------------------------------------------------------#
  95. # 如果步长为2x2的话,利用深度可分离卷积进行高宽压缩
  96. # part2 利用3x3卷积对每一个channel进行卷积
  97. #------------------------------------------------------#
  98. x = layers.DepthwiseConv2D(block_args.kernel_size,
  99. strides=block_args.strides,
  100. padding='same',
  101. use_bias=False,
  102. depthwise_initializer=CONV_KERNEL_INITIALIZER,
  103. name=prefix + 'dwconv')(x)
  104. x = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON, name=prefix + 'bn')(x)
  105. x = layers.Activation(activation, name=prefix + 'activation')(x)
  106. #------------------------------------------------------#
  107. # 完成深度可分离卷积后
  108. # 对深度可分离卷积的结果施加注意力机制
  109. #------------------------------------------------------#
  110. if 0 < block_args.se_ratio <= 1:
  111. num_reduced_filters = max(1, int(block_args.input_filters * block_args.se_ratio))
  112. se_tensor = layers.GlobalAveragePooling2D(name=prefix + 'se_squeeze')(x)
  113. se_tensor = layers.Reshape((1, 1, filters), name=prefix + 'se_reshape')(se_tensor)
  114. #------------------------------------------------------#
  115. # 通道先压缩后上升,最后利用sigmoid将值固定到0-1之间
  116. #------------------------------------------------------#
  117. se_tensor = layers.Conv2D(num_reduced_filters, 1,
  118. activation=activation,
  119. padding='same',
  120. use_bias=True,
  121. kernel_initializer=CONV_KERNEL_INITIALIZER,
  122. name=prefix + 'se_reduce')(se_tensor)
  123. se_tensor = layers.Conv2D(filters, 1,
  124. activation='sigmoid',
  125. padding='same',
  126. use_bias=True,
  127. kernel_initializer=CONV_KERNEL_INITIALIZER,
  128. name=prefix + 'se_expand')(se_tensor)
  129. x = layers.multiply([x, se_tensor], name=prefix + 'se_excite')
  130. #------------------------------------------------------#
  131. # part3 利用1x1卷积进行通道下降
  132. #------------------------------------------------------#
  133. x = layers.Conv2D(block_args.output_filters, 1,
  134. padding='same',
  135. use_bias=False,
  136. kernel_initializer=CONV_KERNEL_INITIALIZER,
  137. name=prefix + 'project_conv')(x)
  138. x = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON, name=prefix + 'project_bn')(x)
  139. #------------------------------------------------------#
  140. # part4 如果满足残差条件,那么就增加残差边
  141. #------------------------------------------------------#
  142. if block_args.id_skip and all(s == 1 for s in block_args.strides) and block_args.input_filters == block_args.output_filters:
  143. if drop_rate and (drop_rate > 0):
  144. x = Dropout(drop_rate,
  145. noise_shape=(None, 1, 1, 1),
  146. name=prefix + 'drop')(x)
  147. x = layers.add([x, inputs], name=prefix + 'add')
  148. return x
  149. def EfficientNet(width_coefficient,
  150. depth_coefficient,
  151. drop_connect_rate=0.2,
  152. depth_divisor=8,
  153. blocks_args=DEFAULT_BLOCKS_ARGS,
  154. inputs=None,
  155. **kwargs):
  156. activation = get_swish(**kwargs)
  157. img_input = inputs
  158. #-------------------------------------------------#
  159. # 创建stem部分
  160. #-------------------------------------------------#
  161. x = img_input
  162. x = layers.Conv2D(round_filters(32, width_coefficient, depth_divisor), 3,
  163. strides=(2, 2),
  164. padding='same',
  165. use_bias=False,
  166. kernel_initializer=CONV_KERNEL_INITIALIZER,
  167. name='stem_conv')(x)
  168. x = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON, name='stem_bn')(x)
  169. x = layers.Activation(activation, name='stem_activation')(x)
  170. features = []
  171. #-------------------------------------------------#
  172. # 计算总的efficient_block的数量
  173. #-------------------------------------------------#
  174. block_num = 0
  175. num_blocks_total = sum(block_args.num_repeat for block_args in blocks_args)
  176. #------------------------------------------------------------------------------#
  177. # 对结构块参数进行循环、一共进行7个大的结构块。
  178. # 每个大结构块下会重复小的efficient_block
  179. #------------------------------------------------------------------------------#
  180. for idx, block_args in enumerate(blocks_args):
  181. assert block_args.num_repeat > 0
  182. #-------------------------------------------------#
  183. # 对使用到的参数进行更新
  184. #-------------------------------------------------#
  185. block_args = block_args._replace(
  186. input_filters = round_filters(block_args.input_filters, width_coefficient, depth_divisor),
  187. output_filters = round_filters(block_args.output_filters, width_coefficient, depth_divisor),
  188. num_repeat = round_repeats(block_args.num_repeat, depth_coefficient))
  189. # 计算drop_rate
  190. drop_rate = drop_connect_rate * float(block_num) / num_blocks_total
  191. x = mb_conv_block(x, block_args,
  192. activation=activation,
  193. drop_rate=drop_rate,
  194. prefix='block{}a_'.format(idx + 1))
  195. block_num += 1
  196. if block_args.num_repeat > 1:
  197. #-------------------------------------------------#
  198. # 对使用到的参数进行更新
  199. #-------------------------------------------------#
  200. block_args = block_args._replace(input_filters=block_args.output_filters, strides=[1, 1])
  201. for bidx in range(block_args.num_repeat - 1):
  202. # 计算drop_rate
  203. drop_rate = drop_connect_rate * float(block_num) / num_blocks_total
  204. x = mb_conv_block(x, block_args,
  205. activation = activation,
  206. drop_rate = drop_rate,
  207. prefix = 'block{}{}_'.format(idx + 1, string.ascii_lowercase[bidx + 1]))
  208. block_num += 1
  209. if idx < len(blocks_args) - 1 and blocks_args[idx + 1].strides[0] == 2:
  210. features.append(x)
  211. elif idx == len(blocks_args) - 1:
  212. features.append(x)
  213. return features
  214. def EfficientNetB0(inputs=None, **kwargs):
  215. return EfficientNet(1.0, 1.0, inputs=inputs, **kwargs)
  216. def EfficientNetB1(inputs=None, **kwargs):
  217. return EfficientNet(1.0, 1.1, inputs=inputs, **kwargs)
  218. def EfficientNetB2(inputs=None, **kwargs):
  219. return EfficientNet(1.1, 1.2, inputs=inputs, **kwargs)
  220. def EfficientNetB3(inputs=None, **kwargs):
  221. return EfficientNet(1.2, 1.4, inputs=inputs, **kwargs)
  222. def EfficientNetB4(inputs=None, **kwargs):
  223. return EfficientNet(1.4, 1.8, inputs=inputs, **kwargs)
  224. def EfficientNetB5(inputs=None, **kwargs):
  225. return EfficientNet(1.6, 2.2, inputs=inputs, **kwargs)
  226. def EfficientNetB6(inputs=None, **kwargs):
  227. return EfficientNet(1.8, 2.6, inputs=inputs, **kwargs)
  228. def EfficientNetB7(inputs=None, **kwargs):
  229. return EfficientNet(2.0, 3.1, inputs=inputs, **kwargs)

2、BiFPN加强特征提取

在这里插入图片描述
BiFPN简单来讲是一个加强版本的FPN,上图是BiFPN,下图是普通的FPN,大家可以看到,与普通的FPN相比,BiFPN的FPN构建更加复杂,中间还增加了许多连接。
在这里插入图片描述
构建BiFPN可以分为多步:
1、获得P3_in、P4_in、P5_in、P6_in、P7_in,通过主干特征提取网络,我们已经可以获得P3、P4、P5,还需要进行两次下采样获得P6、P7
P3、P4、P5在经过1x1卷积调整通道数后,就可以作为P3_in、P4_in、P5_in了,在构建BiFPN的第一步,需要构建两个P4_in、P5_in(原版是这样设计的)。
在这里插入图片描述
实现代码如下:

  1. _, _, C3, C4, C5 = features
  2. # 第一次BIFPN需要 下采样 与 降通道 获得 p3_in p4_in p5_in p6_in p7_in
  3. #-----------------------------下采样 与 降通道----------------------------#
  4. P3_in = C3
  5. P3_in = layers.Conv2D(num_channels, kernel_size=1, padding='same',
  6. name=f'fpn_cells/cell_{
  7. id}/fnode3/resample_0_0_8/conv2d')(P3_in)
  8. P3_in = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON,
  9. name=f'fpn_cells/cell_{
  10. id}/fnode3/resample_0_0_8/bn')(P3_in)
  11. P4_in = C4
  12. P4_in_1 = layers.Conv2D(num_channels, kernel_size=1, padding='same',
  13. name=f'fpn_cells/cell_{
  14. id}/fnode2/resample_0_1_7/conv2d')(P4_in)
  15. P4_in_1 = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON,
  16. name=f'fpn_cells/cell_{
  17. id}/fnode2/resample_0_1_7/bn')(P4_in_1)
  18. P4_in_2 = layers.Conv2D(num_channels, kernel_size=1, padding='same',
  19. name=f'fpn_cells/cell_{
  20. id}/fnode4/resample_0_1_9/conv2d')(P4_in)
  21. P4_in_2 = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON,
  22. name=f'fpn_cells/cell_{
  23. id}/fnode4/resample_0_1_9/bn')(P4_in_2)
  24. P5_in = C5
  25. P5_in_1 = layers.Conv2D(num_channels, kernel_size=1, padding='same',
  26. name=f'fpn_cells/cell_{
  27. id}/fnode1/resample_0_2_6/conv2d')(P5_in)
  28. P5_in_1 = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON,
  29. name=f'fpn_cells/cell_{
  30. id}/fnode1/resample_0_2_6/bn')(P5_in_1)
  31. P5_in_2 = layers.Conv2D(num_channels, kernel_size=1, padding='same',
  32. name=f'fpn_cells/cell_{
  33. id}/fnode5/resample_0_2_10/conv2d')(P5_in)
  34. P5_in_2 = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON,
  35. name=f'fpn_cells/cell_{
  36. id}/fnode5/resample_0_2_10/bn')(P5_in_2)
  37. P6_in = layers.Conv2D(num_channels, kernel_size=1, padding='same', name='resample_p6/conv2d')(C5)
  38. P6_in = layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON, name='resample_p6/bn')(P6_in)
  39. P6_in = layers.MaxPooling2D(pool_size=3, strides=2, padding='same', name='resample_p6/maxpool')(P6_in)
  40. P7_in = layers.MaxPooling2D(pool_size=3, strides=2, padding='same', name='resample_p7/maxpool')(P6_in)
  41. #-------------------------------------------------------------------------#

2、在获得P3_in、P4_in_1、P4_in_2、P5_in_1、P5_in_2、P6_in、P7_in之后需要对P7_in进行上采样,上采样后与P6_in堆叠获得P6_td;之后对P6_td进行上采样,上采样后与P5_in_1进行堆叠获得P5_td;之后对P5_td进行上采样,上采样后与P4_in_1进行堆叠获得P4_td;之后对P4_td进行上采样,上采样后与P3_in进行堆叠获得P3_out
在这里插入图片描述
实现代码如下:

  1. #--------------------------构建BIFPN的上下采样循环-------------------------#
  2. P7_U = layers.UpSampling2D()(P7_in)
  3. P6_td = wBiFPNAdd(name=f'fpn_cells/cell_{
  4. id}/fnode0/add')([P6_in, P7_U])
  5. P6_td = layers.Activation(lambda x: tf.nn.swish(x))(P6_td)
  6. P6_td = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  7. name=f'fpn_cells/cell_{
  8. id}/fnode0/op_after_combine5')(P6_td)
  9. P6_U = layers.UpSampling2D()(P6_td)
  10. P5_td = wBiFPNAdd(name=f'fpn_cells/cell_{
  11. id}/fnode1/add')([P5_in_1, P6_U])
  12. P5_td = layers.Activation(lambda x: tf.nn.swish(x))(P5_td)
  13. P5_td = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  14. name=f'fpn_cells/cell_{
  15. id}/fnode1/op_after_combine6')(P5_td)
  16. P5_U = layers.UpSampling2D()(P5_td)
  17. P4_td = wBiFPNAdd(name=f'fpn_cells/cell_{
  18. id}/fnode2/add')([P4_in_1, P5_U])
  19. P4_td = layers.Activation(lambda x: tf.nn.swish(x))(P4_td)
  20. P4_td = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  21. name=f'fpn_cells/cell_{
  22. id}/fnode2/op_after_combine7')(P4_td)
  23. P4_U = layers.UpSampling2D()(P4_td)
  24. P3_out = wBiFPNAdd(name=f'fpn_cells/cell_{
  25. id}/fnode3/add')([P3_in, P4_U])
  26. P3_out = layers.Activation(lambda x: tf.nn.swish(x))(P3_out)
  27. P3_out = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  28. name=f'fpn_cells/cell_{
  29. id}/fnode3/op_after_combine8')(P3_out)
  30. #-------------------------------------------------------------------------#

3、在获得P3_out、P4_td、P4_in_2、P5_td、P5_in_2、P6_in、P6_td、P7_in之后,之后需要对P3_out进行下采样,下采样后与P4_td、P4_in_2堆叠获得P4_out;之后对P4_out进行下采样,下采样后与P5_td、P5_in_2进行堆叠获得P5_out;之后对P5_out进行下采样,下采样后与P6_in、P6_td进行堆叠获得P6_out;之后对P6_out进行下采样,下采样后与P7_in进行堆叠获得P7_out
在这里插入图片描述
实现代码如下:

  1. #--------------------------构建BIFPN的上下采样循环-------------------------#
  2. P3_D = layers.MaxPooling2D(pool_size=3, strides=2, padding='same')(P3_out)
  3. P4_out = wBiFPNAdd(name=f'fpn_cells/cell_{
  4. id}/fnode4/add')([P4_in_2, P4_td, P3_D])
  5. P4_out = layers.Activation(lambda x: tf.nn.swish(x))(P4_out)
  6. P4_out = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  7. name=f'fpn_cells/cell_{
  8. id}/fnode4/op_after_combine9')(P4_out)
  9. P4_D = layers.MaxPooling2D(pool_size=3, strides=2, padding='same')(P4_out)
  10. P5_out = wBiFPNAdd(name=f'fpn_cells/cell_{
  11. id}/fnode5/add')([P5_in_2, P5_td, P4_D])
  12. P5_out = layers.Activation(lambda x: tf.nn.swish(x))(P5_out)
  13. P5_out = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  14. name=f'fpn_cells/cell_{
  15. id}/fnode5/op_after_combine10')(P5_out)
  16. P5_D = layers.MaxPooling2D(pool_size=3, strides=2, padding='same')(P5_out)
  17. P6_out = wBiFPNAdd(name=f'fpn_cells/cell_{
  18. id}/fnode6/add')([P6_in, P6_td, P5_D])
  19. P6_out = layers.Activation(lambda x: tf.nn.swish(x))(P6_out)
  20. P6_out = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  21. name=f'fpn_cells/cell_{
  22. id}/fnode6/op_after_combine11')(P6_out)
  23. P6_D = layers.MaxPooling2D(pool_size=3, strides=2, padding='same')(P6_out)
  24. P7_out = wBiFPNAdd(name=f'fpn_cells/cell_{
  25. id}/fnode7/add')([P7_in, P6_D])
  26. P7_out = layers.Activation(lambda x: tf.nn.swish(x))(P7_out)
  27. P7_out = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  28. name=f'fpn_cells/cell_{
  29. id}/fnode7/op_after_combine12')(P7_out)
  30. #-------------------------------------------------------------------------#

4、将获得的P3_out、P4_out、P5_out、P6_out、P7_out作为P3_in、P4_in、P5_in、P6_in、P7_in重复2、3步骤进行堆叠即可,对于Effiicientdet B0来讲,还需要重复2次,需要注意P4_in_1和P4_in_2此时不需要分开了,P5也是
在这里插入图片描述
实现代码如下:

  1. P3_in, P4_in, P5_in, P6_in, P7_in = features
  2. P7_U = layers.UpSampling2D()(P7_in)
  3. P6_td = wBiFPNAdd(name=f'fpn_cells/cell_{
  4. id}/fnode0/add')([P6_in, P7_U])
  5. P6_td = layers.Activation(lambda x: tf.nn.swish(x))(P6_td)
  6. P6_td = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  7. name=f'fpn_cells/cell_{
  8. id}/fnode0/op_after_combine5')(P6_td)
  9. P6_U = layers.UpSampling2D()(P6_td)
  10. P5_td = wBiFPNAdd(name=f'fpn_cells/cell_{
  11. id}/fnode1/add')([P5_in, P6_U])
  12. P5_td = layers.Activation(lambda x: tf.nn.swish(x))(P5_td)
  13. P5_td = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  14. name=f'fpn_cells/cell_{
  15. id}/fnode1/op_after_combine6')(P5_td)
  16. P5_U = layers.UpSampling2D()(P5_td)
  17. P4_td = wBiFPNAdd(name=f'fpn_cells/cell_{
  18. id}/fnode2/add')([P4_in, P5_U])
  19. P4_td = layers.Activation(lambda x: tf.nn.swish(x))(P4_td)
  20. P4_td = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  21. name=f'fpn_cells/cell_{
  22. id}/fnode2/op_after_combine7')(P4_td)
  23. P4_U = layers.UpSampling2D()(P4_td)
  24. P3_out = wBiFPNAdd(name=f'fpn_cells/cell_{
  25. id}/fnode3/add')([P3_in, P4_U])
  26. P3_out = layers.Activation(lambda x: tf.nn.swish(x))(P3_out)
  27. P3_out = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  28. name=f'fpn_cells/cell_{
  29. id}/fnode3/op_after_combine8')(P3_out)
  30. P3_D = layers.MaxPooling2D(pool_size=3, strides=2, padding='same')(P3_out)
  31. P4_out = wBiFPNAdd(name=f'fpn_cells/cell_{
  32. id}/fnode4/add')([P4_in, P4_td, P3_D])
  33. P4_out = layers.Activation(lambda x: tf.nn.swish(x))(P4_out)
  34. P4_out = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  35. name=f'fpn_cells/cell_{
  36. id}/fnode4/op_after_combine9')(P4_out)
  37. P4_D = layers.MaxPooling2D(pool_size=3, strides=2, padding='same')(P4_out)
  38. P5_out = wBiFPNAdd(name=f'fpn_cells/cell_{
  39. id}/fnode5/add')([P5_in, P5_td, P4_D])
  40. P5_out = layers.Activation(lambda x: tf.nn.swish(x))(P5_out)
  41. P5_out = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  42. name=f'fpn_cells/cell_{
  43. id}/fnode5/op_after_combine10')(P5_out)
  44. P5_D = layers.MaxPooling2D(pool_size=3, strides=2, padding='same')(P5_out)
  45. P6_out = wBiFPNAdd(name=f'fpn_cells/cell_{
  46. id}/fnode6/add')([P6_in, P6_td, P5_D])
  47. P6_out = layers.Activation(lambda x: tf.nn.swish(x))(P6_out)
  48. P6_out = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  49. name=f'fpn_cells/cell_{
  50. id}/fnode6/op_after_combine11')(P6_out)
  51. P6_D = layers.MaxPooling2D(pool_size=3, strides=2, padding='same')(P6_out)
  52. P7_out = wBiFPNAdd(name=f'fpn_cells/cell_{
  53. id}/fnode7/add')([P7_in, P6_D])
  54. P7_out = layers.Activation(lambda x: tf.nn.swish(x))(P7_out)
  55. P7_out = SeparableConvBlock(num_channels=num_channels, kernel_size=3, strides=1,
  56. name=f'fpn_cells/cell_{
  57. id}/fnode7/op_after_combine12')(P7_out)

3、从特征获取预测结果

在这里插入图片描述
通过第二部的重复运算,我们获得了P3_out, P4_out, P5_out, P6_out, P7_out。

为了和普通特征层区分,我们称之为有效特征层,将这五个有效的特征层传输过ClassNet+BoxNet就可以获得预测结果了。

对于Efficientdet-B0来讲:
ClassNet采用3次64通道的卷积和1次num_anchors x num_classes的卷积,num_anchors指的是该特征层所拥有的先验框数量,num_classes指的是网络一共对多少类的目标进行检测。

BoxNet采用3次64通道的卷积和1次num_anchors x 4的卷积,num_anchors指的是该特征层所拥有的先验框数量,4指的是先验框的调整情况。

需要注意的是,每个特征层所用的ClassNet是同一个ClassNet;每个特征层所用的BoxNet是同一个BoxNet。

其中:
num_anchors x 4的卷积 用于预测 该特征层上 每一个网格点上 每一个先验框的变化情况。**

num_anchors x num_classes的卷积 用于预测 该特征层上 每一个网格点上 每一个预测框对应的种类。

实现代码为:

  1. #------------------------------------------#
  2. # 获得回归预测结果
  3. # 该部分会对先验框进行调整获得预测框
  4. #------------------------------------------#
  5. class BoxNet:
  6. def __init__(self, width, depth, num_anchors=9, name='box_net', **kwargs):
  7. self.name = name
  8. self.width = width
  9. self.depth = depth
  10. self.num_anchors = num_anchors
  11. options = {
  12. 'kernel_size': 3,
  13. 'strides': 1,
  14. 'padding': 'same',
  15. 'bias_initializer': 'zeros',
  16. 'depthwise_initializer': initializers.RandomNormal(stddev=0.01),
  17. 'pointwise_initializer': initializers.RandomNormal(stddev=0.01),
  18. }
  19. self.convs = [layers.SeparableConv2D(filters=width, name=f'{
  20. self.name}/box-{
  21. i}', **options) for i in range(depth)]
  22. self.head = layers.SeparableConv2D(filters=num_anchors * 4, name=f'{
  23. self.name}/box-predict', **options)
  24. self.bns = [[layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON, name=f'{
  25. self.name}/box-{
  26. i}-bn-{
  27. j}') for j in
  28. range(3, 8)] for i in range(depth)]
  29. self.relu = layers.Lambda(lambda x: tf.nn.swish(x))
  30. self.reshape = layers.Reshape((-1, 4))
  31. def call(self, inputs):
  32. feature, level = inputs
  33. for i in range(self.depth):
  34. feature = self.convs[i](feature)
  35. feature = self.bns[i][level](feature)
  36. feature = self.relu(feature)
  37. outputs = self.head(feature)
  38. outputs = self.reshape(outputs)
  39. return outputs
  40. #------------------------------------------#
  41. # 获得分类预测结果
  42. # 该部分会判断先验框对应的物体种类
  43. #------------------------------------------#
  44. class ClassNet:
  45. def __init__(self, width, depth, num_classes=20, num_anchors=9, name='class_net', **kwargs):
  46. self.name = name
  47. self.width = width
  48. self.depth = depth
  49. self.num_classes = num_classes
  50. self.num_anchors = num_anchors
  51. options = {
  52. 'kernel_size': 3,
  53. 'strides': 1,
  54. 'padding': 'same',
  55. 'depthwise_initializer': initializers.RandomNormal(stddev=0.01),
  56. 'pointwise_initializer': initializers.RandomNormal(stddev=0.01),
  57. }
  58. self.convs = [layers.SeparableConv2D(filters=width, bias_initializer='zeros', name=f'{
  59. self.name}/class-{
  60. i}', **options) for i in range(depth)]
  61. self.head = layers.SeparableConv2D(filters=num_classes * num_anchors, bias_initializer=AnchorProbability(probability=0.01), name=f'{
  62. self.name}/class-predict', **options)
  63. self.bns = [[layers.BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON, name=f'{
  64. self.name}/class-{
  65. i}-bn-{
  66. j}') for j
  67. in range(3, 8)] for i in range(depth)]
  68. self.relu = layers.Lambda(lambda x: tf.nn.swish(x))
  69. self.reshape = layers.Reshape((-1, num_classes))
  70. self.activation = layers.Activation('sigmoid')
  71. def call(self, inputs):
  72. feature, level = inputs
  73. for i in range(self.depth):
  74. feature = self.convs[i](feature)
  75. feature = self.bns[i][level](feature)
  76. feature = self.relu(feature)
  77. outputs = self.head(feature)
  78. outputs = self.reshape(outputs)
  79. outputs = self.activation(outputs)
  80. return outputs

4、预测结果的解码

我们通过对每一个特征层的处理,可以获得三个内容,分别是:

num_anchors x 4的卷积 用于预测 该特征层上 每一个网格点上 每一个先验框的变化情况。**

num_anchors x num_classes的卷积 用于预测 该特征层上 每一个网格点上 每一个预测框对应的种类。

每一个有效特征层对应的先验框对应着该特征层上 每一个网格点上 预先设定好的9个框。

我们利用 num_anchors x 4的卷积每一个有效特征层对应的先验框 获得框的真实位置。

每一个有效特征层对应的先验框就是,如图所示的作用:
每一个有效特征层将整个图片分成与其长宽对应的网格,如P3的特征层就是将整个图像分成64x64个网格;然后从每个网格中心建立9个先验框,一共64x64x9个,36864‬个先验框。
在这里插入图片描述
先验框虽然可以代表一定的框的位置信息与框的大小信息,但是其是有限的,无法表示任意情况,因此还需要调整,Efficientdet利用3次64通道的卷积+num_anchors x 4的卷积的结果对先验框进行调整。

num_anchors x 4中的num_anchors表示了这个网格点所包含的先验框数量,其中的4表示了框的左上角xy轴,右下角xy的调整情况。

Efficientdet解码过程就是将对应的先验框的左上角和右下角进行位置的调整,调整完的结果就是预测框的位置了。

当然得到最终的预测结构后还要进行得分排序与非极大抑制筛选这一部分基本上是所有目标检测通用的部分。
1、取出每一类得分大于confidence_threshold的框和得分。
2、利用框的位置和得分进行非极大抑制。

实现代码如下:

  1. import numpy as np
  2. import tensorflow as tf
  3. import keras.backend as K
  4. class BBoxUtility(object):
  5. def __init__(self, num_classes, nms_thresh=0.45, top_k=300):
  6. self.num_classes = num_classes
  7. self._nms_thresh = nms_thresh
  8. self._top_k = top_k
  9. self.boxes = K.placeholder(dtype='float32', shape=(None, 4))
  10. self.scores = K.placeholder(dtype='float32', shape=(None,))
  11. self.nms = tf.image.non_max_suppression(self.boxes, self.scores, self._top_k, iou_threshold=self._nms_thresh)
  12. self.sess = K.get_session()
  13. def bbox_iou(self, b1, b2):
  14. b1_x1, b1_y1, b1_x2, b1_y2 = b1[0], b1[1], b1[2], b1[3]
  15. b2_x1, b2_y1, b2_x2, b2_y2 = b2[:, 0], b2[:, 1], b2[:, 2], b2[:, 3]
  16. inter_rect_x1 = np.maximum(b1_x1, b2_x1)
  17. inter_rect_y1 = np.maximum(b1_y1, b2_y1)
  18. inter_rect_x2 = np.minimum(b1_x2, b2_x2)
  19. inter_rect_y2 = np.minimum(b1_y2, b2_y2)
  20. inter_area = np.maximum(inter_rect_x2 - inter_rect_x1, 0) * \
  21. np.maximum(inter_rect_y2 - inter_rect_y1, 0)
  22. area_b1 = (b1_x2-b1_x1)*(b1_y2-b1_y1)
  23. area_b2 = (b2_x2-b2_x1)*(b2_y2-b2_y1)
  24. iou = inter_area/np.maximum((area_b1+area_b2-inter_area),1e-6)
  25. return iou
  26. def efficientdet_correct_boxes(self, box_xy, box_wh, input_shape, image_shape, letterbox_image):
  27. #-----------------------------------------------------------------#
  28. # 把y轴放前面是因为方便预测框和图像的宽高进行相乘
  29. #-----------------------------------------------------------------#
  30. box_yx = box_xy[..., ::-1]
  31. box_hw = box_wh[..., ::-1]
  32. input_shape = np.array(input_shape)
  33. image_shape = np.array(image_shape)
  34. if letterbox_image:
  35. #-----------------------------------------------------------------#
  36. # 这里求出来的offset是图像有效区域相对于图像左上角的偏移情况
  37. # new_shape指的是宽高缩放情况
  38. #-----------------------------------------------------------------#
  39. new_shape = np.round(image_shape * np.min(input_shape/image_shape))
  40. offset = (input_shape - new_shape)/2./input_shape
  41. scale = input_shape/new_shape
  42. box_yx = (box_yx - offset) * scale
  43. box_hw *= scale
  44. box_mins = box_yx - (box_hw / 2.)
  45. box_maxes = box_yx + (box_hw / 2.)
  46. boxes = np.concatenate([box_mins[..., 0:1], box_mins[..., 1:2], box_maxes[..., 0:1], box_maxes[..., 1:2]], axis=-1)
  47. boxes *= np.concatenate([image_shape, image_shape], axis=-1)
  48. return boxes
  49. def decode_boxes(self, mbox_loc, anchors):
  50. # 获得先验框的宽与高
  51. anchor_width = anchors[:, 2] - anchors[:, 0]
  52. anchor_height = anchors[:, 3] - anchors[:, 1]
  53. # 获得先验框的中心点
  54. anchor_center_x = 0.5 * (anchors[:, 2] + anchors[:, 0])
  55. anchor_center_y = 0.5 * (anchors[:, 3] + anchors[:, 1])
  56. # 真实框距离先验框中心的xy轴偏移情况
  57. decode_bbox_center_x = mbox_loc[:, 0] * anchor_width
  58. decode_bbox_center_x += anchor_center_x
  59. decode_bbox_center_y = mbox_loc[:, 1] * anchor_height
  60. decode_bbox_center_y += anchor_center_y
  61. # 真实框的宽与高的求取
  62. decode_bbox_width = np.exp(mbox_loc[:, 2])
  63. decode_bbox_width *= anchor_width
  64. decode_bbox_height = np.exp(mbox_loc[:, 3])
  65. decode_bbox_height *= anchor_height
  66. # 获取真实框的左上角与右下角
  67. decode_bbox_xmin = decode_bbox_center_x - 0.5 * decode_bbox_width
  68. decode_bbox_ymin = decode_bbox_center_y - 0.5 * decode_bbox_height
  69. decode_bbox_xmax = decode_bbox_center_x + 0.5 * decode_bbox_width
  70. decode_bbox_ymax = decode_bbox_center_y + 0.5 * decode_bbox_height
  71. # 真实框的左上角与右下角进行堆叠
  72. decode_bbox = np.concatenate((decode_bbox_xmin[:, None],
  73. decode_bbox_ymin[:, None],
  74. decode_bbox_xmax[:, None],
  75. decode_bbox_ymax[:, None]), axis=-1)
  76. # 防止超出0与1
  77. decode_bbox = np.minimum(np.maximum(decode_bbox, 0.0), 1.0)
  78. return decode_bbox
  79. def decode_box(self, predictions, anchors, image_shape, input_shape, letterbox_image, confidence=0.5):
  80. #---------------------------------------------------#
  81. # 获得回归预测结果
  82. #---------------------------------------------------#
  83. mbox_loc = predictions[0]
  84. #---------------------------------------------------#
  85. # 获得种类的置信度
  86. #---------------------------------------------------#
  87. mbox_conf = predictions[1]
  88. results = [None for _ in range(len(mbox_loc))]
  89. #----------------------------------------------------------------------------------------------------------------#
  90. # 对每一张图片进行处理,由于在predict.py的时候,我们只输入一张图片,所以for i in range(len(mbox_loc))只进行一次
  91. #----------------------------------------------------------------------------------------------------------------#
  92. for i in range(len(mbox_loc)):
  93. #--------------------------------#
  94. # 利用回归结果对先验框进行解码
  95. #--------------------------------#
  96. decode_bbox = self.decode_boxes(mbox_loc[i], anchors)
  97. #--------------------------------------------------#
  98. # 判断置信度与非极大抑制的过程与视频有一定的差距
  99. # 整体思想相差不大,可以参考注释进行阅读
  100. #--------------------------------------------------#
  101. class_conf = np.expand_dims(np.max(mbox_conf[i], 1), -1)
  102. class_pred = np.expand_dims(np.argmax(mbox_conf[i], 1), -1)
  103. #--------------------------------#
  104. # 判断置信度是否大于门限要求
  105. #--------------------------------#
  106. conf_mask = (class_conf >= confidence)[:, 0]
  107. #--------------------------------#
  108. # 将预测结果进行堆叠
  109. #--------------------------------#
  110. detections = np.concatenate((decode_bbox[conf_mask], class_conf[conf_mask], class_pred[conf_mask]), 1)
  111. unique_labels = np.unique(detections[:,-1])
  112. #-------------------------------------------------------------------#
  113. # 对种类进行循环,
  114. # 非极大抑制的作用是筛选出一定区域内属于同一种类得分最大的框,
  115. # 对种类进行循环可以帮助我们对每一个类分别进行非极大抑制。
  116. #-------------------------------------------------------------------#
  117. for c in unique_labels:
  118. #------------------------------------------#
  119. # 获得某一类得分筛选后全部的预测结果
  120. #------------------------------------------#
  121. detections_class = detections[detections[:, -1] == c]
  122. #------------------------------------------#
  123. # 使用官方自带的非极大抑制会速度更快一些!
  124. #------------------------------------------#
  125. idx = self.sess.run(self.nms, feed_dict={
  126. self.boxes: detections_class[:, :4], self.scores: detections_class[:, 4]})
  127. max_detections = detections_class[idx]
  128. # #------------------------------------------#
  129. # # 非官方的实现部分
  130. # # 获得某一类得分筛选后全部的预测结果
  131. # #------------------------------------------#
  132. # detections_class = detections[detections[:, -1] == c]
  133. # scores = detections_class[:, 4]
  134. # #------------------------------------------#
  135. # # 根据得分对该种类进行从大到小排序。
  136. # #------------------------------------------#
  137. # arg_sort = np.argsort(scores)[::-1]
  138. # detections_class = detections_class[arg_sort]
  139. # max_detections = []
  140. # while np.shape(detections_class)[0]>0:
  141. # #-------------------------------------------------------------------------------------#
  142. # # 每次取出得分最大的框,计算其与其它所有预测框的重合程度,重合程度过大的则剔除。
  143. # #-------------------------------------------------------------------------------------#
  144. # max_detections.append(detections_class[0])
  145. # if len(detections_class) == 1:
  146. # break
  147. # ious = self.bbox_iou(max_detections[-1], detections_class[1:])
  148. # detections_class = detections_class[1:][ious < self._nms_thresh]
  149. results[i] = max_detections if results[i] is None else np.concatenate((results[i], max_detections), axis = 0)
  150. if results[i] is not None:
  151. results[i] = np.array(results[i])
  152. box_xy, box_wh = (results[i][:, 0:2] + results[i][:, 2:4])/2, results[i][:, 2:4] - results[i][:, 0:2]
  153. results[i][:, :4] = self.efficientdet_correct_boxes(box_xy, box_wh, input_shape, image_shape, letterbox_image)
  154. return results

5、在原图上进行绘制

通过第三步,我们可以获得预测框在原图上的位置,而且这些预测框都是经过筛选的。这些筛选后的框可以直接绘制在图片上,就可以获得结果了。

二、训练部分

1、真实框的处理

从预测部分我们知道,每个特征层的预测结果,num_anchors x 4的卷积 用于预测 该特征层上 每一个网格点上 每一个先验框的变化情况。

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

而在训练的时候,我们需要计算loss函数,这个loss函数是相对于Efficientdet网络的预测结果的。我们需要把图片输入到当前的Efficientdet网络中,得到预测结果;同时还需要把真实框的信息,进行编码,这个编码是把真实框的位置信息格式转化为Efficientdet预测结果的格式信息

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

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

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

实现代码如下:

  1. def encode_box(self, box, return_iou=True):
  2. #---------------------------------------------#
  3. # 计算当前真实框和先验框的重合情况
  4. #---------------------------------------------#
  5. iou = self.iou(box)
  6. ignored_box = np.zeros((self.num_anchors, 1))
  7. #---------------------------------------------------#
  8. # 找到处于忽略门限值范围内的先验框
  9. #---------------------------------------------------#
  10. assign_mask_ignore = (iou > self.ignore_threshold) & (iou < self.overlap_threshold)
  11. ignored_box[:, 0][assign_mask_ignore] = iou[assign_mask_ignore]
  12. encoded_box = np.zeros((self.num_anchors, 4 + return_iou))
  13. #---------------------------------------------#
  14. # 找到每一个真实框,重合程度较高的先验框
  15. #---------------------------------------------#
  16. assign_mask = iou > self.overlap_threshold
  17. #---------------------------------------------#
  18. # 如果没有一个先验框重合度大于self.overlap_threshold
  19. # 则选择重合度最大的为正样本
  20. #---------------------------------------------#
  21. if not assign_mask.any():
  22. assign_mask[iou.argmax()] = True
  23. #---------------------------------------------#
  24. # 利用iou进行赋值
  25. #---------------------------------------------#
  26. if return_iou:
  27. encoded_box[:, -1][assign_mask] = iou[assign_mask]
  28. #---------------------------------------------#
  29. # 找到对应的先验框
  30. #---------------------------------------------#
  31. assigned_anchors = self.anchors[assign_mask]
  32. #---------------------------------------------#
  33. # 逆向编码,将真实框转化为efficientdet预测结果的格式
  34. # 先计算真实框的中心与长宽
  35. #---------------------------------------------#
  36. box_center = 0.5 * (box[:2] + box[2:])
  37. box_wh = box[2:] - box[:2]
  38. #---------------------------------------------#
  39. # 再计算重合度较高的先验框的中心与长宽
  40. #---------------------------------------------#
  41. assigned_anchors_center = (assigned_anchors[:, 0:2] + assigned_anchors[:, 2:4]) * 0.5
  42. assigned_anchors_wh = (assigned_anchors[:, 2:4] - assigned_anchors[:, 0:2])
  43. #------------------------------------------------#
  44. # 逆向求取efficientdet应该有的预测结果
  45. # 先求取中心的预测结果,再求取宽高的预测结果
  46. #------------------------------------------------#
  47. encoded_box[:, :2][assign_mask] = box_center - assigned_anchors_center
  48. encoded_box[:, :2][assign_mask] /= assigned_anchors_wh
  49. encoded_box[:, 2:4][assign_mask] = np.log(box_wh / assigned_anchors_wh)
  50. return encoded_box.ravel(), ignored_box.ravel()

利用上述代码我们可以获得,真实框对应的所有的iou较大先验框,并计算了真实框对应的所有iou较大的先验框应该有的预测结果。

但是由于原始图片中可能存在多个真实框,可能同一个先验框会与多个真实框重合度较高,我们只取其中与真实框重合度最高的就可以了。

因此我们还要经过一次筛选,将上述代码获得的真实框对应的所有的iou较大先验框的预测结果中,iou最大的那个真实框筛选出来。

通过assign_boxes我们就获得了,输入进来的这张图片,应该有的预测结果是什么样子的。

实现代码如下:

  1. def assign_boxes(self, boxes):
  2. #---------------------------------------------------#
  3. # assignment分为3个部分
  4. # :4 的内容为网络应该有的回归预测结果
  5. # 4:-1 的内容为先验框所对应的种类,默认为背景
  6. # -1 的内容为当前先验框是否包含目标
  7. #---------------------------------------------------#
  8. assignment = np.zeros((self.num_anchors, 4 + 1 + self.num_classes + 1))
  9. assignment[:, 4] = 0.0
  10. assignment[:, -1] = 0.0
  11. if len(boxes) == 0:
  12. return assignment
  13. #---------------------------------------------------#
  14. # 对每一个真实框都进行iou计算
  15. #---------------------------------------------------#
  16. apply_along_axis_boxes = np.apply_along_axis(self.encode_box, 1, boxes[:, :4])
  17. encoded_boxes = np.array([apply_along_axis_boxes[i, 0] for i in range(len(apply_along_axis_boxes))])
  18. ingored_boxes = np.array([apply_along_axis_boxes[i, 1] for i in range(len(apply_along_axis_boxes))])
  19. #---------------------------------------------------#
  20. # 在reshape后,获得的ingored_boxes的shape为:
  21. # [num_true_box, num_anchors, 1] 其中1为iou
  22. #---------------------------------------------------#
  23. ingored_boxes = ingored_boxes.reshape(-1, self.num_anchors, 1)
  24. ignore_iou = ingored_boxes[:, :, 0].max(axis=0)
  25. ignore_iou_mask = ignore_iou > 0
  26. assignment[:, 4][ignore_iou_mask] = -1
  27. assignment[:, -1][ignore_iou_mask] = -1
  28. #---------------------------------------------------#
  29. # 在reshape后,获得的encoded_boxes的shape为:
  30. # [num_true_box, num_anchors, 4+1]
  31. # 4是编码后的结果,1为iou
  32. #---------------------------------------------------#
  33. encoded_boxes = encoded_boxes.reshape(-1, self.num_anchors, 5)
  34. #---------------------------------------------------#
  35. # [num_anchors]求取每一个先验框重合度最大的真实框
  36. #---------------------------------------------------#
  37. best_iou = encoded_boxes[:, :, -1].max(axis=0)
  38. best_iou_idx = encoded_boxes[:, :, -1].argmax(axis=0)
  39. best_iou_mask = best_iou > 0
  40. best_iou_idx = best_iou_idx[best_iou_mask]
  41. #---------------------------------------------------#
  42. # 计算一共有多少先验框满足需求
  43. #---------------------------------------------------#
  44. assign_num = len(best_iou_idx)
  45. # 将编码后的真实框取出
  46. encoded_boxes = encoded_boxes[:, best_iou_mask, :]
  47. assignment[:, :4][best_iou_mask] = encoded_boxes[best_iou_idx,np.arange(assign_num),:4]
  48. #----------------------------------------------------------#
  49. # 4代表为背景的概率,设定为0,因为这些先验框有对应的物体
  50. #----------------------------------------------------------#
  51. assignment[:, 4][best_iou_mask] = 1
  52. assignment[:, 5:-1][best_iou_mask] = boxes[best_iou_idx, 4:]
  53. #----------------------------------------------------------#
  54. # -8表示先验框是否有对应的物体
  55. #----------------------------------------------------------#
  56. assignment[:, -1][best_iou_mask] = 1
  57. # 通过assign_boxes我们就获得了,输入进来的这张图片,应该有的预测结果是什么样子的
  58. return assignment

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

2、利用处理完的真实框与对应图片的预测结果计算loss

loss的计算分为两个部分:
1、Smooth Loss:获取所有正标签的框的预测结果的回归loss。
2、Focal Loss:获取所有未被忽略的种类的预测结果的交叉熵loss。

由于在Efficientdet的训练过程中,正负样本极其不平衡,即 存在对应真实框的先验框可能只有若干个,但是不存在对应真实框的负样本却有上万个,这就会导致负样本的loss值极大,因此引入了Focal Loss进行正负样本的平衡。

Focal loss是何恺明大神提出的一种新的loss计算方案。其具有两个重要的特点。

  • 控制正负样本的权重
  • 控制容易分类和难分类样本的权重

正负样本的概念如下:
一张图像可能生成成千上万的候选框,但是其中只有很少一部分是包含目标的的,有目标的就是正样本,没有目标的就是负样本。

容易分类和难分类样本的概念如下:
假设存在一个二分类,样本1属于类别1的pt=0.9,样本2属于类别1的pt=0.6,显然前者更可能是类别1,其就是容易分类的样本;后者有可能是类别1,所以其为难分类样本。

如何实现权重控制呢,请往下看:

a、控制正负样本的权重

如下是常用的交叉熵loss,以二分类为例:
在这里插入图片描述
我们可以利用如下Pt简化交叉熵loss。
在这里插入图片描述
此时:
在这里插入图片描述
想要降低负样本的影响,可以在常规的损失函数前增加一个系数αt。与Pt类似,当label=1的时候,αt=α;当label=otherwise的时候,αt=1 - α,a的范围也是0到1。此时我们便可以通过设置α实现控制正负样本对loss的贡献在这里插入图片描述
其中:
在这里插入图片描述
分解开就是:
在这里插入图片描述

b、控制容易分类和难分类样本的权重

按照刚才的思路,一个二分类,样本1属于类别1的pt=0.9,样本2属于类别1的pt=0.6,也就是 是某个类的概率越大,其越容易分类 所以利用1-Pt就可以计算出其属于容易分类或者难分类。
具体实现方式如下。
在这里插入图片描述
其中:
( 1 − p t ) γ (1-p_{t})^{γ} (1−pt)γ
称为调制系数(modulating factor)

1、当pt趋于0的时候,调制系数趋于1,对于总的loss的贡献很大。当pt趋于1的时候,调制系数趋于0,也就是对于总的loss的贡献很小。
2、当γ=0的时候,focal loss就是传统的交叉熵损失,可以通过调整γ实现调制系数的改变。

c、两种权重控制方法合并

通过如下公式就可以实现控制正负样本的权重控制容易分类和难分类样本的权重
在这里插入图片描述

实现代码如下:

  1. import tensorflow as tf
  2. from keras import backend as K
  3. def focal(alpha=0.25, gamma=2.0):
  4. def _focal(y_true, y_pred):
  5. #---------------------------------------------------#
  6. # y_true [batch_size, num_anchor, num_classes+1]
  7. # y_pred [batch_size, num_anchor, num_classes]
  8. #---------------------------------------------------#
  9. labels = y_true[:, :, :-1]
  10. #---------------------------------------------------#
  11. # -1 是需要忽略的, 0 是背景, 1 是存在目标
  12. #---------------------------------------------------#
  13. anchor_state = y_true[:, :, -1]
  14. classification = y_pred
  15. # 找出存在目标的先验框
  16. indices = tf.where(K.not_equal(anchor_state, -1))
  17. labels = tf.gather_nd(labels, indices)
  18. classification = tf.gather_nd(classification, indices)
  19. # 计算每一个先验框应该有的权重
  20. alpha_factor = K.ones_like(labels) * alpha
  21. alpha_factor = tf.where(K.equal(labels, 1), alpha_factor, 1 - alpha_factor)
  22. focal_weight = tf.where(K.equal(labels, 1), 1 - classification, classification)
  23. focal_weight = alpha_factor * focal_weight ** gamma
  24. # 将权重乘上所求得的交叉熵
  25. cls_loss = focal_weight * K.binary_crossentropy(labels, classification)
  26. # 标准化,实际上是正样本的数量
  27. normalizer = tf.where(K.equal(anchor_state, 1))
  28. normalizer = K.cast(K.shape(normalizer)[0], K.floatx())
  29. normalizer = K.maximum(K.cast_to_floatx(1.0), normalizer)
  30. # 将所获得的loss除上正样本的数量
  31. loss = K.sum(cls_loss) / normalizer
  32. return loss
  33. return _focal
  34. def smooth_l1(sigma=3.0):
  35. sigma_squared = sigma ** 2
  36. def _smooth_l1(y_true, y_pred):
  37. #---------------------------------------------------#
  38. # y_true [batch_size, num_anchor, 4+1]
  39. # y_pred [batch_size, num_anchor, 4]
  40. #---------------------------------------------------#
  41. regression = y_pred
  42. regression_target = y_true[:, :, :-1]
  43. anchor_state = y_true[:, :, -1]
  44. # 找出存在目标的先验框
  45. indices = tf.where(K.equal(anchor_state, 1))
  46. regression = tf.gather_nd(regression, indices)
  47. regression_target = tf.gather_nd(regression_target, indices)
  48. # 计算smooth L1损失
  49. regression_diff = regression - regression_target
  50. regression_diff = K.abs(regression_diff)
  51. regression_loss = tf.where(
  52. K.less(regression_diff, 1.0 / sigma_squared),
  53. 0.5 * sigma_squared * K.pow(regression_diff, 2),
  54. regression_diff - 0.5 / sigma_squared
  55. )
  56. # 将所获得的loss除上正样本的数量
  57. normalizer = K.maximum(1, K.shape(indices)[0])
  58. normalizer = K.cast(normalizer, dtype=K.floatx())
  59. return K.sum(regression_loss) / normalizer / 4
  60. return _smooth_l1

训练自己的Efficientdet模型

首先前往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/efficientdet-d0-voc.h5'
  19. #---------------------------------------------------------------------#
  20. # 用于选择所使用的模型的版本,0-7
  21. #---------------------------------------------------------------------#
  22. phi = 0
  23. #------------------------------------------------------#
  24. # 输入的shape大小
  25. #------------------------------------------------------#
  26. input_shape = [image_sizes[phi], image_sizes[phi]]
  27. #---------------------------------------------------------------------#
  28. # 可用于设定先验框的大小,默认的anchors_size,大多数情况下都是通用的!
  29. # 如果想要检测小物体,可以修改anchors_size
  30. # 一般调小浅层先验框的大小就行了!因为浅层负责小物体检测!
  31. #---------------------------------------------------------------------#
  32. anchors_size = [32, 64, 128, 256, 512]
  33. #----------------------------------------------------#
  34. # 训练分为两个阶段,分别是冻结阶段和解冻阶段。
  35. # 显存不足与数据集大小无关,提示显存不足请调小batch_size。
  36. # 受到BatchNorm层影响,batch_size最小为2,不能为1。
  37. #----------------------------------------------------#
  38. #----------------------------------------------------#
  39. # 冻结阶段训练参数
  40. # 此时模型的主干被冻结了,特征提取网络不发生改变
  41. # 占用的显存较小,仅对网络进行微调
  42. #----------------------------------------------------#
  43. Init_Epoch = 0
  44. Freeze_Epoch = 50
  45. Freeze_batch_size = 8
  46. Freeze_lr = 1e-3
  47. #----------------------------------------------------#
  48. # 解冻阶段训练参数
  49. # 此时模型的主干不被冻结了,特征提取网络会发生改变
  50. # 占用的显存较大,网络所有的参数都会发生改变
  51. #----------------------------------------------------#
  52. UnFreeze_Epoch = 100
  53. Unfreeze_batch_size = 4
  54. Unfreeze_lr = 1e-4
  55. #------------------------------------------------------#
  56. # 是否进行冻结训练,默认先冻结主干训练后解冻训练。
  57. #------------------------------------------------------#
  58. Freeze_Train = True
  59. #------------------------------------------------------#
  60. # 用于设置是否使用多线程读取数据,0代表关闭多线程
  61. # 开启后会加快数据读取速度,但是会占用更多内存
  62. # keras里开启多线程有些时候速度反而慢了许多
  63. # 在IO为瓶颈的时候再开启多线程,即GPU运算速度远大于读取图片的速度。
  64. #------------------------------------------------------#
  65. num_workers = 0
  66. #----------------------------------------------------#
  67. # 获得图片路径和标签
  68. #----------------------------------------------------#
  69. train_annotation_path = '2007_train.txt'
  70. 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 条评论,143人围观)

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

相关阅读